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}])