dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 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 |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 22 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 23 | from tempest import exceptions |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 24 | |
| 25 | |
Rohit Karajgi | cb5d954 | 2011-12-02 14:17:06 -0800 | [diff] [blame] | 26 | def rand_name(name='test'): |
Ionuț Arțăriși | 6ec6fc2 | 2012-09-25 16:26:35 +0200 | [diff] [blame] | 27 | return name + str(random.randint(1, 999999)) |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 28 | |
| 29 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 30 | def build_url(host, port, api_version=None, path=None, |
| 31 | params=None, use_ssl=False): |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 32 | """Build the request URL from given host, port, path and parameters""" |
| 33 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 34 | pattern = 'v\d\.\d' |
| 35 | if re.match(pattern, path): |
| 36 | message = 'Version should not be included in path.' |
| 37 | raise exceptions.InvalidConfiguration(message=message) |
| 38 | |
donald-ngo | 7fb1efa | 2011-12-13 17:17:36 -0800 | [diff] [blame] | 39 | if use_ssl: |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 40 | url = "https://" + host |
| 41 | else: |
| 42 | url = "http://" + host |
| 43 | |
| 44 | if port is not None: |
| 45 | url += ":" + port |
| 46 | url += "/" |
| 47 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 48 | if api_version is not None: |
| 49 | url += api_version + "/" |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 50 | |
| 51 | if path is not None: |
| 52 | url += path |
| 53 | |
| 54 | if params is not None: |
| 55 | url += "?" |
| 56 | url += urllib.urlencode(params) |
| 57 | |
| 58 | return url |
Rohit Karajgi | aeddf63 | 2012-05-04 05:39:13 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def parse_image_id(image_ref): |
| 62 | """Return the image id from a given image ref""" |
Jaroslav Henner | 7ccda8c | 2012-12-03 16:25:25 +0100 | [diff] [blame] | 63 | return image_ref.rsplit('/')[-1] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def arbitrary_string(size=4, base_text=None): |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 67 | """ |
| 68 | Return size characters from base_text, repeating the base_text infinitely |
| 69 | if needed. |
| 70 | """ |
| 71 | if not base_text: |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 72 | base_text = 'test' |
Jaroslav Henner | 47737d8 | 2012-12-03 15:59:20 +0100 | [diff] [blame] | 73 | return ''.join(itertools.islice(itertools.cycle(base_text), size)) |