Typo on side_effect rendering the test useless

side_effect does not take a trailing "s", calling
side_effects will simply ignore the call and
continue.  These test work because the no return
value are set, therefore the result is false.

Without changing the tests you can comment out
the try/except without breaking anything.

Fixing the tests shows that the call to
netifaces.ifaddresses was not covered for OSError
and test was missing for ValueError

Change-Id: I770390e99b4b614aff5512da11c0c226dc38b85f
This commit is contained in:
Martin Roy 2016-04-07 17:33:15 -04:00 committed by Martin Roy
parent 24166e2981
commit 512455da9e
2 changed files with 19 additions and 9 deletions

View File

@ -50,14 +50,15 @@ class IPDevice(object):
def device_has_ip(self, ip): def device_has_ip(self, ip):
try: try:
device_addresses = netifaces.ifaddresses(self.device_name) device_addresses = netifaces.ifaddresses(self.device_name)
except ValueError: # The device does not exist on the system except ValueError:
LOG.error(_LE("The device does not exist on the system: %s."),
self.device_name)
return False return False
try:
addresses = [ip_addr['addr'] for ip_addr in
device_addresses.get(netifaces.AF_INET, [])]
return ip in addresses
except OSError: except OSError:
LOG.error(_LE("Failed to get ip addresses for interface: %s."), LOG.error(_LE("Failed to get ip addresses for interface: %s."),
self.device_name) self.device_name)
return False return False
addresses = [ip_addr['addr'] for ip_addr in
device_addresses.get(netifaces.AF_INET, [])]
return ip in addresses

View File

@ -48,7 +48,7 @@ class TestIpWrapper(base.BaseTestCase):
@mock.patch('netifaces.interfaces') @mock.patch('netifaces.interfaces')
def test_get_devices_error(self, mock_interfaces): def test_get_devices_error(self, mock_interfaces):
mock_interfaces.side_effects = OSError mock_interfaces.side_effect = OSError
ret = ip_lib.IPWrapper().get_devices() ret = ip_lib.IPWrapper().get_devices()
self.assertEqual([], ret) self.assertEqual([], ret)
@ -77,7 +77,16 @@ class TestIpDevice(base.BaseTestCase):
@mock.patch('netifaces.ifaddresses') @mock.patch('netifaces.ifaddresses')
def test_device_has_ip_error(self, mock_netifaces): def test_device_has_ip_error(self, mock_netifaces):
mock_netifaces.side_effects = OSError mock_netifaces.side_effect = OSError
ret = ip_lib.IPDevice("fake_dev").device_has_ip(
mock.sentinel.fake_addr)
self.assertFalse(ret)
@mock.patch('netifaces.ifaddresses')
def test_device_not_found(self, mock_netifaces):
mock_netifaces.side_effect = ValueError
ret = ip_lib.IPDevice("fake_dev").device_has_ip( ret = ip_lib.IPDevice("fake_dev").device_has_ip(
mock.sentinel.fake_addr) mock.sentinel.fake_addr)