dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 18 | import itertools |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 19 | import random |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 20 | import re |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 21 | import urllib |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 22 | import uuid |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 23 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 24 | from tempest import exceptions |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 25 | |
| 26 | |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 27 | def rand_uuid(): |
| 28 | return str(uuid.uuid4()) |
| 29 | |
| 30 | |
| 31 | def rand_uuid_hex(): |
| 32 | return uuid.uuid4().hex |
| 33 | |
| 34 | |
Rohit Karajgi | cb5d954 | 2011-12-02 14:17:06 -0800 | [diff] [blame] | 35 | def rand_name(name='test'): |
Masayuki Igawa | 4a2431d | 2013-09-17 12:27:02 +0900 | [diff] [blame] | 36 | return name + "-tempest-" + str(random.randint(1, 0x7fffffff)) |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 37 | |
| 38 | |
David Kranz | 88d4f7c | 2013-03-27 11:16:37 -0400 | [diff] [blame] | 39 | def rand_int_id(start=0, end=0x7fffffff): |
Chris Yeoh | 8abacf3 | 2013-01-21 17:08:32 +1030 | [diff] [blame] | 40 | return random.randint(start, end) |
| 41 | |
| 42 | |
Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [diff] [blame] | 43 | def rand_mac_address(): |
| 44 | """Generate an Ethernet MAC address.""" |
| 45 | # NOTE(vish): We would prefer to use 0xfe here to ensure that linux |
| 46 | # bridge mac addresses don't change, but it appears to |
| 47 | # conflict with libvirt, so we use the next highest octet |
| 48 | # that has the unicast and locally administered bits set |
| 49 | # properly: 0xfa. |
| 50 | # Discussion: https://bugs.launchpad.net/nova/+bug/921838 |
| 51 | mac = [0xfa, 0x16, 0x3e, |
| 52 | random.randint(0x00, 0xff), |
| 53 | random.randint(0x00, 0xff), |
| 54 | random.randint(0x00, 0xff)] |
| 55 | return ':'.join(["%02x" % x for x in mac]) |
| 56 | |
| 57 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 58 | def build_url(host, port, api_version=None, path=None, |
| 59 | params=None, use_ssl=False): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 60 | """Build the request URL from given host, port, path and parameters.""" |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 61 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 62 | pattern = 'v\d\.\d' |
| 63 | if re.match(pattern, path): |
| 64 | message = 'Version should not be included in path.' |
| 65 | raise exceptions.InvalidConfiguration(message=message) |
| 66 | |
donald-ngo | 7fb1efa | 2011-12-13 17:17:36 -0800 | [diff] [blame] | 67 | if use_ssl: |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 68 | url = "https://" + host |
| 69 | else: |
| 70 | url = "http://" + host |
| 71 | |
| 72 | if port is not None: |
| 73 | url += ":" + port |
| 74 | url += "/" |
| 75 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 76 | if api_version is not None: |
| 77 | url += api_version + "/" |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 78 | |
| 79 | if path is not None: |
| 80 | url += path |
| 81 | |
| 82 | if params is not None: |
| 83 | url += "?" |
| 84 | url += urllib.urlencode(params) |
| 85 | |
| 86 | return url |
Rohit Karajgi | aeddf63 | 2012-05-04 05:39:13 -0700 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def parse_image_id(image_ref): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 90 | """Return the image id from a given image ref.""" |
Chris Yeoh | fc9e333 | 2013-01-21 09:28:13 +1030 | [diff] [blame] | 91 | return image_ref.rsplit('/')[-1] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def arbitrary_string(size=4, base_text=None): |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 95 | """ |
| 96 | Return size characters from base_text, repeating the base_text infinitely |
| 97 | if needed. |
| 98 | """ |
| 99 | if not base_text: |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 100 | base_text = 'test' |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 101 | return ''.join(itertools.islice(itertools.cycle(base_text), size)) |