From 71fac629e293e2337e12ca8d6c95c166b3b21409 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Thu, 1 Dec 2016 04:52:02 -0800 Subject: [PATCH] Use get_random_string from neutron-lib Use the method provided by neutron-lib and delete the duplicated neutron one. NeutronLibImpact Change-Id: I3a62d8f58862ab978102ac205b1359424e225eeb --- neutron/common/utils.py | 6 ------ .../drivers/openvswitch/agent/test_ovsdb_handler.py | 3 ++- .../exclusive_resources/test_resource_allocator.py | 4 ++-- neutron/tests/tools.py | 7 +------ neutron/tests/unit/common/test_utils.py | 10 ---------- neutron/tests/unit/db/test_migration.py | 3 ++- neutron/tests/unit/objects/test_base.py | 11 ++++++----- 7 files changed, 13 insertions(+), 31 deletions(-) diff --git a/neutron/common/utils.py b/neutron/common/utils.py index 5ed4a590f38..dc7351329af 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -150,12 +150,6 @@ def get_random_mac(base_mac): return ':'.join(["%02x" % x for x in mac]) -@removals.remove( - message="Use get_random_string from neutron_lib.utils.helpers") -def get_random_string(length): - return helpers.get_random_string(length) - - def get_dhcp_agent_device_id(network_id, host): # Split host so as to always use only the hostname and # not the domain name. This will guarantee consistency diff --git a/neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py b/neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py index 39885b16e8b..32d9d8df06b 100644 --- a/neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py +++ b/neutron/tests/functional/services/trunk/drivers/openvswitch/agent/test_ovsdb_handler.py @@ -16,6 +16,7 @@ import mock from neutron_lib import constants as n_consts +from neutron_lib.utils import helpers from oslo_utils import uuidutils from neutron.agent.common import ovs_lib @@ -27,7 +28,7 @@ from neutron.tests.functional.agent.l2 import base def generate_tap_device_name(): - return n_consts.TAP_DEVICE_PREFIX + common_utils.get_random_string( + return n_consts.TAP_DEVICE_PREFIX + helpers.get_random_string( n_consts.DEVICE_NAME_MAX_LEN - len(n_consts.TAP_DEVICE_PREFIX)) diff --git a/neutron/tests/functional/tests/common/exclusive_resources/test_resource_allocator.py b/neutron/tests/functional/tests/common/exclusive_resources/test_resource_allocator.py index eea791b08a8..0db05015e50 100644 --- a/neutron/tests/functional/tests/common/exclusive_resources/test_resource_allocator.py +++ b/neutron/tests/functional/tests/common/exclusive_resources/test_resource_allocator.py @@ -14,9 +14,9 @@ import os +from neutron_lib.utils import helpers import testtools -from neutron.common import utils from neutron.tests.common.exclusive_resources import resource_allocator from neutron.tests.functional import base @@ -32,7 +32,7 @@ class TestResourceAllocator(base.BaseLoggingTestCase): def setUp(self): super(TestResourceAllocator, self).setUp() self.ra = resource_allocator.ResourceAllocator( - utils.get_random_string(6), lambda: 42) + helpers.get_random_string(6), lambda: 42) self.addCleanup(safe_remove_file, self.ra._state_file_path) def test_allocate_and_release(self): diff --git a/neutron/tests/tools.py b/neutron/tests/tools.py index 488d7ddb537..548f111ebbc 100644 --- a/neutron/tests/tools.py +++ b/neutron/tests/tools.py @@ -18,7 +18,6 @@ import datetime import os import platform import random -import string import time import warnings @@ -202,12 +201,8 @@ class UnorderedList(list): return not self == other -def get_random_string(n=10): - return ''.join(random.choice(string.ascii_lowercase) for _ in range(n)) - - def get_random_string_list(i=3, n=5): - return [get_random_string(n) for _ in range(0, i)] + return [helpers.get_random_string(n) for _ in range(0, i)] def get_random_boolean(): diff --git a/neutron/tests/unit/common/test_utils.py b/neutron/tests/unit/common/test_utils.py index 1a26fbf5368..f334b21f40d 100644 --- a/neutron/tests/unit/common/test_utils.py +++ b/neutron/tests/unit/common/test_utils.py @@ -14,7 +14,6 @@ import os.path import random -import re import sys import eventlet @@ -689,15 +688,6 @@ class TestRoundVal(base.BaseTestCase): self.assertEqual(expected, utils.round_val(value)) -class TestGetRandomString(base.BaseTestCase): - def test_get_random_string(self): - length = 127 - random_string = utils.get_random_string(length) - self.assertEqual(length, len(random_string)) - regex = re.compile('^[0-9a-fA-F]+$') - self.assertIsNotNone(regex.match(random_string)) - - class TestSafeDecodeUtf8(base.BaseTestCase): @helpers.requires_py2 diff --git a/neutron/tests/unit/db/test_migration.py b/neutron/tests/unit/db/test_migration.py index 09d7121be61..0d7d9afb7ca 100644 --- a/neutron/tests/unit/db/test_migration.py +++ b/neutron/tests/unit/db/test_migration.py @@ -25,6 +25,7 @@ from alembic.operations import ops as alembic_ops from alembic import script as alembic_script import fixtures import mock +from neutron_lib.utils import helpers from oslo_utils import fileutils import pkg_resources import sqlalchemy as sa @@ -51,7 +52,7 @@ class FakeRevision(object): self.branch_labels = labels self.down_revision = down_revision self.is_branch_point = is_branch_point - self.revision = tools.get_random_string() + self.revision = helpers.get_random_string(10) self.module = mock.MagicMock() diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index c13d2720cfc..28d7d860d8f 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -19,6 +19,7 @@ import random import mock import netaddr from neutron_lib import exceptions as n_exc +from neutron_lib.utils import helpers from oslo_db import exception as obj_exc from oslo_db.sqlalchemy import utils as db_utils from oslo_utils import timeutils @@ -374,14 +375,14 @@ def get_list_of_random_networks(num=10): def get_random_domain_name(): return '.'.join([ - tools.get_random_string(62)[:random.choice(range(63))] + helpers.get_random_string(62)[:random.choice(range(63))] for i in range(4) ]) def get_random_dict_of_strings(): return { - tools.get_random_string(): tools.get_random_string() + helpers.get_random_string(10): helpers.get_random_string(10) for i in range(10) } @@ -397,7 +398,7 @@ FIELD_TYPE_VALUE_GENERATOR_MAP = { obj_fields.BooleanField: tools.get_random_boolean, obj_fields.DateTimeField: tools.get_random_datetime, obj_fields.IntegerField: tools.get_random_integer, - obj_fields.StringField: tools.get_random_string, + obj_fields.StringField: lambda: helpers.get_random_string(10), obj_fields.ListOfStringsField: tools.get_random_string_list, common_types.UUIDField: uuidutils.generate_uuid, obj_fields.ObjectField: lambda: None, @@ -1217,7 +1218,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase, self._network.create() def _create_network(self): - name = "test-network-%s" % tools.get_random_string(4) + name = "test-network-%s" % helpers.get_random_string(4) _network = net_obj.Network(self.context, name=name) _network.create() @@ -1309,7 +1310,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase, def _create_test_standard_attribute(self): attrs = { 'id': tools.get_random_integer(), - 'resource_type': tools.get_random_string(4), + 'resource_type': helpers.get_random_string(4), 'revision_number': tools.get_random_integer() } self._standard_attribute = obj_db_api.create_object(