blob: 41b3470bead0b4c74d7797576d0318560e31146b [file] [log] [blame]
Andrea Frittolif5da28b2013-12-06 07:08:07 +00001# Copyright 2013 Hewlett-Packard, Ltd.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Matthew Treinishbeae8602014-04-08 17:53:06 -040015
Matthew Treinishbeae8602014-04-08 17:53:06 -040016import re
17import string
18import unicodedata
19
Matthew Treinish21905512015-07-13 10:33:35 -040020from oslo_serialization import jsonutils as json
Matthew Treinish01472ff2015-02-20 17:26:52 -050021from tempest_lib.common.utils import misc
Matthew Treinisha0048cb2014-04-08 17:44:42 -040022import testscenarios
23import testtools
24
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000025from tempest import clients
Matthew Treinish33b6df02015-05-04 13:55:59 -040026from tempest.common import credentials
Andrea Frittolif5da28b2013-12-06 07:08:07 +000027from tempest import config
Matthew Treinisha0f820f2014-09-16 11:25:34 -040028from tempest import exceptions
Andrea Frittolif5da28b2013-12-06 07:08:07 +000029
Andrea Frittolif5da28b2013-12-06 07:08:07 +000030CONF = config.CONF
31
32
Andrea Frittolif5da28b2013-12-06 07:08:07 +000033class ImageUtils(object):
34
35 default_ssh_user = 'root'
36
Matthew Treinish96cadf42015-05-14 19:45:59 -040037 def __init__(self, os):
Andrea Frittolif5da28b2013-12-06 07:08:07 +000038 # Load configuration items
39 self.ssh_users = json.loads(CONF.input_scenario.ssh_user_regex)
40 self.non_ssh_image_pattern = \
41 CONF.input_scenario.non_ssh_image_regex
42 # Setup clients
Andrea Frittolife1e2432014-09-25 10:39:37 +010043 self.images_client = os.images_client
44 self.flavors_client = os.flavors_client
Andrea Frittolif5da28b2013-12-06 07:08:07 +000045
46 def ssh_user(self, image_id):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000047 _image = self.images_client.show_image(image_id)
Andrea Frittolif5da28b2013-12-06 07:08:07 +000048 for regex, user in self.ssh_users:
49 # First match wins
Andrea Frittolife1e2432014-09-25 10:39:37 +010050 if re.match(regex, _image['name']) is not None:
Andrea Frittolif5da28b2013-12-06 07:08:07 +000051 return user
52 else:
53 return self.default_ssh_user
54
55 def _is_sshable_image(self, image):
56 return not re.search(pattern=self.non_ssh_image_pattern,
Andrea Frittolife1e2432014-09-25 10:39:37 +010057 string=str(image['name']))
Andrea Frittolif5da28b2013-12-06 07:08:07 +000058
59 def is_sshable_image(self, image_id):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000060 _image = self.images_client.show_image(image_id)
Andrea Frittolif5da28b2013-12-06 07:08:07 +000061 return self._is_sshable_image(_image)
62
63 def _is_flavor_enough(self, flavor, image):
Andrea Frittolife1e2432014-09-25 10:39:37 +010064 return image['minDisk'] <= flavor['disk']
Andrea Frittolif5da28b2013-12-06 07:08:07 +000065
66 def is_flavor_enough(self, flavor_id, image_id):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000067 _image = self.images_client.show_image(image_id)
ghanshyam19973be2015-08-18 15:46:42 +090068 _flavor = self.flavors_client.show_flavor(flavor_id)['flavor']
Andrea Frittolif5da28b2013-12-06 07:08:07 +000069 return self._is_flavor_enough(_flavor, _image)
70
71
72@misc.singleton
73class InputScenarioUtils(object):
74
75 """
76 Example usage:
77
78 import testscenarios
79 (...)
80 load_tests = testscenarios.load_tests_apply_scenarios
81
82
Andrea Frittolife1e2432014-09-25 10:39:37 +010083 class TestInputScenario(manager.ScenarioTest):
Andrea Frittolif5da28b2013-12-06 07:08:07 +000084
Matthew Treinishd75edef2014-04-11 15:57:16 -040085 scenario_utils = utils.InputScenarioUtils()
Andrea Frittolif5da28b2013-12-06 07:08:07 +000086 scenario_flavor = scenario_utils.scenario_flavors
87 scenario_image = scenario_utils.scenario_images
88 scenarios = testscenarios.multiply_scenarios(scenario_image,
89 scenario_flavor)
90
91 def test_create_server_metadata(self):
92 name = rand_name('instance')
Andrea Frittolife1e2432014-09-25 10:39:37 +010093 self.servers_client.create_server(name=name,
94 flavor_ref=self.flavor_ref,
95 image_ref=self.image_ref)
Andrea Frittolif5da28b2013-12-06 07:08:07 +000096 """
97 validchars = "-_.{ascii}{digit}".format(ascii=string.ascii_letters,
98 digit=string.digits)
99
100 def __init__(self):
Matthew Treinish522c63b2015-05-13 09:47:58 -0400101 network_resources = {
102 'network': False,
103 'router': False,
104 'subnet': False,
105 'dhcp': False,
106 }
Matthew Treinish33b6df02015-05-04 13:55:59 -0400107 self.isolated_creds = credentials.get_isolated_credentials(
108 name='InputScenarioUtils',
Matthew Treinish522c63b2015-05-13 09:47:58 -0400109 identity_version=CONF.identity.auth_version,
110 network_resources=network_resources)
Matthew Treinish33b6df02015-05-04 13:55:59 -0400111 os = clients.Manager(self.isolated_creds.get_primary_creds())
Andrea Frittolife1e2432014-09-25 10:39:37 +0100112 self.images_client = os.images_client
113 self.flavors_client = os.flavors_client
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000114 self.image_pattern = CONF.input_scenario.image_regex
115 self.flavor_pattern = CONF.input_scenario.flavor_regex
116
117 def _normalize_name(self, name):
118 nname = unicodedata.normalize('NFKD', name).encode('ASCII', 'ignore')
119 nname = ''.join(c for c in nname if c in self.validchars)
120 return nname
121
Matthew Treinish96cadf42015-05-14 19:45:59 -0400122 def clear_creds(self):
123 self.isolated_creds.clear_isolated_creds()
124
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000125 @property
126 def scenario_images(self):
127 """
128 :return: a scenario with name and uuid of images
129 """
Andrea Frittolie9674c32014-02-07 20:01:46 +0000130 if not CONF.service_available.glance:
131 return []
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000132 if not hasattr(self, '_scenario_images'):
Matthew Treinish67e570c2015-02-18 16:59:39 +0000133 try:
134 images = self.images_client.list_images()
135 self._scenario_images = [
136 (self._normalize_name(i['name']), dict(image_ref=i['id']))
137 for i in images if re.search(self.image_pattern,
138 str(i['name']))
139 ]
140 except Exception:
141 self._scenario_images = []
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000142 return self._scenario_images
143
144 @property
145 def scenario_flavors(self):
146 """
147 :return: a scenario with name and uuid of flavors
148 """
149 if not hasattr(self, '_scenario_flavors'):
Matthew Treinish67e570c2015-02-18 16:59:39 +0000150 try:
ghanshyam19973be2015-08-18 15:46:42 +0900151 flavors = self.flavors_client.list_flavors()['flavors']
Matthew Treinish67e570c2015-02-18 16:59:39 +0000152 self._scenario_flavors = [
153 (self._normalize_name(f['name']), dict(flavor_ref=f['id']))
154 for f in flavors if re.search(self.flavor_pattern,
155 str(f['name']))
156 ]
157 except Exception:
158 self._scenario_flavors = []
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000159 return self._scenario_flavors
Matthew Treinisha0048cb2014-04-08 17:44:42 -0400160
161
162def load_tests_input_scenario_utils(*args):
163 """
164 Wrapper for testscenarios to set the scenarios to avoid running a getattr
165 on the CONF object at import.
166 """
167 if getattr(args[0], 'suiteClass', None) is not None:
168 loader, standard_tests, pattern = args
169 else:
170 standard_tests, module, loader = args
Matthew Treinish96cadf42015-05-14 19:45:59 -0400171 output = None
172 scenario_utils = None
Matthew Treinisha0f820f2014-09-16 11:25:34 -0400173 try:
174 scenario_utils = InputScenarioUtils()
175 scenario_flavor = scenario_utils.scenario_flavors
176 scenario_image = scenario_utils.scenario_images
Matthew Treinish33b6df02015-05-04 13:55:59 -0400177 except (exceptions.InvalidConfiguration, TypeError):
Matthew Treinish96cadf42015-05-14 19:45:59 -0400178 output = standard_tests
179 finally:
180 if scenario_utils:
181 scenario_utils.clear_creds()
182 if output is not None:
183 return output
Matthew Treinisha0048cb2014-04-08 17:44:42 -0400184 for test in testtools.iterate_tests(standard_tests):
185 setattr(test, 'scenarios', testscenarios.multiply_scenarios(
186 scenario_image,
187 scenario_flavor))
188 return testscenarios.load_tests_apply_scenarios(*args)