blob: 0fa5a8410ef954f3946215634289528f0fec3863 [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
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
Mitsuhiko Yamazaki46818aa2013-04-18 17:49:17 +090022from tempest.common import log as logging
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest.common.utils.data_utils import rand_name
Wayne Vestal Weeks383c71d2012-09-12 16:21:17 -040024from tempest import exceptions
Attila Fazekasdc216422013-01-29 15:12:14 +010025import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040026
Tiago Melloeda03b52012-08-22 23:47:29 -030027
Jay Pipesf38eaac2012-06-21 13:37:35 -040028LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050029
30
Attila Fazekasdc216422013-01-29 15:12:14 +010031class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050032 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050033
Attila Fazekas9a63c942013-01-29 21:46:02 +010034 conclusion = compute.generic_setup_package()
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103035
Jay Pipesf38eaac2012-06-21 13:37:35 -040036 @classmethod
37 def setUpClass(cls):
Jay Pipesf38eaac2012-06-21 13:37:35 -040038 cls.isolated_creds = []
39
40 if cls.config.compute.allow_tenant_isolation:
41 creds = cls._get_isolated_creds()
42 username, tenant_name, password = creds
Matthew Treinish481466b2012-12-20 17:16:01 -050043 os = clients.Manager(username=username,
44 password=password,
James E. Blaire6d8ee12013-01-18 21:33:45 +000045 tenant_name=tenant_name,
46 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050047 else:
James E. Blaire6d8ee12013-01-18 21:33:45 +000048 os = clients.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050049
Jay Pipesf38eaac2012-06-21 13:37:35 -040050 cls.os = os
51 cls.servers_client = os.servers_client
52 cls.flavors_client = os.flavors_client
53 cls.images_client = os.images_client
54 cls.extensions_client = os.extensions_client
55 cls.floating_ips_client = os.floating_ips_client
56 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040057 cls.security_groups_client = os.security_groups_client
Rohit Karajgi07599c52012-11-02 05:35:16 -070058 cls.quotas_client = os.quotas_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040059 cls.limits_client = os.limits_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070060 cls.volumes_extensions_client = os.volumes_extensions_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040061 cls.volumes_client = os.volumes_client
Dan Smith8ad1c472013-02-26 13:03:16 -050062 cls.interfaces_client = os.interfaces_client
Mauro S. M. Rodriguesf5166402013-04-01 10:25:26 -040063 cls.fixed_ips_client = os.fixed_ips_client
Leo Toyodaa5278912013-04-16 15:40:12 +090064 cls.availability_zone_client = os.availability_zone_client
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090065 cls.aggregates_client = os.aggregates_client
Leo Toyoda3ae31e12013-04-19 11:19:57 +090066 cls.services_client = os.services_client
Tony Yang3d5f1632013-06-06 14:17:57 +080067 cls.hypervisor_client = os.hypervisor_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040068 cls.build_interval = cls.config.compute.build_interval
69 cls.build_timeout = cls.config.compute.build_timeout
70 cls.ssh_user = cls.config.compute.ssh_user
71 cls.image_ref = cls.config.compute.image_ref
72 cls.image_ref_alt = cls.config.compute.image_ref_alt
73 cls.flavor_ref = cls.config.compute.flavor_ref
74 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
75 cls.servers = []
Daryl Walleckc7251962012-03-12 17:26:54 -050076
Brant Knudsonc7ca3342013-03-28 21:08:50 -050077 cls.servers_client_v3_auth = os.servers_client_v3_auth
78
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()
Jay Pipesf38eaac2012-06-21 13:37:35 -0400106 password = "pass"
107
ivan-zhu1d78c482013-03-28 15:55:05 +0800108 while True:
109 try:
110 rand_name_root = rand_name(cls.__name__)
111 if cls.isolated_creds:
112 # Main user already created. Create the alt one...
113 rand_name_root += '-alt'
114 tenant_name = rand_name_root + "-tenant"
115 tenant_desc = tenant_name + "-desc"
Dan Smithd6ff6b72012-08-23 10:29:41 -0700116
ivan-zhu1d78c482013-03-28 15:55:05 +0800117 resp, tenant = admin_client.create_tenant(
118 name=tenant_name, description=tenant_desc)
119 break
120 except exceptions.Duplicate:
121 if cls.config.compute.allow_tenant_reuse:
122 tenant = admin_client.get_tenant_by_name(tenant_name)
123 LOG.info('Re-using existing tenant %s', tenant)
124 break
Dan Smithd6ff6b72012-08-23 10:29:41 -0700125
ivan-zhu1d78c482013-03-28 15:55:05 +0800126 while True:
127 try:
128 rand_name_root = rand_name(cls.__name__)
129 if cls.isolated_creds:
130 # Main user already created. Create the alt one...
131 rand_name_root += '-alt'
132 username = rand_name_root + "-user"
133 email = rand_name_root + "@example.com"
134 resp, user = admin_client.create_user(username,
135 password,
136 tenant['id'],
137 email)
138 break
139 except exceptions.Duplicate:
140 if cls.config.compute.allow_tenant_reuse:
141 user = admin_client.get_user_by_username(tenant['id'],
142 username)
143 LOG.info('Re-using existing user %s', user)
144 break
Jay Pipesf38eaac2012-06-21 13:37:35 -0400145 # Store the complete creds (including UUID ids...) for later
146 # but return just the username, tenant_name, password tuple
147 # that the various clients will use.
148 cls.isolated_creds.append((user, tenant))
149
150 return username, tenant_name, password
151
152 @classmethod
153 def clear_isolated_creds(cls):
154 if not cls.isolated_creds:
Attila Fazekas6c4cce22013-02-14 14:01:13 +0100155 return
Jay Pipesf38eaac2012-06-21 13:37:35 -0400156 admin_client = cls._get_identity_admin_client()
157
158 for user, tenant in cls.isolated_creds:
159 admin_client.delete_user(user['id'])
160 admin_client.delete_tenant(tenant['id'])
161
162 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -0400163 def clear_servers(cls):
164 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700165 try:
166 cls.servers_client.delete_server(server['id'])
167 except Exception:
168 pass
169
Jay Pipes444c3e62012-10-04 19:26:35 -0400170 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -0700171 try:
172 cls.servers_client.wait_for_server_termination(server['id'])
173 except Exception:
174 pass
175
176 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400177 def tearDownClass(cls):
Jay Pipes444c3e62012-10-04 19:26:35 -0400178 cls.clear_servers()
Jay Pipesf38eaac2012-06-21 13:37:35 -0400179 cls.clear_isolated_creds()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700180
Jay Pipes444c3e62012-10-04 19:26:35 -0400181 @classmethod
Sean Dague22897e12013-02-25 17:54:09 -0500182 def create_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500183 """Wrapper utility that returns a test server."""
Sean Dague22897e12013-02-25 17:54:09 -0500184 name = rand_name(cls.__name__ + "-instance")
185 if 'name' in kwargs:
186 name = kwargs.pop('name')
187 flavor = kwargs.get('flavor', cls.flavor_ref)
188 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700189
Andrea Frittoli938d1512013-05-16 15:42:27 +0100190 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500191 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100192
193 # handle the case of multiple servers
194 servers = [body]
195 if 'min_count' in kwargs or 'max_count' in kwargs:
196 # Get servers created which name match with name param.
197 r, b = cls.servers_client.list_servers()
198 servers = [s for s in b['servers'] if s['name'].startswith(name)]
199
200 cls.servers.extend(servers)
David Kranzcf0040c2012-06-26 09:46:56 -0400201
Sean Dague22897e12013-02-25 17:54:09 -0500202 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100203 for server in servers:
204 cls.servers_client.wait_for_server_status(
205 server['id'], kwargs['wait_until'])
Sean Dague9b669e32012-12-13 18:40:08 -0500206
Andrea Frittoli938d1512013-05-16 15:42:27 +0100207 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500208
David Kranzcf0040c2012-06-26 09:46:56 -0400209 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500210 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400211 start_time = int(time.time())
212 while True:
213 try:
214 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500215 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400216 pass
217 else:
218 return
219 if int(time.time()) - start_time >= self.build_timeout:
220 condition()
221 return
222 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400223
224
Attila Fazekas19044d52013-02-16 07:35:06 +0100225class BaseComputeAdminTest(BaseComputeTest):
Sean Daguef237ccb2013-01-04 15:19:14 -0500226 """Base test case class for all Compute Admin API tests."""
Jay Pipesf38eaac2012-06-21 13:37:35 -0400227
228 @classmethod
229 def setUpClass(cls):
Attila Fazekas4ba36582013-02-12 08:26:17 +0100230 super(BaseComputeAdminTest, cls).setUpClass()
231 admin_username = cls.config.compute_admin.username
232 admin_password = cls.config.compute_admin.password
233 admin_tenant = cls.config.compute_admin.tenant_name
Jay Pipesf38eaac2012-06-21 13:37:35 -0400234
Attila Fazekas4ba36582013-02-12 08:26:17 +0100235 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400236 msg = ("Missing Compute Admin API credentials "
237 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800238 raise cls.skipException(msg)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400239
Attila Fazekas4ba36582013-02-12 08:26:17 +0100240 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)