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 | |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 18 | import time |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 19 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 20 | from tempest.api import compute |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 21 | from tempest import clients |
Mitsuhiko Yamazaki | 46818aa | 2013-04-18 17:49:17 +0900 | [diff] [blame] | 22 | from tempest.common import log as logging |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | from tempest.common.utils.data_utils import rand_name |
Wayne Vestal Weeks | 383c71d | 2012-09-12 16:21:17 -0400 | [diff] [blame] | 24 | from tempest import exceptions |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 25 | import tempest.test |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 26 | |
Tiago Mello | eda03b5 | 2012-08-22 23:47:29 -0300 | [diff] [blame] | 27 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 28 | LOG = logging.getLogger(__name__) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 29 | |
| 30 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 31 | class BaseComputeTest(tempest.test.BaseTestCase): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 32 | """Base test case class for all Compute API tests.""" |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 33 | |
Attila Fazekas | 9a63c94 | 2013-01-29 21:46:02 +0100 | [diff] [blame] | 34 | conclusion = compute.generic_setup_package() |
Chris Yeoh | 8a79b9d | 2013-01-18 19:32:47 +1030 | [diff] [blame] | 35 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 36 | @classmethod |
| 37 | def setUpClass(cls): |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 38 | 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 Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 43 | os = clients.Manager(username=username, |
| 44 | password=password, |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 45 | tenant_name=tenant_name, |
| 46 | interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 47 | else: |
James E. Blair | e6d8ee1 | 2013-01-18 21:33:45 +0000 | [diff] [blame] | 48 | os = clients.Manager(interface=cls._interface) |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 49 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 50 | 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 Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 57 | cls.security_groups_client = os.security_groups_client |
Rohit Karajgi | 07599c5 | 2012-11-02 05:35:16 -0700 | [diff] [blame] | 58 | cls.quotas_client = os.quotas_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 59 | cls.limits_client = os.limits_client |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 60 | cls.volumes_extensions_client = os.volumes_extensions_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 61 | cls.volumes_client = os.volumes_client |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 62 | cls.interfaces_client = os.interfaces_client |
Mauro S. M. Rodrigues | f516640 | 2013-04-01 10:25:26 -0400 | [diff] [blame] | 63 | cls.fixed_ips_client = os.fixed_ips_client |
Leo Toyoda | a527891 | 2013-04-16 15:40:12 +0900 | [diff] [blame] | 64 | cls.availability_zone_client = os.availability_zone_client |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 65 | cls.aggregates_client = os.aggregates_client |
Leo Toyoda | 3ae31e1 | 2013-04-19 11:19:57 +0900 | [diff] [blame] | 66 | cls.services_client = os.services_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 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 | |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 76 | cls.servers_client_v3_auth = os.servers_client_v3_auth |
| 77 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 78 | @classmethod |
| 79 | def _get_identity_admin_client(cls): |
| 80 | """ |
| 81 | Returns an instance of the Identity Admin API client |
| 82 | """ |
Attila Fazekas | 407b6db | 2013-01-19 12:48:36 +0100 | [diff] [blame] | 83 | os = clients.AdminManager(interface=cls._interface) |
| 84 | admin_client = os.identity_client |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 85 | return admin_client |
Daryl Walleck | c725196 | 2012-03-12 17:26:54 -0500 | [diff] [blame] | 86 | |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 87 | @classmethod |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 88 | def _get_client_args(cls): |
| 89 | |
| 90 | return ( |
| 91 | cls.config, |
Attila Fazekas | cadcb1f | 2013-01-21 23:10:53 +0100 | [diff] [blame] | 92 | cls.config.identity.admin_username, |
| 93 | cls.config.identity.admin_password, |
Jay Pipes | 7c88eb2 | 2013-01-16 21:32:43 -0500 | [diff] [blame] | 94 | cls.config.identity.uri |
Mate Lakat | 99ee914 | 2012-09-14 12:34:46 +0100 | [diff] [blame] | 95 | ) |
| 96 | |
| 97 | @classmethod |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 98 | def _get_isolated_creds(cls): |
| 99 | """ |
| 100 | Creates a new set of user/tenant/password credentials for a |
| 101 | **regular** user of the Compute API so that a test case can |
| 102 | operate in an isolated tenant container. |
| 103 | """ |
| 104 | admin_client = cls._get_identity_admin_client() |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 105 | password = "pass" |
| 106 | |
ivan-zhu | 1d78c48 | 2013-03-28 15:55:05 +0800 | [diff] [blame] | 107 | while True: |
| 108 | try: |
| 109 | rand_name_root = rand_name(cls.__name__) |
| 110 | if cls.isolated_creds: |
| 111 | # Main user already created. Create the alt one... |
| 112 | rand_name_root += '-alt' |
| 113 | tenant_name = rand_name_root + "-tenant" |
| 114 | tenant_desc = tenant_name + "-desc" |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 115 | |
ivan-zhu | 1d78c48 | 2013-03-28 15:55:05 +0800 | [diff] [blame] | 116 | resp, tenant = admin_client.create_tenant( |
| 117 | name=tenant_name, description=tenant_desc) |
| 118 | break |
| 119 | except exceptions.Duplicate: |
| 120 | if cls.config.compute.allow_tenant_reuse: |
| 121 | tenant = admin_client.get_tenant_by_name(tenant_name) |
| 122 | LOG.info('Re-using existing tenant %s', tenant) |
| 123 | break |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 124 | |
ivan-zhu | 1d78c48 | 2013-03-28 15:55:05 +0800 | [diff] [blame] | 125 | while True: |
| 126 | try: |
| 127 | rand_name_root = rand_name(cls.__name__) |
| 128 | if cls.isolated_creds: |
| 129 | # Main user already created. Create the alt one... |
| 130 | rand_name_root += '-alt' |
| 131 | username = rand_name_root + "-user" |
| 132 | email = rand_name_root + "@example.com" |
| 133 | resp, user = admin_client.create_user(username, |
| 134 | password, |
| 135 | tenant['id'], |
| 136 | email) |
| 137 | break |
| 138 | except exceptions.Duplicate: |
| 139 | if cls.config.compute.allow_tenant_reuse: |
| 140 | user = admin_client.get_user_by_username(tenant['id'], |
| 141 | username) |
| 142 | LOG.info('Re-using existing user %s', user) |
| 143 | break |
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: |
Attila Fazekas | 6c4cce2 | 2013-02-14 14:01:13 +0100 | [diff] [blame] | 154 | return |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 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 |
Sean Dague | 22897e1 | 2013-02-25 17:54:09 -0500 | [diff] [blame] | 181 | def create_server(cls, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 182 | """Wrapper utility that returns a test server.""" |
Sean Dague | 22897e1 | 2013-02-25 17:54:09 -0500 | [diff] [blame] | 183 | name = rand_name(cls.__name__ + "-instance") |
| 184 | if 'name' in kwargs: |
| 185 | name = kwargs.pop('name') |
| 186 | flavor = kwargs.get('flavor', cls.flavor_ref) |
| 187 | image_id = kwargs.get('image_id', cls.image_ref) |
Rohit Karajgi | dc300b2 | 2012-05-04 08:11:00 -0700 | [diff] [blame] | 188 | |
Andrea Frittoli | 938d151 | 2013-05-16 15:42:27 +0100 | [diff] [blame] | 189 | resp, body = cls.servers_client.create_server( |
Sean Dague | 22897e1 | 2013-02-25 17:54:09 -0500 | [diff] [blame] | 190 | name, image_id, flavor, **kwargs) |
Andrea Frittoli | 938d151 | 2013-05-16 15:42:27 +0100 | [diff] [blame] | 191 | |
| 192 | # handle the case of multiple servers |
| 193 | servers = [body] |
| 194 | if 'min_count' in kwargs or 'max_count' in kwargs: |
| 195 | # Get servers created which name match with name param. |
| 196 | r, b = cls.servers_client.list_servers() |
| 197 | servers = [s for s in b['servers'] if s['name'].startswith(name)] |
| 198 | |
| 199 | cls.servers.extend(servers) |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 200 | |
Sean Dague | 22897e1 | 2013-02-25 17:54:09 -0500 | [diff] [blame] | 201 | if 'wait_until' in kwargs: |
Andrea Frittoli | 938d151 | 2013-05-16 15:42:27 +0100 | [diff] [blame] | 202 | for server in servers: |
| 203 | cls.servers_client.wait_for_server_status( |
| 204 | server['id'], kwargs['wait_until']) |
Sean Dague | 9b669e3 | 2012-12-13 18:40:08 -0500 | [diff] [blame] | 205 | |
Andrea Frittoli | 938d151 | 2013-05-16 15:42:27 +0100 | [diff] [blame] | 206 | return resp, body |
Sean Dague | 9b669e3 | 2012-12-13 18:40:08 -0500 | [diff] [blame] | 207 | |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 208 | def wait_for(self, condition): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 209 | """Repeatedly calls condition() until a timeout.""" |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 210 | start_time = int(time.time()) |
| 211 | while True: |
| 212 | try: |
| 213 | condition() |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 214 | except Exception: |
David Kranz | cf0040c | 2012-06-26 09:46:56 -0400 | [diff] [blame] | 215 | pass |
| 216 | else: |
| 217 | return |
| 218 | if int(time.time()) - start_time >= self.build_timeout: |
| 219 | condition() |
| 220 | return |
| 221 | time.sleep(self.build_interval) |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 222 | |
| 223 | |
Attila Fazekas | 19044d5 | 2013-02-16 07:35:06 +0100 | [diff] [blame] | 224 | class BaseComputeAdminTest(BaseComputeTest): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 225 | """Base test case class for all Compute Admin API tests.""" |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 226 | |
| 227 | @classmethod |
| 228 | def setUpClass(cls): |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 229 | super(BaseComputeAdminTest, cls).setUpClass() |
| 230 | admin_username = cls.config.compute_admin.username |
| 231 | admin_password = cls.config.compute_admin.password |
| 232 | admin_tenant = cls.config.compute_admin.tenant_name |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 233 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 234 | if not (admin_username and admin_password and admin_tenant): |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 235 | msg = ("Missing Compute Admin API credentials " |
| 236 | "in configuration.") |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 237 | raise cls.skipException(msg) |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 238 | |
Attila Fazekas | 4ba3658 | 2013-02-12 08:26:17 +0100 | [diff] [blame] | 239 | cls.os_adm = clients.ComputeAdminManager(interface=cls._interface) |