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 |
| 20 | import nose |
| 21 | |
| 22 | import unittest2 as unittest |
| 23 | |
| 24 | from tempest import config |
| 25 | from tempest import openstack |
| 26 | from tempest.common.utils.data_utils import rand_name |
| 27 | from tempest.services.identity.json.admin_client import AdminClient |
| 28 | from tempest import exceptions |
| 29 | |
| 30 | LOG = logging.getLogger(__name__) |
| 31 | |
| 32 | |
| 33 | class BaseVolumeTest(unittest.TestCase): |
| 34 | |
| 35 | """Base test case class for all Cinder API tests""" |
| 36 | |
| 37 | @classmethod |
| 38 | def setUpClass(cls): |
| 39 | cls.config = config.TempestConfig() |
| 40 | cls.isolated_creds = [] |
| 41 | |
| 42 | if cls.config.compute.allow_tenant_isolation: |
| 43 | creds = cls._get_isolated_creds() |
| 44 | username, tenant_name, password = creds |
| 45 | os = openstack.Manager(username=username, |
| 46 | password=password, |
| 47 | tenant_name=tenant_name) |
| 48 | else: |
| 49 | os = openstack.Manager() |
| 50 | |
| 51 | cls.os = os |
| 52 | cls.volumes_client = os.volumes_client |
| 53 | cls.build_interval = cls.config.volume.build_interval |
| 54 | cls.build_timeout = cls.config.volume.build_timeout |
| 55 | cls.volumes = {} |
| 56 | |
| 57 | skip_msg = ("%s skipped as Cinder endpoint is not available" % |
| 58 | cls.__name__) |
| 59 | try: |
| 60 | cls.volumes_client.keystone_auth(cls.os.username, |
| 61 | cls.os.password, |
| 62 | cls.os.auth_url, |
| 63 | cls.volumes_client.service, |
| 64 | cls.os.tenant_name) |
| 65 | except exceptions.EndpointNotFound: |
| 66 | raise nose.SkipTest(skip_msg) |
| 67 | |
| 68 | @classmethod |
| 69 | def _get_identity_admin_client(cls): |
| 70 | """ |
| 71 | Returns an instance of the Identity Admin API client |
| 72 | """ |
| 73 | client_args = (cls.config, |
| 74 | cls.config.identity_admin.username, |
| 75 | cls.config.identity_admin.password, |
| 76 | cls.config.identity.auth_url) |
| 77 | tenant_name = cls.config.identity_admin.tenant_name |
| 78 | admin_client = AdminClient(*client_args, tenant_name=tenant_name) |
| 79 | return admin_client |
| 80 | |
| 81 | @classmethod |
| 82 | def _get_isolated_creds(cls): |
| 83 | """ |
| 84 | Creates a new set of user/tenant/password credentials for a |
| 85 | **regular** user of the Volume API so that a test case can |
| 86 | operate in an isolated tenant container. |
| 87 | """ |
| 88 | admin_client = cls._get_identity_admin_client() |
| 89 | rand_name_root = cls.__name__ |
| 90 | if cls.isolated_creds: |
| 91 | # Main user already created. Create the alt one... |
| 92 | rand_name_root += '-alt' |
| 93 | username = rand_name_root + "-user" |
| 94 | email = rand_name_root + "@example.com" |
| 95 | tenant_name = rand_name_root + "-tenant" |
| 96 | tenant_desc = tenant_name + "-desc" |
| 97 | password = "pass" |
| 98 | |
| 99 | resp, tenant = admin_client.create_tenant(name=tenant_name, |
| 100 | description=tenant_desc) |
| 101 | resp, user = admin_client.create_user(username, |
| 102 | password, |
| 103 | tenant['id'], |
| 104 | email) |
| 105 | # Store the complete creds (including UUID ids...) for later |
| 106 | # but return just the username, tenant_name, password tuple |
| 107 | # that the various clients will use. |
| 108 | cls.isolated_creds.append((user, tenant)) |
| 109 | |
| 110 | return username, tenant_name, password |
| 111 | |
| 112 | @classmethod |
| 113 | def clear_isolated_creds(cls): |
| 114 | if not cls.isolated_creds: |
| 115 | pass |
| 116 | admin_client = cls._get_identity_admin_client() |
| 117 | |
| 118 | for user, tenant in cls.isolated_creds: |
| 119 | admin_client.delete_user(user['id']) |
| 120 | admin_client.delete_tenant(tenant['id']) |
| 121 | |
| 122 | @classmethod |
| 123 | def tearDownClass(cls): |
| 124 | cls.clear_isolated_creds() |
| 125 | |
| 126 | def create_volume(self, size=1, metadata={}): |
| 127 | """Wrapper utility that returns a test volume""" |
| 128 | display_name = rand_name(self.__class__.__name__ + "-volume") |
| 129 | resp, volume = self.volumes_client.create_volume(size=size, |
| 130 | display_name=display_name, |
| 131 | metdata=metadata) |
| 132 | self.volumes_client.wait_for_volume_status(volume['id'], 'available') |
| 133 | self.volumes.append(volume) |
| 134 | return volume |
| 135 | |
| 136 | def wait_for(self, condition): |
| 137 | """Repeatedly calls condition() until a timeout""" |
| 138 | start_time = int(time.time()) |
| 139 | while True: |
| 140 | try: |
| 141 | condition() |
| 142 | except: |
| 143 | pass |
| 144 | else: |
| 145 | return |
| 146 | if int(time.time()) - start_time >= self.build_timeout: |
| 147 | condition() |
| 148 | return |
| 149 | time.sleep(self.build_interval) |