blob: 9cb425a5becd713a5f29574173f9304026da4bc3 [file] [log] [blame]
Jay Pipes13b479b2012-06-11 14:52:27 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipes13b479b2012-06-11 14:52:27 -04004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
David Kranzcf0040c2012-06-26 09:46:56 -040018import time
Jay Pipesf38eaac2012-06-21 13:37:35 -040019
Matthew Treinish481466b2012-12-20 17:16:01 -050020from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090021from tempest.common.utils import data_utils
David Kranz33138312013-09-24 17:09:32 -040022from tempest import exceptions
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
Tiago Melloeda03b52012-08-22 23:47:29 -030026
Jay Pipesf38eaac2012-06-21 13:37:35 -040027LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050028
29
Attila Fazekasdc216422013-01-29 15:12:14 +010030class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050031 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050032
Attila Fazekas430dae32013-10-17 15:19:32 +020033 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103034
Jay Pipesf38eaac2012-06-21 13:37:35 -040035 @classmethod
36 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020037 super(BaseComputeTest, cls).setUpClass()
Matthew Treinish6b41e242013-07-19 16:49:28 -040038 if not cls.config.service_available.nova:
39 skip_msg = ("%s skipped as nova is not available" % cls.__name__)
40 raise cls.skipException(skip_msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040041
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070042 os = cls.get_client_manager()
Daryl Walleckc7251962012-03-12 17:26:54 -050043
Jay Pipesf38eaac2012-06-21 13:37:35 -040044 cls.os = os
Jay Pipesf38eaac2012-06-21 13:37:35 -040045 cls.build_interval = cls.config.compute.build_interval
46 cls.build_timeout = cls.config.compute.build_timeout
47 cls.ssh_user = cls.config.compute.ssh_user
48 cls.image_ref = cls.config.compute.image_ref
49 cls.image_ref_alt = cls.config.compute.image_ref_alt
50 cls.flavor_ref = cls.config.compute.flavor_ref
51 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
ivan-zhuf2b00502013-10-18 10:06:52 +080052 cls.image_ssh_user = cls.config.compute.image_ssh_user
53 cls.image_ssh_password = cls.config.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040054 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040055 cls.images = []
Matthew Treinishf7fca6a2013-12-09 16:27:23 +000056 cls.multi_user = cls.get_multi_user()
57
58 @classmethod
59 def get_multi_user(cls):
60 multi_user = True
61 # Determine if there are two regular users that can be
62 # used in testing. If the test cases are allowed to create
63 # users (config.compute.allow_tenant_isolation is true,
64 # then we allow multi-user.
65 if not cls.config.compute.allow_tenant_isolation:
66 user1 = cls.config.identity.username
67 user2 = cls.config.identity.alt_username
68 if not user2 or user1 == user2:
69 multi_user = False
70 else:
71 user2_password = cls.config.identity.alt_password
72 user2_tenant_name = cls.config.identity.alt_tenant_name
73 if not user2_password or not user2_tenant_name:
74 msg = ("Alternate user specified but not alternate "
75 "tenant or password: alt_tenant_name=%s "
76 "alt_password=%s"
77 % (user2_tenant_name, user2_password))
78 raise exceptions.InvalidConfiguration(msg)
79 return multi_user
Daryl Walleckc7251962012-03-12 17:26:54 -050080
Jay Pipesf38eaac2012-06-21 13:37:35 -040081 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -040082 def clear_servers(cls):
83 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070084 try:
85 cls.servers_client.delete_server(server['id'])
86 except Exception:
87 pass
88
Jay Pipes444c3e62012-10-04 19:26:35 -040089 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070090 try:
91 cls.servers_client.wait_for_server_termination(server['id'])
92 except Exception:
93 pass
94
95 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -040096 def clear_images(cls):
97 for image_id in cls.images:
98 try:
99 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -0400100 except exceptions.NotFound:
101 # The image may have already been deleted which is OK.
102 pass
Sean Dagued62bf1c2013-06-05 14:36:25 -0400103 except Exception as exc:
104 LOG.info('Exception raised deleting image %s', image_id)
105 LOG.exception(exc)
106 pass
107
108 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400109 def tearDownClass(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -0400110 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -0400111 cls.clear_servers()
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -0700112 cls.clear_isolated_creds()
Matthew Treinishb86cda92013-07-29 11:22:23 -0400113 super(BaseComputeTest, cls).tearDownClass()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700114
Jay Pipes444c3e62012-10-04 19:26:35 -0400115 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900116 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900118 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -0500119 if 'name' in kwargs:
120 name = kwargs.pop('name')
121 flavor = kwargs.get('flavor', cls.flavor_ref)
122 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700123
Andrea Frittoli938d1512013-05-16 15:42:27 +0100124 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500125 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100126
127 # handle the case of multiple servers
128 servers = [body]
129 if 'min_count' in kwargs or 'max_count' in kwargs:
130 # Get servers created which name match with name param.
131 r, b = cls.servers_client.list_servers()
132 servers = [s for s in b['servers'] if s['name'].startswith(name)]
133
134 cls.servers.extend(servers)
David Kranzcf0040c2012-06-26 09:46:56 -0400135
Sean Dague22897e12013-02-25 17:54:09 -0500136 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100137 for server in servers:
138 cls.servers_client.wait_for_server_status(
139 server['id'], kwargs['wait_until'])
Sean Dague9b669e32012-12-13 18:40:08 -0500140
Andrea Frittoli938d1512013-05-16 15:42:27 +0100141 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500142
David Kranzcf0040c2012-06-26 09:46:56 -0400143 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500144 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400145 start_time = int(time.time())
146 while True:
147 try:
148 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500149 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400150 pass
151 else:
152 return
153 if int(time.time()) - start_time >= self.build_timeout:
154 condition()
155 return
156 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400157
158
ivan-zhuf2b00502013-10-18 10:06:52 +0800159class BaseV2ComputeTest(BaseComputeTest):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400160
161 @classmethod
162 def setUpClass(cls):
ivan-zhuf2b00502013-10-18 10:06:52 +0800163 super(BaseV2ComputeTest, cls).setUpClass()
164 cls.servers_client = cls.os.servers_client
165 cls.flavors_client = cls.os.flavors_client
166 cls.images_client = cls.os.images_client
167 cls.extensions_client = cls.os.extensions_client
168 cls.floating_ips_client = cls.os.floating_ips_client
169 cls.keypairs_client = cls.os.keypairs_client
170 cls.security_groups_client = cls.os.security_groups_client
171 cls.quotas_client = cls.os.quotas_client
172 cls.limits_client = cls.os.limits_client
173 cls.volumes_extensions_client = cls.os.volumes_extensions_client
174 cls.volumes_client = cls.os.volumes_client
175 cls.interfaces_client = cls.os.interfaces_client
176 cls.fixed_ips_client = cls.os.fixed_ips_client
177 cls.availability_zone_client = cls.os.availability_zone_client
178 cls.aggregates_client = cls.os.aggregates_client
179 cls.services_client = cls.os.services_client
ivan-zhuef7a1bd2013-10-22 17:56:46 +0800180 cls.instance_usages_audit_log_client = \
181 cls.os.instance_usages_audit_log_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800182 cls.hypervisor_client = cls.os.hypervisor_client
183 cls.servers_client_v3_auth = cls.os.servers_client_v3_auth
ivan-zhud57f3cf2013-11-06 16:59:52 +0800184 cls.certificates_client = cls.os.certificates_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800185
ivan-zhu8f992be2013-07-31 14:56:58 +0800186 @classmethod
187 def create_image_from_server(cls, server_id, **kwargs):
188 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900189 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800190 if 'name' in kwargs:
191 name = kwargs.pop('name')
192
193 resp, image = cls.images_client.create_image(
194 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900195 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800196 cls.images.append(image_id)
197
198 if 'wait_until' in kwargs:
199 cls.images_client.wait_for_image_status(image_id,
200 kwargs['wait_until'])
201 resp, image = cls.images_client.get_image(image_id)
202
Bob Ball621e4602013-12-06 19:53:43 +0000203 if kwargs['wait_until'] == 'ACTIVE':
204 if kwargs.get('wait_for_server', True):
205 cls.servers_client.wait_for_server_status(server_id,
206 'ACTIVE')
207
ivan-zhu8f992be2013-07-31 14:56:58 +0800208 return resp, image
209
210 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000211 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800212 # Destroy an existing server and creates a new one
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000213 if server_id:
214 try:
215 cls.servers_client.delete_server(server_id)
216 cls.servers_client.wait_for_server_termination(server_id)
217 except Exception as exc:
218 LOG.exception(exc)
219 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900220 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
ivan-zhu8f992be2013-07-31 14:56:58 +0800221 cls.password = server['adminPass']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000222 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800223
ivan-zhuf2b00502013-10-18 10:06:52 +0800224
225class BaseV2ComputeAdminTest(BaseV2ComputeTest):
226 """Base test case class for Compute Admin V2 API tests."""
227
228 @classmethod
229 def setUpClass(cls):
230 super(BaseV2ComputeAdminTest, cls).setUpClass()
Attila Fazekas4ba36582013-02-12 08:26:17 +0100231 admin_username = cls.config.compute_admin.username
232 admin_password = cls.config.compute_admin.password
233 admin_tenant = cls.config.compute_admin.tenant_name
Attila Fazekas4ba36582013-02-12 08:26:17 +0100234 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400235 msg = ("Missing Compute Admin API credentials "
236 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800237 raise cls.skipException(msg)
Attila Fazekas430dae32013-10-17 15:19:32 +0200238 if (cls.config.compute.allow_tenant_isolation or
239 cls.force_tenant_isolation is True):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400240 creds = cls.isolated_creds.get_admin_creds()
Matthew Treinish47ff7912013-07-22 17:09:55 -0400241 admin_username, admin_tenant_name, admin_password = creds
242 cls.os_adm = clients.Manager(username=admin_username,
243 password=admin_password,
244 tenant_name=admin_tenant_name,
245 interface=cls._interface)
246 else:
247 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
ivan-zhu8f992be2013-07-31 14:56:58 +0800248
249
250class BaseV3ComputeTest(BaseComputeTest):
251
252 @classmethod
253 def setUpClass(cls):
254 super(BaseV3ComputeTest, cls).setUpClass()
255 if not cls.config.compute_feature_enabled.api_v3:
armando-migliaccio3e2a0282013-11-22 14:47:16 -0800256 cls.tearDownClass()
ivan-zhu8f992be2013-07-31 14:56:58 +0800257 skip_msg = ("%s skipped as nova v3 api is not available" %
258 cls.__name__)
259 raise cls.skipException(skip_msg)
260
261 cls.servers_client = cls.os.servers_v3_client
262 cls.images_client = cls.os.image_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800263 cls.services_client = cls.os.services_v3_client
ivan-zhu31d98482013-08-22 10:51:48 +0800264 cls.extensions_client = cls.os.extensions_v3_client
ivan-zhuac7b3802013-08-21 16:03:53 +0800265 cls.availability_zone_client = cls.os.availability_zone_v3_client
ivan-zhu91feab92013-08-15 18:25:33 +0800266 cls.interfaces_client = cls.os.interfaces_v3_client
ivan-zhu6f5f9e92013-08-21 22:16:37 +0800267 cls.hypervisor_client = cls.os.hypervisor_v3_client
ivan-zhubec84a32013-08-21 15:32:05 +0800268 cls.tenant_usages_client = cls.os.tenant_usages_v3_client
ivan-zhu50969522013-08-26 11:10:39 +0800269 cls.volumes_client = cls.os.volumes_client
ivan-zhu0e062922013-12-17 16:14:12 +0800270 cls.certificates_client = cls.os.certificates_v3_client
ivan-zhu8f992be2013-07-31 14:56:58 +0800271
272 @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
279 resp, image = cls.servers_client.create_image(
280 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900281 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800282 cls.images.append(image_id)
283
284 if 'wait_until' in kwargs:
285 cls.images_client.wait_for_image_status(image_id,
286 kwargs['wait_until'])
287 resp, image = cls.images_client.get_image_meta(image_id)
288
289 return resp, image
290
291 @classmethod
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000292 def rebuild_server(cls, server_id, **kwargs):
ivan-zhu8f992be2013-07-31 14:56:58 +0800293 # Destroy an existing server and creates a new one
294 try:
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000295 cls.servers_client.delete_server(server_id)
296 cls.servers_client.wait_for_server_termination(server_id)
ivan-zhu8f992be2013-07-31 14:56:58 +0800297 except Exception as exc:
298 LOG.exception(exc)
299 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900300 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +0900301 cls.password = server['admin_password']
Matthew Treinish2cd19a42013-12-02 21:54:42 +0000302 return server['id']
ivan-zhu8f992be2013-07-31 14:56:58 +0800303
304
305class BaseV3ComputeAdminTest(BaseV3ComputeTest):
306 """Base test case class for all Compute Admin API V3 tests."""
307
308 @classmethod
309 def setUpClass(cls):
310 super(BaseV3ComputeAdminTest, cls).setUpClass()
311 admin_username = cls.config.compute_admin.username
312 admin_password = cls.config.compute_admin.password
313 admin_tenant = cls.config.compute_admin.tenant_name
314 if not (admin_username and admin_password and admin_tenant):
315 msg = ("Missing Compute Admin API credentials "
316 "in configuration.")
317 raise cls.skipException(msg)
318 if cls.config.compute.allow_tenant_isolation:
319 creds = cls.isolated_creds.get_admin_creds()
320 admin_username, admin_tenant_name, admin_password = creds
321 os_adm = clients.Manager(username=admin_username,
322 password=admin_password,
323 tenant_name=admin_tenant_name,
324 interface=cls._interface)
325 else:
326 os_adm = clients.ComputeAdminManager(interface=cls._interface)
327
328 cls.os_adm = os_adm
329 cls.severs_admin_client = cls.os_adm.servers_v3_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800330 cls.services_admin_client = cls.os_adm.services_v3_client
ivan-zhuac7b3802013-08-21 16:03:53 +0800331 cls.availability_zone_admin_client = \
332 cls.os_adm.availability_zone_v3_client
ivan-zhu6f5f9e92013-08-21 22:16:37 +0800333 cls.hypervisor_admin_client = cls.os_adm.hypervisor_v3_client
ivan-zhubec84a32013-08-21 15:32:05 +0800334 cls.tenant_usages_admin_client = cls.os_adm.tenant_usages_v3_client