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: #1567608changes/71/303071/1
parent
3e26a7851e
commit
a5b1a6ec07
|
@ -48,10 +48,14 @@ class IPDevice(object):
|
|||
self.device_name = name
|
||||
|
||||
def device_has_ip(self, ip):
|
||||
try:
|
||||
device_addresses = netifaces.ifaddresses(self.device_name)
|
||||
except ValueError: # The device does not exist on the system
|
||||
return False
|
||||
|
||||
try:
|
||||
addresses = [ip_addr['addr'] for ip_addr in
|
||||
netifaces.ifaddresses(self.device_name).get(
|
||||
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."),
|
||||
|
|
|
@ -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!!@@@')
|
||||
self.assertFalse(not_a_device.device_has_ip(TEST_IP))
|
||||
|
|
Loading…
Reference in New Issue