blob: 4f93e1c46b5e6b361c9ec6b6b713175b3a1fd1e9 [file] [log] [blame]
dwalleck5d734432012-10-04 01:11:47 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
dwalleck5d734432012-10-04 01:11:47 -05004# 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 Treinisha83a16e2012-12-07 13:44:02 -050018import itertools
Daryl Walleck1465d612011-11-02 02:22:15 -050019import random
Daryl Walleck587385b2012-03-03 13:00:26 -060020import re
Rohit Karajgie1b050d2011-12-02 16:13:18 -080021import urllib
Attila Fazekasa709b762013-10-08 11:52:44 +020022import uuid
Jaroslav Henner47737d82012-12-03 15:59:20 +010023
Daryl Walleck587385b2012-03-03 13:00:26 -060024from tempest import exceptions
Daryl Walleck1465d612011-11-02 02:22:15 -050025
26
Attila Fazekasa709b762013-10-08 11:52:44 +020027def rand_uuid():
28 return str(uuid.uuid4())
29
30
31def rand_uuid_hex():
32 return uuid.uuid4().hex
33
34
Rohit Karajgicb5d9542011-12-02 14:17:06 -080035def rand_name(name='test'):
Masayuki Igawa4a2431d2013-09-17 12:27:02 +090036 return name + "-tempest-" + str(random.randint(1, 0x7fffffff))
Rohit Karajgie1b050d2011-12-02 16:13:18 -080037
38
David Kranz88d4f7c2013-03-27 11:16:37 -040039def rand_int_id(start=0, end=0x7fffffff):
Chris Yeoh8abacf32013-01-21 17:08:32 +103040 return random.randint(start, end)
41
42
Daryl Walleck587385b2012-03-03 13:00:26 -060043def build_url(host, port, api_version=None, path=None,
44 params=None, use_ssl=False):
Sean Daguef237ccb2013-01-04 15:19:14 -050045 """Build the request URL from given host, port, path and parameters."""
Rohit Karajgie1b050d2011-12-02 16:13:18 -080046
Daryl Walleck587385b2012-03-03 13:00:26 -060047 pattern = 'v\d\.\d'
48 if re.match(pattern, path):
49 message = 'Version should not be included in path.'
50 raise exceptions.InvalidConfiguration(message=message)
51
donald-ngo7fb1efa2011-12-13 17:17:36 -080052 if use_ssl:
Rohit Karajgie1b050d2011-12-02 16:13:18 -080053 url = "https://" + host
54 else:
55 url = "http://" + host
56
57 if port is not None:
58 url += ":" + port
59 url += "/"
60
Daryl Walleck587385b2012-03-03 13:00:26 -060061 if api_version is not None:
62 url += api_version + "/"
Rohit Karajgie1b050d2011-12-02 16:13:18 -080063
64 if path is not None:
65 url += path
66
67 if params is not None:
68 url += "?"
69 url += urllib.urlencode(params)
70
71 return url
Rohit Karajgiaeddf632012-05-04 05:39:13 -070072
73
74def parse_image_id(image_ref):
Sean Daguef237ccb2013-01-04 15:19:14 -050075 """Return the image id from a given image ref."""
Chris Yeohfc9e3332013-01-21 09:28:13 +103076 return image_ref.rsplit('/')[-1]
dwalleck5d734432012-10-04 01:11:47 -050077
78
79def arbitrary_string(size=4, base_text=None):
Jaroslav Henner47737d82012-12-03 15:59:20 +010080 """
81 Return size characters from base_text, repeating the base_text infinitely
82 if needed.
83 """
84 if not base_text:
dwalleck5d734432012-10-04 01:11:47 -050085 base_text = 'test'
Jaroslav Henner47737d82012-12-03 15:59:20 +010086 return ''.join(itertools.islice(itertools.cycle(base_text), size))