blob: 5539894b9bc108a88cc46165c6d744071302592c [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
David Kranz33138312013-09-24 17:09:32 -040020from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040021from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010022import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040023
Tiago Melloeda03b52012-08-22 23:47:29 -030024
Jay Pipesf38eaac2012-06-21 13:37:35 -040025LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050026
27
Attila Fazekasdc216422013-01-29 15:12:14 +010028class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050029 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050030
Attila Fazekas430dae32013-10-17 15:19:32 +020031 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103032
Jay Pipesf38eaac2012-06-21 13:37:35 -040033 @classmethod
34 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020035 super(BaseComputeTest, cls).setUpClass()
Jay Pipesf38eaac2012-06-21 13:37:35 -040036
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070037 os = cls.get_client_manager()
Daryl Walleckc7251962012-03-12 17:26:54 -050038
Jay Pipesf38eaac2012-06-21 13:37:35 -040039 cls.os = os
Jay Pipesf38eaac2012-06-21 13:37:35 -040040 cls.build_interval = cls.config.compute.build_interval
41 cls.build_timeout = cls.config.compute.build_timeout
42 cls.ssh_user = cls.config.compute.ssh_user
43 cls.image_ref = cls.config.compute.image_ref
44 cls.image_ref_alt = cls.config.compute.image_ref_alt
45 cls.flavor_ref = cls.config.compute.flavor_ref
46 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
ivan-zhuf2b00502013-10-18 10:06:52 +080047 cls.image_ssh_user = cls.config.compute.image_ssh_user
48 cls.image_ssh_password = cls.config.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040049 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040050 cls.images = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +000051 cls.multi_user = cls.get_multi_user()
52
53 @classmethod
54 def get_multi_user(cls):
55 multi_user = True
56 # Determine if there are two regular users that can be
57 # used in testing. If the test cases are allowed to create
58 # users (config.compute.allow_tenant_isolation is true,
59 # then we allow multi-user.
60 if not cls.config.compute.allow_tenant_isolation:
61 user1 = cls.config.identity.username
62 user2 = cls.config.identity.alt_username
63 if not user2 or user1 == user2:
64 multi_user = False
65 else:
66 user2_password = cls.config.identity.alt_password
67 user2_tenant_name = cls.config.identity.alt_tenant_name
68 if not user2_password or not user2_tenant_name:
69 msg = ("Alternate user specified but not alternate "
70 "tenant or password: alt_tenant_name=%s "
71 "alt_password=%s"
72 % (user2_tenant_name, user2_password))
73 raise exceptions.InvalidConfiguration(msg)
74 return multi_user
Daryl Walleckc7251962012-03-12 17:26:54 -050075
Jay Pipesf38eaac2012-06-21 13:37:35 -040076 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -040077 def clear_servers(cls):
78 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070079 try:
80 cls.servers_client.delete_server(server['id'])
81 except Exception:
82 pass
83
Jay Pipes444c3e62012-10-04 19:26:35 -040084 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070085 try:
86 cls.servers_client.wait_for_server_termination(server['id'])
87 except Exception:
88 pass
89
90 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -040091 def clear_images(cls):
92 for image_id in cls.images:
93 try:
94 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -040095 except exceptions.NotFound:
96 # The image may have already been deleted which is OK.
97 pass
Yair Frieda039f872014-01-02 12:11:10 +020098 except Exception:
99 LOG.exception('Exception raised deleting image %s' % image_id)
Sean Dagued62bf1c2013-06-05 14:36:25 -0400100 pass
101
102 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400103 def tearDownClass(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -0400104 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -0400105 cls.clear_servers()
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -0700106 cls.clear_isolated_creds()
Matthew Treinishb86cda92013-07-29 11:22:23 -0400107 super(BaseComputeTest, cls).tearDownClass()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700108
Jay Pipes444c3e62012-10-04 19:26:35 -0400109 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900110 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900112 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -0500113 if 'name' in kwargs:
114 name = kwargs.pop('name')
115 flavor = kwargs.get('flavor', cls.flavor_ref)
116 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700117
Andrea Frittoli938d1512013-05-16 15:42:27 +0100118 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500119 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100120
121 # handle the case of multiple servers
122 servers = [body]
123 if 'min_count' in kwargs or 'max_count' in kwargs:
124 # Get servers created which name match with name param.
125 r, b = cls.servers_client.list_servers()
126 servers = [s for s in b['servers'] if s['name'].startswith(name)]
127
Sean Dague22897e12013-02-25 17:54:09 -0500128 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100129 for server in servers:
Ken'ichi Ohmichi51c8c262013-12-21 03:30:37 +0900130 try:
131 cls.servers_client.wait_for_server_status(
132 server['id'], kwargs['wait_until'])
133 except Exception as ex:
134 if ('preserve_server_on_error' not in kwargs
135 or kwargs['preserve_server_on_error'] is False):
136 for server in servers:
137 try:
138 cls.servers_client.delete_server(server['id'])
139 except Exception:
140 pass
141 raise ex
142
143 cls.servers.extend(servers)
Sean Dague9b669e32012-12-13 18:40:08 -0500144
Andrea Frittoli938d1512013-05-16 15:42:27 +0100145 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500146
David Kranzcf0040c2012-06-26 09:46:56 -0400147 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500148 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400149 start_time = int(time.time())
150 while True:
151 try:
152 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500153 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400154 pass
155 else:
156 return
157 if int(time.time()) - start_time >= self.build_timeout:
158 condition()
159 return
160 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400161
162
ivan-zhuf2b00502013-10-18 10:06:52 +0800163class BaseV2ComputeTest(BaseComputeTest):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400164
165 @classmethod
166 def setUpClass(cls):
Salvatore Orlando5a337242014-01-15 22:49:22 +0000167 # By default compute tests do not create network resources
168 cls.set_network_resources()
ivan-zhuf2b00502013-10-18 10:06:52 +0800169 super(BaseV2ComputeTest, cls).setUpClass()
170 cls.servers_client = cls.os.servers_client
171 cls.flavors_client = cls.os.flavors_client
172 cls.images_client = cls.os.images_client
173 cls.extensions_client = cls.os.extensions_client
174 cls.floating_ips_client = cls.os.floating_ips_client
175 cls.keypairs_client = cls.os.keypairs_client
176 cls.security_groups_client = cls.os.security_groups_client
177 cls.quotas_client = cls.os.quotas_client
178 cls.limits_client = cls.os.limits_client
179 cls.volumes_extensions_client = cls.os.volumes_extensions_client
180 cls.volumes_client = cls.os.volumes_client
181 cls.interfaces_client = cls.os.interfaces_client
182 cls.fixed_ips_client = cls.os.fixed_ips_client
183 cls.availability_zone_client = cls.os.availability_zone_client
184 cls.aggregates_client = cls.os.aggregates_client
185 cls.services_client = cls.os.services_client
ivan-zhuef7a1bd2013-10-22 17:56:46 +0800186 cls.instance_usages_audit_log_client = \
187 cls.os.instance_usages_audit_log_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800188 cls.hypervisor_client = cls.os.hypervisor_client
189 cls.servers_client_v3_auth = cls.os.servers_client_v3_auth
ivan-zhud57f3cf2013-11-06 16:59:52 +0800190 cls.certificates_client = cls.os.certificates_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800191
ivan-zhu8f992be2013-07-31 14:56:58 +0800192 @classmethod
193 def create_image_from_server(cls, server_id, **kwargs):
194 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900195 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800196 if 'name' in kwargs:
197 name = kwargs.pop('name')
198
199 resp, image = cls.images_client.create_image(
200 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900201 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800202 cls.images.append(image_id)
203
204 if 'wait_until' in kwargs:
205 cls.images_client.wait_for_image_status(image_id,
206 kwargs['wait_until'])
207 resp, image = cls.images_client.get_image(image_id)
208
Bob Ball621e4602013-12-06 19:53:43 +0000209 if kwargs['wait_until'] == 'ACTIVE':
210 if kwargs.get('wait_for_server', True):
211 cls.servers_client.wait_for_server_status(server_id,
212 'ACTIVE')
213
ivan-zhu8f992be2013-07-31 14:56:58 +0800214 return resp, image
215
216 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000217 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800218 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000219 if server_id:
220 try:
221 cls.servers_client.delete_server(server_id)
222 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200223 except Exception:
224 LOG.exception('Failed to delete server %s' % server_id)
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000225 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900226 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
ivan-zhu8f992be2013-07-31 14:56:58 +0800227 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000228 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800229
ivan-zhuf2b00502013-10-18 10:06:52 +0800230
231class BaseV2ComputeAdminTest(BaseV2ComputeTest):
232 """Base test case class for Compute Admin V2 API tests."""
233
234 @classmethod
235 def setUpClass(cls):
236 super(BaseV2ComputeAdminTest, cls).setUpClass()
Attila Fazekas4ba36582013-02-12 08:26:17 +0100237 admin_username = cls.config.compute_admin.username
238 admin_password = cls.config.compute_admin.password
239 admin_tenant = cls.config.compute_admin.tenant_name
Attila Fazekas4ba36582013-02-12 08:26:17 +0100240 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400241 msg = ("Missing Compute Admin API credentials "
242 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800243 raise cls.skipException(msg)
Attila Fazekas430dae32013-10-17 15:19:32 +0200244 if (cls.config.compute.allow_tenant_isolation or
245 cls.force_tenant_isolation is True):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400246 creds = cls.isolated_creds.get_admin_creds()
Matthew Treinish47ff7912013-07-22 17:09:55 -0400247 admin_username, admin_tenant_name, admin_password = creds
248 cls.os_adm = clients.Manager(username=admin_username,
249 password=admin_password,
250 tenant_name=admin_tenant_name,
251 interface=cls._interface)
252 else:
253 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
ivan-zhu8f992be2013-07-31 14:56:58 +0800254
255
256class BaseV3ComputeTest(BaseComputeTest):
257
258 @classmethod
259 def setUpClass(cls):
Matthew Treinisheb29dac2014-01-16 23:43:06 -0500260 # By default compute tests do not create network resources
Sean Dague25eef9a2014-01-23 08:00:56 -0500261 if cls._interface == "xml":
262 skip_msg = ("XML interface is being removed from Nova v3. "
263 "%s will be removed shortly" % cls.__name__)
264 raise cls.skipException(skip_msg)
265
Matthew Treinisheb29dac2014-01-16 23:43:06 -0500266 cls.set_network_resources()
ivan-zhu8f992be2013-07-31 14:56:58 +0800267 super(BaseV3ComputeTest, cls).setUpClass()
268 if not cls.config.compute_feature_enabled.api_v3:
armando-migliaccio3e2a0282013-11-22 14:47:16 -0800269 cls.tearDownClass()
ivan-zhu8f992be2013-07-31 14:56:58 +0800270 skip_msg = ("%s skipped as nova v3 api is not available" %
271 cls.__name__)
272 raise cls.skipException(skip_msg)
273
274 cls.servers_client = cls.os.servers_v3_client
275 cls.images_client = cls.os.image_client
ivan-zhub25900a2013-12-13 16:28:45 +0800276 cls.flavors_client = cls.os.flavors_v3_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800277 cls.services_client = cls.os.services_v3_client
ivan-zhu31d98482013-08-22 10:51:48 +0800278 cls.extensions_client = cls.os.extensions_v3_client
ivan-zhuac7b3802013-08-21 16:03:53 +0800279 cls.availability_zone_client = cls.os.availability_zone_v3_client
ivan-zhu91feab92013-08-15 18:25:33 +0800280 cls.interfaces_client = cls.os.interfaces_v3_client
ivan-zhu56156ae2013-12-17 15:48:13 +0800281 cls.instance_usages_audit_log_client = \
282 cls.os.instance_usages_audit_log_v3_client
ivan-zhu6f5f9e92013-08-21 22:16:37 +0800283 cls.hypervisor_client = cls.os.hypervisor_v3_client
ivan-zhuf0bf3012013-11-25 16:03:42 +0800284 cls.keypairs_client = cls.os.keypairs_v3_client
ivan-zhubec84a32013-08-21 15:32:05 +0800285 cls.tenant_usages_client = cls.os.tenant_usages_v3_client
ivan-zhu50969522013-08-26 11:10:39 +0800286 cls.volumes_client = cls.os.volumes_client
ivan-zhu0e062922013-12-17 16:14:12 +0800287 cls.certificates_client = cls.os.certificates_v3_client
ivan-zhu7e7e6a32013-11-20 16:07:29 +0800288 cls.keypairs_client = cls.os.keypairs_v3_client
ivan-zhu00fe64f2013-08-20 19:35:51 +0800289 cls.aggregates_client = cls.os.aggregates_v3_client
290 cls.hosts_client = cls.os.hosts_v3_client
ivan-zhub6d69ee2013-12-17 14:16:31 +0800291 cls.quotas_client = cls.os.quotas_v3_client
Ken'ichi Ohmichi3d6b6fa2014-01-08 04:42:16 +0900292 cls.version_client = cls.os.version_v3_client
ivan-zhu8f992be2013-07-31 14:56:58 +0800293
294 @classmethod
295 def create_image_from_server(cls, server_id, **kwargs):
296 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900297 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800298 if 'name' in kwargs:
299 name = kwargs.pop('name')
300
301 resp, image = cls.servers_client.create_image(
302 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900303 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800304 cls.images.append(image_id)
305
306 if 'wait_until' in kwargs:
307 cls.images_client.wait_for_image_status(image_id,
308 kwargs['wait_until'])
309 resp, image = cls.images_client.get_image_meta(image_id)
310
311 return resp, image
312
313 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000314 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800315 # Destroy an existing server and creates a new one
316 try:
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000317 cls.servers_client.delete_server(server_id)
318 cls.servers_client.wait_for_server_termination(server_id)
Yair Frieda039f872014-01-02 12:11:10 +0200319 except Exception:
320 LOG.exception('Failed to delete server %s' % server_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800321 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900322 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +0900323 cls.password = server['admin_password']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000324 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800325
326
327class BaseV3ComputeAdminTest(BaseV3ComputeTest):
328 """Base test case class for all Compute Admin API V3 tests."""
329
330 @classmethod
331 def setUpClass(cls):
332 super(BaseV3ComputeAdminTest, cls).setUpClass()
333 admin_username = cls.config.compute_admin.username
334 admin_password = cls.config.compute_admin.password
335 admin_tenant = cls.config.compute_admin.tenant_name
336 if not (admin_username and admin_password and admin_tenant):
337 msg = ("Missing Compute Admin API credentials "
338 "in configuration.")
339 raise cls.skipException(msg)
340 if cls.config.compute.allow_tenant_isolation:
341 creds = cls.isolated_creds.get_admin_creds()
342 admin_username, admin_tenant_name, admin_password = creds
343 os_adm = clients.Manager(username=admin_username,
344 password=admin_password,
345 tenant_name=admin_tenant_name,
346 interface=cls._interface)
347 else:
348 os_adm = clients.ComputeAdminManager(interface=cls._interface)
349
350 cls.os_adm = os_adm
ivan-zhu00fe64f2013-08-20 19:35:51 +0800351 cls.servers_admin_client = cls.os_adm.servers_v3_client
ivan-zhu56156ae2013-12-17 15:48:13 +0800352 cls.instance_usages_audit_log_admin_client = \
353 cls.os_adm.instance_usages_audit_log_v3_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800354 cls.services_admin_client = cls.os_adm.services_v3_client
ivan-zhuac7b3802013-08-21 16:03:53 +0800355 cls.availability_zone_admin_client = \
356 cls.os_adm.availability_zone_v3_client
ivan-zhu6f5f9e92013-08-21 22:16:37 +0800357 cls.hypervisor_admin_client = cls.os_adm.hypervisor_v3_client
ivan-zhubec84a32013-08-21 15:32:05 +0800358 cls.tenant_usages_admin_client = cls.os_adm.tenant_usages_v3_client
ivan-zhub25900a2013-12-13 16:28:45 +0800359 cls.flavors_admin_client = cls.os_adm.flavors_v3_client
ivan-zhu00fe64f2013-08-20 19:35:51 +0800360 cls.aggregates_admin_client = cls.os_adm.aggregates_v3_client
361 cls.hosts_admin_client = cls.os_adm.hosts_v3_client
ivan-zhub6d69ee2013-12-17 14:16:31 +0800362 cls.quotas_admin_client = cls.os_adm.quotas_v3_client