Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import itertools |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | import random |
| 18 | import string |
| 19 | import uuid |
| 20 | |
janonymous | 69413b9 | 2016-12-06 13:34:19 +0530 | [diff] [blame] | 21 | from oslo_utils import uuidutils |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 22 | import six.moves |
| 23 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 24 | |
| 25 | def rand_uuid(): |
| 26 | """Generate a random UUID string |
| 27 | |
| 28 | :return: a random UUID (e.g. '1dc12c7d-60eb-4b61-a7a2-17cf210155b6') |
| 29 | :rtype: string |
| 30 | """ |
janonymous | 69413b9 | 2016-12-06 13:34:19 +0530 | [diff] [blame] | 31 | return uuidutils.generate_uuid() |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def rand_uuid_hex(): |
| 35 | """Generate a random UUID hex string |
| 36 | |
| 37 | :return: a random UUID (e.g. '0b98cf96d90447bda4b46f31aeb1508c') |
| 38 | :rtype: string |
| 39 | """ |
| 40 | return uuid.uuid4().hex |
| 41 | |
| 42 | |
Ken'ichi Ohmichi | 11cf2c5 | 2017-02-28 14:50:44 -0800 | [diff] [blame] | 43 | def rand_name(name='', prefix='tempest'): |
zhufl | 0892cb2 | 2016-05-06 14:46:00 +0800 | [diff] [blame] | 44 | """Generate a random name that includes a random number |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 45 | |
| 46 | :param str name: The name that you want to include |
| 47 | :param str prefix: The prefix that you want to include |
| 48 | :return: a random name. The format is |
guo yunxian | 1d840a6 | 2016-07-19 19:30:23 +0800 | [diff] [blame] | 49 | '<prefix>-<name>-<random number>'. |
| 50 | (e.g. 'prefixfoo-namebar-154876201') |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 51 | :rtype: string |
| 52 | """ |
Terry Wilson | ad4c9dd | 2018-12-18 21:26:40 +0000 | [diff] [blame] | 53 | rand_name = str(random.randint(1, 0x7fffffff)) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 54 | if name: |
| 55 | rand_name = name + '-' + rand_name |
| 56 | if prefix: |
| 57 | rand_name = prefix + '-' + rand_name |
| 58 | return rand_name |
| 59 | |
| 60 | |
| 61 | def rand_password(length=15): |
| 62 | """Generate a random password |
| 63 | |
| 64 | :param int length: The length of password that you expect to set |
| 65 | (If it's smaller than 3, it's same as 3.) |
| 66 | :return: a random password. The format is |
Sergey Vilgelm | eac094a | 2018-11-21 18:27:51 -0600 | [diff] [blame] | 67 | ``'<random upper letter>-<random number>-<random special character> |
| 68 | -<random ascii letters or digit characters or special symbols>'`` |
| 69 | (e.g. ``G2*ac8&lKFFgh%2``) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 70 | :rtype: string |
| 71 | """ |
| 72 | upper = random.choice(string.ascii_uppercase) |
| 73 | ascii_char = string.ascii_letters |
| 74 | digits = string.digits |
| 75 | digit = random.choice(string.digits) |
user | ca01431 | 2016-07-25 16:18:05 +0300 | [diff] [blame] | 76 | puncs = '~!@#%^&*_=+' |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 77 | punc = random.choice(puncs) |
| 78 | seed = ascii_char + digits + puncs |
| 79 | pre = upper + digit + punc |
| 80 | password = pre + ''.join(random.choice(seed) for x in range(length - 3)) |
| 81 | return password |
| 82 | |
| 83 | |
| 84 | def rand_url(): |
zhufl | 0892cb2 | 2016-05-06 14:46:00 +0800 | [diff] [blame] | 85 | """Generate a random url that includes a random number |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 86 | |
| 87 | :return: a random url. The format is 'https://url-<random number>.com'. |
| 88 | (e.g. 'https://url-154876201.com') |
| 89 | :rtype: string |
| 90 | """ |
| 91 | randbits = str(random.randint(1, 0x7fffffff)) |
| 92 | return 'https://url-' + randbits + '.com' |
| 93 | |
| 94 | |
| 95 | def rand_int_id(start=0, end=0x7fffffff): |
| 96 | """Generate a random integer value |
| 97 | |
| 98 | :param int start: The value that you expect to start here |
| 99 | :param int end: The value that you expect to end here |
| 100 | :return: a random integer value |
| 101 | :rtype: int |
| 102 | """ |
| 103 | return random.randint(start, end) |
| 104 | |
| 105 | |
| 106 | def rand_mac_address(): |
| 107 | """Generate an Ethernet MAC address |
| 108 | |
| 109 | :return: an random Ethernet MAC address |
| 110 | :rtype: string |
| 111 | """ |
| 112 | # NOTE(vish): We would prefer to use 0xfe here to ensure that linux |
| 113 | # bridge mac addresses don't change, but it appears to |
| 114 | # conflict with libvirt, so we use the next highest octet |
| 115 | # that has the unicast and locally administered bits set |
| 116 | # properly: 0xfa. |
| 117 | # Discussion: https://bugs.launchpad.net/nova/+bug/921838 |
| 118 | mac = [0xfa, 0x16, 0x3e, |
| 119 | random.randint(0x00, 0xff), |
| 120 | random.randint(0x00, 0xff), |
| 121 | random.randint(0x00, 0xff)] |
| 122 | return ':'.join(["%02x" % x for x in mac]) |
| 123 | |
| 124 | |
Lenny Verkhovsky | 92b46e3 | 2016-03-14 13:14:58 +0200 | [diff] [blame] | 125 | def rand_infiniband_guid_address(): |
| 126 | """Generate an Infiniband GUID address |
| 127 | |
| 128 | :return: an random Infiniband GUID address |
| 129 | :rtype: string |
| 130 | """ |
| 131 | guid = [] |
| 132 | for i in range(8): |
| 133 | guid.append("%02x" % random.randint(0x00, 0xff)) |
| 134 | return ':'.join(guid) |
| 135 | |
| 136 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 137 | def parse_image_id(image_ref): |
| 138 | """Return the image id from a given image ref |
| 139 | |
| 140 | This function just returns the last word of the given image ref string |
| 141 | splitting with '/'. |
| 142 | :param str image_ref: a string that includes the image id |
| 143 | :return: the image id string |
| 144 | :rtype: string |
| 145 | """ |
| 146 | return image_ref.rsplit('/')[-1] |
| 147 | |
| 148 | |
| 149 | def arbitrary_string(size=4, base_text=None): |
| 150 | """Return size characters from base_text |
| 151 | |
| 152 | This generates a string with an arbitrary number of characters, generated |
| 153 | by looping the base_text string. If the size is smaller than the size of |
zhangyanxian | 438824a | 2016-09-20 09:29:40 +0000 | [diff] [blame] | 154 | base_text, returning string is shrunk to the size. |
Pablo Sanchez | deabf43 | 2016-06-23 09:58:42 +0200 | [diff] [blame] | 155 | :param int size: a returning characters size |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 156 | :param str base_text: a string you want to repeat |
| 157 | :return: size string |
| 158 | :rtype: string |
| 159 | """ |
| 160 | if not base_text: |
| 161 | base_text = 'test' |
| 162 | return ''.join(itertools.islice(itertools.cycle(base_text), size)) |
| 163 | |
| 164 | |
| 165 | def random_bytes(size=1024): |
| 166 | """Return size randomly selected bytes as a string |
| 167 | |
| 168 | :param int size: a returning bytes size |
| 169 | :return: size randomly bytes |
| 170 | :rtype: string |
| 171 | """ |
Sirushti Murugesan | 12dc973 | 2016-07-13 22:49:17 +0530 | [diff] [blame] | 172 | return b''.join([six.int2byte(random.randint(0, 255)) |
afazekas | 40fcb9b | 2019-03-08 11:25:11 +0100 | [diff] [blame] | 173 | for i in range(size)]) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 174 | |
| 175 | |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 176 | # Courtesy of http://stackoverflow.com/a/312464 |
| 177 | def chunkify(sequence, chunksize): |
| 178 | """Yield successive chunks from `sequence`.""" |
Sirushti Murugesan | 12dc973 | 2016-07-13 22:49:17 +0530 | [diff] [blame] | 179 | for i in range(0, len(sequence), chunksize): |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 180 | yield sequence[i:i + chunksize] |