blob: 4995209c459395b47c52018c0717ed1330bbb2ca [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
19from oslo_utils import excutils
Matthew Treinish01472ff2015-02-20 17:26:52 -050020from tempest_lib.common.utils import data_utils
21from tempest_lib import exceptions as lib_exc
22
Matthew Treinish481466b2012-12-20 17:16:01 -050023from tempest import clients
Emily Hugenbruche7991d92014-12-12 16:53:36 +000024from tempest.common import credentials
Rohan Kanade9ce97df2013-12-10 18:59:35 +053025from tempest.common import fixed_network
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000026from tempest import config
David Kranz33138312013-09-24 17:09:32 -040027from tempest import exceptions
Attila Fazekasdc216422013-01-29 15:12:14 +010028import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040029
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000030CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030031
Jay Pipesf38eaac2012-06-21 13:37:35 -040032LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050033
34
Attila Fazekasdc216422013-01-29 15:12:14 +010035class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050036 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050037
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000038 _api_version = 2
Attila Fazekas430dae32013-10-17 15:19:32 +020039 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103040
Jay Pipesf38eaac2012-06-21 13:37:35 -040041 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +000042 def skip_checks(cls):
43 super(BaseComputeTest, cls).skip_checks()
44 if cls._api_version != 2:
45 msg = ("Unexpected API version is specified (%s)" %
46 cls._api_version)
47 raise exceptions.InvalidConfiguration(message=msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040048
Emily Hugenbruche7991d92014-12-12 16:53:36 +000049 @classmethod
50 def setup_credentials(cls):
51 cls.set_network_resources()
52 super(BaseComputeTest, cls).setup_credentials()
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000053 # TODO(andreaf) WE should care also for the alt_manager here
54 # but only once client lazy load in the manager is done
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010055 cls.os = cls.get_client_manager()
Emily Hugenbruche7991d92014-12-12 16:53:36 +000056 # Note that we put this here and not in skip_checks because in
57 # the case of preprovisioned users we won't know if we can get
58 # two distinct users until we go and lock them
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010059 cls.multi_user = cls.check_multi_user()
Daryl Walleckc7251962012-03-12 17:26:54 -050060
Emily Hugenbruche7991d92014-12-12 16:53:36 +000061 @classmethod
62 def setup_clients(cls):
63 super(BaseComputeTest, cls).setup_clients()
64 cls.servers_client = cls.os.servers_client
65 cls.flavors_client = cls.os.flavors_client
66 cls.images_client = cls.os.images_client
67 cls.extensions_client = cls.os.extensions_client
68 cls.floating_ips_client = cls.os.floating_ips_client
69 cls.keypairs_client = cls.os.keypairs_client
70 cls.security_groups_client = cls.os.security_groups_client
71 cls.quotas_client = cls.os.quotas_client
72 # NOTE(mriedem): os-quota-class-sets is v2 API only
73 cls.quota_classes_client = cls.os.quota_classes_client
74 # NOTE(mriedem): os-networks is v2 API only
75 cls.networks_client = cls.os.networks_client
76 cls.limits_client = cls.os.limits_client
77 cls.volumes_extensions_client = cls.os.volumes_extensions_client
78 cls.volumes_client = cls.os.volumes_client
79 cls.interfaces_client = cls.os.interfaces_client
80 cls.fixed_ips_client = cls.os.fixed_ips_client
81 cls.availability_zone_client = cls.os.availability_zone_client
82 cls.agents_client = cls.os.agents_client
83 cls.aggregates_client = cls.os.aggregates_client
84 cls.services_client = cls.os.services_client
85 cls.instance_usages_audit_log_client = (
86 cls.os.instance_usages_audit_log_client)
87 cls.hypervisor_client = cls.os.hypervisor_client
88 cls.certificates_client = cls.os.certificates_client
89 cls.migrations_client = cls.os.migrations_client
90 cls.security_group_default_rules_client = (
91 cls.os.security_group_default_rules_client)
92
93 @classmethod
94 def resource_setup(cls):
95 super(BaseComputeTest, cls).resource_setup()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000096 cls.build_interval = CONF.compute.build_interval
97 cls.build_timeout = CONF.compute.build_timeout
98 cls.ssh_user = CONF.compute.ssh_user
99 cls.image_ref = CONF.compute.image_ref
100 cls.image_ref_alt = CONF.compute.image_ref_alt
101 cls.flavor_ref = CONF.compute.flavor_ref
102 cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
103 cls.image_ssh_user = CONF.compute.image_ssh_user
104 cls.image_ssh_password = CONF.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -0400105 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -0400106 cls.images = []
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800107 cls.security_groups = []
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530108 cls.server_groups = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000109
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000110 @classmethod
111 def resource_cleanup(cls):
112 cls.clear_images()
113 cls.clear_servers()
114 cls.clear_security_groups()
115 cls.clear_server_groups()
116 super(BaseComputeTest, cls).resource_cleanup()
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900117
Matthew Treinishf7fca6a2013-12-09 16:27:23 +0000118 @classmethod
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100119 def check_multi_user(cls):
120 # We have a list of accounts now, so just checking if the list is gt 2
121 if not cls.isolated_creds.is_multi_user():
122 msg = "Not enough users available for multi-user testing"
123 raise exceptions.InvalidConfiguration(msg)
124 return True
Daryl Walleckc7251962012-03-12 17:26:54 -0500125
Jay Pipesf38eaac2012-06-21 13:37:35 -0400126 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400127 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200128 LOG.debug('Clearing servers: %s', ','.join(
129 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400130 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700131 try:
132 cls.servers_client.delete_server(server['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900133 except lib_exc.NotFound:
Joe Gordon0c335792014-09-23 12:36:11 -0700134 # Something else already cleaned up the server, nothing to be
135 # worried about
Dan Smith74e7bcb2012-08-21 09:18:26 -0700136 pass
Joe Gordon0c335792014-09-23 12:36:11 -0700137 except Exception:
138 LOG.exception('Deleting server %s failed' % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700139
Jay Pipes444c3e62012-10-04 19:26:35 -0400140 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700141 try:
142 cls.servers_client.wait_for_server_termination(server['id'])
143 except Exception:
Joe Gordon0c335792014-09-23 12:36:11 -0700144 LOG.exception('Waiting for deletion of server %s failed'
145 % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700146
147 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100148 def server_check_teardown(cls):
149 """Checks is the shared server clean enough for subsequent test.
150 Method will delete the server when it's dirty.
151 The setUp method is responsible for creating a new server.
152 Exceptions raised in tearDown class are fails the test case,
153 This method supposed to use only by tierDown methods, when
154 the shared server_id is stored in the server_id of the class.
155 """
156 if getattr(cls, 'server_id', None) is not None:
157 try:
158 cls.servers_client.wait_for_server_status(cls.server_id,
159 'ACTIVE')
160 except Exception as exc:
161 LOG.exception(exc)
162 cls.servers_client.delete_server(cls.server_id)
163 cls.servers_client.wait_for_server_termination(cls.server_id)
164 cls.server_id = None
165 raise
166
167 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400168 def clear_images(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200169 LOG.debug('Clearing images: %s', ','.join(cls.images))
Sean Dagued62bf1c2013-06-05 14:36:25 -0400170 for image_id in cls.images:
171 try:
172 cls.images_client.delete_image(image_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900173 except lib_exc.NotFound:
David Kranz33138312013-09-24 17:09:32 -0400174 # The image may have already been deleted which is OK.
175 pass
Yair Frieda039f872014-01-02 12:11:10 +0200176 except Exception:
177 LOG.exception('Exception raised deleting image %s' % image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400178
179 @classmethod
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800180 def clear_security_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200181 LOG.debug('Clearing security groups: %s', ','.join(
182 str(sg['id']) for sg in cls.security_groups))
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800183 for sg in cls.security_groups:
184 try:
David Kranz9964b4e2015-02-06 15:45:29 -0500185 cls.security_groups_client.delete_security_group(sg['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900186 except lib_exc.NotFound:
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800187 # The security group may have already been deleted which is OK.
188 pass
189 except Exception as exc:
190 LOG.info('Exception raised deleting security group %s',
191 sg['id'])
192 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800193
194 @classmethod
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530195 def clear_server_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200196 LOG.debug('Clearing server groups: %s', ','.join(cls.server_groups))
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530197 for server_group_id in cls.server_groups:
198 try:
Ken'ichi Ohmichib8d1d012015-01-22 06:49:17 +0000199 cls.servers_client.delete_server_group(server_group_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900200 except lib_exc.NotFound:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530201 # The server-group may have already been deleted which is OK.
202 pass
203 except Exception:
204 LOG.exception('Exception raised deleting server-group %s',
205 server_group_id)
206
207 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900208 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500209 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900210 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -0500211 if 'name' in kwargs:
212 name = kwargs.pop('name')
213 flavor = kwargs.get('flavor', cls.flavor_ref)
214 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700215
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530216 kwargs = fixed_network.set_networks_kwarg(
217 cls.get_tenant_network(), kwargs) or {}
David Kranz0fb14292015-02-11 15:55:20 -0500218 body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500219 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100220
221 # handle the case of multiple servers
222 servers = [body]
223 if 'min_count' in kwargs or 'max_count' in kwargs:
224 # Get servers created which name match with name param.
David Kranzae99b9a2015-02-16 13:37:01 -0500225 b = cls.servers_client.list_servers()
Andrea Frittoli938d1512013-05-16 15:42:27 +0100226 servers = [s for s in b['servers'] if s['name'].startswith(name)]
227
Sean Dague22897e12013-02-25 17:54:09 -0500228 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100229 for server in servers:
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900230 try:
231 cls.servers_client.wait_for_server_status(
232 server['id'], kwargs['wait_until'])
Matthew Treinish83d97be2014-09-19 16:34:54 -0400233 except Exception:
234 with excutils.save_and_reraise_exception():
235 if ('preserve_server_on_error' not in kwargs
236 or kwargs['preserve_server_on_error'] is False):
237 for server in servers:
238 try:
239 cls.servers_client.delete_server(
240 server['id'])
241 except Exception:
242 pass
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900243
244 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500245
David Kranz0fb14292015-02-11 15:55:20 -0500246 return body
Sean Dague9b669e32012-12-13 18:40:08 -0500247
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800248 @classmethod
249 def create_security_group(cls, name=None, description=None):
250 if name is None:
251 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
252 if description is None:
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +0000253 description = data_utils.rand_name('description')
David Kranz9964b4e2015-02-06 15:45:29 -0500254 body = \
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800255 cls.security_groups_client.create_security_group(name,
256 description)
257 cls.security_groups.append(body)
258
David Kranz9964b4e2015-02-06 15:45:29 -0500259 return body
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800260
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530261 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900262 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530263 if not name:
264 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900265 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530266 policy = ['affinity']
David Kranzae99b9a2015-02-16 13:37:01 -0500267 body = cls.servers_client.create_server_group(name, policy)
David Kranzda5d4ec2014-06-24 16:04:57 -0400268 cls.server_groups.append(body['id'])
David Kranzae99b9a2015-02-16 13:37:01 -0500269 return body
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530270
David Kranzcf0040c2012-06-26 09:46:56 -0400271 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500272 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400273 start_time = int(time.time())
274 while True:
275 try:
276 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500277 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400278 pass
279 else:
280 return
281 if int(time.time()) - start_time >= self.build_timeout:
282 condition()
283 return
284 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400285
Matt Riedemann807d0562014-01-27 12:03:10 -0800286 @staticmethod
287 def _delete_volume(volumes_client, volume_id):
288 """Deletes the given volume and waits for it to be gone."""
289 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000290 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800291 # TODO(mriedem): We should move the wait_for_resource_deletion
292 # into the delete_volume method as a convenience to the caller.
293 volumes_client.wait_for_resource_deletion(volume_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900294 except lib_exc.NotFound:
Matt Riedemann807d0562014-01-27 12:03:10 -0800295 LOG.warn("Unable to delete volume '%s' since it was not found. "
296 "Maybe it was already deleted?" % volume_id)
297
Attila Fazekas423834d2014-03-14 17:33:13 +0100298 @classmethod
299 def prepare_instance_network(cls):
300 if (CONF.compute.ssh_auth_method != 'disabled' and
301 CONF.compute.ssh_connect_method == 'floating'):
302 cls.set_network_resources(network=True, subnet=True, router=True,
303 dhcp=True)
304
ivan-zhu8f992be2013-07-31 14:56:58 +0800305 @classmethod
306 def create_image_from_server(cls, server_id, **kwargs):
307 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900308 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800309 if 'name' in kwargs:
310 name = kwargs.pop('name')
311
David Kranza5299eb2015-01-15 17:24:05 -0500312 image = cls.images_client.create_image(server_id, name)
313 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800314 cls.images.append(image_id)
315
316 if 'wait_until' in kwargs:
317 cls.images_client.wait_for_image_status(image_id,
318 kwargs['wait_until'])
David Kranza5299eb2015-01-15 17:24:05 -0500319 image = cls.images_client.get_image(image_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800320
Bob Ball621e4602013-12-06 19:53:43 +0000321 if kwargs['wait_until'] == 'ACTIVE':
322 if kwargs.get('wait_for_server', True):
323 cls.servers_client.wait_for_server_status(server_id,
324 'ACTIVE')
David Kranza5299eb2015-01-15 17:24:05 -0500325 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800326
327 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000328 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800329 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000330 if server_id:
331 try:
332 cls.servers_client.delete_server(server_id)
333 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200334 except Exception:
335 LOG.exception('Failed to delete server %s' % server_id)
David Kranz0fb14292015-02-11 15:55:20 -0500336 server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000337 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000338 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800339
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800340 @classmethod
341 def delete_volume(cls, volume_id):
342 """Deletes the given volume and waits for it to be gone."""
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000343 cls._delete_volume(cls.volumes_extensions_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900344
345
346class BaseV2ComputeTest(BaseComputeTest):
347 _api_version = 2
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800348
ivan-zhuf2b00502013-10-18 10:06:52 +0800349
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900350class BaseComputeAdminTest(BaseComputeTest):
351 """Base test case class for Compute Admin API tests."""
ivan-zhuf2b00502013-10-18 10:06:52 +0800352
353 @classmethod
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000354 def skip_checks(cls):
355 if not credentials.is_admin_available():
356 msg = ("Missing Identity Admin API credentials in configuration.")
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100357 raise cls.skipException(msg)
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000358 super(BaseComputeAdminTest, cls).skip_checks()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100359
Emily Hugenbruche7991d92014-12-12 16:53:36 +0000360 @classmethod
361 def setup_credentials(cls):
362 super(BaseComputeAdminTest, cls).setup_credentials()
363 creds = cls.isolated_creds.get_admin_creds()
364 cls.os_adm = clients.Manager(credentials=creds)
365
366 @classmethod
367 def setup_clients(cls):
368 super(BaseComputeAdminTest, cls).setup_clients()
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000369 cls.availability_zone_admin_client = (
370 cls.os_adm.availability_zone_client)
ivan-zhu8f992be2013-07-31 14:56:58 +0800371
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900372
373class BaseV2ComputeAdminTest(BaseComputeAdminTest):
374 """Base test case class for Compute Admin V2 API tests."""
375 _api_version = 2