Make _generate_hostname() and int2base() work in python3

Change-Id: I371fe8791eee5f39aa228961c05f6c5c0f25f1bf
Closes-bug: #1604000
This commit is contained in:
Valerii Kovalchuk 2016-07-18 16:57:34 +03:00
parent 61f9587017
commit af5ad53697
4 changed files with 9 additions and 6 deletions

View File

@ -125,7 +125,7 @@ def int2base(x, base):
:param base: number base, max value is 36
:return: integer converted to the specified base
"""
digs = string.digits + string.lowercase
digs = string.digits + string.ascii_lowercase
if x < 0:
sign = -1
elif x == 0:
@ -136,7 +136,7 @@ def int2base(x, base):
digits = []
while x:
digits.append(digs[x % base])
x /= base
x //= base
if sign < 0:
digits.append('-')
digits.reverse()

View File

@ -54,7 +54,7 @@ def _generate_hostname(pattern, number):
counter = _random_string_counter or 1
# generate first 5 random chars
prefix = ''.join(random.choice(string.lowercase) for _ in range(5))
prefix = ''.join(random.choice(string.ascii_lowercase) for _ in range(5))
# convert timestamp to higher base to shorten hostname string
# (up to 8 chars)
timestamp = helpers.int2base(int(time.time() * 1000), 36)[:8]

View File

@ -22,3 +22,9 @@ class TestHelper(testtools.TestCase):
u'\u043d\u0435 \u0430\u0441\u043a\u0438']
for name in names:
self.assertIsInstance(helpers.to_str(name), str)
def test_int2base(self):
for x in range(30):
self.assertEqual("{0:b}".format(x), helpers.int2base(x, 2))
self.assertEqual("{0:o}".format(x), helpers.int2base(x, 8))
self.assertEqual("{0:x}".format(x), helpers.int2base(x, 16))

View File

@ -14,10 +14,8 @@
import re
import testtools
import unittest
from muranodashboard.dynamic_ui import yaql_functions
import six
class TestYAQLFunctions(testtools.TestCase):
@ -28,7 +26,6 @@ class TestYAQLFunctions(testtools.TestCase):
self.assertEqual(
yaql_functions._generate_hostname('foo-#', 22), 'foo-22')
@unittest.skipIf(six.PY3, "lp bug #1604000")
def test_generate_hostname_random(self):
random = yaql_functions._generate_hostname('', 3)
self.assertTrue(bool(re.match(r'^\w{14}$', random)))