blob: 8657db81812a2cd2734da28b57c7f285ccabb18f [file] [log] [blame]
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07001# 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
18import logging
19import time
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070020
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021import nose
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022import unittest2 as unittest
23
Matthew Treinish481466b2012-12-20 17:16:01 -050024from tempest import clients
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070025from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest import config
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070027from tempest import exceptions
28
29LOG = logging.getLogger(__name__)
30
31
32class BaseVolumeTest(unittest.TestCase):
33
Sean Daguef237ccb2013-01-04 15:19:14 -050034 """Base test case class for all Cinder API tests."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070035
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 Treinish481466b2012-12-20 17:16:01 -050044 os = clients.Manager(username=username,
45 password=password,
46 tenant_name=tenant_name)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070047 else:
Matthew Treinish481466b2012-12-20 17:16:01 -050048 os = clients.Manager()
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070049
50 cls.os = os
51 cls.volumes_client = os.volumes_client
Rohit Karajgia42fe442012-09-21 03:08:33 -070052 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 Karajgidd47d7e2012-07-31 04:11:01 -070055 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 Luoe0884a32012-09-25 17:24:17 +080060 cls.__name__)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070061 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 Karajgia42fe442012-09-21 03:08:33 -070068 cls.clear_isolated_creds()
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070069 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 Treinish481466b2012-12-20 17:16:01 -050076 os = clients.IdentityManager()
Vincent Hou6b8a7b72012-08-25 01:24:33 +080077 return os.admin_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070078
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 Daguef237ccb2013-01-04 15:19:14 -0500125 """Wrapper utility that returns a test volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700126 display_name = rand_name(self.__class__.__name__ + "-volume")
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800127 cli_resp = self.volumes_client.create_volume(size=size,
128 display_name=display_name,
129 metdata=metadata)
130 resp, volume = cli_resp
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700131 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 Daguef237ccb2013-01-04 15:19:14 -0500136 """Repeatedly calls condition() until a timeout."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700137 start_time = int(time.time())
138 while True:
139 try:
140 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500141 except Exception:
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700142 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 Treinish9854d5b2012-09-20 10:22:13 -0400149
150
151class BaseVolumeTestJSON(BaseVolumeTest):
152 @classmethod
153 def setUpClass(cls):
154 cls._interface = "json"
155 super(BaseVolumeTestJSON, cls).setUpClass()
156
157
158class BaseVolumeTestXML(BaseVolumeTest):
159 @classmethod
160 def setUpClass(cls):
161 cls._interface = "xml"
162 super(BaseVolumeTestXML, cls).setUpClass()