blob: 9e2e88344e7e604a1ae5fb646011410cbff06318 [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
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040021import nose
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103022import testresources
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023import unittest2 as unittest
Jay Pipes13b479b2012-06-11 14:52:27 -040024
Matthew Treinish481466b2012-12-20 17:16:01 -050025from tempest import clients
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest.common.utils.data_utils import rand_name
Jay Pipesf38eaac2012-06-21 13:37:35 -040027from tempest import config
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040028from tempest import exceptions
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103029from tempest.tests import compute
Jay Pipesf38eaac2012-06-21 13:37:35 -040030
James E. Blaire6d8ee12013-01-18 21:33:45 +000031__all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML',
32 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML']
Tiago Melloeda03b52012-08-22 23:47:29 -030033
Jay Pipesf38eaac2012-06-21 13:37:35 -040034LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050035
36
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103037class BaseCompTest(unittest.TestCase,
38 testresources.ResourcedTestCase):
Daryl Walleckc7251962012-03-12 17:26:54 -050039
Sean Daguef237ccb2013-01-04 15:19:14 -050040 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050041
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103042 resources = [('compute_init', compute.generic_setup_package())]
43
Jay Pipesf38eaac2012-06-21 13:37:35 -040044 @classmethod
45 def setUpClass(cls):
46 cls.config = config.TempestConfig()
47 cls.isolated_creds = []
48
49 if cls.config.compute.allow_tenant_isolation:
50 creds = cls._get_isolated_creds()
51 username, tenant_name, password = creds
Matthew Treinish481466b2012-12-20 17:16:01 -050052 os = clients.Manager(username=username,
53 password=password,
James E. Blaire6d8ee12013-01-18 21:33:45 +000054 tenant_name=tenant_name,
55 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050056 else:
James E. Blaire6d8ee12013-01-18 21:33:45 +000057 os = clients.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050058
Jay Pipesf38eaac2012-06-21 13:37:35 -040059 cls.os = os
60 cls.servers_client = os.servers_client
61 cls.flavors_client = os.flavors_client
62 cls.images_client = os.images_client
63 cls.extensions_client = os.extensions_client
64 cls.floating_ips_client = os.floating_ips_client
65 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040066 cls.security_groups_client = os.security_groups_client
67 cls.console_outputs_client = os.console_outputs_client
Rohit Karajgi07599c52012-11-02 05:35:16 -070068 cls.quotas_client = os.quotas_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040069 cls.limits_client = os.limits_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070070 cls.volumes_extensions_client = os.volumes_extensions_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040071 cls.volumes_client = os.volumes_client
72 cls.build_interval = cls.config.compute.build_interval
73 cls.build_timeout = cls.config.compute.build_timeout
74 cls.ssh_user = cls.config.compute.ssh_user
75 cls.image_ref = cls.config.compute.image_ref
76 cls.image_ref_alt = cls.config.compute.image_ref_alt
77 cls.flavor_ref = cls.config.compute.flavor_ref
78 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
79 cls.servers = []
Daryl Walleckc7251962012-03-12 17:26:54 -050080
Jay Pipesf38eaac2012-06-21 13:37:35 -040081 @classmethod
82 def _get_identity_admin_client(cls):
83 """
84 Returns an instance of the Identity Admin API client
85 """
Attila Fazekas407b6db2013-01-19 12:48:36 +010086 os = clients.AdminManager(interface=cls._interface)
87 admin_client = os.identity_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040088 return admin_client
Daryl Walleckc7251962012-03-12 17:26:54 -050089
Jay Pipesf38eaac2012-06-21 13:37:35 -040090 @classmethod
Mate Lakat99ee9142012-09-14 12:34:46 +010091 def _get_client_args(cls):
92
93 return (
94 cls.config,
Attila Fazekascadcb1f2013-01-21 23:10:53 +010095 cls.config.identity.admin_username,
96 cls.config.identity.admin_password,
Jay Pipes7c88eb22013-01-16 21:32:43 -050097 cls.config.identity.uri
Mate Lakat99ee9142012-09-14 12:34:46 +010098 )
99
100 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400101 def _get_isolated_creds(cls):
102 """
103 Creates a new set of user/tenant/password credentials for a
104 **regular** user of the Compute API so that a test case can
105 operate in an isolated tenant container.
106 """
107 admin_client = cls._get_identity_admin_client()
ivan-zhuac81b7d2013-01-05 17:45:41 +0800108 rand_name_root = rand_name(cls.__name__)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400109 if cls.isolated_creds:
110 # Main user already created. Create the alt one...
111 rand_name_root += '-alt'
112 username = rand_name_root + "-user"
113 email = rand_name_root + "@example.com"
114 tenant_name = rand_name_root + "-tenant"
115 tenant_desc = tenant_name + "-desc"
116 password = "pass"
117
Dan Smithd6ff6b72012-08-23 10:29:41 -0700118 try:
119 resp, tenant = admin_client.create_tenant(name=tenant_name,
120 description=tenant_desc)
121 except exceptions.Duplicate:
122 if cls.config.compute.allow_tenant_reuse:
123 tenant = admin_client.get_tenant_by_name(tenant_name)
124 LOG.info('Re-using existing tenant %s' % tenant)
125 else:
126 msg = ('Unable to create isolated tenant %s because ' +
127 'it already exists. If this is related to a ' +
128 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400129 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700130 raise exceptions.Duplicate(msg)
131
132 try:
133 resp, user = admin_client.create_user(username,
134 password,
135 tenant['id'],
136 email)
137 except exceptions.Duplicate:
138 if cls.config.compute.allow_tenant_reuse:
139 user = admin_client.get_user_by_username(tenant['id'],
140 username)
141 LOG.info('Re-using existing user %s' % user)
142 else:
Jay Pipes444c3e62012-10-04 19:26:35 -0400143 msg = ('Unable to create isolated user %s because ' +
Dan Smithd6ff6b72012-08-23 10:29:41 -0700144 'it already exists. If this is related to a ' +
145 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400146 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700147 raise exceptions.Duplicate(msg)
148
Jay Pipesf38eaac2012-06-21 13:37:35 -0400149 # Store the complete creds (including UUID ids...) for later
150 # but return just the username, tenant_name, password tuple
151 # that the various clients will use.
152 cls.isolated_creds.append((user, tenant))
153
154 return username, tenant_name, password
155
156 @classmethod
157 def clear_isolated_creds(cls):
158 if not cls.isolated_creds:
159 pass
160 admin_client = cls._get_identity_admin_client()
161
162 for user, tenant in cls.isolated_creds:
163 admin_client.delete_user(user['id'])
164 admin_client.delete_tenant(tenant['id'])
165
166 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400167 def clear_servers(cls):
168 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700169 try:
170 cls.servers_client.delete_server(server['id'])
171 except Exception:
172 pass
173
Jay Pipes444c3e62012-10-04 19:26:35 -0400174 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700175 try:
176 cls.servers_client.wait_for_server_termination(server['id'])
177 except Exception:
178 pass
179
180 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400181 def tearDownClass(cls):
Jay Pipes444c3e62012-10-04 19:26:35 -0400182 cls.clear_servers()
Jay Pipesf38eaac2012-06-21 13:37:35 -0400183 cls.clear_isolated_creds()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700184
Jay Pipes444c3e62012-10-04 19:26:35 -0400185 @classmethod
Rohit Karajgi07599c52012-11-02 05:35:16 -0700186 def create_server(cls, image_id=None, flavor=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500187 """Wrapper utility that returns a test server."""
Jay Pipes444c3e62012-10-04 19:26:35 -0400188 server_name = rand_name(cls.__name__ + "-instance")
Rohit Karajgi07599c52012-11-02 05:35:16 -0700189
190 if not flavor:
191 flavor = cls.flavor_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700192 if not image_id:
Jay Pipes444c3e62012-10-04 19:26:35 -0400193 image_id = cls.image_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700194
Jay Pipes444c3e62012-10-04 19:26:35 -0400195 resp, server = cls.servers_client.create_server(
Rohit Karajgidc300b22012-05-04 08:11:00 -0700196 server_name, image_id, flavor)
Jay Pipes444c3e62012-10-04 19:26:35 -0400197 cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
198 cls.servers.append(server)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700199 return server
David Kranzcf0040c2012-06-26 09:46:56 -0400200
Sean Dague9b669e32012-12-13 18:40:08 -0500201 @classmethod
202 def create_server_with_extras(cls, name, image_id=None,
203 flavor=None, **kwargs):
204 # TODO(sdague) transitional function because many
205 # server tests were using extra args and resp so can't
206 # easily be ported to create_server. Will be merged
207 # later
208 if not flavor:
209 flavor = cls.flavor_ref
210 if not image_id:
211 image_id = cls.image_ref
212
213 resp, server = cls.servers_client.create_server(name,
214 image_id, flavor,
215 **kwargs)
216 cls.servers.append(server)
217 return resp, server
218
David Kranzcf0040c2012-06-26 09:46:56 -0400219 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500220 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400221 start_time = int(time.time())
222 while True:
223 try:
224 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500225 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400226 pass
227 else:
228 return
229 if int(time.time()) - start_time >= self.build_timeout:
230 condition()
231 return
232 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400233
234
James E. Blaire6d8ee12013-01-18 21:33:45 +0000235class BaseComputeTestJSON(BaseCompTest):
236 @classmethod
237 def setUpClass(cls):
238 cls._interface = "json"
239 super(BaseComputeTestJSON, cls).setUpClass()
240
241# NOTE(danms): For transition, keep the old name active as JSON
242BaseComputeTest = BaseComputeTestJSON
243
244
245class BaseComputeTestXML(BaseCompTest):
246 @classmethod
247 def setUpClass(cls):
248 cls._interface = "xml"
249 super(BaseComputeTestXML, cls).setUpClass()
250
251
Jay Pipesf38eaac2012-06-21 13:37:35 -0400252class BaseComputeAdminTest(unittest.TestCase):
253
Sean Daguef237ccb2013-01-04 15:19:14 -0500254 """Base test case class for all Compute Admin API tests."""
Jay Pipesf38eaac2012-06-21 13:37:35 -0400255
256 @classmethod
257 def setUpClass(cls):
258 cls.config = config.TempestConfig()
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100259 cls.admin_username = cls.config.identity.admin_username
260 cls.admin_password = cls.config.identity.admin_password
261 cls.admin_tenant = cls.config.identity.admin_tenant_name
Jay Pipesf38eaac2012-06-21 13:37:35 -0400262
263 if not cls.admin_username and cls.admin_password and cls.admin_tenant:
264 msg = ("Missing Compute Admin API credentials "
265 "in configuration.")
266 raise nose.SkipTest(msg)
267
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100268 cls.os = clients.ComputeAdminManager(interface=cls._interface)
James E. Blaire6d8ee12013-01-18 21:33:45 +0000269
270
271class BaseComputeAdminTestJSON(BaseComputeAdminTest):
272 @classmethod
273 def setUpClass(cls):
274 cls._interface = "json"
275 super(BaseComputeAdminTestJSON, cls).setUpClass()
276
277
278class BaseComputeAdminTestXML(BaseComputeAdminTest):
279 @classmethod
280 def setUpClass(cls):
281 cls._interface = "xml"
282 super(BaseComputeAdminTestXML, cls).setUpClass()