blob: 4ef47aa554e1d61a16e18c7634770cbab9d7d978 [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 -050019from tempest_lib import exceptions as lib_exc
20
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
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000023from tempest import config
David Kranz33138312013-09-24 17:09:32 -040024from tempest import exceptions
Attila Fazekasdc216422013-01-29 15:12:14 +010025import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040026
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000027CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030028
Jay Pipesf38eaac2012-06-21 13:37:35 -040029LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050030
31
Attila Fazekasdc216422013-01-29 15:12:14 +010032class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050034
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000035 _api_version = 2
Attila Fazekas430dae32013-10-17 15:19:32 +020036 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103037
Andrea Frittolib21de6c2015-02-06 20:12:38 +000038 # TODO(andreaf) We should care also for the alt_manager here
39 # but only once client lazy load in the manager is done
40 credentials = ['primary']
41
Jay Pipesf38eaac2012-06-21 13:37:35 -040042 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +000043 def skip_checks(cls):
44 super(BaseComputeTest, cls).skip_checks()
45 if cls._api_version != 2:
46 msg = ("Unexpected API version is specified (%s)" %
47 cls._api_version)
48 raise exceptions.InvalidConfiguration(message=msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040049
Emily Hugenbruche7991d92014-12-12 16:53:36 +000050 @classmethod
51 def setup_credentials(cls):
52 cls.set_network_resources()
53 super(BaseComputeTest, cls).setup_credentials()
Daryl Walleckc7251962012-03-12 17:26:54 -050054
Emily Hugenbruche7991d92014-12-12 16:53:36 +000055 @classmethod
56 def setup_clients(cls):
57 super(BaseComputeTest, cls).setup_clients()
58 cls.servers_client = cls.os.servers_client
Ken'ichi Ohmichi7ca54b82015-07-07 01:10:26 +000059 cls.server_groups_client = cls.os.server_groups_client
Emily Hugenbruche7991d92014-12-12 16:53:36 +000060 cls.flavors_client = cls.os.flavors_client
61 cls.images_client = cls.os.images_client
62 cls.extensions_client = cls.os.extensions_client
63 cls.floating_ips_client = cls.os.floating_ips_client
64 cls.keypairs_client = cls.os.keypairs_client
65 cls.security_groups_client = cls.os.security_groups_client
66 cls.quotas_client = cls.os.quotas_client
67 # NOTE(mriedem): os-quota-class-sets is v2 API only
68 cls.quota_classes_client = cls.os.quota_classes_client
69 # NOTE(mriedem): os-networks is v2 API only
70 cls.networks_client = cls.os.networks_client
71 cls.limits_client = cls.os.limits_client
72 cls.volumes_extensions_client = cls.os.volumes_extensions_client
73 cls.volumes_client = cls.os.volumes_client
74 cls.interfaces_client = cls.os.interfaces_client
75 cls.fixed_ips_client = cls.os.fixed_ips_client
76 cls.availability_zone_client = cls.os.availability_zone_client
77 cls.agents_client = cls.os.agents_client
78 cls.aggregates_client = cls.os.aggregates_client
79 cls.services_client = cls.os.services_client
80 cls.instance_usages_audit_log_client = (
81 cls.os.instance_usages_audit_log_client)
82 cls.hypervisor_client = cls.os.hypervisor_client
83 cls.certificates_client = cls.os.certificates_client
84 cls.migrations_client = cls.os.migrations_client
85 cls.security_group_default_rules_client = (
86 cls.os.security_group_default_rules_client)
87
88 @classmethod
89 def resource_setup(cls):
90 super(BaseComputeTest, cls).resource_setup()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000091 cls.build_interval = CONF.compute.build_interval
92 cls.build_timeout = CONF.compute.build_timeout
93 cls.ssh_user = CONF.compute.ssh_user
94 cls.image_ref = CONF.compute.image_ref
95 cls.image_ref_alt = CONF.compute.image_ref_alt
96 cls.flavor_ref = CONF.compute.flavor_ref
97 cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
98 cls.image_ssh_user = CONF.compute.image_ssh_user
99 cls.image_ssh_password = CONF.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -0400100 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -0400101 cls.images = []
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800102 cls.security_groups = []
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530103 cls.server_groups = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000104
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000105 @classmethod
106 def resource_cleanup(cls):
107 cls.clear_images()
108 cls.clear_servers()
109 cls.clear_security_groups()
110 cls.clear_server_groups()
111 super(BaseComputeTest, cls).resource_cleanup()
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900112
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000113 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400114 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200115 LOG.debug('Clearing servers: %s', ','.join(
116 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400117 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700118 try:
119 cls.servers_client.delete_server(server['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900120 except lib_exc.NotFound:
Joe Gordon0c335792014-09-23 12:36:11 -0700121 # Something else already cleaned up the server, nothing to be
122 # worried about
Dan Smith74e7bcb2012-08-21 09:18:26 -0700123 pass
Joe Gordon0c335792014-09-23 12:36:11 -0700124 except Exception:
125 LOG.exception('Deleting server %s failed' % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700126
Jay Pipes444c3e62012-10-04 19:26:35 -0400127 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700128 try:
129 cls.servers_client.wait_for_server_termination(server['id'])
130 except Exception:
Joe Gordon0c335792014-09-23 12:36:11 -0700131 LOG.exception('Waiting for deletion of server %s failed'
132 % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700133
134 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100135 def server_check_teardown(cls):
136 """Checks is the shared server clean enough for subsequent test.
137 Method will delete the server when it's dirty.
138 The setUp method is responsible for creating a new server.
139 Exceptions raised in tearDown class are fails the test case,
140 This method supposed to use only by tierDown methods, when
141 the shared server_id is stored in the server_id of the class.
142 """
143 if getattr(cls, 'server_id', None) is not None:
144 try:
145 cls.servers_client.wait_for_server_status(cls.server_id,
146 'ACTIVE')
147 except Exception as exc:
148 LOG.exception(exc)
149 cls.servers_client.delete_server(cls.server_id)
150 cls.servers_client.wait_for_server_termination(cls.server_id)
151 cls.server_id = None
152 raise
153
154 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400155 def clear_images(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200156 LOG.debug('Clearing images: %s', ','.join(cls.images))
Sean Dagued62bf1c2013-06-05 14:36:25 -0400157 for image_id in cls.images:
158 try:
159 cls.images_client.delete_image(image_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900160 except lib_exc.NotFound:
David Kranz33138312013-09-24 17:09:32 -0400161 # The image may have already been deleted which is OK.
162 pass
Yair Frieda039f872014-01-02 12:11:10 +0200163 except Exception:
164 LOG.exception('Exception raised deleting image %s' % image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400165
166 @classmethod
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800167 def clear_security_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200168 LOG.debug('Clearing security groups: %s', ','.join(
169 str(sg['id']) for sg in cls.security_groups))
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800170 for sg in cls.security_groups:
171 try:
David Kranz9964b4e2015-02-06 15:45:29 -0500172 cls.security_groups_client.delete_security_group(sg['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900173 except lib_exc.NotFound:
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800174 # The security group may have already been deleted which is OK.
175 pass
176 except Exception as exc:
177 LOG.info('Exception raised deleting security group %s',
178 sg['id'])
179 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800180
181 @classmethod
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530182 def clear_server_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200183 LOG.debug('Clearing server groups: %s', ','.join(cls.server_groups))
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530184 for server_group_id in cls.server_groups:
185 try:
Ken'ichi Ohmichi7ca54b82015-07-07 01:10:26 +0000186 cls.server_groups_client.delete_server_group(server_group_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900187 except lib_exc.NotFound:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530188 # The server-group may have already been deleted which is OK.
189 pass
190 except Exception:
191 LOG.exception('Exception raised deleting server-group %s',
192 server_group_id)
193
194 @classmethod
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000195 def create_test_server(cls, validatable=False, **kwargs):
196 """Wrapper utility that returns a test server.
Rohit Karajgidc300b22012-05-04 08:11:00 -0700197
Joseph Lanouxb3e1f872015-01-30 11:13:07 +0000198 This wrapper utility calls the common create test server and
199 returns a test server. The purpose of this wrapper is to minimize
200 the impact on the code of the tests already using this
201 function.
202 """
203 tenant_network = cls.get_tenant_network()
204 body, servers = compute.create_test_server(
205 cls.os,
206 validatable,
207 validation_resources=cls.validation_resources,
208 tenant_network=tenant_network,
209 **kwargs)
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900210
211 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500212
David Kranz0fb14292015-02-11 15:55:20 -0500213 return body
Sean Dague9b669e32012-12-13 18:40:08 -0500214
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800215 @classmethod
216 def create_security_group(cls, name=None, description=None):
217 if name is None:
218 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
219 if description is None:
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +0000220 description = data_utils.rand_name('description')
David Kranz9964b4e2015-02-06 15:45:29 -0500221 body = \
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800222 cls.security_groups_client.create_security_group(name,
223 description)
224 cls.security_groups.append(body)
225
David Kranz9964b4e2015-02-06 15:45:29 -0500226 return body
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800227
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530228 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900229 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530230 if not name:
231 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900232 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530233 policy = ['affinity']
Ken'ichi Ohmichi7ca54b82015-07-07 01:10:26 +0000234 body = cls.server_groups_client.create_server_group(name, policy)
David Kranzda5d4ec2014-06-24 16:04:57 -0400235 cls.server_groups.append(body['id'])
David Kranzae99b9a2015-02-16 13:37:01 -0500236 return body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530237
David Kranzcf0040c2012-06-26 09:46:56 -0400238 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500239 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400240 start_time = int(time.time())
241 while True:
242 try:
243 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500244 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400245 pass
246 else:
247 return
248 if int(time.time()) - start_time >= self.build_timeout:
249 condition()
250 return
251 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400252
Matt Riedemann807d0562014-01-27 12:03:10 -0800253 @staticmethod
254 def _delete_volume(volumes_client, volume_id):
255 """Deletes the given volume and waits for it to be gone."""
256 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000257 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800258 # TODO(mriedem): We should move the wait_for_resource_deletion
259 # into the delete_volume method as a convenience to the caller.
260 volumes_client.wait_for_resource_deletion(volume_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900261 except lib_exc.NotFound:
Matt Riedemann807d0562014-01-27 12:03:10 -0800262 LOG.warn("Unable to delete volume '%s' since it was not found. "
263 "Maybe it was already deleted?" % volume_id)
264
Attila Fazekas423834d2014-03-14 17:33:13 +0100265 @classmethod
266 def prepare_instance_network(cls):
267 if (CONF.compute.ssh_auth_method != 'disabled' and
268 CONF.compute.ssh_connect_method == 'floating'):
269 cls.set_network_resources(network=True, subnet=True, router=True,
270 dhcp=True)
271
ivan-zhu8f992be2013-07-31 14:56:58 +0800272 @classmethod
273 def create_image_from_server(cls, server_id, **kwargs):
274 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900275 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800276 if 'name' in kwargs:
277 name = kwargs.pop('name')
278
David Kranza5299eb2015-01-15 17:24:05 -0500279 image = cls.images_client.create_image(server_id, name)
280 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800281 cls.images.append(image_id)
282
283 if 'wait_until' in kwargs:
284 cls.images_client.wait_for_image_status(image_id,
285 kwargs['wait_until'])
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000286 image = cls.images_client.show_image(image_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800287
Bob Ball621e4602013-12-06 19:53:43 +0000288 if kwargs['wait_until'] == 'ACTIVE':
289 if kwargs.get('wait_for_server', True):
290 cls.servers_client.wait_for_server_status(server_id,
291 'ACTIVE')
David Kranza5299eb2015-01-15 17:24:05 -0500292 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800293
294 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000295 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800296 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000297 if server_id:
298 try:
299 cls.servers_client.delete_server(server_id)
300 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200301 except Exception:
302 LOG.exception('Failed to delete server %s' % server_id)
David Kranz0fb14292015-02-11 15:55:20 -0500303 server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000304 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000305 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800306
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800307 @classmethod
Jesse Keating613b4982015-05-04 15:05:19 -0700308 def delete_server(cls, server_id):
309 """Deletes an existing server and waits for it to be gone."""
310 try:
311 cls.servers_client.delete_server(server_id)
312 cls.servers_client.wait_for_server_termination(server_id)
313 except Exception:
314 LOG.exception('Failed to delete server %s' % server_id)
315
316 @classmethod
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800317 def delete_volume(cls, volume_id):
318 """Deletes the given volume and waits for it to be gone."""
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000319 cls._delete_volume(cls.volumes_extensions_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900320
321
322class BaseV2ComputeTest(BaseComputeTest):
323 _api_version = 2
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800324
ivan-zhuf2b00502013-10-18 10:06:52 +0800325
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900326class BaseComputeAdminTest(BaseComputeTest):
327 """Base test case class for Compute Admin API tests."""
ivan-zhuf2b00502013-10-18 10:06:52 +0800328
Andrea Frittolib21de6c2015-02-06 20:12:38 +0000329 credentials = ['primary', 'admin']
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000330
331 @classmethod
332 def setup_clients(cls):
333 super(BaseComputeAdminTest, cls).setup_clients()
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000334 cls.availability_zone_admin_client = (
335 cls.os_adm.availability_zone_client)
ivan-zhu8f992be2013-07-31 14:56:58 +0800336
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900337
338class BaseV2ComputeAdminTest(BaseComputeAdminTest):
339 """Base test case class for Compute Admin V2 API tests."""
340 _api_version = 2