Jay Pipes | 13b479b | 2012-06-11 14:52:27 -0400 | [diff] [blame] | 1 | # 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 18 | import logging |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 19 | import time |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 20 | |
Wayne Vestal Weeks | 383c71d | 2012-09-12 16:21:17 -0400 | [diff] [blame] | 21 | import nose |
Chris Yeoh | 8a79b9d | 2013-01-18 19:32:47 +1030 | [diff] [blame] | 22 | import testresources |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | import unittest2 as unittest |
Jay Pipes | 13b479b | 2012-06-11 14:52:27 -0400 | [diff] [blame] | 24 | |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 25 | from tempest import clients |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 26 | from tempest.common.utils.data_utils import rand_name |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 27 | from tempest import config |
Wayne Vestal Weeks | 383c71d | 2012-09-12 16:21:17 -0400 | [diff] [blame] | 28 | from tempest import exceptions |
Chris Yeoh | 8a79b9d | 2013-01-18 19:32:47 +1030 | [diff] [blame] | 29 | from tempest.tests import compute |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 30 | |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 31 | __all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML', |
| 32 | 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML'] |
Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 33 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 34 | LOG = logging.getLogger(__name__) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 35 | |
| 36 | |
Chris Yeoh | 8a79b9d | 2013-01-18 19:32:47 +1030 | [diff] [blame] | 37 | class BaseCompTest(unittest.TestCase, |
| 38 | testresources.ResourcedTestCase): |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 39 | |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 40 | """Base test case class for all Compute API tests.""" |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 41 | |
Chris Yeoh | 8a79b9d | 2013-01-18 19:32:47 +1030 | [diff] [blame] | 42 | resources = [('compute_init', compute.generic_setup_package())] |
| 43 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 44 | @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 Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 52 | os = clients.Manager(username=username, |
| 53 | password=password, |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 54 | tenant_name=tenant_name, |
| 55 | interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 56 | else: |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 57 | os = clients.Manager(interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 58 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 59 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 66 | cls.security_groups_client = os.security_groups_client |
| 67 | cls.console_outputs_client = os.console_outputs_client |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 68 | cls.quotas_client = os.quotas_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 69 | cls.limits_client = os.limits_client |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 70 | cls.volumes_extensions_client = os.volumes_extensions_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 71 | 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 Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 80 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 81 | @classmethod |
| 82 | def _get_identity_admin_client(cls): |
| 83 | """ |
| 84 | Returns an instance of the Identity Admin API client |
| 85 | """ |
Attila Fazekas | 407b6db | 2013-01-19 12:48:36 +0100 | [diff] [blame] | 86 | os = clients.AdminManager(interface=cls._interface) |
| 87 | admin_client = os.identity_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 88 | return admin_client |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 89 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 90 | @classmethod |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 91 | def _get_client_args(cls): |
| 92 | |
| 93 | return ( |
| 94 | cls.config, |
Attila Fazekas | cadcb1f | 2013-01-21 23:10:53 +0100 | [diff] [blame] | 95 | cls.config.identity.admin_username, |
| 96 | cls.config.identity.admin_password, |
Jay Pipes | 7c88eb2 | 2013-01-16 21:32:43 -0500 | [diff] [blame] | 97 | cls.config.identity.uri |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 98 | ) |
| 99 | |
| 100 | @classmethod |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 101 | 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-zhu | ac81b7d | 2013-01-05 17:45:41 +0800 | [diff] [blame] | 108 | rand_name_root = rand_name(cls.__name__) |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 109 | 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 Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 118 | 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 129 | 'allow_tenant_reuse in tempest.conf') % tenant_name |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 130 | 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 143 | msg = ('Unable to create isolated user %s because ' + |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 144 | 'it already exists. If this is related to a ' + |
| 145 | 'previous test failure, try using ' + |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 146 | 'allow_tenant_reuse in tempest.conf') % tenant_name |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 147 | raise exceptions.Duplicate(msg) |
| 148 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 149 | # 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 167 | def clear_servers(cls): |
| 168 | for server in cls.servers: |
Dan Smith | 74e7bcb | 2012-08-21 09:18:26 -0700 | [diff] [blame] | 169 | try: |
| 170 | cls.servers_client.delete_server(server['id']) |
| 171 | except Exception: |
| 172 | pass |
| 173 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 174 | for server in cls.servers: |
Dan Smith | 74e7bcb | 2012-08-21 09:18:26 -0700 | [diff] [blame] | 175 | try: |
| 176 | cls.servers_client.wait_for_server_termination(server['id']) |
| 177 | except Exception: |
| 178 | pass |
| 179 | |
| 180 | @classmethod |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 181 | def tearDownClass(cls): |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 182 | cls.clear_servers() |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 183 | cls.clear_isolated_creds() |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 184 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 185 | @classmethod |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 186 | def create_server(cls, image_id=None, flavor=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 187 | """Wrapper utility that returns a test server.""" |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 188 | server_name = rand_name(cls.__name__ + "-instance") |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 189 | |
| 190 | if not flavor: |
| 191 | flavor = cls.flavor_ref |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 192 | if not image_id: |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 193 | image_id = cls.image_ref |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 194 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 195 | resp, server = cls.servers_client.create_server( |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 196 | server_name, image_id, flavor) |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 197 | cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 198 | cls.servers.append(server) |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 199 | return server |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 200 | |
Sean Dague | 9b669e3 | 2012-12-13 18:40:08 -0500 | [diff] [blame] | 201 | @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 Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 219 | def wait_for(self, condition): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 220 | """Repeatedly calls condition() until a timeout.""" |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 221 | start_time = int(time.time()) |
| 222 | while True: |
| 223 | try: |
| 224 | condition() |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 225 | except Exception: |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 226 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 233 | |
| 234 | |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 235 | class 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 |
| 242 | BaseComputeTest = BaseComputeTestJSON |
| 243 | |
| 244 | |
| 245 | class BaseComputeTestXML(BaseCompTest): |
| 246 | @classmethod |
| 247 | def setUpClass(cls): |
| 248 | cls._interface = "xml" |
| 249 | super(BaseComputeTestXML, cls).setUpClass() |
| 250 | |
| 251 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 252 | class BaseComputeAdminTest(unittest.TestCase): |
| 253 | |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 254 | """Base test case class for all Compute Admin API tests.""" |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 255 | |
| 256 | @classmethod |
| 257 | def setUpClass(cls): |
| 258 | cls.config = config.TempestConfig() |
Attila Fazekas | cadcb1f | 2013-01-21 23:10:53 +0100 | [diff] [blame] | 259 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 262 | |
| 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 Fazekas | cadcb1f | 2013-01-21 23:10:53 +0100 | [diff] [blame] | 268 | cls.os = clients.ComputeAdminManager(interface=cls._interface) |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 269 | |
| 270 | |
| 271 | class BaseComputeAdminTestJSON(BaseComputeAdminTest): |
| 272 | @classmethod |
| 273 | def setUpClass(cls): |
| 274 | cls._interface = "json" |
| 275 | super(BaseComputeAdminTestJSON, cls).setUpClass() |
| 276 | |
| 277 | |
| 278 | class BaseComputeAdminTestXML(BaseComputeAdminTest): |
| 279 | @classmethod |
| 280 | def setUpClass(cls): |
| 281 | cls._interface = "xml" |
| 282 | super(BaseComputeAdminTestXML, cls).setUpClass() |