blob: ebf3b541d9da4267a5ca8d9665a8d5278cbc7a82 [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
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070020import nose
Jay Pipesf38eaac2012-06-21 13:37:35 -040021
Jay Pipes13b479b2012-06-11 14:52:27 -040022import unittest2 as unittest
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040023import nose
Jay Pipes13b479b2012-06-11 14:52:27 -040024
Jay Pipesf38eaac2012-06-21 13:37:35 -040025from tempest import config
Daryl Walleckc7251962012-03-12 17:26:54 -050026from tempest import openstack
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040027from tempest import exceptions
Rohit Karajgidc300b22012-05-04 08:11:00 -070028from tempest.common.utils.data_utils import rand_name
Jay Pipesf38eaac2012-06-21 13:37:35 -040029
Tiago Melloeda03b52012-08-22 23:47:29 -030030__all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML',
31 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML']
32
Jay Pipesf38eaac2012-06-21 13:37:35 -040033LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050034
35
Matthew Treinish4e086902012-08-17 17:52:22 -040036class BaseCompTest(unittest.TestCase):
Daryl Walleckc7251962012-03-12 17:26:54 -050037
Jay Pipesf38eaac2012-06-21 13:37:35 -040038 """Base test case class for all Compute API tests"""
Daryl Walleckc7251962012-03-12 17:26:54 -050039
Jay Pipesf38eaac2012-06-21 13:37:35 -040040 @classmethod
41 def setUpClass(cls):
42 cls.config = config.TempestConfig()
43 cls.isolated_creds = []
44
45 if cls.config.compute.allow_tenant_isolation:
46 creds = cls._get_isolated_creds()
47 username, tenant_name, password = creds
48 os = openstack.Manager(username=username,
49 password=password,
Dan Smithcf8fab62012-08-14 08:03:48 -070050 tenant_name=tenant_name,
51 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050052 else:
Dan Smithcf8fab62012-08-14 08:03:48 -070053 os = openstack.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050054
Jay Pipesf38eaac2012-06-21 13:37:35 -040055 cls.os = os
56 cls.servers_client = os.servers_client
57 cls.flavors_client = os.flavors_client
58 cls.images_client = os.images_client
59 cls.extensions_client = os.extensions_client
60 cls.floating_ips_client = os.floating_ips_client
61 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040062 cls.security_groups_client = os.security_groups_client
63 cls.console_outputs_client = os.console_outputs_client
64 cls.limits_client = os.limits_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070065 cls.volumes_extensions_client = os.volumes_extensions_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040066 cls.volumes_client = os.volumes_client
67 cls.build_interval = cls.config.compute.build_interval
68 cls.build_timeout = cls.config.compute.build_timeout
69 cls.ssh_user = cls.config.compute.ssh_user
70 cls.image_ref = cls.config.compute.image_ref
71 cls.image_ref_alt = cls.config.compute.image_ref_alt
72 cls.flavor_ref = cls.config.compute.flavor_ref
73 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
74 cls.servers = []
Daryl Walleckc7251962012-03-12 17:26:54 -050075
Jay Pipesf38eaac2012-06-21 13:37:35 -040076 @classmethod
77 def _get_identity_admin_client(cls):
78 """
79 Returns an instance of the Identity Admin API client
80 """
Vincent Hou6b8a7b72012-08-25 01:24:33 +080081 os = openstack.IdentityManager(interface=cls._interface)
82 admin_client = os.admin_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040083 return admin_client
Daryl Walleckc7251962012-03-12 17:26:54 -050084
Jay Pipesf38eaac2012-06-21 13:37:35 -040085 @classmethod
Mate Lakat99ee9142012-09-14 12:34:46 +010086 def _get_client_args(cls):
87
88 return (
89 cls.config,
90 cls.config.identity_admin.username,
91 cls.config.identity_admin.password,
92 cls.config.identity.auth_url
93 )
94
95 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -040096 def _get_isolated_creds(cls):
97 """
98 Creates a new set of user/tenant/password credentials for a
99 **regular** user of the Compute API so that a test case can
100 operate in an isolated tenant container.
101 """
102 admin_client = cls._get_identity_admin_client()
103 rand_name_root = cls.__name__
104 if cls.isolated_creds:
105 # Main user already created. Create the alt one...
106 rand_name_root += '-alt'
107 username = rand_name_root + "-user"
108 email = rand_name_root + "@example.com"
109 tenant_name = rand_name_root + "-tenant"
110 tenant_desc = tenant_name + "-desc"
111 password = "pass"
112
Dan Smithd6ff6b72012-08-23 10:29:41 -0700113 try:
114 resp, tenant = admin_client.create_tenant(name=tenant_name,
115 description=tenant_desc)
116 except exceptions.Duplicate:
117 if cls.config.compute.allow_tenant_reuse:
118 tenant = admin_client.get_tenant_by_name(tenant_name)
119 LOG.info('Re-using existing tenant %s' % tenant)
120 else:
121 msg = ('Unable to create isolated tenant %s because ' +
122 'it already exists. If this is related to a ' +
123 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400124 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700125 raise exceptions.Duplicate(msg)
126
127 try:
128 resp, user = admin_client.create_user(username,
129 password,
130 tenant['id'],
131 email)
132 except exceptions.Duplicate:
133 if cls.config.compute.allow_tenant_reuse:
134 user = admin_client.get_user_by_username(tenant['id'],
135 username)
136 LOG.info('Re-using existing user %s' % user)
137 else:
Jay Pipes444c3e62012-10-04 19:26:35 -0400138 msg = ('Unable to create isolated user %s because ' +
Dan Smithd6ff6b72012-08-23 10:29:41 -0700139 'it already exists. If this is related to a ' +
140 'previous test failure, try using ' +
Jay Pipes444c3e62012-10-04 19:26:35 -0400141 'allow_tenant_reuse in tempest.conf') % tenant_name
Dan Smithd6ff6b72012-08-23 10:29:41 -0700142 raise exceptions.Duplicate(msg)
143
Jay Pipesf38eaac2012-06-21 13:37:35 -0400144 # Store the complete creds (including UUID ids...) for later
145 # but return just the username, tenant_name, password tuple
146 # that the various clients will use.
147 cls.isolated_creds.append((user, tenant))
148
149 return username, tenant_name, password
150
151 @classmethod
152 def clear_isolated_creds(cls):
153 if not cls.isolated_creds:
154 pass
155 admin_client = cls._get_identity_admin_client()
156
157 for user, tenant in cls.isolated_creds:
158 admin_client.delete_user(user['id'])
159 admin_client.delete_tenant(tenant['id'])
160
161 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400162 def clear_servers(cls):
163 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700164 try:
165 cls.servers_client.delete_server(server['id'])
166 except Exception:
167 pass
168
Jay Pipes444c3e62012-10-04 19:26:35 -0400169 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700170 try:
171 cls.servers_client.wait_for_server_termination(server['id'])
172 except Exception:
173 pass
174
175 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400176 def tearDownClass(cls):
Jay Pipes444c3e62012-10-04 19:26:35 -0400177 cls.clear_servers()
Jay Pipesf38eaac2012-06-21 13:37:35 -0400178 cls.clear_isolated_creds()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700179
Jay Pipes444c3e62012-10-04 19:26:35 -0400180 @classmethod
181 def create_server(cls, image_id=None):
Rohit Karajgidc300b22012-05-04 08:11:00 -0700182 """Wrapper utility that returns a test server"""
Jay Pipes444c3e62012-10-04 19:26:35 -0400183 server_name = rand_name(cls.__name__ + "-instance")
184 flavor = cls.flavor_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700185 if not image_id:
Jay Pipes444c3e62012-10-04 19:26:35 -0400186 image_id = cls.image_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700187
Jay Pipes444c3e62012-10-04 19:26:35 -0400188 resp, server = cls.servers_client.create_server(
Rohit Karajgidc300b22012-05-04 08:11:00 -0700189 server_name, image_id, flavor)
Jay Pipes444c3e62012-10-04 19:26:35 -0400190 cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
191 cls.servers.append(server)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700192 return server
David Kranzcf0040c2012-06-26 09:46:56 -0400193
194 def wait_for(self, condition):
195 """Repeatedly calls condition() until a timeout"""
196 start_time = int(time.time())
197 while True:
198 try:
199 condition()
200 except:
201 pass
202 else:
203 return
204 if int(time.time()) - start_time >= self.build_timeout:
205 condition()
206 return
207 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400208
209
Matthew Treinish4e086902012-08-17 17:52:22 -0400210class BaseComputeTestJSON(BaseCompTest):
Dan Smithcf8fab62012-08-14 08:03:48 -0700211 @classmethod
212 def setUpClass(cls):
213 cls._interface = "json"
214 super(BaseComputeTestJSON, cls).setUpClass()
215
216# NOTE(danms): For transition, keep the old name active as JSON
217BaseComputeTest = BaseComputeTestJSON
218
219
Matthew Treinish4e086902012-08-17 17:52:22 -0400220class BaseComputeTestXML(BaseCompTest):
Dan Smithcf8fab62012-08-14 08:03:48 -0700221 @classmethod
222 def setUpClass(cls):
223 cls._interface = "xml"
224 super(BaseComputeTestXML, cls).setUpClass()
225
226
Jay Pipesf38eaac2012-06-21 13:37:35 -0400227class BaseComputeAdminTest(unittest.TestCase):
228
229 """Base test case class for all Compute Admin API tests"""
230
231 @classmethod
232 def setUpClass(cls):
233 cls.config = config.TempestConfig()
234 cls.admin_username = cls.config.compute_admin.username
235 cls.admin_password = cls.config.compute_admin.password
236 cls.admin_tenant = cls.config.compute_admin.tenant_name
237
238 if not cls.admin_username and cls.admin_password and cls.admin_tenant:
239 msg = ("Missing Compute Admin API credentials "
240 "in configuration.")
241 raise nose.SkipTest(msg)
242
Tiago Melloeda03b52012-08-22 23:47:29 -0300243 cls.os = openstack.AdminManager(interface=cls._interface)
244
245
246class BaseComputeAdminTestJSON(BaseComputeAdminTest):
247 @classmethod
248 def setUpClass(cls):
249 cls._interface = "json"
250 super(BaseComputeAdminTestJSON, cls).setUpClass()
251
252
253class BaseComputeAdminTestXML(BaseComputeAdminTest):
254 @classmethod
255 def setUpClass(cls):
256 cls._interface = "xml"
257 super(BaseComputeAdminTestXML, cls).setUpClass()