blob: 5c7714e0f4b5e512563029b09ff21c72a305d9bf [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120022from tempest.common.utils import data_utils
Ken'ichi Ohmichi8b9c7802015-07-08 05:57:37 +000023from tempest.common import waiters
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000024from tempest import config
Sean Dague20e98612016-01-06 14:33:28 -050025from tempest import exceptions
Ghanshyam1f47cf92016-02-25 04:57:18 +090026from tempest.lib.common import api_version_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()
Emily Hugenbruche7991d92014-12-12 16:53:36 +000066 cls.servers_client = cls.os.servers_client
Ken'ichi Ohmichi7ca54b82015-07-07 01:10:26 +000067 cls.server_groups_client = cls.os.server_groups_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000068 cls.flavors_client = cls.os.flavors_client
Ghanshyamae76c122015-12-22 13:41:35 +090069 cls.compute_images_client = cls.os.compute_images_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000070 cls.extensions_client = cls.os.extensions_client
Ken'ichi Ohmichi03af1c52015-07-13 00:28:05 +000071 cls.floating_ip_pools_client = cls.os.floating_ip_pools_client
John Warrene74890a2015-11-11 15:18:01 -050072 cls.floating_ips_client = cls.os.compute_floating_ips_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000073 cls.keypairs_client = cls.os.keypairs_client
John Warren5cdbf422016-01-05 12:42:43 -050074 cls.security_group_rules_client = (
75 cls.os.compute_security_group_rules_client)
John Warrenf2345512015-12-10 13:39:30 -050076 cls.security_groups_client = cls.os.compute_security_groups_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000077 cls.quotas_client = cls.os.quotas_client
John Warren9487a182015-09-14 18:12:56 -040078 cls.compute_networks_client = cls.os.compute_networks_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000079 cls.limits_client = cls.os.limits_client
80 cls.volumes_extensions_client = cls.os.volumes_extensions_client
Gaozexub9c9d6e2015-09-10 17:08:04 +080081 cls.snapshots_extensions_client = cls.os.snapshots_extensions_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000082 cls.interfaces_client = cls.os.interfaces_client
83 cls.fixed_ips_client = cls.os.fixed_ips_client
84 cls.availability_zone_client = cls.os.availability_zone_client
85 cls.agents_client = cls.os.agents_client
86 cls.aggregates_client = cls.os.aggregates_client
87 cls.services_client = cls.os.services_client
88 cls.instance_usages_audit_log_client = (
89 cls.os.instance_usages_audit_log_client)
90 cls.hypervisor_client = cls.os.hypervisor_client
91 cls.certificates_client = cls.os.certificates_client
92 cls.migrations_client = cls.os.migrations_client
93 cls.security_group_default_rules_client = (
94 cls.os.security_group_default_rules_client)
Ken'ichi Ohmichi2b6012b2015-09-03 01:56:19 +000095 cls.versions_client = cls.os.compute_versions_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000096
Ivan Kolodyazhnybcfc32e2015-08-06 13:31:36 +030097 if CONF.volume_feature_enabled.api_v1:
98 cls.volumes_client = cls.os.volumes_client
99 else:
100 cls.volumes_client = cls.os.volumes_v2_client
101
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):
126 cls.clear_images()
127 cls.clear_servers()
128 cls.clear_security_groups()
129 cls.clear_server_groups()
Matt Riedemann342b37c2016-09-21 15:38:12 -0400130 cls.clear_volumes()
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000131 super(BaseV2ComputeTest, cls).resource_cleanup()
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900132
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000133 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400134 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200135 LOG.debug('Clearing servers: %s', ','.join(
136 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400137 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700138 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100139 test_utils.call_and_ignore_notfound_exc(
140 cls.servers_client.delete_server, server['id'])
Joe Gordon0c335792014-09-23 12:36:11 -0700141 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100142 LOG.exception('Deleting server %s failed', server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700143
Jay Pipes444c3e62012-10-04 19:26:35 -0400144 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700145 try:
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000146 waiters.wait_for_server_termination(cls.servers_client,
147 server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700148 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100149 LOG.exception('Waiting for deletion of server %s failed',
150 server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700151
152 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100153 def server_check_teardown(cls):
154 """Checks is the shared server clean enough for subsequent test.
Ken'ichi Ohmichi88363cb2015-11-19 08:00:54 +0000155
Attila Fazekas305e65b2013-10-29 13:23:07 +0100156 Method will delete the server when it's dirty.
157 The setUp method is responsible for creating a new server.
158 Exceptions raised in tearDown class are fails the test case,
Marian Horban6afb0232015-11-10 22:47:12 -0500159 This method supposed to use only by tearDown methods, when
Attila Fazekas305e65b2013-10-29 13:23:07 +0100160 the shared server_id is stored in the server_id of the class.
161 """
162 if getattr(cls, 'server_id', None) is not None:
163 try:
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000164 waiters.wait_for_server_status(cls.servers_client,
165 cls.server_id, 'ACTIVE')
Attila Fazekas305e65b2013-10-29 13:23:07 +0100166 except Exception as exc:
167 LOG.exception(exc)
168 cls.servers_client.delete_server(cls.server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000169 waiters.wait_for_server_termination(cls.servers_client,
170 cls.server_id)
Attila Fazekas305e65b2013-10-29 13:23:07 +0100171 cls.server_id = None
172 raise
173
174 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400175 def clear_images(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200176 LOG.debug('Clearing images: %s', ','.join(cls.images))
Sean Dagued62bf1c2013-06-05 14:36:25 -0400177 for image_id in cls.images:
178 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100179 test_utils.call_and_ignore_notfound_exc(
180 cls.compute_images_client.delete_image, image_id)
Yair Frieda039f872014-01-02 12:11:10 +0200181 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100182 LOG.exception('Exception raised deleting image %s', image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400183
184 @classmethod
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800185 def clear_security_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200186 LOG.debug('Clearing security groups: %s', ','.join(
187 str(sg['id']) for sg in cls.security_groups))
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800188 for sg in cls.security_groups:
189 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100190 test_utils.call_and_ignore_notfound_exc(
191 cls.security_groups_client.delete_security_group, sg['id'])
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800192 except Exception as exc:
193 LOG.info('Exception raised deleting security group %s',
194 sg['id'])
195 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800196
197 @classmethod
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530198 def clear_server_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200199 LOG.debug('Clearing server groups: %s', ','.join(cls.server_groups))
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530200 for server_group_id in cls.server_groups:
201 try:
Jordan Pittier9e227c52016-02-09 14:35:18 +0100202 test_utils.call_and_ignore_notfound_exc(
203 cls.server_groups_client.delete_server_group,
204 server_group_id
205 )
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530206 except Exception:
207 LOG.exception('Exception raised deleting server-group %s',
208 server_group_id)
209
210 @classmethod
Joe Gordon8843f0f2015-03-17 15:07:34 -0700211 def create_test_server(cls, validatable=False, volume_backed=False,
212 **kwargs):
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000213 """Wrapper utility that returns a test server.
Rohit Karajgidc300b22012-05-04 08:11:00 -0700214
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000215 This wrapper utility calls the common create test server and
216 returns a test server. The purpose of this wrapper is to minimize
217 the impact on the code of the tests already using this
218 function.
Joe Gordon8843f0f2015-03-17 15:07:34 -0700219
220 :param validatable: Whether the server will be pingable or sshable.
221 :param volume_backed: Whether the instance is volume backed or not.
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000222 """
zhufl7ae22682016-09-18 15:22:33 +0800223 if 'name' not in kwargs:
224 kwargs['name'] = data_utils.rand_name(cls.__name__ + "-server")
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000225 tenant_network = cls.get_tenant_network()
226 body, servers = compute.create_test_server(
227 cls.os,
228 validatable,
229 validation_resources=cls.validation_resources,
230 tenant_network=tenant_network,
Joe Gordon8843f0f2015-03-17 15:07:34 -0700231 volume_backed=volume_backed,
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000232 **kwargs)
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900233
234 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500235
David Kranz0fb14292015-02-11 15:55:20 -0500236 return body
Sean Dague9b669e32012-12-13 18:40:08 -0500237
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800238 @classmethod
239 def create_security_group(cls, name=None, description=None):
240 if name is None:
241 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
242 if description is None:
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +0000243 description = data_utils.rand_name('description')
ghanshyamb610b772015-08-24 17:29:38 +0900244 body = cls.security_groups_client.create_security_group(
245 name=name, description=description)['security_group']
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800246 cls.security_groups.append(body)
247
David Kranz9964b4e2015-02-06 15:45:29 -0500248 return body
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800249
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530250 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900251 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530252 if not name:
253 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900254 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530255 policy = ['affinity']
Ken'ichi Ohmichi1f36daa2015-09-30 01:41:34 +0000256 body = cls.server_groups_client.create_server_group(
257 name=name, policies=policy)['server_group']
David Kranzda5d4ec2014-06-24 16:04:57 -0400258 cls.server_groups.append(body['id'])
David Kranzae99b9a2015-02-16 13:37:01 -0500259 return body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530260
David Kranzcf0040c2012-06-26 09:46:56 -0400261 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500262 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400263 start_time = int(time.time())
264 while True:
265 try:
266 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500267 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400268 pass
269 else:
270 return
271 if int(time.time()) - start_time >= self.build_timeout:
272 condition()
273 return
274 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400275
Matt Riedemann807d0562014-01-27 12:03:10 -0800276 @staticmethod
277 def _delete_volume(volumes_client, volume_id):
278 """Deletes the given volume and waits for it to be gone."""
279 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000280 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800281 # TODO(mriedem): We should move the wait_for_resource_deletion
282 # into the delete_volume method as a convenience to the caller.
283 volumes_client.wait_for_resource_deletion(volume_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900284 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000285 LOG.warning("Unable to delete volume '%s' since it was not found. "
Jordan Pittier525ec712016-12-07 17:51:26 +0100286 "Maybe it was already deleted?", volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800287
Attila Fazekas423834d2014-03-14 17:33:13 +0100288 @classmethod
289 def prepare_instance_network(cls):
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000290 if (CONF.validation.auth_method != 'disabled' and
291 CONF.validation.connect_method == 'floating'):
Attila Fazekas423834d2014-03-14 17:33:13 +0100292 cls.set_network_resources(network=True, subnet=True, router=True,
293 dhcp=True)
294
ivan-zhu8f992be2013-07-31 14:56:58 +0800295 @classmethod
296 def create_image_from_server(cls, server_id, **kwargs):
297 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900298 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800299 if 'name' in kwargs:
300 name = kwargs.pop('name')
301
Ghanshyamae76c122015-12-22 13:41:35 +0900302 image = cls.compute_images_client.create_image(server_id, name=name)
David Kranza5299eb2015-01-15 17:24:05 -0500303 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800304 cls.images.append(image_id)
305
306 if 'wait_until' in kwargs:
Matt Riedemann13954352017-02-07 14:03:54 -0500307 try:
308 waiters.wait_for_image_status(cls.compute_images_client,
309 image_id, kwargs['wait_until'])
310 except lib_exc.NotFound:
311 if kwargs['wait_until'].upper() == 'ACTIVE':
312 # If the image is not found after create_image returned
313 # that means the snapshot failed in nova-compute and nova
314 # deleted the image. There should be a compute fault
315 # recorded with the server in that case, so get the server
316 # and dump some details.
317 server = (
318 cls.servers_client.show_server(server_id)['server'])
319 if 'fault' in server:
320 raise exceptions.SnapshotNotFoundException(
321 server['fault'], image_id=image_id)
322 else:
323 raise exceptions.SnapshotNotFoundException(
324 image_id=image_id)
325 else:
326 raise
Ghanshyamae76c122015-12-22 13:41:35 +0900327 image = cls.compute_images_client.show_image(image_id)['image']
ivan-zhu8f992be2013-07-31 14:56:58 +0800328
David Kranza5299eb2015-01-15 17:24:05 -0500329 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800330
331 @classmethod
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000332 def rebuild_server(cls, server_id, validatable=False, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800333 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000334 if server_id:
zhufl9b682902016-12-15 09:16:34 +0800335 cls.delete_server(server_id)
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000336
Ghanshyam3390d9f2015-12-25 12:48:02 +0900337 cls.password = data_utils.rand_password()
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000338 server = cls.create_test_server(
339 validatable,
340 wait_until='ACTIVE',
Ghanshyam3390d9f2015-12-25 12:48:02 +0900341 adminPass=cls.password,
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000342 **kwargs)
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000343 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800344
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800345 @classmethod
Jesse Keating613b4982015-05-04 15:05:19 -0700346 def delete_server(cls, server_id):
347 """Deletes an existing server and waits for it to be gone."""
348 try:
349 cls.servers_client.delete_server(server_id)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000350 waiters.wait_for_server_termination(cls.servers_client,
351 server_id)
Jesse Keating613b4982015-05-04 15:05:19 -0700352 except Exception:
Jordan Pittier525ec712016-12-07 17:51:26 +0100353 LOG.exception('Failed to delete server %s', server_id)
Jesse Keating613b4982015-05-04 15:05:19 -0700354
355 @classmethod
zhufl3d018b02016-11-25 16:43:04 +0800356 def resize_server(cls, server_id, new_flavor_id, **kwargs):
357 """resize and confirm_resize an server, waits for it to be ACTIVE."""
358 cls.servers_client.resize_server(server_id, new_flavor_id, **kwargs)
359 waiters.wait_for_server_status(cls.servers_client, server_id,
360 'VERIFY_RESIZE')
361 cls.servers_client.confirm_resize_server(server_id)
362 waiters.wait_for_server_status(cls.servers_client, server_id, 'ACTIVE')
363
364 @classmethod
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800365 def delete_volume(cls, volume_id):
366 """Deletes the given volume and waits for it to be gone."""
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000367 cls._delete_volume(cls.volumes_extensions_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900368
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000369 @classmethod
370 def get_server_ip(cls, server):
371 """Get the server fixed or floating IP.
372
Sean Dague20e98612016-01-06 14:33:28 -0500373 Based on the configuration we're in, return a correct ip
374 address for validating that a guest is up.
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000375 """
376 if CONF.validation.connect_method == 'floating':
Sean Dague20e98612016-01-06 14:33:28 -0500377 return cls.validation_resources['floating_ip']['ip']
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000378 elif CONF.validation.connect_method == 'fixed':
Sean Dague20e98612016-01-06 14:33:28 -0500379 addresses = server['addresses'][CONF.validation.network_for_ssh]
380 for address in addresses:
381 if address['version'] == CONF.validation.ip_version_for_ssh:
382 return address['addr']
zhufl955f82b2016-07-22 11:14:34 +0800383 raise exceptions.ServerUnreachable(server_id=server['id'])
Sean Dague20e98612016-01-06 14:33:28 -0500384 else:
guo yunxianffc4fc02016-11-15 09:56:08 +0800385 raise lib_exc.InvalidConfiguration()
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000386
Ghanshyam05049dd2016-02-12 17:44:48 +0900387 def setUp(self):
388 super(BaseV2ComputeTest, self).setUp()
389 self.useFixture(api_microversion_fixture.APIMicroversionFixture(
390 self.request_microversion))
391
Matt Riedemann342b37c2016-09-21 15:38:12 -0400392 @classmethod
zhufl8d23f922016-12-12 17:29:42 +0800393 def create_volume(cls, image_ref=None, **kwargs):
Matt Riedemann342b37c2016-09-21 15:38:12 -0400394 """Create a volume and wait for it to become 'available'.
395
Artom Lifshitzfc8f8e62016-04-13 11:08:32 +0000396 :param image_ref: Specify an image id to create a bootable volume.
zhufl8d23f922016-12-12 17:29:42 +0800397 :**kwargs: other parameters to create volume.
Matt Riedemann342b37c2016-09-21 15:38:12 -0400398 :returns: The available volume.
399 """
zhufl8d23f922016-12-12 17:29:42 +0800400 if 'size' not in kwargs:
401 kwargs['size'] = CONF.volume.volume_size
402 if 'display_name' not in kwargs:
403 vol_name = data_utils.rand_name(cls.__name__ + '-volume')
404 kwargs['display_name'] = vol_name
Artom Lifshitzfc8f8e62016-04-13 11:08:32 +0000405 if image_ref is not None:
zhufl8d23f922016-12-12 17:29:42 +0800406 kwargs['imageRef'] = image_ref
407 volume = cls.volumes_client.create_volume(**kwargs)['volume']
Matt Riedemann342b37c2016-09-21 15:38:12 -0400408 cls.volumes.append(volume)
lkuchlan52d7b0d2016-11-07 20:53:19 +0200409 waiters.wait_for_volume_resource_status(cls.volumes_client,
410 volume['id'], 'available')
Matt Riedemann342b37c2016-09-21 15:38:12 -0400411 return volume
412
413 @classmethod
414 def clear_volumes(cls):
415 LOG.debug('Clearing volumes: %s', ','.join(
416 volume['id'] for volume in cls.volumes))
417 for volume in cls.volumes:
418 try:
419 test_utils.call_and_ignore_notfound_exc(
420 cls.volumes_client.delete_volume, volume['id'])
421 except Exception:
422 LOG.exception('Deleting volume %s failed', volume['id'])
423
424 for volume in cls.volumes:
425 try:
426 cls.volumes_client.wait_for_resource_deletion(volume['id'])
427 except Exception:
428 LOG.exception('Waiting for deletion of volume %s failed',
429 volume['id'])
430
Matt Riedemanncb16a662016-10-01 18:30:05 -0400431 def attach_volume(self, server, volume, device=None):
432 """Attaches volume to server and waits for 'in-use' volume status.
433
434 The volume will be detached when the test tears down.
435
436 :param server: The server to which the volume will be attached.
437 :param volume: The volume to attach.
438 :param device: Optional mountpoint for the attached volume. Note that
439 this is not guaranteed for all hypervisors and is not recommended.
440 """
441 attach_kwargs = dict(volumeId=volume['id'])
442 if device:
443 attach_kwargs['device'] = device
Matt Riedemann342b37c2016-09-21 15:38:12 -0400444 self.servers_client.attach_volume(
Matt Riedemanncb16a662016-10-01 18:30:05 -0400445 server['id'], **attach_kwargs)
Matt Riedemann342b37c2016-09-21 15:38:12 -0400446 # On teardown detach the volume and wait for it to be available. This
447 # is so we don't error out when trying to delete the volume during
448 # teardown.
lkuchlan52d7b0d2016-11-07 20:53:19 +0200449 self.addCleanup(waiters.wait_for_volume_resource_status,
Matt Riedemann342b37c2016-09-21 15:38:12 -0400450 self.volumes_client, volume['id'], 'available')
451 # Ignore 404s on detach in case the server is deleted or the volume
452 # is already detached.
453 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
454 self.servers_client.detach_volume,
455 server['id'], volume['id'])
lkuchlan52d7b0d2016-11-07 20:53:19 +0200456 waiters.wait_for_volume_resource_status(self.volumes_client,
457 volume['id'], 'in-use')
Matt Riedemann342b37c2016-09-21 15:38:12 -0400458
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900459
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000460class BaseV2ComputeAdminTest(BaseV2ComputeTest):
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900461 """Base test case class for Compute Admin API tests."""
ivan-zhuf2b00502013-10-18 10:06:52 +0800462
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000463 credentials = ['primary', 'admin']
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000464
465 @classmethod
466 def setup_clients(cls):
Ken'ichi Ohmichi02a8ccd2015-11-05 06:05:29 +0000467 super(BaseV2ComputeAdminTest, cls).setup_clients()
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000468 cls.availability_zone_admin_client = (
469 cls.os_adm.availability_zone_client)
zhufl36eeab02017-01-18 11:49:04 +0800470 cls.admin_flavors_client = cls.os_adm.flavors_client
471
472 def create_flavor(self, ram, vcpus, disk, name=None,
473 is_public='True', **kwargs):
474 if name is None:
475 name = data_utils.rand_name(self.__class__.__name__ + "-flavor")
476 id = kwargs.pop('id', data_utils.rand_int_id(start=1000))
477 client = self.admin_flavors_client
478 flavor = client.create_flavor(
479 ram=ram, vcpus=vcpus, disk=disk, name=name,
480 id=id, is_public=is_public, **kwargs)['flavor']
481 self.addCleanup(client.wait_for_resource_deletion, flavor['id'])
482 self.addCleanup(client.delete_flavor, flavor['id'])
483 return flavor