blob: 4b6816b6de722859f6e4bd1a52f274ef24621849 [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
Sean Dague1937d092013-05-17 16:36:38 -040020from tempest.api import compute
Matthew Treinish481466b2012-12-20 17:16:01 -050021from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090022from tempest.common.utils import data_utils
David Kranz33138312013-09-24 17:09:32 -040023from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040024from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010025import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040026
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
Attila Fazekas9a63c942013-01-29 21:46:02 +010034 conclusion = compute.generic_setup_package()
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
38 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020039 super(BaseComputeTest, cls).setUpClass()
Matthew Treinish6b41e242013-07-19 16:49:28 -040040 if not cls.config.service_available.nova:
41 skip_msg = ("%s skipped as nova is not available" % cls.__name__)
42 raise cls.skipException(skip_msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040043
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070044 os = cls.get_client_manager()
Daryl Walleckc7251962012-03-12 17:26:54 -050045
Jay Pipesf38eaac2012-06-21 13:37:35 -040046 cls.os = os
Jay Pipesf38eaac2012-06-21 13:37:35 -040047 cls.build_interval = cls.config.compute.build_interval
48 cls.build_timeout = cls.config.compute.build_timeout
49 cls.ssh_user = cls.config.compute.ssh_user
50 cls.image_ref = cls.config.compute.image_ref
51 cls.image_ref_alt = cls.config.compute.image_ref_alt
52 cls.flavor_ref = cls.config.compute.flavor_ref
53 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
ivan-zhuf2b00502013-10-18 10:06:52 +080054 cls.image_ssh_user = cls.config.compute.image_ssh_user
55 cls.image_ssh_password = cls.config.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040056 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040057 cls.images = []
Daryl Walleckc7251962012-03-12 17:26:54 -050058
Jay Pipesf38eaac2012-06-21 13:37:35 -040059 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -040060 def clear_servers(cls):
61 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070062 try:
63 cls.servers_client.delete_server(server['id'])
64 except Exception:
65 pass
66
Jay Pipes444c3e62012-10-04 19:26:35 -040067 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070068 try:
69 cls.servers_client.wait_for_server_termination(server['id'])
70 except Exception:
71 pass
72
73 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -040074 def clear_images(cls):
75 for image_id in cls.images:
76 try:
77 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -040078 except exceptions.NotFound:
79 # The image may have already been deleted which is OK.
80 pass
Sean Dagued62bf1c2013-06-05 14:36:25 -040081 except Exception as exc:
82 LOG.info('Exception raised deleting image %s', image_id)
83 LOG.exception(exc)
84 pass
85
86 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -040087 def tearDownClass(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -040088 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -040089 cls.clear_servers()
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070090 cls.clear_isolated_creds()
Matthew Treinishb86cda92013-07-29 11:22:23 -040091 super(BaseComputeTest, cls).tearDownClass()
Rohit Karajgidc300b22012-05-04 08:11:00 -070092
Jay Pipes444c3e62012-10-04 19:26:35 -040093 @classmethod
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090094 def create_test_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050095 """Wrapper utility that returns a test server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +090096 name = data_utils.rand_name(cls.__name__ + "-instance")
Sean Dague22897e12013-02-25 17:54:09 -050097 if 'name' in kwargs:
98 name = kwargs.pop('name')
99 flavor = kwargs.get('flavor', cls.flavor_ref)
100 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700101
Andrea Frittoli938d1512013-05-16 15:42:27 +0100102 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500103 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100104
105 # handle the case of multiple servers
106 servers = [body]
107 if 'min_count' in kwargs or 'max_count' in kwargs:
108 # Get servers created which name match with name param.
109 r, b = cls.servers_client.list_servers()
110 servers = [s for s in b['servers'] if s['name'].startswith(name)]
111
112 cls.servers.extend(servers)
David Kranzcf0040c2012-06-26 09:46:56 -0400113
Sean Dague22897e12013-02-25 17:54:09 -0500114 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100115 for server in servers:
116 cls.servers_client.wait_for_server_status(
117 server['id'], kwargs['wait_until'])
Sean Dague9b669e32012-12-13 18:40:08 -0500118
Andrea Frittoli938d1512013-05-16 15:42:27 +0100119 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500120
David Kranzcf0040c2012-06-26 09:46:56 -0400121 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500122 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400123 start_time = int(time.time())
124 while True:
125 try:
126 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500127 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400128 pass
129 else:
130 return
131 if int(time.time()) - start_time >= self.build_timeout:
132 condition()
133 return
134 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400135
136
ivan-zhuf2b00502013-10-18 10:06:52 +0800137class BaseV2ComputeTest(BaseComputeTest):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400138
139 @classmethod
140 def setUpClass(cls):
ivan-zhuf2b00502013-10-18 10:06:52 +0800141 super(BaseV2ComputeTest, cls).setUpClass()
142 cls.servers_client = cls.os.servers_client
143 cls.flavors_client = cls.os.flavors_client
144 cls.images_client = cls.os.images_client
145 cls.extensions_client = cls.os.extensions_client
146 cls.floating_ips_client = cls.os.floating_ips_client
147 cls.keypairs_client = cls.os.keypairs_client
148 cls.security_groups_client = cls.os.security_groups_client
149 cls.quotas_client = cls.os.quotas_client
150 cls.limits_client = cls.os.limits_client
151 cls.volumes_extensions_client = cls.os.volumes_extensions_client
152 cls.volumes_client = cls.os.volumes_client
153 cls.interfaces_client = cls.os.interfaces_client
154 cls.fixed_ips_client = cls.os.fixed_ips_client
155 cls.availability_zone_client = cls.os.availability_zone_client
156 cls.aggregates_client = cls.os.aggregates_client
157 cls.services_client = cls.os.services_client
ivan-zhuef7a1bd2013-10-22 17:56:46 +0800158 cls.instance_usages_audit_log_client = \
159 cls.os.instance_usages_audit_log_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800160 cls.hypervisor_client = cls.os.hypervisor_client
161 cls.servers_client_v3_auth = cls.os.servers_client_v3_auth
ivan-zhud57f3cf2013-11-06 16:59:52 +0800162 cls.certificates_client = cls.os.certificates_client
ivan-zhuf2b00502013-10-18 10:06:52 +0800163
ivan-zhu8f992be2013-07-31 14:56:58 +0800164 @classmethod
165 def create_image_from_server(cls, server_id, **kwargs):
166 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900167 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800168 if 'name' in kwargs:
169 name = kwargs.pop('name')
170
171 resp, image = cls.images_client.create_image(
172 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900173 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800174 cls.images.append(image_id)
175
176 if 'wait_until' in kwargs:
177 cls.images_client.wait_for_image_status(image_id,
178 kwargs['wait_until'])
179 resp, image = cls.images_client.get_image(image_id)
180
181 return resp, image
182
183 @classmethod
184 def rebuild_server(cls, **kwargs):
185 # Destroy an existing server and creates a new one
186 try:
187 cls.servers_client.delete_server(cls.server_id)
188 cls.servers_client.wait_for_server_termination(cls.server_id)
189 except Exception as exc:
190 LOG.exception(exc)
191 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900192 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
ivan-zhu8f992be2013-07-31 14:56:58 +0800193 cls.server_id = server['id']
194 cls.password = server['adminPass']
195
ivan-zhuf2b00502013-10-18 10:06:52 +0800196
197class BaseV2ComputeAdminTest(BaseV2ComputeTest):
198 """Base test case class for Compute Admin V2 API tests."""
199
200 @classmethod
201 def setUpClass(cls):
202 super(BaseV2ComputeAdminTest, cls).setUpClass()
Attila Fazekas4ba36582013-02-12 08:26:17 +0100203 admin_username = cls.config.compute_admin.username
204 admin_password = cls.config.compute_admin.password
205 admin_tenant = cls.config.compute_admin.tenant_name
Attila Fazekas4ba36582013-02-12 08:26:17 +0100206 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400207 msg = ("Missing Compute Admin API credentials "
208 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800209 raise cls.skipException(msg)
Attila Fazekas430dae32013-10-17 15:19:32 +0200210 if (cls.config.compute.allow_tenant_isolation or
211 cls.force_tenant_isolation is True):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400212 creds = cls.isolated_creds.get_admin_creds()
Matthew Treinish47ff7912013-07-22 17:09:55 -0400213 admin_username, admin_tenant_name, admin_password = creds
214 cls.os_adm = clients.Manager(username=admin_username,
215 password=admin_password,
216 tenant_name=admin_tenant_name,
217 interface=cls._interface)
218 else:
219 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
ivan-zhu8f992be2013-07-31 14:56:58 +0800220
221
222class BaseV3ComputeTest(BaseComputeTest):
223
224 @classmethod
225 def setUpClass(cls):
226 super(BaseV3ComputeTest, cls).setUpClass()
227 if not cls.config.compute_feature_enabled.api_v3:
228 skip_msg = ("%s skipped as nova v3 api is not available" %
229 cls.__name__)
230 raise cls.skipException(skip_msg)
231
232 cls.servers_client = cls.os.servers_v3_client
233 cls.images_client = cls.os.image_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800234 cls.services_client = cls.os.services_v3_client
ivan-zhu31d98482013-08-22 10:51:48 +0800235 cls.extensions_client = cls.os.extensions_v3_client
ivan-zhu8f992be2013-07-31 14:56:58 +0800236
237 @classmethod
238 def create_image_from_server(cls, server_id, **kwargs):
239 """Wrapper utility that returns an image created from the server."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900240 name = data_utils.rand_name(cls.__name__ + "-image")
ivan-zhu8f992be2013-07-31 14:56:58 +0800241 if 'name' in kwargs:
242 name = kwargs.pop('name')
243
244 resp, image = cls.servers_client.create_image(
245 server_id, name)
Masayuki Igawa259c1132013-10-31 17:48:44 +0900246 image_id = data_utils.parse_image_id(resp['location'])
ivan-zhu8f992be2013-07-31 14:56:58 +0800247 cls.images.append(image_id)
248
249 if 'wait_until' in kwargs:
250 cls.images_client.wait_for_image_status(image_id,
251 kwargs['wait_until'])
252 resp, image = cls.images_client.get_image_meta(image_id)
253
254 return resp, image
255
256 @classmethod
257 def rebuild_server(cls, **kwargs):
258 # Destroy an existing server and creates a new one
259 try:
260 cls.servers_client.delete_server(cls.server_id)
261 cls.servers_client.wait_for_server_termination(cls.server_id)
262 except Exception as exc:
263 LOG.exception(exc)
264 pass
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +0900265 resp, server = cls.create_test_server(wait_until='ACTIVE', **kwargs)
ivan-zhu8f992be2013-07-31 14:56:58 +0800266 cls.server_id = server['id']
Ken'ichi Ohmichie985ca82013-11-12 15:10:35 +0900267 cls.password = server['admin_password']
ivan-zhu8f992be2013-07-31 14:56:58 +0800268
269
270class BaseV3ComputeAdminTest(BaseV3ComputeTest):
271 """Base test case class for all Compute Admin API V3 tests."""
272
273 @classmethod
274 def setUpClass(cls):
275 super(BaseV3ComputeAdminTest, cls).setUpClass()
276 admin_username = cls.config.compute_admin.username
277 admin_password = cls.config.compute_admin.password
278 admin_tenant = cls.config.compute_admin.tenant_name
279 if not (admin_username and admin_password and admin_tenant):
280 msg = ("Missing Compute Admin API credentials "
281 "in configuration.")
282 raise cls.skipException(msg)
283 if cls.config.compute.allow_tenant_isolation:
284 creds = cls.isolated_creds.get_admin_creds()
285 admin_username, admin_tenant_name, admin_password = creds
286 os_adm = clients.Manager(username=admin_username,
287 password=admin_password,
288 tenant_name=admin_tenant_name,
289 interface=cls._interface)
290 else:
291 os_adm = clients.ComputeAdminManager(interface=cls._interface)
292
293 cls.os_adm = os_adm
294 cls.severs_admin_client = cls.os_adm.servers_v3_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800295 cls.services_admin_client = cls.os_adm.services_v3_client