]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
/common/utils.py py34 incompatibility
authorBogdan Tabor <bogdan.tabor@ericpol.com>
Wed, 23 Sep 2015 08:52:25 +0000 (10:52 +0200)
committerarmando-migliaccio <armamig@gmail.com>
Fri, 2 Oct 2015 16:26:08 +0000 (09:26 -0700)
Fixes bug found during unit test of get_random_string() method.
For Python3 it is required to encode string as bytes object
before hashing.

Related-blueprint: neutron-python3
Closes-bug: #1502148
Change-Id: I93061a831b45fc8b3ede0211af665ce02f36f38a
(cherry picked from commit 3d54459c2971f01581354c8a6b9c58abea7743d8)

neutron/common/utils.py
neutron/tests/unit/common/test_utils.py

index da003b9ae5dda7aa08b38309974eeac3b66c4ee4..1fe6ab1ce5ca9fdbaa135ccf627c6acde94ce44b 100644 (file)
@@ -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]
 
index 973a938dc7130cb45e9df8af40b30ae050350305..90992606f9d82625568618ffe3d9bbdda553ec1c 100644 (file)
@@ -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))