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 |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 22 | import unittest2 as unittest |
Jay Pipes | 13b479b | 2012-06-11 14:52:27 -0400 | [diff] [blame] | 23 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | from tempest.common.utils.data_utils import rand_name |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 25 | from tempest import config |
Wayne Vestal Weeks | 383c71d | 2012-09-12 16:21:17 -0400 | [diff] [blame] | 26 | from tempest import exceptions |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 27 | from tempest import openstack |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 28 | |
Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 29 | __all__ = ['BaseComputeTest', 'BaseComputeTestJSON', 'BaseComputeTestXML', |
| 30 | 'BaseComputeAdminTestJSON', 'BaseComputeAdminTestXML'] |
| 31 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 32 | LOG = logging.getLogger(__name__) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 33 | |
| 34 | |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 35 | class BaseCompTest(unittest.TestCase): |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 36 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 37 | """Base test case class for all Compute API tests""" |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 38 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 39 | @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 Smith | cf8fab6 | 2012-08-14 08:03:48 -0700 | [diff] [blame] | 49 | tenant_name=tenant_name, |
| 50 | interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 51 | else: |
Dan Smith | cf8fab6 | 2012-08-14 08:03:48 -0700 | [diff] [blame] | 52 | os = openstack.Manager(interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 53 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 54 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 61 | cls.security_groups_client = os.security_groups_client |
| 62 | cls.console_outputs_client = os.console_outputs_client |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 63 | cls.quotas_client = os.quotas_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 64 | cls.limits_client = os.limits_client |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 65 | cls.volumes_extensions_client = os.volumes_extensions_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 66 | 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 Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 75 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 76 | @classmethod |
| 77 | def _get_identity_admin_client(cls): |
| 78 | """ |
| 79 | Returns an instance of the Identity Admin API client |
| 80 | """ |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 81 | os = openstack.IdentityManager(interface=cls._interface) |
| 82 | admin_client = os.admin_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 83 | return admin_client |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 84 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 85 | @classmethod |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 86 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 96 | 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 Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 113 | 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 124 | 'allow_tenant_reuse in tempest.conf') % tenant_name |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 125 | 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 138 | msg = ('Unable to create isolated user %s because ' + |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 139 | 'it already exists. If this is related to a ' + |
| 140 | 'previous test failure, try using ' + |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 141 | 'allow_tenant_reuse in tempest.conf') % tenant_name |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 142 | raise exceptions.Duplicate(msg) |
| 143 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 144 | # 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 Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 162 | def clear_servers(cls): |
| 163 | for server in cls.servers: |
Dan Smith | 74e7bcb | 2012-08-21 09:18:26 -0700 | [diff] [blame] | 164 | try: |
| 165 | cls.servers_client.delete_server(server['id']) |
| 166 | except Exception: |
| 167 | pass |
| 168 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 169 | for server in cls.servers: |
Dan Smith | 74e7bcb | 2012-08-21 09:18:26 -0700 | [diff] [blame] | 170 | try: |
| 171 | cls.servers_client.wait_for_server_termination(server['id']) |
| 172 | except Exception: |
| 173 | pass |
| 174 | |
| 175 | @classmethod |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 176 | def tearDownClass(cls): |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 177 | cls.clear_servers() |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 178 | cls.clear_isolated_creds() |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 179 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 180 | @classmethod |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 181 | def create_server(cls, image_id=None, flavor=None): |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 182 | """Wrapper utility that returns a test server""" |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 183 | server_name = rand_name(cls.__name__ + "-instance") |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 184 | |
| 185 | if not flavor: |
| 186 | flavor = cls.flavor_ref |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 187 | if not image_id: |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 188 | image_id = cls.image_ref |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 189 | |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 190 | resp, server = cls.servers_client.create_server( |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 191 | server_name, image_id, flavor) |
Jay Pipes | 444c3e6 | 2012-10-04 19:26:35 -0400 | [diff] [blame] | 192 | cls.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 193 | cls.servers.append(server) |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 194 | return server |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 195 | |
| 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 Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame^] | 202 | except Exception: |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 203 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 210 | |
| 211 | |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 212 | class BaseComputeTestJSON(BaseCompTest): |
Dan Smith | cf8fab6 | 2012-08-14 08:03:48 -0700 | [diff] [blame] | 213 | @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 |
| 219 | BaseComputeTest = BaseComputeTestJSON |
| 220 | |
| 221 | |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 222 | class BaseComputeTestXML(BaseCompTest): |
Dan Smith | cf8fab6 | 2012-08-14 08:03:48 -0700 | [diff] [blame] | 223 | @classmethod |
| 224 | def setUpClass(cls): |
| 225 | cls._interface = "xml" |
| 226 | super(BaseComputeTestXML, cls).setUpClass() |
| 227 | |
| 228 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 229 | class 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 Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 245 | cls.os = openstack.AdminManager(interface=cls._interface) |
| 246 | |
| 247 | |
| 248 | class BaseComputeAdminTestJSON(BaseComputeAdminTest): |
| 249 | @classmethod |
| 250 | def setUpClass(cls): |
| 251 | cls._interface = "json" |
| 252 | super(BaseComputeAdminTestJSON, cls).setUpClass() |
| 253 | |
| 254 | |
| 255 | class BaseComputeAdminTestXML(BaseComputeAdminTest): |
| 256 | @classmethod |
| 257 | def setUpClass(cls): |
| 258 | cls._interface = "xml" |
| 259 | super(BaseComputeAdminTestXML, cls).setUpClass() |