blob: f2af1fc600ee7de92707dc0c7177f2043d38631d [file] [log] [blame]
Jay Pipes13b479b2012-06-11 14:52:27 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# 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
Jay Pipesf38eaac2012-06-21 13:37:35 -040018import logging
David Kranzcf0040c2012-06-26 09:46:56 -040019import time
Jay Pipesf38eaac2012-06-21 13:37:35 -040020
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103021import testresources
ivan-zhu1feeb382013-01-24 10:14:39 +080022import testtools
Jay Pipes13b479b2012-06-11 14:52:27 -040023
Matthew Treinish481466b2012-12-20 17:16:01 -050024from tempest import clients
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025from tempest.common.utils.data_utils import rand_name
Jay Pipesf38eaac2012-06-21 13:37:35 -040026from tempest import config
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040027from tempest import exceptions
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103028from tempest.tests import compute
Jay Pipesf38eaac2012-06-21 13:37:35 -040029
James E. Blaire6d8ee12013-01-18 21:33:45 +000030__all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML',
31 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML']
Tiago Melloeda03b52012-08-22 23:47:29 -030032
Jay Pipesf38eaac2012-06-21 13:37:35 -040033LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050034
35
Chris Yeoh01cb2792013-02-09 22:25:37 +103036class BaseCompTest(testtools.testcase.WithAttributes,
37 testtools.TestCase,
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103038 testresources.ResourcedTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050040
ivan-zhu1feeb382013-01-24 10:14:39 +080041 resources = [('compute_init', compute.ComputeResource())]
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103042
Jay Pipesf38eaac2012-06-21 13:37:35 -040043 @classmethod
44 def setUpClass(cls):
45 cls.config = config.TempestConfig()
46 cls.isolated_creds = []
47
48 if cls.config.compute.allow_tenant_isolation:
49 creds = cls._get_isolated_creds()
50 username, tenant_name, password = creds
Matthew Treinish481466b2012-12-20 17:16:01 -050051 os = clients.Manager(username=username,
52 password=password,
James E. Blaire6d8ee12013-01-18 21:33:45 +000053 tenant_name=tenant_name,
54 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050055 else:
James E. Blaire6d8ee12013-01-18 21:33:45 +000056 os = clients.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050057
Jay Pipesf38eaac2012-06-21 13:37:35 -040058 cls.os = os
59 cls.servers_client = os.servers_client
60 cls.flavors_client = os.flavors_client
61 cls.images_client = os.images_client
62 cls.extensions_client = os.extensions_client
63 cls.floating_ips_client = os.floating_ips_client
64 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040065 cls.security_groups_client = os.security_groups_client
Rohit Karajgi07599c52012-11-02 05:35:16 -070066 cls.quotas_client = os.quotas_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040067 cls.limits_client = os.limits_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070068 cls.volumes_extensions_client = os.volumes_extensions_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040069 cls.volumes_client = os.volumes_client
70 cls.build_interval = cls.config.compute.build_interval
71 cls.build_timeout = cls.config.compute.build_timeout
72 cls.ssh_user = cls.config.compute.ssh_user
73 cls.image_ref = cls.config.compute.image_ref
74 cls.image_ref_alt = cls.config.compute.image_ref_alt
75 cls.flavor_ref = cls.config.compute.flavor_ref
76 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
77 cls.servers = []
Daryl Walleckc7251962012-03-12 17:26:54 -050078
Jay Pipesf38eaac2012-06-21 13:37:35 -040079 @classmethod
80 def _get_identity_admin_client(cls):
81 """
82 Returns an instance of the Identity Admin API client
83 """
Attila Fazekas407b6db2013-01-19 12:48:36 +010084 os = clients.AdminManager(interface=cls._interface)
85 admin_client = os.identity_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040086 return admin_client
Daryl Walleckc7251962012-03-12 17:26:54 -050087
Jay Pipesf38eaac2012-06-21 13:37:35 -040088 @classmethod
Mate Lakat99ee9142012-09-14 12:34:46 +010089 def _get_client_args(cls):
90
91 return (
92 cls.config,
Attila Fazekascadcb1f2013-01-21 23:10:53 +010093 cls.config.identity.admin_username,
94 cls.config.identity.admin_password,
Jay Pipes7c88eb22013-01-16 21:32:43 -050095 cls.config.identity.uri
Mate Lakat99ee9142012-09-14 12:34:46 +010096 )
97
98 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -040099 def _get_isolated_creds(cls):
100 """
101 Creates a new set of user/tenant/password credentials for a
102 **regular** user of the Compute API so that a test case can
103 operate in an isolated tenant container.
104 """
105 admin_client = cls._get_identity_admin_client()
ivan-zhuac81b7d2013-01-05 17:45:41 +0800106 rand_name_root = rand_name(cls.__name__)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400107 if cls.isolated_creds:
108 # Main user already created. Create the alt one...
109 rand_name_root += '-alt'
110 username = rand_name_root + "-user"
111 email = rand_name_root + "@example.com"
112 tenant_name = rand_name_root + "-tenant"
113 tenant_desc = tenant_name + "-desc"
114 password = "pass"
115
Dan Smithd6ff6b72012-08-23 10:29:41 -0700116 try:
117 resp, tenant = admin_client.create_tenant(name=tenant_name,
118 description=tenant_desc)
119 except exceptions.Duplicate:
120 if cls.config.compute.allow_tenant_reuse:
121 tenant = admin_client.get_tenant_by_name(tenant_name)
122 LOG.info('Re-using existing tenant %s' % tenant)
123 else:
124 msg = ('Unable to create isolated tenant %s because ' +
125 'it already exists. If this is related to a ' +
126 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400127 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700128 raise exceptions.Duplicate(msg)
129
130 try:
131 resp, user = admin_client.create_user(username,
132 password,
133 tenant['id'],
134 email)
135 except exceptions.Duplicate:
136 if cls.config.compute.allow_tenant_reuse:
137 user = admin_client.get_user_by_username(tenant['id'],
138 username)
139 LOG.info('Re-using existing user %s' % user)
140 else:
Jay Pipes444c3e62012-10-04 19:26:35 -0400141 msg = ('Unable to create isolated user %s because ' +
Dan Smithd6ff6b72012-08-23 10:29:41 -0700142 'it already exists. If this is related to a ' +
143 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400144 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700145 raise exceptions.Duplicate(msg)
146
Jay Pipesf38eaac2012-06-21 13:37:35 -0400147 # Store the complete creds (including UUID ids...) for later
148 # but return just the username, tenant_name, password tuple
149 # that the various clients will use.
150 cls.isolated_creds.append((user, tenant))
151
152 return username, tenant_name, password
153
154 @classmethod
155 def clear_isolated_creds(cls):
156 if not cls.isolated_creds:
157 pass
158 admin_client = cls._get_identity_admin_client()
159
160 for user, tenant in cls.isolated_creds:
161 admin_client.delete_user(user['id'])
162 admin_client.delete_tenant(tenant['id'])
163
164 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400165 def clear_servers(cls):
166 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700167 try:
168 cls.servers_client.delete_server(server['id'])
169 except Exception:
170 pass
171
Jay Pipes444c3e62012-10-04 19:26:35 -0400172 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700173 try:
174 cls.servers_client.wait_for_server_termination(server['id'])
175 except Exception:
176 pass
177
178 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400179 def tearDownClass(cls):
Jay Pipes444c3e62012-10-04 19:26:35 -0400180 cls.clear_servers()
Jay Pipesf38eaac2012-06-21 13:37:35 -0400181 cls.clear_isolated_creds()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700182
Jay Pipes444c3e62012-10-04 19:26:35 -0400183 @classmethod
Rohit Karajgi07599c52012-11-02 05:35:16 -0700184 def create_server(cls, image_id=None, flavor=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500185 """Wrapper utility that returns a test server."""
Jay Pipes444c3e62012-10-04 19:26:35 -0400186 server_name = rand_name(cls.__name__ + "-instance")
Rohit Karajgi07599c52012-11-02 05:35:16 -0700187
188 if not flavor:
189 flavor = cls.flavor_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700190 if not image_id:
Jay Pipes444c3e62012-10-04 19:26:35 -0400191 image_id = cls.image_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700192
Jay Pipes444c3e62012-10-04 19:26:35 -0400193 resp, server = cls.servers_client.create_server(
Rohit Karajgidc300b22012-05-04 08:11:00 -0700194 server_name, image_id, flavor)
Jay Pipes444c3e62012-10-04 19:26:35 -0400195 cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
196 cls.servers.append(server)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700197 return server
David Kranzcf0040c2012-06-26 09:46:56 -0400198
Sean Dague9b669e32012-12-13 18:40:08 -0500199 @classmethod
200 def create_server_with_extras(cls, name, image_id=None,
201 flavor=None, **kwargs):
202 # TODO(sdague) transitional function because many
203 # server tests were using extra args and resp so can't
204 # easily be ported to create_server. Will be merged
205 # later
206 if not flavor:
207 flavor = cls.flavor_ref
208 if not image_id:
209 image_id = cls.image_ref
210
211 resp, server = cls.servers_client.create_server(name,
212 image_id, flavor,
213 **kwargs)
214 cls.servers.append(server)
215 return resp, server
216
David Kranzcf0040c2012-06-26 09:46:56 -0400217 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500218 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400219 start_time = int(time.time())
220 while True:
221 try:
222 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500223 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400224 pass
225 else:
226 return
227 if int(time.time()) - start_time >= self.build_timeout:
228 condition()
229 return
230 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400231
232
James E. Blaire6d8ee12013-01-18 21:33:45 +0000233class BaseComputeTestJSON(BaseCompTest):
234 @classmethod
235 def setUpClass(cls):
236 cls._interface = "json"
237 super(BaseComputeTestJSON, cls).setUpClass()
238
239# NOTE(danms): For transition, keep the old name active as JSON
240BaseComputeTest = BaseComputeTestJSON
241
242
243class BaseComputeTestXML(BaseCompTest):
244 @classmethod
245 def setUpClass(cls):
246 cls._interface = "xml"
247 super(BaseComputeTestXML, cls).setUpClass()
248
249
ivan-zhu1feeb382013-01-24 10:14:39 +0800250class BaseComputeAdminTest(testtools.TestCase):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400251
Sean Daguef237ccb2013-01-04 15:19:14 -0500252 """Base test case class for all Compute Admin API tests."""
Jay Pipesf38eaac2012-06-21 13:37:35 -0400253
254 @classmethod
255 def setUpClass(cls):
256 cls.config = config.TempestConfig()
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100257 cls.admin_username = cls.config.identity.admin_username
258 cls.admin_password = cls.config.identity.admin_password
259 cls.admin_tenant = cls.config.identity.admin_tenant_name
Jay Pipesf38eaac2012-06-21 13:37:35 -0400260
261 if not cls.admin_username and cls.admin_password and cls.admin_tenant:
262 msg = ("Missing Compute Admin API credentials "
263 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800264 raise cls.skipException(msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400265
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100266 cls.os = clients.ComputeAdminManager(interface=cls._interface)
James E. Blaire6d8ee12013-01-18 21:33:45 +0000267
268
269class BaseComputeAdminTestJSON(BaseComputeAdminTest):
270 @classmethod
271 def setUpClass(cls):
272 cls._interface = "json"
273 super(BaseComputeAdminTestJSON, cls).setUpClass()
274
275
276class BaseComputeAdminTestXML(BaseComputeAdminTest):
277 @classmethod
278 def setUpClass(cls):
279 cls._interface = "xml"
280 super(BaseComputeAdminTestXML, cls).setUpClass()