Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [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 | |
| 18 | import logging |
| 19 | import time |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | import nose |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 22 | import unittest2 as unittest |
| 23 | |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 24 | from tempest import clients |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 25 | from tempest.common.utils.data_utils import rand_name |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 26 | from tempest import config |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 27 | from tempest import exceptions |
| 28 | |
| 29 | LOG = logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | class BaseVolumeTest(unittest.TestCase): |
| 33 | |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 34 | """Base test case class for all Cinder API tests.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 35 | |
| 36 | @classmethod |
| 37 | def setUpClass(cls): |
| 38 | cls.config = config.TempestConfig() |
| 39 | cls.isolated_creds = [] |
| 40 | |
| 41 | if cls.config.compute.allow_tenant_isolation: |
| 42 | creds = cls._get_isolated_creds() |
| 43 | username, tenant_name, password = creds |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 44 | os = clients.Manager(username=username, |
| 45 | password=password, |
| 46 | tenant_name=tenant_name) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 47 | else: |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 48 | os = clients.Manager() |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 49 | |
| 50 | cls.os = os |
| 51 | cls.volumes_client = os.volumes_client |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 52 | cls.servers_client = os.servers_client |
| 53 | cls.image_ref = cls.config.compute.image_ref |
| 54 | cls.flavor_ref = cls.config.compute.flavor_ref |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 55 | cls.build_interval = cls.config.volume.build_interval |
| 56 | cls.build_timeout = cls.config.volume.build_timeout |
| 57 | cls.volumes = {} |
| 58 | |
| 59 | skip_msg = ("%s skipped as Cinder endpoint is not available" % |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 60 | cls.__name__) |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 61 | try: |
| 62 | cls.volumes_client.keystone_auth(cls.os.username, |
| 63 | cls.os.password, |
| 64 | cls.os.auth_url, |
| 65 | cls.volumes_client.service, |
| 66 | cls.os.tenant_name) |
| 67 | except exceptions.EndpointNotFound: |
Rohit Karajgi | a42fe44 | 2012-09-21 03:08:33 -0700 | [diff] [blame] | 68 | cls.clear_isolated_creds() |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 69 | raise nose.SkipTest(skip_msg) |
| 70 | |
| 71 | @classmethod |
| 72 | def _get_identity_admin_client(cls): |
| 73 | """ |
| 74 | Returns an instance of the Identity Admin API client |
| 75 | """ |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 76 | os = clients.IdentityManager() |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 77 | return os.admin_client |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 78 | |
| 79 | @classmethod |
| 80 | def _get_isolated_creds(cls): |
| 81 | """ |
| 82 | Creates a new set of user/tenant/password credentials for a |
| 83 | **regular** user of the Volume API so that a test case can |
| 84 | operate in an isolated tenant container. |
| 85 | """ |
| 86 | admin_client = cls._get_identity_admin_client() |
| 87 | rand_name_root = cls.__name__ |
| 88 | if cls.isolated_creds: |
| 89 | # Main user already created. Create the alt one... |
| 90 | rand_name_root += '-alt' |
| 91 | username = rand_name_root + "-user" |
| 92 | email = rand_name_root + "@example.com" |
| 93 | tenant_name = rand_name_root + "-tenant" |
| 94 | tenant_desc = tenant_name + "-desc" |
| 95 | password = "pass" |
| 96 | |
| 97 | resp, tenant = admin_client.create_tenant(name=tenant_name, |
| 98 | description=tenant_desc) |
| 99 | resp, user = admin_client.create_user(username, |
| 100 | password, |
| 101 | tenant['id'], |
| 102 | email) |
| 103 | # Store the complete creds (including UUID ids...) for later |
| 104 | # but return just the username, tenant_name, password tuple |
| 105 | # that the various clients will use. |
| 106 | cls.isolated_creds.append((user, tenant)) |
| 107 | |
| 108 | return username, tenant_name, password |
| 109 | |
| 110 | @classmethod |
| 111 | def clear_isolated_creds(cls): |
| 112 | if not cls.isolated_creds: |
| 113 | pass |
| 114 | admin_client = cls._get_identity_admin_client() |
| 115 | |
| 116 | for user, tenant in cls.isolated_creds: |
| 117 | admin_client.delete_user(user['id']) |
| 118 | admin_client.delete_tenant(tenant['id']) |
| 119 | |
| 120 | @classmethod |
| 121 | def tearDownClass(cls): |
| 122 | cls.clear_isolated_creds() |
| 123 | |
| 124 | def create_volume(self, size=1, metadata={}): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 125 | """Wrapper utility that returns a test volume.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 126 | display_name = rand_name(self.__class__.__name__ + "-volume") |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 127 | cli_resp = self.volumes_client.create_volume(size=size, |
| 128 | display_name=display_name, |
| 129 | metdata=metadata) |
| 130 | resp, volume = cli_resp |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 131 | self.volumes_client.wait_for_volume_status(volume['id'], 'available') |
| 132 | self.volumes.append(volume) |
| 133 | return volume |
| 134 | |
| 135 | def wait_for(self, condition): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 136 | """Repeatedly calls condition() until a timeout.""" |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 137 | start_time = int(time.time()) |
| 138 | while True: |
| 139 | try: |
| 140 | condition() |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 141 | except Exception: |
Rohit Karajgi | dd47d7e | 2012-07-31 04:11:01 -0700 | [diff] [blame] | 142 | pass |
| 143 | else: |
| 144 | return |
| 145 | if int(time.time()) - start_time >= self.build_timeout: |
| 146 | condition() |
| 147 | return |
| 148 | time.sleep(self.build_interval) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 149 | |
| 150 | |
| 151 | class BaseVolumeTestJSON(BaseVolumeTest): |
| 152 | @classmethod |
| 153 | def setUpClass(cls): |
| 154 | cls._interface = "json" |
| 155 | super(BaseVolumeTestJSON, cls).setUpClass() |
| 156 | |
| 157 | |
| 158 | class BaseVolumeTestXML(BaseVolumeTest): |
| 159 | @classmethod |
| 160 | def setUpClass(cls): |
| 161 | cls._interface = "xml" |
| 162 | super(BaseVolumeTestXML, cls).setUpClass() |