diff --git a/tempest_lib/common/utils/data_utils.py b/tempest_lib/common/utils/data_utils.py index c900d4d..046dd6c 100644 --- a/tempest_lib/common/utils/data_utils.py +++ b/tempest_lib/common/utils/data_utils.py @@ -16,6 +16,7 @@ import itertools import netaddr import random +import string import uuid @@ -56,6 +57,28 @@ def rand_name(name='', prefix=None): return rand_name +def rand_password(length=15): + """Generate a random password + + :param int length: The length of password that you expect to set + :return: a random password. The format is + '-- + -' + (e.g. 'G2*ac8&lKFFgh%2') + :rtype: string + """ + upper = random.choice(string.ascii_uppercase) + ascii_char = string.ascii_letters + digits = string.digits + digit = random.choice(string.digits) + puncs = '~!@#$%^&*_=+' + punc = random.choice(puncs) + seed = ascii_char + digits + puncs + pre = upper + digit + punc + password = pre + ''.join(random.choice(seed) for x in range(length - 3)) + return password + + def rand_url(): """Generate a random url that inclues a random number diff --git a/tempest_lib/tests/common/utils/test_data_utils.py b/tempest_lib/tests/common/utils/test_data_utils.py index e8a7c33..81dee57 100644 --- a/tempest_lib/tests/common/utils/test_data_utils.py +++ b/tempest_lib/tests/common/utils/test_data_utils.py @@ -56,6 +56,20 @@ class TestDataUtils(base.TestCase): actual2 = data_utils.rand_name(prefix='prefix-str') self.assertNotEqual(actual, actual2) + def test_rand_password(self): + actual = data_utils.rand_password() + self.assertIsInstance(actual, str) + self.assertRegexpMatches(actual, "[A-Za-z0-9~!@#$%^&*_=+]{15,}") + actual2 = data_utils.rand_password() + self.assertNotEqual(actual, actual2) + + def test_rand_password_with_len(self): + actual = data_utils.rand_password(8) + self.assertIsInstance(actual, str) + self.assertRegexpMatches(actual, "[A-Za-z0-9~!@#$%^&*_=+]{8}") + actual2 = data_utils.rand_password(8) + self.assertNotEqual(actual, actual2) + def test_rand_url(self): actual = data_utils.rand_url() self.assertIsInstance(actual, str)