blob: 5094b46da99e9eb446f089adb4f89365e92f02f5 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022import unittest2 as unittest
Jay Pipes13b479b2012-06-11 14:52:27 -040023
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest.common.utils.data_utils import rand_name
Jay Pipesf38eaac2012-06-21 13:37:35 -040025from tempest import config
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040026from tempest import exceptions
Rohit Karajgi07599c52012-11-02 05:35:16 -070027from tempest import openstack
Jay Pipesf38eaac2012-06-21 13:37:35 -040028
Tiago Melloeda03b52012-08-22 23:47:29 -030029__all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML',
30 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML']
31
Jay Pipesf38eaac2012-06-21 13:37:35 -040032LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050033
34
Matthew Treinish4e086902012-08-17 17:52:22 -040035class BaseCompTest(unittest.TestCase):
Daryl Walleckc7251962012-03-12 17:26:54 -050036
Jay Pipesf38eaac2012-06-21 13:37:35 -040037 """Base test case class for all Compute API tests"""
Daryl Walleckc7251962012-03-12 17:26:54 -050038
Jay Pipesf38eaac2012-06-21 13:37:35 -040039 @classmethod
40 def setUpClass(cls):
41 cls.config = config.TempestConfig()
42 cls.isolated_creds = []
43
44 if cls.config.compute.allow_tenant_isolation:
45 creds = cls._get_isolated_creds()
46 username, tenant_name, password = creds
47 os = openstack.Manager(username=username,
48 password=password,
Dan Smithcf8fab62012-08-14 08:03:48 -070049 tenant_name=tenant_name,
50 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050051 else:
Dan Smithcf8fab62012-08-14 08:03:48 -070052 os = openstack.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050053
Jay Pipesf38eaac2012-06-21 13:37:35 -040054 cls.os = os
55 cls.servers_client = os.servers_client
56 cls.flavors_client = os.flavors_client
57 cls.images_client = os.images_client
58 cls.extensions_client = os.extensions_client
59 cls.floating_ips_client = os.floating_ips_client
60 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040061 cls.security_groups_client = os.security_groups_client
62 cls.console_outputs_client = os.console_outputs_client
Rohit Karajgi07599c52012-11-02 05:35:16 -070063 cls.quotas_client = os.quotas_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040064 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
Rohit Karajgi07599c52012-11-02 05:35:16 -0700181 def create_server(cls, image_id=None, flavor=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")
Rohit Karajgi07599c52012-11-02 05:35:16 -0700184
185 if not flavor:
186 flavor = cls.flavor_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700187 if not image_id:
Jay Pipes444c3e62012-10-04 19:26:35 -0400188 image_id = cls.image_ref
Rohit Karajgidc300b22012-05-04 08:11:00 -0700189
Jay Pipes444c3e62012-10-04 19:26:35 -0400190 resp, server = cls.servers_client.create_server(
Rohit Karajgidc300b22012-05-04 08:11:00 -0700191 server_name, image_id, flavor)
Jay Pipes444c3e62012-10-04 19:26:35 -0400192 cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
193 cls.servers.append(server)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700194 return server
David Kranzcf0040c2012-06-26 09:46:56 -0400195
196 def wait_for(self, condition):
197 """Repeatedly calls condition() until a timeout"""
198 start_time = int(time.time())
199 while True:
200 try:
201 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500202 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400203 pass
204 else:
205 return
206 if int(time.time()) - start_time >= self.build_timeout:
207 condition()
208 return
209 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400210
211
Matthew Treinish4e086902012-08-17 17:52:22 -0400212class BaseComputeTestJSON(BaseCompTest):
Dan Smithcf8fab62012-08-14 08:03:48 -0700213 @classmethod
214 def setUpClass(cls):
215 cls._interface = "json"
216 super(BaseComputeTestJSON, cls).setUpClass()
217
218# NOTE(danms): For transition, keep the old name active as JSON
219BaseComputeTest = BaseComputeTestJSON
220
221
Matthew Treinish4e086902012-08-17 17:52:22 -0400222class BaseComputeTestXML(BaseCompTest):
Dan Smithcf8fab62012-08-14 08:03:48 -0700223 @classmethod
224 def setUpClass(cls):
225 cls._interface = "xml"
226 super(BaseComputeTestXML, cls).setUpClass()
227
228
Jay Pipesf38eaac2012-06-21 13:37:35 -0400229class BaseComputeAdminTest(unittest.TestCase):
230
231 """Base test case class for all Compute Admin API tests"""
232
233 @classmethod
234 def setUpClass(cls):
235 cls.config = config.TempestConfig()
236 cls.admin_username = cls.config.compute_admin.username
237 cls.admin_password = cls.config.compute_admin.password
238 cls.admin_tenant = cls.config.compute_admin.tenant_name
239
240 if not cls.admin_username and cls.admin_password and cls.admin_tenant:
241 msg = ("Missing Compute Admin API credentials "
242 "in configuration.")
243 raise nose.SkipTest(msg)
244
Tiago Melloeda03b52012-08-22 23:47:29 -0300245 cls.os = openstack.AdminManager(interface=cls._interface)
246
247
248class BaseComputeAdminTestJSON(BaseComputeAdminTest):
249 @classmethod
250 def setUpClass(cls):
251 cls._interface = "json"
252 super(BaseComputeAdminTestJSON, cls).setUpClass()
253
254
255class BaseComputeAdminTestXML(BaseComputeAdminTest):
256 @classmethod
257 def setUpClass(cls):
258 cls._interface = "xml"
259 super(BaseComputeAdminTestXML, cls).setUpClass()