From ae4b331725012c7694132d17e28312159aedb1f3 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Fri, 14 Dec 2018 22:04:48 +0000 Subject: [PATCH] fullstack: retry on updating port's IP address If the update_port call failed with error IpAddressAlreadyAllocatedClient, retry a few more times in order to find IP addresses that are available. Change-Id: I7c5d51b01fa56083b1a689fa629a9a34c8b77012 Closes-Bug: #1808595 --- neutron/tests/fullstack/base.py | 16 ++++++++++++++++ neutron/tests/fullstack/test_l3_agent.py | 5 +---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/neutron/tests/fullstack/base.py b/neutron/tests/fullstack/base.py index 93989bf1c24..6fe81a37c87 100644 --- a/neutron/tests/fullstack/base.py +++ b/neutron/tests/fullstack/base.py @@ -15,6 +15,7 @@ from concurrent import futures import os +import netaddr from oslo_config import cfg from oslo_log import log as logging @@ -121,3 +122,18 @@ class BaseFullStackTestCase(testlib_api.MySQLTestCaseMixin, exception=RuntimeError("Could not ping the other VM, " "re-starting %s leads to network " "disruption" % agent_names)) + + def _find_available_ips(self, network, subnet, num): + ports = self.safe_client.list_ports(network_id=network['id']) + used_ips = [ip['ip_address'] + for port in ports for ip in port['fixed_ips']] + used_ips.append(subnet['gateway_ip']) + available_ips = [] + for ip in netaddr.IPNetwork(subnet['cidr']).iter_hosts(): + ip = str(ip) + if ip not in used_ips: + available_ips.append(ip) + if len(available_ips) >= num: + return available_ips + + self.fail("Cannot find enough free IP addresses.") diff --git a/neutron/tests/fullstack/test_l3_agent.py b/neutron/tests/fullstack/test_l3_agent.py index e0cbea07054..9b7fc0c9196 100644 --- a/neutron/tests/fullstack/test_l3_agent.py +++ b/neutron/tests/fullstack/test_l3_agent.py @@ -107,10 +107,7 @@ class TestL3Agent(base.BaseFullStackTestCase): gateway_port = self.safe_client.list_ports( device_id=router['id'], device_owner=constants.DEVICE_OWNER_ROUTER_GW)[0] - ip_1 = str(netaddr.IPNetwork( - ext_sub['gateway_ip']).next(100)).split('/')[0] - ip_2 = str(netaddr.IPNetwork( - ext_sub['gateway_ip']).next(101)).split('/')[0] + ip_1, ip_2 = self._find_available_ips(ext_net, ext_sub, 2) self.safe_client.update_port(gateway_port['id'], fixed_ips=[ {'ip_address': ip_1}, {'ip_address': ip_2}])