From 833ce26860c93bc8efb446a247c916d638a040ef Mon Sep 17 00:00:00 2001 From: John Schwarz Date: Thu, 2 Apr 2015 18:17:03 +0300 Subject: [PATCH] create_resource should return maximum length str Previously, get_rand_name(max_length, prefix) returned a randomized suffix integer which was concatenated to the end of the given prefix. Effectively, the suffix was any decimal number between 1 and 0x7fffffff, so multiple calls to the function could return strings with different length. This is unexpected since running an already randomized name into the same function shouldn't return a different string. The suggested solution is to actually fill all the space needed until the string is 'max_length' in size. Also, a check is added to create_resource to make sure that it only generates a new port name if the input prefix is less than the maximum device name and if the prefix is long enough, don't generate a random port suffix. Change-Id: I0d5a20c676f627bce2a377e3c451043150ca734c --- neutron/tests/base.py | 19 +++++++++++++++++-- neutron/tests/common/base.py | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index b39565881..3afb30cf2 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -65,8 +65,23 @@ def fake_consume_in_threads(self): def get_rand_name(max_length=None, prefix='test'): - name = prefix + str(random.randint(1, 0x7fffffff)) - return name[:max_length] if max_length is not None else name + """Return a random string. + + The string will start with 'prefix' and will be exactly 'max_length'. + If 'max_length' is None, then exactly 8 random characters, each + hexadecimal, will be added. In case len(prefix) <= len(max_length), + ValueError will be raised to indicate the problem. + """ + + if max_length: + length = max_length - len(prefix) + if length <= 0: + raise ValueError("'max_length' must be bigger than 'len(prefix)'.") + + suffix = ''.join(str(random.randint(0, 9)) for i in range(length)) + else: + suffix = hex(random.randint(0x10000000, 0x7fffffff))[2:] + return prefix + suffix def bool_from_env(key, strict=False, default=False): diff --git a/neutron/tests/common/base.py b/neutron/tests/common/base.py index fa5add7ef..5d1fbb3f0 100644 --- a/neutron/tests/common/base.py +++ b/neutron/tests/common/base.py @@ -18,12 +18,20 @@ from neutron.tests import base def create_resource(prefix, creation_func, *args, **kwargs): """Create a new resource that does not already exist. + If prefix isn't 'max_length' in size, a random suffix is concatenated to + ensure it is random. Otherwise, 'prefix' is used as is. + :param prefix: The prefix for a randomly generated name :param creation_func: A function taking the name of the resource to be created as it's first argument. An error is assumed to indicate a name collision. :param *args *kwargs: These will be passed to the create function. """ + + # Don't generate a random name if prefix is already full-length. + if len(prefix) == n_const.DEVICE_NAME_MAX_LEN: + return creation_func(prefix, *args, **kwargs) + while True: name = base.get_rand_name( max_length=n_const.DEVICE_NAME_MAX_LEN, -- 2.45.2