blob: 18401f01d86e19c61b3f986e4642b4370b419d96 [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
Matthew Treinish01472ff2015-02-20 17:26:52 -050018from tempest_lib.common.utils import data_utils
19from tempest_lib import exceptions as lib_exc
20
Matthew Treinish481466b2012-12-20 17:16:01 -050021from tempest import clients
Emily Hugenbruche7991d92014-12-12 16:53:36 +000022from tempest.common import credentials
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000023from tempest import config
David Kranz33138312013-09-24 17:09:32 -040024from tempest import exceptions
Matthew Treinish83d97be2014-09-19 16:34:54 -040025from tempest.openstack.common import excutils
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040026from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010027import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040028
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000029CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030030
Jay Pipesf38eaac2012-06-21 13:37:35 -040031LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050032
33
Attila Fazekasdc216422013-01-29 15:12:14 +010034class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050035 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050036
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000037 _api_version = 2
Attila Fazekas430dae32013-10-17 15:19:32 +020038 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103039
Jay Pipesf38eaac2012-06-21 13:37:35 -040040 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +000041 def skip_checks(cls):
42 super(BaseComputeTest, cls).skip_checks()
43 if cls._api_version != 2:
44 msg = ("Unexpected API version is specified (%s)" %
45 cls._api_version)
46 raise exceptions.InvalidConfiguration(message=msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040047
Emily Hugenbruche7991d92014-12-12 16:53:36 +000048 @classmethod
49 def setup_credentials(cls):
50 cls.set_network_resources()
51 super(BaseComputeTest, cls).setup_credentials()
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000052 # TODO(andreaf) WE should care also for the alt_manager here
53 # but only once client lazy load in the manager is done
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010054 cls.os = cls.get_client_manager()
Emily Hugenbruche7991d92014-12-12 16:53:36 +000055 # Note that we put this here and not in skip_checks because in
56 # the case of preprovisioned users we won't know if we can get
57 # two distinct users until we go and lock them
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010058 cls.multi_user = cls.check_multi_user()
Daryl Walleckc7251962012-03-12 17:26:54 -050059
Emily Hugenbruche7991d92014-12-12 16:53:36 +000060 @classmethod
61 def setup_clients(cls):
62 super(BaseComputeTest, cls).setup_clients()
63 cls.servers_client = cls.os.servers_client
64 cls.flavors_client = cls.os.flavors_client
65 cls.images_client = cls.os.images_client
66 cls.extensions_client = cls.os.extensions_client
67 cls.floating_ips_client = cls.os.floating_ips_client
68 cls.keypairs_client = cls.os.keypairs_client
69 cls.security_groups_client = cls.os.security_groups_client
70 cls.quotas_client = cls.os.quotas_client
71 # NOTE(mriedem): os-quota-class-sets is v2 API only
72 cls.quota_classes_client = cls.os.quota_classes_client
73 # NOTE(mriedem): os-networks is v2 API only
74 cls.networks_client = cls.os.networks_client
75 cls.limits_client = cls.os.limits_client
76 cls.volumes_extensions_client = cls.os.volumes_extensions_client
77 cls.volumes_client = cls.os.volumes_client
78 cls.interfaces_client = cls.os.interfaces_client
79 cls.fixed_ips_client = cls.os.fixed_ips_client
80 cls.availability_zone_client = cls.os.availability_zone_client
81 cls.agents_client = cls.os.agents_client
82 cls.aggregates_client = cls.os.aggregates_client
83 cls.services_client = cls.os.services_client
84 cls.instance_usages_audit_log_client = (
85 cls.os.instance_usages_audit_log_client)
86 cls.hypervisor_client = cls.os.hypervisor_client
87 cls.certificates_client = cls.os.certificates_client
88 cls.migrations_client = cls.os.migrations_client
89 cls.security_group_default_rules_client = (
90 cls.os.security_group_default_rules_client)
91
92 @classmethod
93 def resource_setup(cls):
94 super(BaseComputeTest, cls).resource_setup()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000095 cls.build_interval = CONF.compute.build_interval
96 cls.build_timeout = CONF.compute.build_timeout
97 cls.ssh_user = CONF.compute.ssh_user
98 cls.image_ref = CONF.compute.image_ref
99 cls.image_ref_alt = CONF.compute.image_ref_alt
100 cls.flavor_ref = CONF.compute.flavor_ref
101 cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
102 cls.image_ssh_user = CONF.compute.image_ssh_user
103 cls.image_ssh_password = CONF.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -0400104 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -0400105 cls.images = []
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800106 cls.security_groups = []
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530107 cls.server_groups = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000108
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000109 @classmethod
110 def resource_cleanup(cls):
111 cls.clear_images()
112 cls.clear_servers()
113 cls.clear_security_groups()
114 cls.clear_server_groups()
115 super(BaseComputeTest, cls).resource_cleanup()
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900116
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000117 @classmethod
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100118 def check_multi_user(cls):
119 # We have a list of accounts now, so just checking if the list is gt 2
120 if not cls.isolated_creds.is_multi_user():
121 msg = "Not enough users available for multi-user testing"
122 raise exceptions.InvalidConfiguration(msg)
123 return True
Daryl Walleckc7251962012-03-12 17:26:54 -0500124
Jay Pipesf38eaac2012-06-21 13:37:35 -0400125 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400126 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200127 LOG.debug('Clearing servers: %s', ','.join(
128 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400129 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700130 try:
131 cls.servers_client.delete_server(server['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900132 except lib_exc.NotFound:
Joe Gordon0c335792014-09-23 12:36:11 -0700133 # Something else already cleaned up the server, nothing to be
134 # worried about
Dan Smith74e7bcb2012-08-21 09:18:26 -0700135 pass
Joe Gordon0c335792014-09-23 12:36:11 -0700136 except Exception:
137 LOG.exception('Deleting server %s failed' % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700138
Jay Pipes444c3e62012-10-04 19:26:35 -0400139 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700140 try:
141 cls.servers_client.wait_for_server_termination(server['id'])
142 except Exception:
Joe Gordon0c335792014-09-23 12:36:11 -0700143 LOG.exception('Waiting for deletion of server %s failed'
144 % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700145
146 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100147 def server_check_teardown(cls):
148 """Checks is the shared server clean enough for subsequent test.
149 Method will delete the server when it's dirty.
150 The setUp method is responsible for creating a new server.
151 Exceptions raised in tearDown class are fails the test case,
152 This method supposed to use only by tierDown methods, when
153 the shared server_id is stored in the server_id of the class.
154 """
155 if getattr(cls, 'server_id', None) is not None:
156 try:
157 cls.servers_client.wait_for_server_status(cls.server_id,
158 'ACTIVE')
159 except Exception as exc:
160 LOG.exception(exc)
161 cls.servers_client.delete_server(cls.server_id)
162 cls.servers_client.wait_for_server_termination(cls.server_id)
163 cls.server_id = None
164 raise
165
166 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400167 def clear_images(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200168 LOG.debug('Clearing images: %s', ','.join(cls.images))
Sean Dagued62bf1c2013-06-05 14:36:25 -0400169 for image_id in cls.images:
170 try:
171 cls.images_client.delete_image(image_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900172 except lib_exc.NotFound:
David Kranz33138312013-09-24 17:09:32 -0400173 # The image may have already been deleted which is OK.
174 pass
Yair Frieda039f872014-01-02 12:11:10 +0200175 except Exception:
176 LOG.exception('Exception raised deleting image %s' % image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400177
178 @classmethod
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800179 def clear_security_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200180 LOG.debug('Clearing security groups: %s', ','.join(
181 str(sg['id']) for sg in cls.security_groups))
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800182 for sg in cls.security_groups:
183 try:
David Kranz9964b4e2015-02-06 15:45:29 -0500184 cls.security_groups_client.delete_security_group(sg['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900185 except lib_exc.NotFound:
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800186 # The security group may have already been deleted which is OK.
187 pass
188 except Exception as exc:
189 LOG.info('Exception raised deleting security group %s',
190 sg['id'])
191 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800192
193 @classmethod
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530194 def clear_server_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200195 LOG.debug('Clearing server groups: %s', ','.join(cls.server_groups))
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530196 for server_group_id in cls.server_groups:
197 try:
Ken'ichi Ohmichib8d1d012015-01-22 06:49:17 +0000198 cls.servers_client.delete_server_group(server_group_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900199 except lib_exc.NotFound:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530200 # The server-group may have already been deleted which is OK.
201 pass
202 except Exception:
203 LOG.exception('Exception raised deleting server-group %s',
204 server_group_id)
205
206 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900207 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500208 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900209 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -0500210 if 'name' in kwargs:
211 name = kwargs.pop('name')
212 flavor = kwargs.get('flavor', cls.flavor_ref)
213 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700214
David Kranz0fb14292015-02-11 15:55:20 -0500215 body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500216 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100217
218 # handle the case of multiple servers
219 servers = [body]
220 if 'min_count' in kwargs or 'max_count' in kwargs:
221 # Get servers created which name match with name param.
David Kranzae99b9a2015-02-16 13:37:01 -0500222 b = cls.servers_client.list_servers()
Andrea Frittoli938d1512013-05-16 15:42:27 +0100223 servers = [s for s in b['servers'] if s['name'].startswith(name)]
224
Sean Dague22897e12013-02-25 17:54:09 -0500225 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100226 for server in servers:
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900227 try:
228 cls.servers_client.wait_for_server_status(
229 server['id'], kwargs['wait_until'])
Matthew Treinish83d97be2014-09-19 16:34:54 -0400230 except Exception:
231 with excutils.save_and_reraise_exception():
232 if ('preserve_server_on_error' not in kwargs
233 or kwargs['preserve_server_on_error'] is False):
234 for server in servers:
235 try:
236 cls.servers_client.delete_server(
237 server['id'])
238 except Exception:
239 pass
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900240
241 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500242
David Kranz0fb14292015-02-11 15:55:20 -0500243 return body
Sean Dague9b669e32012-12-13 18:40:08 -0500244
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800245 @classmethod
246 def create_security_group(cls, name=None, description=None):
247 if name is None:
248 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
249 if description is None:
250 description = data_utils.rand_name('description-')
David Kranz9964b4e2015-02-06 15:45:29 -0500251 body = \
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800252 cls.security_groups_client.create_security_group(name,
253 description)
254 cls.security_groups.append(body)
255
David Kranz9964b4e2015-02-06 15:45:29 -0500256 return body
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800257
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530258 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900259 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530260 if not name:
261 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900262 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530263 policy = ['affinity']
David Kranzae99b9a2015-02-16 13:37:01 -0500264 body = cls.servers_client.create_server_group(name, policy)
David Kranzda5d4ec2014-06-24 16:04:57 -0400265 cls.server_groups.append(body['id'])
David Kranzae99b9a2015-02-16 13:37:01 -0500266 return body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530267
David Kranzcf0040c2012-06-26 09:46:56 -0400268 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500269 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400270 start_time = int(time.time())
271 while True:
272 try:
273 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500274 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400275 pass
276 else:
277 return
278 if int(time.time()) - start_time >= self.build_timeout:
279 condition()
280 return
281 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400282
Matt Riedemann807d0562014-01-27 12:03:10 -0800283 @staticmethod
284 def _delete_volume(volumes_client, volume_id):
285 """Deletes the given volume and waits for it to be gone."""
286 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000287 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800288 # TODO(mriedem): We should move the wait_for_resource_deletion
289 # into the delete_volume method as a convenience to the caller.
290 volumes_client.wait_for_resource_deletion(volume_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900291 except lib_exc.NotFound:
Matt Riedemann807d0562014-01-27 12:03:10 -0800292 LOG.warn("Unable to delete volume '%s' since it was not found. "
293 "Maybe it was already deleted?" % volume_id)
294
Attila Fazekas423834d2014-03-14 17:33:13 +0100295 @classmethod
296 def prepare_instance_network(cls):
297 if (CONF.compute.ssh_auth_method != 'disabled' and
298 CONF.compute.ssh_connect_method == 'floating'):
299 cls.set_network_resources(network=True, subnet=True, router=True,
300 dhcp=True)
301
ivan-zhu8f992be2013-07-31 14:56:58 +0800302 @classmethod
303 def create_image_from_server(cls, server_id, **kwargs):
304 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900305 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800306 if 'name' in kwargs:
307 name = kwargs.pop('name')
308
David Kranza5299eb2015-01-15 17:24:05 -0500309 image = cls.images_client.create_image(server_id, name)
310 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800311 cls.images.append(image_id)
312
313 if 'wait_until' in kwargs:
314 cls.images_client.wait_for_image_status(image_id,
315 kwargs['wait_until'])
David Kranza5299eb2015-01-15 17:24:05 -0500316 image = cls.images_client.get_image(image_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800317
Bob Ball621e4602013-12-06 19:53:43 +0000318 if kwargs['wait_until'] == 'ACTIVE':
319 if kwargs.get('wait_for_server', True):
320 cls.servers_client.wait_for_server_status(server_id,
321 'ACTIVE')
David Kranza5299eb2015-01-15 17:24:05 -0500322 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800323
324 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000325 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800326 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000327 if server_id:
328 try:
329 cls.servers_client.delete_server(server_id)
330 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200331 except Exception:
332 LOG.exception('Failed to delete server %s' % server_id)
David Kranz0fb14292015-02-11 15:55:20 -0500333 server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000334 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000335 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800336
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800337 @classmethod
338 def delete_volume(cls, volume_id):
339 """Deletes the given volume and waits for it to be gone."""
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000340 cls._delete_volume(cls.volumes_extensions_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900341
342
343class BaseV2ComputeTest(BaseComputeTest):
344 _api_version = 2
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800345
ivan-zhuf2b00502013-10-18 10:06:52 +0800346
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900347class BaseComputeAdminTest(BaseComputeTest):
348 """Base test case class for Compute Admin API tests."""
ivan-zhuf2b00502013-10-18 10:06:52 +0800349
350 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000351 def skip_checks(cls):
352 if not credentials.is_admin_available():
353 msg = ("Missing Identity Admin API credentials in configuration.")
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100354 raise cls.skipException(msg)
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000355 super(BaseComputeAdminTest, cls).skip_checks()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100356
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000357 @classmethod
358 def setup_credentials(cls):
359 super(BaseComputeAdminTest, cls).setup_credentials()
360 creds = cls.isolated_creds.get_admin_creds()
361 cls.os_adm = clients.Manager(credentials=creds)
362
363 @classmethod
364 def setup_clients(cls):
365 super(BaseComputeAdminTest, cls).setup_clients()
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000366 cls.availability_zone_admin_client = (
367 cls.os_adm.availability_zone_client)
ivan-zhu8f992be2013-07-31 14:56:58 +0800368
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900369
370class BaseV2ComputeAdminTest(BaseComputeAdminTest):
371 """Base test case class for Compute Admin V2 API tests."""
372 _api_version = 2