From 22893d1c396c1925e58c57d492f685d12296ed83 Mon Sep 17 00:00:00 2001 From: Assaf Muller Date: Thu, 7 Apr 2016 16:53:38 -0400 Subject: [PATCH] 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 (cherry picked from commit a5b1a6ec07a9bf1cdb20fdfd9f23fae86f15ea85) --- neutron/agent/windows/ip_lib.py | 8 ++++++-- neutron/tests/functional/agent/windows/test_ip_lib.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/neutron/agent/windows/ip_lib.py b/neutron/agent/windows/ip_lib.py index fea99674794..d865fd2d152 100644 --- a/neutron/agent/windows/ip_lib.py +++ b/neutron/agent/windows/ip_lib.py @@ -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."), diff --git a/neutron/tests/functional/agent/windows/test_ip_lib.py b/neutron/tests/functional/agent/windows/test_ip_lib.py index ba8dd842614..106af1283ac 100644 --- a/neutron/tests/functional/agent/windows/test_ip_lib.py +++ b/neutron/tests/functional/agent/windows/test_ip_lib.py @@ -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))