diff --git a/neutron/common/utils.py b/neutron/common/utils.py index da003b9ae5d..1fe6ab1ce5c 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -312,7 +312,8 @@ def get_random_string(length): rndstr = "" random.seed(datetime.datetime.now().microsecond) while len(rndstr) < length: - rndstr += hashlib.sha224(str(random.random())).hexdigest() + base_str = str(random.random()).encode('utf-8') + rndstr += hashlib.sha224(base_str).hexdigest() return rndstr[0:length] diff --git a/neutron/tests/unit/common/test_utils.py b/neutron/tests/unit/common/test_utils.py index 973a938dc71..90992606f9d 100644 --- a/neutron/tests/unit/common/test_utils.py +++ b/neutron/tests/unit/common/test_utils.py @@ -13,6 +13,7 @@ # under the License. import errno +import re import eventlet import mock @@ -706,3 +707,12 @@ class TestRoundVal(base.BaseTestCase): (1, 1.49), (2, 1.5)): 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))