Fix Windows IPDevice.device_has_ip racefulness

get_device_by_ip gets all devices on the system,
then iterates on them and checks if the given IP
is configured on each device. Ths method does
not account for devices that are deleted after
the devices are listed.

Change-Id: Ibada2b4ed60f02539e00021cdc7b3ddb97f1840c
Closes-Bug: #1567608
Assaf Muller 2016-04-07 16:53:38 -04:00
parent 3e26a7851e
commit a5b1a6ec07
2 changed files with 10 additions and 2 deletions

View File

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

View File

@ -28,3 +28,7 @@ class IpLibTestCase(base.BaseTestCase):
def test_ipwrapper_get_device_by_ip(self):
ip_dev = ip_lib.IPWrapper().get_device_by_ip(TEST_IP)
self.assertEqual('lo', ip_dev.device_name)
def test_device_has_ip(self):
not_a_device = ip_lib.IPDevice('#!#._not_a_device_bleargh!!@@@')