blob: c5df5907d6184ab7fe6dec26e1d6ddfc788b256d [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# 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
16import itertools
Matthew Treinish9e26ca82016-02-23 11:43:20 -050017import random
18import string
19import uuid
20
janonymous69413b92016-12-06 13:34:19 +053021from oslo_utils import uuidutils
Jordan Pittier4408c4a2016-04-29 15:05:09 +020022import six.moves
23
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024
25def 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 """
janonymous69413b92016-12-06 13:34:19 +053031 return uuidutils.generate_uuid()
Matthew Treinish9e26ca82016-02-23 11:43:20 -050032
33
34def 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 Ohmichi11cf2c52017-02-28 14:50:44 -080043def rand_name(name='', prefix='tempest'):
zhufl0892cb22016-05-06 14:46:00 +080044 """Generate a random name that includes a random number
Matthew Treinish9e26ca82016-02-23 11:43:20 -050045
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 yunxian1d840a62016-07-19 19:30:23 +080049 '<prefix>-<name>-<random number>'.
50 (e.g. 'prefixfoo-namebar-154876201')
Matthew Treinish9e26ca82016-02-23 11:43:20 -050051 :rtype: string
52 """
53 randbits = str(random.randint(1, 0x7fffffff))
54 rand_name = randbits
55 if name:
56 rand_name = name + '-' + rand_name
57 if prefix:
58 rand_name = prefix + '-' + rand_name
59 return rand_name
60
61
62def rand_password(length=15):
63 """Generate a random password
64
65 :param int length: The length of password that you expect to set
66 (If it's smaller than 3, it's same as 3.)
67 :return: a random password. The format is
68 '<random upper letter>-<random number>-<random special character>
69 -<random ascii letters or digit characters or special symbols>'
70 (e.g. 'G2*ac8&lKFFgh%2')
71 :rtype: string
72 """
73 upper = random.choice(string.ascii_uppercase)
74 ascii_char = string.ascii_letters
75 digits = string.digits
76 digit = random.choice(string.digits)
userca014312016-07-25 16:18:05 +030077 puncs = '~!@#%^&*_=+'
Matthew Treinish9e26ca82016-02-23 11:43:20 -050078 punc = random.choice(puncs)
79 seed = ascii_char + digits + puncs
80 pre = upper + digit + punc
81 password = pre + ''.join(random.choice(seed) for x in range(length - 3))
82 return password
83
84
85def rand_url():
zhufl0892cb22016-05-06 14:46:00 +080086 """Generate a random url that includes a random number
Matthew Treinish9e26ca82016-02-23 11:43:20 -050087
88 :return: a random url. The format is 'https://url-<random number>.com'.
89 (e.g. 'https://url-154876201.com')
90 :rtype: string
91 """
92 randbits = str(random.randint(1, 0x7fffffff))
93 return 'https://url-' + randbits + '.com'
94
95
96def rand_int_id(start=0, end=0x7fffffff):
97 """Generate a random integer value
98
99 :param int start: The value that you expect to start here
100 :param int end: The value that you expect to end here
101 :return: a random integer value
102 :rtype: int
103 """
104 return random.randint(start, end)
105
106
107def rand_mac_address():
108 """Generate an Ethernet MAC address
109
110 :return: an random Ethernet MAC address
111 :rtype: string
112 """
113 # NOTE(vish): We would prefer to use 0xfe here to ensure that linux
114 # bridge mac addresses don't change, but it appears to
115 # conflict with libvirt, so we use the next highest octet
116 # that has the unicast and locally administered bits set
117 # properly: 0xfa.
118 # Discussion: https://bugs.launchpad.net/nova/+bug/921838
119 mac = [0xfa, 0x16, 0x3e,
120 random.randint(0x00, 0xff),
121 random.randint(0x00, 0xff),
122 random.randint(0x00, 0xff)]
123 return ':'.join(["%02x" % x for x in mac])
124
125
Lenny Verkhovsky92b46e32016-03-14 13:14:58 +0200126def rand_infiniband_guid_address():
127 """Generate an Infiniband GUID address
128
129 :return: an random Infiniband GUID address
130 :rtype: string
131 """
132 guid = []
133 for i in range(8):
134 guid.append("%02x" % random.randint(0x00, 0xff))
135 return ':'.join(guid)
136
137
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500138def parse_image_id(image_ref):
139 """Return the image id from a given image ref
140
141 This function just returns the last word of the given image ref string
142 splitting with '/'.
143 :param str image_ref: a string that includes the image id
144 :return: the image id string
145 :rtype: string
146 """
147 return image_ref.rsplit('/')[-1]
148
149
150def arbitrary_string(size=4, base_text=None):
151 """Return size characters from base_text
152
153 This generates a string with an arbitrary number of characters, generated
154 by looping the base_text string. If the size is smaller than the size of
zhangyanxian438824a2016-09-20 09:29:40 +0000155 base_text, returning string is shrunk to the size.
Pablo Sanchezdeabf432016-06-23 09:58:42 +0200156 :param int size: a returning characters size
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500157 :param str base_text: a string you want to repeat
158 :return: size string
159 :rtype: string
160 """
161 if not base_text:
162 base_text = 'test'
163 return ''.join(itertools.islice(itertools.cycle(base_text), size))
164
165
166def random_bytes(size=1024):
167 """Return size randomly selected bytes as a string
168
169 :param int size: a returning bytes size
170 :return: size randomly bytes
171 :rtype: string
172 """
Sirushti Murugesan12dc9732016-07-13 22:49:17 +0530173 return b''.join([six.int2byte(random.randint(0, 255))
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500174 for i in range(size)])
175
176
Jordan Pittier4408c4a2016-04-29 15:05:09 +0200177# Courtesy of http://stackoverflow.com/a/312464
178def chunkify(sequence, chunksize):
179 """Yield successive chunks from `sequence`."""
Sirushti Murugesan12dc9732016-07-13 22:49:17 +0530180 for i in range(0, len(sequence), chunksize):
Jordan Pittier4408c4a2016-04-29 15:05:09 +0200181 yield sequence[i:i + chunksize]