blob: c245eb40aeb1c9b40b9c67e5255024963d5c6033 [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 Treinish481466b2012-12-20 17:16:01 -050018from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090019from tempest.common.utils import data_utils
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000020from tempest import config
David Kranz33138312013-09-24 17:09:32 -040021from tempest import exceptions
Matthew Treinish83d97be2014-09-19 16:34:54 -040022from tempest.openstack.common import excutils
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040023from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010024import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040025
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000026CONF = config.CONF
Tiago Melloeda03b52012-08-22 23:47:29 -030027
Jay Pipesf38eaac2012-06-21 13:37:35 -040028LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050029
30
Attila Fazekasdc216422013-01-29 15:12:14 +010031class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050032 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050033
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +000034 _api_version = 2
Attila Fazekas430dae32013-10-17 15:19:32 +020035 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103036
Jay Pipesf38eaac2012-06-21 13:37:35 -040037 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010038 def resource_setup(cls):
Ken'ichi Ohmichi5fd4ecf2014-04-25 13:38:15 +090039 cls.set_network_resources()
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010040 super(BaseComputeTest, cls).resource_setup()
Jay Pipesf38eaac2012-06-21 13:37:35 -040041
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000042 # TODO(andreaf) WE should care also for the alt_manager here
43 # but only once client lazy load in the manager is done
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010044 cls.os = cls.get_client_manager()
45 cls.multi_user = cls.check_multi_user()
Daryl Walleckc7251962012-03-12 17:26:54 -050046
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000047 cls.build_interval = CONF.compute.build_interval
48 cls.build_timeout = CONF.compute.build_timeout
49 cls.ssh_user = CONF.compute.ssh_user
50 cls.image_ref = CONF.compute.image_ref
51 cls.image_ref_alt = CONF.compute.image_ref_alt
52 cls.flavor_ref = CONF.compute.flavor_ref
53 cls.flavor_ref_alt = CONF.compute.flavor_ref_alt
54 cls.image_ssh_user = CONF.compute.image_ssh_user
55 cls.image_ssh_password = CONF.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040056 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040057 cls.images = []
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +080058 cls.security_groups = []
Abhijeet.Jain87dd4452014-04-23 15:51:23 +053059 cls.server_groups = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +000060
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +090061 if cls._api_version == 2:
62 cls.servers_client = cls.os.servers_client
63 cls.flavors_client = cls.os.flavors_client
64 cls.images_client = cls.os.images_client
65 cls.extensions_client = cls.os.extensions_client
66 cls.floating_ips_client = cls.os.floating_ips_client
67 cls.keypairs_client = cls.os.keypairs_client
68 cls.security_groups_client = cls.os.security_groups_client
69 cls.quotas_client = cls.os.quotas_client
Matt Riedemann848805f2014-06-16 13:23:51 -070070 # NOTE(mriedem): os-quota-class-sets is v2 API only
71 cls.quota_classes_client = cls.os.quota_classes_client
Matt Riedemann4a994012014-08-08 08:25:03 -070072 # NOTE(mriedem): os-networks is v2 API only
73 cls.networks_client = cls.os.networks_client
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +090074 cls.limits_client = cls.os.limits_client
75 cls.volumes_extensions_client = cls.os.volumes_extensions_client
76 cls.volumes_client = cls.os.volumes_client
77 cls.interfaces_client = cls.os.interfaces_client
78 cls.fixed_ips_client = cls.os.fixed_ips_client
79 cls.availability_zone_client = cls.os.availability_zone_client
80 cls.agents_client = cls.os.agents_client
81 cls.aggregates_client = cls.os.aggregates_client
82 cls.services_client = cls.os.services_client
83 cls.instance_usages_audit_log_client = \
84 cls.os.instance_usages_audit_log_client
85 cls.hypervisor_client = cls.os.hypervisor_client
86 cls.certificates_client = cls.os.certificates_client
87 cls.migrations_client = cls.os.migrations_client
Ghanshyam9d9201d2014-04-16 19:28:09 +090088 cls.security_group_default_rules_client = (
89 cls.os.security_group_default_rules_client)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +090090 else:
91 msg = ("Unexpected API version is specified (%s)" %
92 cls._api_version)
93 raise exceptions.InvalidConfiguration(message=msg)
94
Matthew Treinishf7fca6a2013-12-09 16:27:23 +000095 @classmethod
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010096 def check_multi_user(cls):
97 # We have a list of accounts now, so just checking if the list is gt 2
98 if not cls.isolated_creds.is_multi_user():
99 msg = "Not enough users available for multi-user testing"
100 raise exceptions.InvalidConfiguration(msg)
101 return True
Daryl Walleckc7251962012-03-12 17:26:54 -0500102
Jay Pipesf38eaac2012-06-21 13:37:35 -0400103 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400104 def clear_servers(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200105 LOG.debug('Clearing servers: %s', ','.join(
106 server['id'] for server in cls.servers))
Jay Pipes444c3e62012-10-04 19:26:35 -0400107 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700108 try:
109 cls.servers_client.delete_server(server['id'])
Joe Gordon0c335792014-09-23 12:36:11 -0700110 except exceptions.NotFound:
111 # Something else already cleaned up the server, nothing to be
112 # worried about
Dan Smith74e7bcb2012-08-21 09:18:26 -0700113 pass
Joe Gordon0c335792014-09-23 12:36:11 -0700114 except Exception:
115 LOG.exception('Deleting server %s failed' % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700116
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.wait_for_server_termination(server['id'])
120 except Exception:
Joe Gordon0c335792014-09-23 12:36:11 -0700121 LOG.exception('Waiting for deletion of server %s failed'
122 % server['id'])
Dan Smith74e7bcb2012-08-21 09:18:26 -0700123
124 @classmethod
Attila Fazekas305e65b2013-10-29 13:23:07 +0100125 def server_check_teardown(cls):
126 """Checks is the shared server clean enough for subsequent test.
127 Method will delete the server when it's dirty.
128 The setUp method is responsible for creating a new server.
129 Exceptions raised in tearDown class are fails the test case,
130 This method supposed to use only by tierDown methods, when
131 the shared server_id is stored in the server_id of the class.
132 """
133 if getattr(cls, 'server_id', None) is not None:
134 try:
135 cls.servers_client.wait_for_server_status(cls.server_id,
136 'ACTIVE')
137 except Exception as exc:
138 LOG.exception(exc)
139 cls.servers_client.delete_server(cls.server_id)
140 cls.servers_client.wait_for_server_termination(cls.server_id)
141 cls.server_id = None
142 raise
143
144 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400145 def clear_images(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200146 LOG.debug('Clearing images: %s', ','.join(cls.images))
Sean Dagued62bf1c2013-06-05 14:36:25 -0400147 for image_id in cls.images:
148 try:
149 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -0400150 except exceptions.NotFound:
151 # The image may have already been deleted which is OK.
152 pass
Yair Frieda039f872014-01-02 12:11:10 +0200153 except Exception:
154 LOG.exception('Exception raised deleting image %s' % image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400155
156 @classmethod
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800157 def clear_security_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200158 LOG.debug('Clearing security groups: %s', ','.join(
159 str(sg['id']) for sg in cls.security_groups))
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800160 for sg in cls.security_groups:
161 try:
162 resp, body =\
163 cls.security_groups_client.delete_security_group(sg['id'])
164 except exceptions.NotFound:
165 # The security group may have already been deleted which is OK.
166 pass
167 except Exception as exc:
168 LOG.info('Exception raised deleting security group %s',
169 sg['id'])
170 LOG.exception(exc)
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800171
172 @classmethod
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530173 def clear_server_groups(cls):
Salvatore1422a9a2014-10-08 15:53:25 +0200174 LOG.debug('Clearing server groups: %s', ','.join(cls.server_groups))
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530175 for server_group_id in cls.server_groups:
176 try:
Ken'ichi Ohmichib8d1d012015-01-22 06:49:17 +0000177 cls.servers_client.delete_server_group(server_group_id)
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530178 except exceptions.NotFound:
179 # The server-group may have already been deleted which is OK.
180 pass
181 except Exception:
182 LOG.exception('Exception raised deleting server-group %s',
183 server_group_id)
184
185 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +0100186 def resource_cleanup(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -0400187 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -0400188 cls.clear_servers()
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800189 cls.clear_security_groups()
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530190 cls.clear_server_groups()
Andrea Frittoli50bb80d2014-09-15 12:34:27 +0100191 super(BaseComputeTest, cls).resource_cleanup()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700192
Jay Pipes444c3e62012-10-04 19:26:35 -0400193 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900194 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500195 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900196 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -0500197 if 'name' in kwargs:
198 name = kwargs.pop('name')
199 flavor = kwargs.get('flavor', cls.flavor_ref)
200 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700201
Andrea Frittoli938d1512013-05-16 15:42:27 +0100202 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500203 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100204
205 # handle the case of multiple servers
206 servers = [body]
207 if 'min_count' in kwargs or 'max_count' in kwargs:
208 # Get servers created which name match with name param.
209 r, b = cls.servers_client.list_servers()
210 servers = [s for s in b['servers'] if s['name'].startswith(name)]
211
Sean Dague22897e12013-02-25 17:54:09 -0500212 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100213 for server in servers:
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900214 try:
215 cls.servers_client.wait_for_server_status(
216 server['id'], kwargs['wait_until'])
Matthew Treinish83d97be2014-09-19 16:34:54 -0400217 except Exception:
218 with excutils.save_and_reraise_exception():
219 if ('preserve_server_on_error' not in kwargs
220 or kwargs['preserve_server_on_error'] is False):
221 for server in servers:
222 try:
223 cls.servers_client.delete_server(
224 server['id'])
225 except Exception:
226 pass
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900227
228 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500229
Andrea Frittoli938d1512013-05-16 15:42:27 +0100230 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500231
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800232 @classmethod
233 def create_security_group(cls, name=None, description=None):
234 if name is None:
235 name = data_utils.rand_name(cls.__name__ + "-securitygroup")
236 if description is None:
237 description = data_utils.rand_name('description-')
238 resp, body = \
239 cls.security_groups_client.create_security_group(name,
240 description)
241 cls.security_groups.append(body)
242
243 return resp, body
244
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530245 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +0900246 def create_test_server_group(cls, name="", policy=None):
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530247 if not name:
248 name = data_utils.rand_name(cls.__name__ + "-Server-Group")
Ghanshyam2a180b82014-06-16 13:54:22 +0900249 if policy is None:
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530250 policy = ['affinity']
251 resp, body = cls.servers_client.create_server_group(name, policy)
David Kranzda5d4ec2014-06-24 16:04:57 -0400252 cls.server_groups.append(body['id'])
Abhijeet.Jain87dd4452014-04-23 15:51:23 +0530253 return resp, body
254
David Kranzcf0040c2012-06-26 09:46:56 -0400255 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500256 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400257 start_time = int(time.time())
258 while True:
259 try:
260 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500261 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400262 pass
263 else:
264 return
265 if int(time.time()) - start_time >= self.build_timeout:
266 condition()
267 return
268 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400269
Matt Riedemann807d0562014-01-27 12:03:10 -0800270 @staticmethod
271 def _delete_volume(volumes_client, volume_id):
272 """Deletes the given volume and waits for it to be gone."""
273 try:
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000274 volumes_client.delete_volume(volume_id)
Matt Riedemann807d0562014-01-27 12:03:10 -0800275 # TODO(mriedem): We should move the wait_for_resource_deletion
276 # into the delete_volume method as a convenience to the caller.
277 volumes_client.wait_for_resource_deletion(volume_id)
278 except exceptions.NotFound:
279 LOG.warn("Unable to delete volume '%s' since it was not found. "
280 "Maybe it was already deleted?" % volume_id)
281
Attila Fazekas423834d2014-03-14 17:33:13 +0100282 @classmethod
283 def prepare_instance_network(cls):
284 if (CONF.compute.ssh_auth_method != 'disabled' and
285 CONF.compute.ssh_connect_method == 'floating'):
286 cls.set_network_resources(network=True, subnet=True, router=True,
287 dhcp=True)
288
ivan-zhu8f992be2013-07-31 14:56:58 +0800289 @classmethod
290 def create_image_from_server(cls, server_id, **kwargs):
291 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900292 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800293 if 'name' in kwargs:
294 name = kwargs.pop('name')
295
David Kranza5299eb2015-01-15 17:24:05 -0500296 image = cls.images_client.create_image(server_id, name)
297 image_id = data_utils.parse_image_id(image.response['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800298 cls.images.append(image_id)
299
300 if 'wait_until' in kwargs:
301 cls.images_client.wait_for_image_status(image_id,
302 kwargs['wait_until'])
David Kranza5299eb2015-01-15 17:24:05 -0500303 image = cls.images_client.get_image(image_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800304
Bob Ball621e4602013-12-06 19:53:43 +0000305 if kwargs['wait_until'] == 'ACTIVE':
306 if kwargs.get('wait_for_server', True):
307 cls.servers_client.wait_for_server_status(server_id,
308 'ACTIVE')
David Kranza5299eb2015-01-15 17:24:05 -0500309 return image
ivan-zhu8f992be2013-07-31 14:56:58 +0800310
311 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000312 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800313 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000314 if server_id:
315 try:
316 cls.servers_client.delete_server(server_id)
317 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200318 except Exception:
319 LOG.exception('Failed to delete server %s' % server_id)
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900320 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000321 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000322 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800323
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800324 @classmethod
325 def delete_volume(cls, volume_id):
326 """Deletes the given volume and waits for it to be gone."""
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000327 cls._delete_volume(cls.volumes_extensions_client, volume_id)
Ken'ichi Ohmichi543a5d42014-05-02 08:44:15 +0900328
329
330class BaseV2ComputeTest(BaseComputeTest):
331 _api_version = 2
332 _interface = "json"
Matt Riedemann5dc594c2014-01-27 11:40:28 -0800333
ivan-zhuf2b00502013-10-18 10:06:52 +0800334
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900335class BaseComputeAdminTest(BaseComputeTest):
336 """Base test case class for Compute Admin API tests."""
337 _interface = "json"
ivan-zhuf2b00502013-10-18 10:06:52 +0800338
339 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +0100340 def resource_setup(cls):
341 super(BaseComputeAdminTest, cls).resource_setup()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100342 try:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400343 creds = cls.isolated_creds.get_admin_creds()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100344 cls.os_adm = clients.Manager(
345 credentials=creds, interface=cls._interface)
346 except NotImplementedError:
347 msg = ("Missing Compute Admin API credentials in configuration.")
348 raise cls.skipException(msg)
349
Ken'ichi Ohmichi9f5adf82014-12-12 04:01:32 +0000350 cls.availability_zone_admin_client = (
351 cls.os_adm.availability_zone_client)
ivan-zhu8f992be2013-07-31 14:56:58 +0800352
Ken'ichi Ohmichibcefa3d2014-05-09 08:14:05 +0900353
354class BaseV2ComputeAdminTest(BaseComputeAdminTest):
355 """Base test case class for Compute Admin V2 API tests."""
356 _api_version = 2