blob: fcdfbda4466d35b1ee430ca02a88148e26449d45 [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
Sean Dagued62bf1c2013-06-05 14:36:25 -040022from tempest.common.utils.data_utils import parse_image_id
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest.common.utils.data_utils import rand_name
David Kranz33138312013-09-24 17:09:32 -040024from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040025from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010026import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040027
Tiago Melloeda03b52012-08-22 23:47:29 -030028
Jay Pipesf38eaac2012-06-21 13:37:35 -040029LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050030
31
Attila Fazekasdc216422013-01-29 15:12:14 +010032class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050034
Attila Fazekas9a63c942013-01-29 21:46:02 +010035 conclusion = compute.generic_setup_package()
Attila Fazekas430dae32013-10-17 15:19:32 +020036 force_tenant_isolation = False
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103037
Jay Pipesf38eaac2012-06-21 13:37:35 -040038 @classmethod
39 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020040 super(BaseComputeTest, cls).setUpClass()
Matthew Treinish6b41e242013-07-19 16:49:28 -040041 if not cls.config.service_available.nova:
42 skip_msg = ("%s skipped as nova is not available" % cls.__name__)
43 raise cls.skipException(skip_msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -040044
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070045 os = cls.get_client_manager()
Daryl Walleckc7251962012-03-12 17:26:54 -050046
Jay Pipesf38eaac2012-06-21 13:37:35 -040047 cls.os = os
Jay Pipesf38eaac2012-06-21 13:37:35 -040048 cls.build_interval = cls.config.compute.build_interval
49 cls.build_timeout = cls.config.compute.build_timeout
50 cls.ssh_user = cls.config.compute.ssh_user
51 cls.image_ref = cls.config.compute.image_ref
52 cls.image_ref_alt = cls.config.compute.image_ref_alt
53 cls.flavor_ref = cls.config.compute.flavor_ref
54 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
ivan-zhuf2b00502013-10-18 10:06:52 +080055 cls.image_ssh_user = cls.config.compute.image_ssh_user
56 cls.image_ssh_password = cls.config.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040057 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040058 cls.images = []
Daryl Walleckc7251962012-03-12 17:26:54 -050059
Jay Pipesf38eaac2012-06-21 13:37:35 -040060 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -040061 def clear_servers(cls):
62 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070063 try:
64 cls.servers_client.delete_server(server['id'])
65 except Exception:
66 pass
67
Jay Pipes444c3e62012-10-04 19:26:35 -040068 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070069 try:
70 cls.servers_client.wait_for_server_termination(server['id'])
71 except Exception:
72 pass
73
74 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -040075 def clear_images(cls):
76 for image_id in cls.images:
77 try:
78 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -040079 except exceptions.NotFound:
80 # The image may have already been deleted which is OK.
81 pass
Sean Dagued62bf1c2013-06-05 14:36:25 -040082 except Exception as exc:
83 LOG.info('Exception raised deleting image %s', image_id)
84 LOG.exception(exc)
85 pass
86
87 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -040088 def tearDownClass(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -040089 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -040090 cls.clear_servers()
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070091 cls.clear_isolated_creds()
Matthew Treinishb86cda92013-07-29 11:22:23 -040092 super(BaseComputeTest, cls).tearDownClass()
Rohit Karajgidc300b22012-05-04 08:11:00 -070093
Jay Pipes444c3e62012-10-04 19:26:35 -040094 @classmethod
Sean Dague22897e12013-02-25 17:54:09 -050095 def create_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050096 """Wrapper utility that returns a test server."""
Sean Dague22897e12013-02-25 17:54:09 -050097 name = rand_name(cls.__name__ + "-instance")
98 if 'name' in kwargs:
99 name = kwargs.pop('name')
100 flavor = kwargs.get('flavor', cls.flavor_ref)
101 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700102
Andrea Frittoli938d1512013-05-16 15:42:27 +0100103 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500104 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100105
106 # handle the case of multiple servers
107 servers = [body]
108 if 'min_count' in kwargs or 'max_count' in kwargs:
109 # Get servers created which name match with name param.
110 r, b = cls.servers_client.list_servers()
111 servers = [s for s in b['servers'] if s['name'].startswith(name)]
112
113 cls.servers.extend(servers)
David Kranzcf0040c2012-06-26 09:46:56 -0400114
Sean Dague22897e12013-02-25 17:54:09 -0500115 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100116 for server in servers:
117 cls.servers_client.wait_for_server_status(
118 server['id'], kwargs['wait_until'])
Sean Dague9b669e32012-12-13 18:40:08 -0500119
Andrea Frittoli938d1512013-05-16 15:42:27 +0100120 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500121
David Kranzcf0040c2012-06-26 09:46:56 -0400122 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500123 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400124 start_time = int(time.time())
125 while True:
126 try:
127 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500128 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400129 pass
130 else:
131 return
132 if int(time.time()) - start_time >= self.build_timeout:
133 condition()
134 return
135 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400136
137
ivan-zhuf2b00502013-10-18 10:06:52 +0800138class BaseV2ComputeTest(BaseComputeTest):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400139
140 @classmethod
141 def setUpClass(cls):
ivan-zhuf2b00502013-10-18 10:06:52 +0800142 super(BaseV2ComputeTest, cls).setUpClass()
143 cls.servers_client = cls.os.servers_client
144 cls.flavors_client = cls.os.flavors_client
145 cls.images_client = cls.os.images_client
146 cls.extensions_client = cls.os.extensions_client
147 cls.floating_ips_client = cls.os.floating_ips_client
148 cls.keypairs_client = cls.os.keypairs_client
149 cls.security_groups_client = cls.os.security_groups_client
150 cls.quotas_client = cls.os.quotas_client
151 cls.limits_client = cls.os.limits_client
152 cls.volumes_extensions_client = cls.os.volumes_extensions_client
153 cls.volumes_client = cls.os.volumes_client
154 cls.interfaces_client = cls.os.interfaces_client
155 cls.fixed_ips_client = cls.os.fixed_ips_client
156 cls.availability_zone_client = cls.os.availability_zone_client
157 cls.aggregates_client = cls.os.aggregates_client
158 cls.services_client = cls.os.services_client
159 cls.hypervisor_client = cls.os.hypervisor_client
160 cls.servers_client_v3_auth = cls.os.servers_client_v3_auth
161
ivan-zhu8f992be2013-07-31 14:56:58 +0800162 @classmethod
163 def create_image_from_server(cls, server_id, **kwargs):
164 """Wrapper utility that returns an image created from the server."""
165 name = rand_name(cls.__name__ + "-image")
166 if 'name' in kwargs:
167 name = kwargs.pop('name')
168
169 resp, image = cls.images_client.create_image(
170 server_id, name)
171 image_id = parse_image_id(resp['location'])
172 cls.images.append(image_id)
173
174 if 'wait_until' in kwargs:
175 cls.images_client.wait_for_image_status(image_id,
176 kwargs['wait_until'])
177 resp, image = cls.images_client.get_image(image_id)
178
179 return resp, image
180
181 @classmethod
182 def rebuild_server(cls, **kwargs):
183 # Destroy an existing server and creates a new one
184 try:
185 cls.servers_client.delete_server(cls.server_id)
186 cls.servers_client.wait_for_server_termination(cls.server_id)
187 except Exception as exc:
188 LOG.exception(exc)
189 pass
190 resp, server = cls.create_server(wait_until='ACTIVE', **kwargs)
191 cls.server_id = server['id']
192 cls.password = server['adminPass']
193
ivan-zhuf2b00502013-10-18 10:06:52 +0800194
195class BaseV2ComputeAdminTest(BaseV2ComputeTest):
196 """Base test case class for Compute Admin V2 API tests."""
197
198 @classmethod
199 def setUpClass(cls):
200 super(BaseV2ComputeAdminTest, cls).setUpClass()
Attila Fazekas4ba36582013-02-12 08:26:17 +0100201 admin_username = cls.config.compute_admin.username
202 admin_password = cls.config.compute_admin.password
203 admin_tenant = cls.config.compute_admin.tenant_name
Attila Fazekas4ba36582013-02-12 08:26:17 +0100204 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400205 msg = ("Missing Compute Admin API credentials "
206 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800207 raise cls.skipException(msg)
Attila Fazekas430dae32013-10-17 15:19:32 +0200208 if (cls.config.compute.allow_tenant_isolation or
209 cls.force_tenant_isolation is True):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400210 creds = cls.isolated_creds.get_admin_creds()
Matthew Treinish47ff7912013-07-22 17:09:55 -0400211 admin_username, admin_tenant_name, admin_password = creds
212 cls.os_adm = clients.Manager(username=admin_username,
213 password=admin_password,
214 tenant_name=admin_tenant_name,
215 interface=cls._interface)
216 else:
217 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
ivan-zhu8f992be2013-07-31 14:56:58 +0800218
219
220class BaseV3ComputeTest(BaseComputeTest):
221
222 @classmethod
223 def setUpClass(cls):
224 super(BaseV3ComputeTest, cls).setUpClass()
225 if not cls.config.compute_feature_enabled.api_v3:
226 skip_msg = ("%s skipped as nova v3 api is not available" %
227 cls.__name__)
228 raise cls.skipException(skip_msg)
229
230 cls.servers_client = cls.os.servers_v3_client
231 cls.images_client = cls.os.image_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800232 cls.services_client = cls.os.services_v3_client
ivan-zhu8f992be2013-07-31 14:56:58 +0800233
234 @classmethod
235 def create_image_from_server(cls, server_id, **kwargs):
236 """Wrapper utility that returns an image created from the server."""
237 name = rand_name(cls.__name__ + "-image")
238 if 'name' in kwargs:
239 name = kwargs.pop('name')
240
241 resp, image = cls.servers_client.create_image(
242 server_id, name)
243 image_id = parse_image_id(resp['location'])
244 cls.images.append(image_id)
245
246 if 'wait_until' in kwargs:
247 cls.images_client.wait_for_image_status(image_id,
248 kwargs['wait_until'])
249 resp, image = cls.images_client.get_image_meta(image_id)
250
251 return resp, image
252
253 @classmethod
254 def rebuild_server(cls, **kwargs):
255 # Destroy an existing server and creates a new one
256 try:
257 cls.servers_client.delete_server(cls.server_id)
258 cls.servers_client.wait_for_server_termination(cls.server_id)
259 except Exception as exc:
260 LOG.exception(exc)
261 pass
262 resp, server = cls.create_server(wait_until='ACTIVE', **kwargs)
263 cls.server_id = server['id']
264 cls.password = server['admin_pass']
265
266
267class BaseV3ComputeAdminTest(BaseV3ComputeTest):
268 """Base test case class for all Compute Admin API V3 tests."""
269
270 @classmethod
271 def setUpClass(cls):
272 super(BaseV3ComputeAdminTest, cls).setUpClass()
273 admin_username = cls.config.compute_admin.username
274 admin_password = cls.config.compute_admin.password
275 admin_tenant = cls.config.compute_admin.tenant_name
276 if not (admin_username and admin_password and admin_tenant):
277 msg = ("Missing Compute Admin API credentials "
278 "in configuration.")
279 raise cls.skipException(msg)
280 if cls.config.compute.allow_tenant_isolation:
281 creds = cls.isolated_creds.get_admin_creds()
282 admin_username, admin_tenant_name, admin_password = creds
283 os_adm = clients.Manager(username=admin_username,
284 password=admin_password,
285 tenant_name=admin_tenant_name,
286 interface=cls._interface)
287 else:
288 os_adm = clients.ComputeAdminManager(interface=cls._interface)
289
290 cls.os_adm = os_adm
291 cls.severs_admin_client = cls.os_adm.servers_v3_client
ivan-zhu5c86ae62013-08-20 21:09:01 +0800292 cls.services_admin_client = cls.os_adm.services_v3_client