ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 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 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 16 | import itertools |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 17 | import random |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 18 | import uuid |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 19 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 20 | |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 21 | def rand_uuid(): |
| 22 | return str(uuid.uuid4()) |
| 23 | |
| 24 | |
| 25 | def rand_uuid_hex(): |
| 26 | return uuid.uuid4().hex |
| 27 | |
| 28 | |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 29 | def rand_name(name=''): |
| 30 | randbits = str(random.randint(1, 0x7fffffff)) |
| 31 | if name: |
| 32 | return name + '-' + randbits |
| 33 | else: |
| 34 | return randbits |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 35 | |
| 36 | |
David Kranz | 88d4f7c | 2013-03-27 11:16:37 -0400 | [diff] [blame] | 37 | def rand_int_id(start=0, end=0x7fffffff): |
Chris Yeoh | 8abacf3 | 2013-01-21 17:08:32 +1030 | [diff] [blame] | 38 | return random.randint(start, end) |
| 39 | |
| 40 | |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 41 | def rand_mac_address(): |
| 42 | """Generate an Ethernet MAC address.""" |
| 43 | # NOTE(vish): We would prefer to use 0xfe here to ensure that linux |
| 44 | # bridge mac addresses don't change, but it appears to |
| 45 | # conflict with libvirt, so we use the next highest octet |
| 46 | # that has the unicast and locally administered bits set |
| 47 | # properly: 0xfa. |
| 48 | # Discussion: https://bugs.launchpad.net/nova/+bug/921838 |
| 49 | mac = [0xfa, 0x16, 0x3e, |
| 50 | random.randint(0x00, 0xff), |
| 51 | random.randint(0x00, 0xff), |
| 52 | random.randint(0x00, 0xff)] |
| 53 | return ':'.join(["%02x" % x for x in mac]) |
| 54 | |
| 55 | |
Rohit Karajgi | aeddf63 | 2012-05-04 05:39:13 -0700 | [diff] [blame] | 56 | def parse_image_id(image_ref): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 57 | """Return the image id from a given image ref.""" |
Chris Yeoh | fc9e333 | 2013-01-21 09:28:13 +1030 | [diff] [blame] | 58 | return image_ref.rsplit('/')[-1] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def arbitrary_string(size=4, base_text=None): |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 62 | """ |
| 63 | Return size characters from base_text, repeating the base_text infinitely |
| 64 | if needed. |
| 65 | """ |
| 66 | if not base_text: |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 67 | base_text = 'test' |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 68 | return ''.join(itertools.islice(itertools.cycle(base_text), size)) |