blob: 1acd732f9283bace59b8ae58448303930f5a2be7 [file] [log] [blame]
Brianna Poulos011292a2017-03-15 16:24:38 -04001# Copyright 2012 OpenStack Foundation
2# Copyright 2013 IBM Corp.
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17from oslo_log import log
18
Brianna Poulos011292a2017-03-15 16:24:38 -040019from tempest.common import image as common_image
20from tempest.common import waiters
21from tempest import config
dane-fichter53f4fea2017-03-01 13:20:02 -050022from tempest import exceptions
Brianna Poulos011292a2017-03-15 16:24:38 -040023from tempest.lib.common.utils import data_utils
24from tempest.lib.common.utils import test_utils
25from tempest.lib import exceptions as lib_exc
Ihar Hrachyshkaecce1f62018-01-18 13:32:05 -080026from tempest.scenario import manager
Brianna Poulos011292a2017-03-15 16:24:38 -040027
28CONF = config.CONF
29
30LOG = log.getLogger(__name__)
31
32
Ihar Hrachyshka2d0cf0a2018-01-18 13:40:09 -080033# we inherit from NetworkScenarioTest since some test cases need access to
34# check_*_connectivity methods to validate instances are up and accessible
35class ScenarioTest(manager.NetworkScenarioTest):
Brianna Poulos011292a2017-03-15 16:24:38 -040036 """Base class for scenario tests. Uses tempest own clients. """
37
38 credentials = ['primary']
39
40 @classmethod
41 def setup_clients(cls):
42 super(ScenarioTest, cls).setup_clients()
43 # Clients (in alphabetical order)
Vu Cong Tuan17b61932017-06-21 17:52:43 +070044 cls.flavors_client = cls.os_primary.flavors_client
Brianna Poulos011292a2017-03-15 16:24:38 -040045 cls.compute_floating_ips_client = (
Vu Cong Tuan17b61932017-06-21 17:52:43 +070046 cls.os_primary.compute_floating_ips_client)
Brianna Poulos011292a2017-03-15 16:24:38 -040047 if CONF.service_available.glance:
48 # Check if glance v1 is available to determine which client to use.
49 if CONF.image_feature_enabled.api_v1:
Vu Cong Tuan17b61932017-06-21 17:52:43 +070050 cls.image_client = cls.os_primary.image_client
Brianna Poulos011292a2017-03-15 16:24:38 -040051 elif CONF.image_feature_enabled.api_v2:
Vu Cong Tuan17b61932017-06-21 17:52:43 +070052 cls.image_client = cls.os_primary.image_client_v2
Brianna Poulos011292a2017-03-15 16:24:38 -040053 else:
54 raise lib_exc.InvalidConfiguration(
55 'Either api_v1 or api_v2 must be True in '
56 '[image-feature-enabled].')
57 # Compute image client
Vu Cong Tuan17b61932017-06-21 17:52:43 +070058 cls.compute_images_client = cls.os_primary.compute_images_client
59 cls.keypairs_client = cls.os_primary.keypairs_client
Brianna Poulos011292a2017-03-15 16:24:38 -040060 # Nova security groups client
61 cls.compute_security_groups_client = (
Vu Cong Tuan17b61932017-06-21 17:52:43 +070062 cls.os_primary.compute_security_groups_client)
Brianna Poulos011292a2017-03-15 16:24:38 -040063 cls.compute_security_group_rules_client = (
Vu Cong Tuan17b61932017-06-21 17:52:43 +070064 cls.os_primary.compute_security_group_rules_client)
65 cls.servers_client = cls.os_primary.servers_client
Brianna Poulos011292a2017-03-15 16:24:38 -040066 # Neutron network client
Vu Cong Tuan17b61932017-06-21 17:52:43 +070067 cls.networks_client = cls.os_primary.networks_client
68 cls.ports_client = cls.os_primary.ports_client
69 cls.routers_client = cls.os_primary.routers_client
70 cls.subnets_client = cls.os_primary.subnets_client
71 cls.floating_ips_client = cls.os_primary.floating_ips_client
72 cls.security_groups_client = cls.os_primary.security_groups_client
Brianna Poulos011292a2017-03-15 16:24:38 -040073 cls.security_group_rules_client = (
Vu Cong Tuan17b61932017-06-21 17:52:43 +070074 cls.os_primary.security_group_rules_client)
Brianna Poulos011292a2017-03-15 16:24:38 -040075
Ghanshyam Mann48d10b82019-12-12 16:45:44 +000076 cls.volumes_client = cls.os_primary.volumes_client_latest
77 cls.snapshots_client = cls.os_primary.snapshots_client_latest
Brianna Poulos011292a2017-03-15 16:24:38 -040078
79 # ## Test functions library
80 #
81 # The create_[resource] functions only return body and discard the
82 # resp part which is not used in scenario tests
Brianna Poulos011292a2017-03-15 16:24:38 -040083
Brianna Poulos011292a2017-03-15 16:24:38 -040084 def _image_create(self, name, fmt, path,
85 disk_format=None, properties=None):
86 if properties is None:
87 properties = {}
88 name = data_utils.rand_name('%s-' % name)
89 params = {
90 'name': name,
91 'container_format': fmt,
92 'disk_format': disk_format or fmt,
93 }
94 if CONF.image_feature_enabled.api_v1:
95 params['is_public'] = 'False'
96 params['properties'] = properties
97 params = {'headers': common_image.image_meta_to_headers(**params)}
98 else:
99 params['visibility'] = 'private'
100 # Additional properties are flattened out in the v2 API.
101 params.update(properties)
102 body = self.image_client.create_image(**params)
103 image = body['image'] if 'image' in body else body
104 self.addCleanup(self.image_client.delete_image, image['id'])
105 self.assertEqual("queued", image['status'])
106 with open(path, 'rb') as image_file:
107 if CONF.image_feature_enabled.api_v1:
108 self.image_client.update_image(image['id'], data=image_file)
109 else:
110 self.image_client.store_image_file(image['id'], image_file)
Marian Krcmarik1972c462020-11-20 01:33:02 +0100111
112 if CONF.image_feature_enabled.import_image:
113 available_stores = []
114 try:
115 available_stores = self.image_client.info_stores()['stores']
116 except exceptions.NotFound:
117 pass
118 available_import_methods = self.image_client.info_import()[
119 'import-methods']['value']
120 if ('copy-image' in available_import_methods and
121 len(available_stores) > 1):
122 self.image_client.image_import(image['id'],
123 method='copy-image',
124 all_stores=True,
125 all_stores_must_succeed=False)
126 failed_stores = waiters.wait_for_image_copied_to_stores(
127 self.image_client, image['id'])
128 self.assertEqual(0, len(failed_stores),
129 "Failed to copy the following stores: %s" %
130 str(failed_stores))
131
Brianna Poulos011292a2017-03-15 16:24:38 -0400132 return image['id']
133
Brianna Poulos011292a2017-03-15 16:24:38 -0400134 def create_floating_ip(self, thing, pool_name=None):
135 """Create a floating IP and associates to a server on Nova"""
136
137 if not pool_name:
138 pool_name = CONF.network.floating_network_name
139 floating_ip = (self.compute_floating_ips_client.
140 create_floating_ip(pool=pool_name)['floating_ip'])
141 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
142 self.compute_floating_ips_client.delete_floating_ip,
143 floating_ip['id'])
144 self.compute_floating_ips_client.associate_floating_ip_to_server(
145 floating_ip['ip'], thing['id'])
146 return floating_ip
dane-fichter53f4fea2017-03-01 13:20:02 -0500147
148 def nova_volume_attach(self, server, volume_to_attach):
149 volume = self.servers_client.attach_volume(
150 server['id'], volumeId=volume_to_attach['id'], device='/dev/%s'
151 % CONF.compute.volume_device_name)['volumeAttachment']
152 self.assertEqual(volume_to_attach['id'], volume['id'])
153 waiters.wait_for_volume_resource_status(self.volumes_client,
154 volume['id'], 'in-use')
155
yatin5ddcc7e2018-03-27 14:49:36 +0530156 self.addCleanup(self.nova_volume_detach, server, volume)
dane-fichter53f4fea2017-03-01 13:20:02 -0500157 # Return the updated volume after the attachment
158 return self.volumes_client.show_volume(volume['id'])['volume']
159
160 def nova_volume_detach(self, server, volume):
161 self.servers_client.detach_volume(server['id'], volume['id'])
162 waiters.wait_for_volume_resource_status(self.volumes_client,
163 volume['id'], 'available')
164
165 volume = self.volumes_client.show_volume(volume['id'])['volume']
166 self.assertEqual('available', volume['status'])
167
dane-fichter53f4fea2017-03-01 13:20:02 -0500168 def get_server_ip(self, server):
169 """Get the server fixed or floating IP.
170
171 Based on the configuration we're in, return a correct ip
172 address for validating that a guest is up.
173 """
174 if CONF.validation.connect_method == 'floating':
175 # The tests calling this method don't have a floating IP
176 # and can't make use of the validation resources. So the
177 # method is creating the floating IP there.
178 return self.create_floating_ip(server)['ip']
179 elif CONF.validation.connect_method == 'fixed':
180 # Determine the network name to look for based on config or creds
181 # provider network resources.
182 if CONF.validation.network_for_ssh:
183 addresses = server['addresses'][
184 CONF.validation.network_for_ssh]
185 else:
186 creds_provider = self._get_credentials_provider()
187 net_creds = creds_provider.get_primary_creds()
188 network = getattr(net_creds, 'network', None)
189 addresses = (server['addresses'][network['name']]
190 if network else [])
191 for address in addresses:
jacky06a318f6d2019-01-04 23:55:03 +0800192 ip_version_for_ssh = CONF.validation.ip_version_for_ssh
193 if (address['version'] == ip_version_for_ssh and
194 address['OS-EXT-IPS:type'] == 'fixed'):
dane-fichter53f4fea2017-03-01 13:20:02 -0500195 return address['addr']
196 raise exceptions.ServerUnreachable(server_id=server['id'])
197 else:
198 raise lib_exc.InvalidConfiguration()
199
dane-fichter53f4fea2017-03-01 13:20:02 -0500200 def _default_security_group(self, client=None, tenant_id=None):
201 """Get default secgroup for given tenant_id.
202
203 :returns: default secgroup for given tenant
204 """
205 if client is None:
206 client = self.security_groups_client
207 if not tenant_id:
208 tenant_id = client.tenant_id
209 sgs = [
210 sg for sg in list(client.list_security_groups().values())[0]
211 if sg['tenant_id'] == tenant_id and sg['name'] == 'default'
212 ]
213 msg = "No default security group for tenant %s." % (tenant_id)
214 self.assertGreater(len(sgs), 0, msg)
215 return sgs[0]
216
217 def _create_security_group(self):
218 # Create security group
219 sg_name = data_utils.rand_name(self.__class__.__name__)
220 sg_desc = sg_name + " description"
221 secgroup = self.compute_security_groups_client.create_security_group(
222 name=sg_name, description=sg_desc)['security_group']
223 self.assertEqual(secgroup['name'], sg_name)
224 self.assertEqual(secgroup['description'], sg_desc)
225 self.addCleanup(
226 test_utils.call_and_ignore_notfound_exc,
227 self.compute_security_groups_client.delete_security_group,
228 secgroup['id'])
229
230 # Add rules to the security group
231 self._create_loginable_secgroup_rule(secgroup['id'])
232
233 return secgroup
234
235 def _create_loginable_secgroup_rule(self, secgroup_id=None):
236 _client = self.compute_security_groups_client
237 _client_rules = self.compute_security_group_rules_client
238 if secgroup_id is None:
239 sgs = _client.list_security_groups()['security_groups']
240 for sg in sgs:
241 if sg['name'] == 'default':
242 secgroup_id = sg['id']
243
244 # These rules are intended to permit inbound ssh and icmp
245 # traffic from all sources, so no group_id is provided.
246 # Setting a group_id would only permit traffic from ports
247 # belonging to the same security group.
248 rulesets = [
249 {
250 # ssh
251 'ip_protocol': 'tcp',
252 'from_port': 22,
253 'to_port': 22,
254 'cidr': '0.0.0.0/0',
255 },
256 {
257 # ping
258 'ip_protocol': 'icmp',
259 'from_port': -1,
260 'to_port': -1,
261 'cidr': '0.0.0.0/0',
262 }
263 ]
264 rules = list()
265 for ruleset in rulesets:
266 sg_rule = _client_rules.create_security_group_rule(
267 parent_group_id=secgroup_id, **ruleset)['security_group_rule']
268 rules.append(sg_rule)
269 return rules
270
271 def _create_security_group_rule(self, secgroup=None,
272 sec_group_rules_client=None,
273 tenant_id=None,
274 security_groups_client=None, **kwargs):
275 """Create a rule from a dictionary of rule parameters.
276
277 Create a rule in a secgroup. if secgroup not defined will search for
278 default secgroup in tenant_id.
279
280 :param secgroup: the security group.
281 :param tenant_id: if secgroup not passed -- the tenant in which to
282 search for default secgroup
283 :param kwargs: a dictionary containing rule parameters:
284 for example, to allow incoming ssh:
285 rule = {
286 direction: 'ingress'
287 protocol:'tcp',
288 port_range_min: 22,
289 port_range_max: 22
290 }
291 """
292 if sec_group_rules_client is None:
293 sec_group_rules_client = self.security_group_rules_client
294 if security_groups_client is None:
295 security_groups_client = self.security_groups_client
296 if not tenant_id:
297 tenant_id = security_groups_client.tenant_id
298 if secgroup is None:
299 secgroup = self._default_security_group(
300 client=security_groups_client, tenant_id=tenant_id)
301
302 ruleset = dict(security_group_id=secgroup['id'],
303 tenant_id=secgroup['tenant_id'])
304 ruleset.update(kwargs)
305
306 sg_rule = sec_group_rules_client.create_security_group_rule(**ruleset)
307 sg_rule = sg_rule['security_group_rule']
308
309 self.assertEqual(secgroup['tenant_id'], sg_rule['tenant_id'])
310 self.assertEqual(secgroup['id'], sg_rule['security_group_id'])
311
312 return sg_rule