blob: d89344604579e3f2ccb53b5a62aefee5b1063b04 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes13b479b2012-06-11 14:52:27 -04002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
David Kranzcf0040c2012-06-26 09:46:56 -040016import time
Jay Pipesf38eaac2012-06-21 13:37:35 -040017
Doug Hellmann583ce2c2015-03-11 14:55:46 +000018from oslo_log import log as logging
Matthew Treinish01472ff2015-02-20 17:26:52 -050019
Ghanshyam05049dd2016-02-12 17:44:48 +090020from tempest.api.compute import api_microversion_fixture
Joseph Lanouxb3e1f872015-01-30 11:13:07 +000021from tempest.common import compute
Ken'ichi Ohmichi8b9c7802015-07-08 05:57:37 +000022from tempest.common import waiters
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000023from tempest import config
Sean Dague20e98612016-01-06 14:33:28 -050024from tempest import exceptions
Ghanshyam1f47cf92016-02-25 04:57:18 +090025from tempest.lib.common import api_version_utils
Ken'ichi Ohmichi757833a2017-03-10 10:30:30 -080026from tempest.lib.common.utils import data_utils
Jordan Pittier9e227c52016-02-09 14:35:18 +010027from tempest.lib.common.utils import test_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050028from tempest.lib import exceptions as lib_exc
Attila Fazekasdc216422013-01-29 15:12:14 +010029import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040030
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000031CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030032
Jay Pipesf38eaac2012-06-21 13:37:35 -040033LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050034
35
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000036class BaseV2ComputeTest(api_version_utils.BaseMicroversionTest,
37 tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050038 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050039
Attila Fazekas430dae32013-10-17 15:19:32 +020040 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103041
Andrea Frittolib21de6c2015-02-06 20:12:38 +000042 # TODO(andreaf) We should care also for the alt_manager here
43 # but only once client lazy load in the manager is done
44 credentials = ['primary']
45
Jay Pipesf38eaac2012-06-21 13:37:35 -040046 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +000047 def skip_checks(cls):
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +000048 super(BaseV2ComputeTest, cls).skip_checks()
Matthew Treinishf8ff3582015-08-25 12:41:56 -040049 if not CONF.service_available.nova:
50 raise cls.skipException("Nova is not available")
ghanshyam29591532016-03-11 17:12:43 +090051 cfg_min_version = CONF.compute.min_microversion
52 cfg_max_version = CONF.compute.max_microversion
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000053 api_version_utils.check_skip_with_microversion(cls.min_microversion,
54 cls.max_microversion,
55 cfg_min_version,
56 cfg_max_version)
Jay Pipesf38eaac2012-06-21 13:37:35 -040057
Emily Hugenbruche7991d92014-12-12 16:53:36 +000058 @classmethod
59 def setup_credentials(cls):
60 cls.set_network_resources()
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +000061 super(BaseV2ComputeTest, cls).setup_credentials()
Daryl Walleckc7251962012-03-12 17:26:54 -050062
Emily Hugenbruche7991d92014-12-12 16:53:36 +000063 @classmethod
64 def setup_clients(cls):
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +000065 super(BaseV2ComputeTest, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +020066 cls.servers_client = cls.os_primary.servers_client
67 cls.server_groups_client = cls.os_primary.server_groups_client
68 cls.flavors_client = cls.os_primary.flavors_client
69 cls.compute_images_client = cls.os_primary.compute_images_client
70 cls.extensions_client = cls.os_primary.extensions_client
71 cls.floating_ip_pools_client = cls.os_primary.floating_ip_pools_client
72 cls.floating_ips_client = cls.os_primary.compute_floating_ips_client
73 cls.keypairs_client = cls.os_primary.keypairs_client
John Warren5cdbf422016-01-05 12:42:43 -050074 cls.security_group_rules_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +020075 cls.os_primary.compute_security_group_rules_client)
76 cls.security_groups_client =\
77 cls.os_primary.compute_security_groups_client
78 cls.quotas_client = cls.os_primary.quotas_client
79 cls.compute_networks_client = cls.os_primary.compute_networks_client
80 cls.limits_client = cls.os_primary.limits_client
81 cls.volumes_extensions_client =\
82 cls.os_primary.volumes_extensions_client
83 cls.snapshots_extensions_client =\
84 cls.os_primary.snapshots_extensions_client
85 cls.interfaces_client = cls.os_primary.interfaces_client
86 cls.fixed_ips_client = cls.os_primary.fixed_ips_client
87 cls.availability_zone_client = cls.os_primary.availability_zone_client
88 cls.agents_client = cls.os_primary.agents_client
89 cls.aggregates_client = cls.os_primary.aggregates_client
90 cls.services_client = cls.os_primary.services_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000091 cls.instance_usages_audit_log_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +020092 cls.os_primary.instance_usages_audit_log_client)
93 cls.hypervisor_client = cls.os_primary.hypervisor_client
94 cls.certificates_client = cls.os_primary.certificates_client
95 cls.migrations_client = cls.os_primary.migrations_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000096 cls.security_group_default_rules_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +020097 cls.os_primary.security_group_default_rules_client)
98 cls.versions_client = cls.os_primary.compute_versions_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000099
Jordan Pittier8160d312017-04-18 11:52:23 +0200100 cls.volumes_client = cls.os_primary.volumes_v2_client
Ivan Kolodyazhnybcfc32e2015-08-06 13:31:36 +0300101
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000102 @classmethod
103 def resource_setup(cls):
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000104 super(BaseV2ComputeTest, cls).resource_setup()
Ghanshyam05049dd2016-02-12 17:44:48 +0900105 cls.request_microversion = (
106 api_version_utils.select_request_microversion(
107 cls.min_microversion,
ghanshyam29591532016-03-11 17:12:43 +0900108 CONF.compute.min_microversion))
Matthew Treinishb0a78fc2014-01-29 16:49:12 +0000109 cls.build_interval = CONF.compute.build_interval
110 cls.build_timeout = CONF.compute.build_timeout
Matthew Treinishb0a78fc2014-01-29 16:49:12 +0000111 cls.image_ref = CONF.compute.image_ref
112 cls.image_ref_alt = CONF.compute.image_ref_alt
113 cls.flavor_ref = CONF.compute.flavor_ref
114 cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
lanoux283273b2015-12-04 03:01:54 -0800115 cls.ssh_user = CONF.validation.image_ssh_user
116 cls.image_ssh_user = CONF.validation.image_ssh_user
117 cls.image_ssh_password = CONF.validation.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -0400118 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -0400119 cls.images = []
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800120 cls.security_groups = []
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530121 cls.server_groups = []
Matt Riedemann342b37c2016-09-21 15:38:12 -0400122 cls.volumes = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000123
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000124 @classmethod
125 def resource_cleanup(cls):
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900126 cls.clear_resources('images', cls.images,
127 cls.compute_images_client.delete_image)
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000128 cls.clear_servers()
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900129 cls.clear_resources('security groups', cls.security_groups,
130 cls.security_groups_client.delete_security_group)
131 cls.clear_resources('server groups', cls.server_groups,
132 cls.server_groups_client.delete_server_group)
Matt Riedemann342b37c2016-09-21 15:38:12 -0400133 cls.clear_volumes()
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000134 super(BaseV2ComputeTest, cls).resource_cleanup()
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900135
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000136 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400137 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200138 LOG.debug('Clearing servers: %s', ','.join(
139 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400140 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700141 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100142 test_utils.call_and_ignore_notfound_exc(
143 cls.servers_client.delete_server, server['id'])
Joe Gordon0c335792014-09-23 12:36:11 -0700144 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100145 LOG.exception('Deleting server %s failed', server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700146
Jay Pipes444c3e62012-10-04 19:26:35 -0400147 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700148 try:
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000149 waiters.wait_for_server_termination(cls.servers_client,
150 server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700151 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100152 LOG.exception('Waiting for deletion of server %s failed',
153 server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700154
155 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100156 def server_check_teardown(cls):
157 """Checks is the shared server clean enough for subsequent test.
Ken'ichi Ohmichi88363cb2015-11-19 08:00:54 +0000158
Attila Fazekas305e65b2013-10-29 13:23:07 +0100159 Method will delete the server when it's dirty.
160 The setUp method is responsible for creating a new server.
161 Exceptions raised in tearDown class are fails the test case,
Marian Horban6afb0232015-11-10 22:47:12 -0500162 This method supposed to use only by tearDown methods, when
Attila Fazekas305e65b2013-10-29 13:23:07 +0100163 the shared server_id is stored in the server_id of the class.
164 """
165 if getattr(cls, 'server_id', None) is not None:
166 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000167 waiters.wait_for_server_status(cls.servers_client,
168 cls.server_id, 'ACTIVE')
Attila Fazekas305e65b2013-10-29 13:23:07 +0100169 except Exception as exc:
170 LOG.exception(exc)
171 cls.servers_client.delete_server(cls.server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000172 waiters.wait_for_server_termination(cls.servers_client,
173 cls.server_id)
Attila Fazekas305e65b2013-10-29 13:23:07 +0100174 cls.server_id = None
175 raise
176
177 @classmethod
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900178 def clear_resources(cls, resource_name, resources, resource_del_func):
179 LOG.debug('Clearing %s: %s', resource_name,
180 ','.join(map(str, resources)))
181 for res_id in resources:
Sean Dagued62bf1c2013-06-05 14:36:25 -0400182 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100183 test_utils.call_and_ignore_notfound_exc(
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900184 resource_del_func, res_id)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800185 except Exception as exc:
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900186 LOG.exception('Exception raised deleting %s: %s',
187 resource_name, res_id)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800188 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800189
190 @classmethod
Joe Gordon8843f0f2015-03-17 15:07:34 -0700191 def create_test_server(cls, validatable=False, volume_backed=False,
192 **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000193 """Wrapper utility that returns a test server.
Rohit Karajgidc300b22012-05-04 08:11:00 -0700194
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000195 This wrapper utility calls the common create test server and
196 returns a test server. The purpose of this wrapper is to minimize
197 the impact on the code of the tests already using this
198 function.
Joe Gordon8843f0f2015-03-17 15:07:34 -0700199
200 :param validatable: Whether the server will be pingable or sshable.
201 :param volume_backed: Whether the instance is volume backed or not.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000202 """
zhufl7ae22682016-09-18 15:22:33 +0800203 if 'name' not in kwargs:
204 kwargs['name'] = data_utils.rand_name(cls.__name__ + "-server")
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000205 tenant_network = cls.get_tenant_network()
206 body, servers = compute.create_test_server(
zhufl04190882017-05-23 10:21:48 +0800207 cls.os_primary,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000208 validatable,
209 validation_resources=cls.validation_resources,
210 tenant_network=tenant_network,
Joe Gordon8843f0f2015-03-17 15:07:34 -0700211 volume_backed=volume_backed,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000212 **kwargs)
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900213
214 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500215
David Kranz0fb14292015-02-11 15:55:20 -0500216 return body
Sean Dague9b669e32012-12-13 18:40:08 -0500217
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800218 @classmethod
219 def create_security_group(cls, name=None, description=None):
220 if name is None:
221 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
222 if description is None:
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +0000223 description = data_utils.rand_name('description')
ghanshyamb610b772015-08-24 17:29:38 +0900224 body = cls.security_groups_client.create_security_group(
225 name=name, description=description)['security_group']
Masayuki Igawa77d3f0f2017-03-10 18:57:00 +0900226 cls.security_groups.append(body['id'])
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800227
David Kranz9964b4e2015-02-06 15:45:29 -0500228 return body
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800229
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530230 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900231 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530232 if not name:
233 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900234 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530235 policy = ['affinity']
Ken'ichi Ohmichi1f36daa2015-09-30 01:41:34 +0000236 body = cls.server_groups_client.create_server_group(
237 name=name, policies=policy)['server_group']
David Kranzda5d4ec2014-06-24 16:04:57 -0400238 cls.server_groups.append(body['id'])
David Kranzae99b9a2015-02-16 13:37:01 -0500239 return body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530240
David Kranzcf0040c2012-06-26 09:46:56 -0400241 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500242 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400243 start_time = int(time.time())
244 while True:
245 try:
246 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500247 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400248 pass
249 else:
250 return
251 if int(time.time()) - start_time >= self.build_timeout:
252 condition()
253 return
254 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400255
Matt Riedemann807d0562014-01-27 12:03:10 -0800256 @staticmethod
257 def _delete_volume(volumes_client, volume_id):
258 """Deletes the given volume and waits for it to be gone."""
259 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000260 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800261 # TODO(mriedem): We should move the wait_for_resource_deletion
262 # into the delete_volume method as a convenience to the caller.
263 volumes_client.wait_for_resource_deletion(volume_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900264 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000265 LOG.warning("Unable to delete volume '%s' since it was not found. "
Jordan Pittier525ec712016-12-07 17:51:26 +0100266 "Maybe it was already deleted?", volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800267
Attila Fazekas423834d2014-03-14 17:33:13 +0100268 @classmethod
269 def prepare_instance_network(cls):
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000270 if (CONF.validation.auth_method != 'disabled' and
271 CONF.validation.connect_method == 'floating'):
Attila Fazekas423834d2014-03-14 17:33:13 +0100272 cls.set_network_resources(network=True, subnet=True, router=True,
273 dhcp=True)
274
ivan-zhu8f992be2013-07-31 14:56:58 +0800275 @classmethod
276 def create_image_from_server(cls, server_id, **kwargs):
277 """Wrapper utility that returns an image created from the server."""
zhufl35a694b2017-02-14 17:10:53 +0800278 name = kwargs.pop('name',
279 data_utils.rand_name(cls.__name__ + "-image"))
280 wait_until = kwargs.pop('wait_until', None)
281 wait_for_server = kwargs.pop('wait_for_server', True)
ivan-zhu8f992be2013-07-31 14:56:58 +0800282
zhufl35a694b2017-02-14 17:10:53 +0800283 image = cls.compute_images_client.create_image(server_id, name=name,
284 **kwargs)
David Kranza5299eb2015-01-15 17:24:05 -0500285 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800286 cls.images.append(image_id)
287
zhufl35a694b2017-02-14 17:10:53 +0800288 if wait_until is not None:
Matt Riedemann13954352017-02-07 14:03:54 -0500289 try:
290 waiters.wait_for_image_status(cls.compute_images_client,
zhufl35a694b2017-02-14 17:10:53 +0800291 image_id, wait_until)
Matt Riedemann13954352017-02-07 14:03:54 -0500292 except lib_exc.NotFound:
zhufl35a694b2017-02-14 17:10:53 +0800293 if wait_until.upper() == 'ACTIVE':
Matt Riedemann13954352017-02-07 14:03:54 -0500294 # If the image is not found after create_image returned
295 # that means the snapshot failed in nova-compute and nova
296 # deleted the image. There should be a compute fault
297 # recorded with the server in that case, so get the server
298 # and dump some details.
299 server = (
300 cls.servers_client.show_server(server_id)['server'])
301 if 'fault' in server:
302 raise exceptions.SnapshotNotFoundException(
303 server['fault'], image_id=image_id)
304 else:
305 raise exceptions.SnapshotNotFoundException(
306 image_id=image_id)
307 else:
308 raise
Ghanshyamae76c122015-12-22 13:41:35 +0900309 image = cls.compute_images_client.show_image(image_id)['image']
ivan-zhu8f992be2013-07-31 14:56:58 +0800310
zhufl35a694b2017-02-14 17:10:53 +0800311 if wait_until.upper() == 'ACTIVE':
312 if wait_for_server:
Bob Ball5fe62392017-02-20 09:51:00 +0000313 waiters.wait_for_server_status(cls.servers_client,
314 server_id, 'ACTIVE')
David Kranza5299eb2015-01-15 17:24:05 -0500315 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800316
317 @classmethod
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000318 def rebuild_server(cls, server_id, validatable=False, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800319 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000320 if server_id:
zhufl9b682902016-12-15 09:16:34 +0800321 cls.delete_server(server_id)
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000322
Ghanshyam3390d9f2015-12-25 12:48:02 +0900323 cls.password = data_utils.rand_password()
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000324 server = cls.create_test_server(
325 validatable,
326 wait_until='ACTIVE',
Ghanshyam3390d9f2015-12-25 12:48:02 +0900327 adminPass=cls.password,
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000328 **kwargs)
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000329 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800330
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800331 @classmethod
Jesse Keating613b4982015-05-04 15:05:19 -0700332 def delete_server(cls, server_id):
333 """Deletes an existing server and waits for it to be gone."""
334 try:
335 cls.servers_client.delete_server(server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000336 waiters.wait_for_server_termination(cls.servers_client,
337 server_id)
Jesse Keating613b4982015-05-04 15:05:19 -0700338 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100339 LOG.exception('Failed to delete server %s', server_id)
Jesse Keating613b4982015-05-04 15:05:19 -0700340
341 @classmethod
zhufl3d018b02016-11-25 16:43:04 +0800342 def resize_server(cls, server_id, new_flavor_id, **kwargs):
343 """resize and confirm_resize an server, waits for it to be ACTIVE."""
344 cls.servers_client.resize_server(server_id, new_flavor_id, **kwargs)
345 waiters.wait_for_server_status(cls.servers_client, server_id,
346 'VERIFY_RESIZE')
347 cls.servers_client.confirm_resize_server(server_id)
348 waiters.wait_for_server_status(cls.servers_client, server_id, 'ACTIVE')
349
350 @classmethod
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800351 def delete_volume(cls, volume_id):
352 """Deletes the given volume and waits for it to be gone."""
Felipe Monteiroa40e9b72017-05-05 17:47:10 +0100353 cls._delete_volume(cls.volumes_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900354
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000355 @classmethod
356 def get_server_ip(cls, server):
357 """Get the server fixed or floating IP.
358
Sean Dague20e98612016-01-06 14:33:28 -0500359 Based on the configuration we're in, return a correct ip
360 address for validating that a guest is up.
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000361 """
362 if CONF.validation.connect_method == 'floating':
Sean Dague20e98612016-01-06 14:33:28 -0500363 return cls.validation_resources['floating_ip']['ip']
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000364 elif CONF.validation.connect_method == 'fixed':
Sean Dague20e98612016-01-06 14:33:28 -0500365 addresses = server['addresses'][CONF.validation.network_for_ssh]
366 for address in addresses:
367 if address['version'] == CONF.validation.ip_version_for_ssh:
368 return address['addr']
zhufl955f82b2016-07-22 11:14:34 +0800369 raise exceptions.ServerUnreachable(server_id=server['id'])
Sean Dague20e98612016-01-06 14:33:28 -0500370 else:
guo yunxianffc4fc02016-11-15 09:56:08 +0800371 raise lib_exc.InvalidConfiguration()
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000372
Ghanshyam05049dd2016-02-12 17:44:48 +0900373 def setUp(self):
374 super(BaseV2ComputeTest, self).setUp()
375 self.useFixture(api_microversion_fixture.APIMicroversionFixture(
376 self.request_microversion))
377
Matt Riedemann342b37c2016-09-21 15:38:12 -0400378 @classmethod
zhufl8d23f922016-12-12 17:29:42 +0800379 def create_volume(cls, image_ref=None, **kwargs):
Matt Riedemann342b37c2016-09-21 15:38:12 -0400380 """Create a volume and wait for it to become 'available'.
381
Artom Lifshitzfc8f8e62016-04-13 11:08:32 +0000382 :param image_ref: Specify an image id to create a bootable volume.
zhufl8d23f922016-12-12 17:29:42 +0800383 :**kwargs: other parameters to create volume.
Matt Riedemann342b37c2016-09-21 15:38:12 -0400384 :returns: The available volume.
385 """
zhufl8d23f922016-12-12 17:29:42 +0800386 if 'size' not in kwargs:
387 kwargs['size'] = CONF.volume.volume_size
388 if 'display_name' not in kwargs:
389 vol_name = data_utils.rand_name(cls.__name__ + '-volume')
390 kwargs['display_name'] = vol_name
Artom Lifshitzfc8f8e62016-04-13 11:08:32 +0000391 if image_ref is not None:
zhufl8d23f922016-12-12 17:29:42 +0800392 kwargs['imageRef'] = image_ref
393 volume = cls.volumes_client.create_volume(**kwargs)['volume']
Matt Riedemann342b37c2016-09-21 15:38:12 -0400394 cls.volumes.append(volume)
lkuchlan52d7b0d2016-11-07 20:53:19 +0200395 waiters.wait_for_volume_resource_status(cls.volumes_client,
396 volume['id'], 'available')
Matt Riedemann342b37c2016-09-21 15:38:12 -0400397 return volume
398
399 @classmethod
400 def clear_volumes(cls):
401 LOG.debug('Clearing volumes: %s', ','.join(
402 volume['id'] for volume in cls.volumes))
403 for volume in cls.volumes:
404 try:
405 test_utils.call_and_ignore_notfound_exc(
406 cls.volumes_client.delete_volume, volume['id'])
407 except Exception:
408 LOG.exception('Deleting volume %s failed', volume['id'])
409
410 for volume in cls.volumes:
411 try:
412 cls.volumes_client.wait_for_resource_deletion(volume['id'])
413 except Exception:
414 LOG.exception('Waiting for deletion of volume %s failed',
415 volume['id'])
416
Matt Riedemanncb16a662016-10-01 18:30:05 -0400417 def attach_volume(self, server, volume, device=None):
418 """Attaches volume to server and waits for 'in-use' volume status.
419
420 The volume will be detached when the test tears down.
421
422 :param server: The server to which the volume will be attached.
423 :param volume: The volume to attach.
424 :param device: Optional mountpoint for the attached volume. Note that
425 this is not guaranteed for all hypervisors and is not recommended.
426 """
427 attach_kwargs = dict(volumeId=volume['id'])
428 if device:
429 attach_kwargs['device'] = device
zhufl36f0a972017-02-28 15:43:33 +0800430 attachment = self.servers_client.attach_volume(
431 server['id'], **attach_kwargs)['volumeAttachment']
Matt Riedemann342b37c2016-09-21 15:38:12 -0400432 # On teardown detach the volume and wait for it to be available. This
433 # is so we don't error out when trying to delete the volume during
434 # teardown.
lkuchlan52d7b0d2016-11-07 20:53:19 +0200435 self.addCleanup(waiters.wait_for_volume_resource_status,
Matt Riedemann342b37c2016-09-21 15:38:12 -0400436 self.volumes_client, volume['id'], 'available')
437 # Ignore 404s on detach in case the server is deleted or the volume
438 # is already detached.
439 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
440 self.servers_client.detach_volume,
441 server['id'], volume['id'])
lkuchlan52d7b0d2016-11-07 20:53:19 +0200442 waiters.wait_for_volume_resource_status(self.volumes_client,
443 volume['id'], 'in-use')
zhufl36f0a972017-02-28 15:43:33 +0800444 return attachment
Matt Riedemann342b37c2016-09-21 15:38:12 -0400445
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900446
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000447class BaseV2ComputeAdminTest(BaseV2ComputeTest):
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900448 """Base test case class for Compute Admin API tests."""
ivan-zhuf2b00502013-10-18 10:06:52 +0800449
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000450 credentials = ['primary', 'admin']
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000451
452 @classmethod
453 def setup_clients(cls):
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000454 super(BaseV2ComputeAdminTest, cls).setup_clients()
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000455 cls.availability_zone_admin_client = (
Jordan Pittier8160d312017-04-18 11:52:23 +0200456 cls.os_admin.availability_zone_client)
457 cls.admin_flavors_client = cls.os_admin.flavors_client
458 cls.admin_servers_client = cls.os_admin.servers_client
zhufl36eeab02017-01-18 11:49:04 +0800459
460 def create_flavor(self, ram, vcpus, disk, name=None,
461 is_public='True', **kwargs):
462 if name is None:
463 name = data_utils.rand_name(self.__class__.__name__ + "-flavor")
464 id = kwargs.pop('id', data_utils.rand_int_id(start=1000))
465 client = self.admin_flavors_client
466 flavor = client.create_flavor(
467 ram=ram, vcpus=vcpus, disk=disk, name=name,
468 id=id, is_public=is_public, **kwargs)['flavor']
469 self.addCleanup(client.wait_for_resource_deletion, flavor['id'])
470 self.addCleanup(client.delete_flavor, flavor['id'])
471 return flavor