]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
create_resource should return maximum length str
authorJohn Schwarz <jschwarz@redhat.com>
Thu, 2 Apr 2015 15:17:03 +0000 (18:17 +0300)
committerJohn Schwarz <jschwarz@redhat.com>
Tue, 14 Apr 2015 12:39:07 +0000 (15:39 +0300)
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
neutron/tests/common/base.py

index b3956588100e3973459d0ec99545c3a5775ec665..3afb30cf24dbf12dc6ff06b7d8050fbd9aba0c09 100644 (file)
@@ -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):
index fa5add7efcfb509b367af27c3d0e7c98310dae83..5d1fbb3f0310e9ab20bad46f0e83907924e2a4b2 100644 (file)
@@ -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,