From 6f455aa39802089e86b0d24e7e9e8cdc2ca44406 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Wed, 13 Jul 2016 16:20:06 -0700 Subject: [PATCH] Reverse order of get_unused_ip_addresses If a test author forgets to disable DHCP on a subnet used for a test (e.g. I17e201ef8822cace86bf805d6bd5a2e4d0e9084d), returning the first IP address in the subnet can race with the creation of the DHCP port. As a defensive measure this adjusts the logic to get unused IP addresses starting with the last addresses in the subnet instead. Change-Id: I31b9fcc9140de08e907bec08584a8c29ec6004e9 --- tempest/common/utils/net_utils.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tempest/common/utils/net_utils.py b/tempest/common/utils/net_utils.py index d98fb320fa..fd0391d7cd 100644 --- a/tempest/common/utils/net_utils.py +++ b/tempest/common/utils/net_utils.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import itertools import netaddr from tempest.lib import exceptions as lib_exc @@ -39,10 +38,11 @@ def get_unused_ip_addresses(ports_client, subnets_client, alloc_set.add(fixed_ip['ip_address']) av_set = subnet_set - alloc_set - ip_list = [str(ip) for ip in itertools.islice(av_set, count)] - - if len(ip_list) != count: - msg = "Insufficient IP addresses available" - raise lib_exc.BadRequest(message=msg) - - return ip_list + addrs = [] + for cidr in reversed(av_set.iter_cidrs()): + for ip in reversed(cidr): + addrs.append(str(ip)) + if len(addrs) == count: + return addrs + msg = "Insufficient IP addresses available" + raise lib_exc.BadRequest(message=msg)