blob: fc510cbfb8da8a501a0041976964a5df8ba8ff29 [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
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070018import time
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070019
Matthew Treinish481466b2012-12-20 17:16:01 -050020from tempest import clients
Mitsuhiko Yamazaki46818aa2013-04-18 17:49:17 +090021from tempest.common import log as logging
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022from tempest.common.utils.data_utils import rand_name
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070023from tempest import exceptions
Attila Fazekasdc216422013-01-29 15:12:14 +010024import tempest.test
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070025
26LOG = logging.getLogger(__name__)
27
28
Attila Fazekasdc216422013-01-29 15:12:14 +010029class BaseVolumeTest(tempest.test.BaseTestCase):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070030
Sean Daguef237ccb2013-01-04 15:19:14 -050031 """Base test case class for all Cinder API tests."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070032
33 @classmethod
34 def setUpClass(cls):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070035 cls.isolated_creds = []
36
37 if cls.config.compute.allow_tenant_isolation:
38 creds = cls._get_isolated_creds()
39 username, tenant_name, password = creds
Matthew Treinish481466b2012-12-20 17:16:01 -050040 os = clients.Manager(username=username,
41 password=password,
Attila Fazekas786236c2013-01-31 16:06:51 +010042 tenant_name=tenant_name,
43 interface=cls._interface)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070044 else:
Attila Fazekas786236c2013-01-31 16:06:51 +010045 os = clients.Manager(interface=cls._interface)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070046
47 cls.os = os
48 cls.volumes_client = os.volumes_client
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010049 cls.snapshots_client = os.snapshots_client
Rohit Karajgia42fe442012-09-21 03:08:33 -070050 cls.servers_client = os.servers_client
51 cls.image_ref = cls.config.compute.image_ref
52 cls.flavor_ref = cls.config.compute.flavor_ref
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070053 cls.build_interval = cls.config.volume.build_interval
54 cls.build_timeout = cls.config.volume.build_timeout
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010055 cls.snapshots = []
56 cls.volumes = []
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070057
58 skip_msg = ("%s skipped as Cinder endpoint is not available" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +080059 cls.__name__)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070060 try:
61 cls.volumes_client.keystone_auth(cls.os.username,
62 cls.os.password,
63 cls.os.auth_url,
64 cls.volumes_client.service,
65 cls.os.tenant_name)
66 except exceptions.EndpointNotFound:
Rohit Karajgia42fe442012-09-21 03:08:33 -070067 cls.clear_isolated_creds()
ivan-zhu1feeb382013-01-24 10:14:39 +080068 raise cls.skipException(skip_msg)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070069
70 @classmethod
71 def _get_identity_admin_client(cls):
72 """
73 Returns an instance of the Identity Admin API client
74 """
Attila Fazekascadcb1f2013-01-21 23:10:53 +010075 os = clients.ComputeAdminManager()
Attila Fazekas407b6db2013-01-19 12:48:36 +010076 return os.identity_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070077
78 @classmethod
79 def _get_isolated_creds(cls):
80 """
81 Creates a new set of user/tenant/password credentials for a
82 **regular** user of the Volume API so that a test case can
83 operate in an isolated tenant container.
84 """
85 admin_client = cls._get_identity_admin_client()
Chris Yeoh5b89eb42013-02-04 15:33:29 +103086 rand_name_root = rand_name(cls.__name__)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070087 if cls.isolated_creds:
88 # Main user already created. Create the alt one...
89 rand_name_root += '-alt'
90 username = rand_name_root + "-user"
91 email = rand_name_root + "@example.com"
92 tenant_name = rand_name_root + "-tenant"
93 tenant_desc = tenant_name + "-desc"
94 password = "pass"
95
96 resp, tenant = admin_client.create_tenant(name=tenant_name,
97 description=tenant_desc)
98 resp, user = admin_client.create_user(username,
99 password,
100 tenant['id'],
101 email)
102 # Store the complete creds (including UUID ids...) for later
103 # but return just the username, tenant_name, password tuple
104 # that the various clients will use.
105 cls.isolated_creds.append((user, tenant))
106
107 return username, tenant_name, password
108
109 @classmethod
110 def clear_isolated_creds(cls):
111 if not cls.isolated_creds:
Attila Fazekas6c4cce22013-02-14 14:01:13 +0100112 return
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700113 admin_client = cls._get_identity_admin_client()
114
115 for user, tenant in cls.isolated_creds:
116 admin_client.delete_user(user['id'])
117 admin_client.delete_tenant(tenant['id'])
118
119 @classmethod
120 def tearDownClass(cls):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100121 cls.clear_snapshots()
122 cls.clear_volumes()
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700123 cls.clear_isolated_creds()
124
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100125 @classmethod
126 def create_snapshot(cls, volume_id=1, **kwargs):
127 """Wrapper utility that returns a test snapshot."""
128 resp, snapshot = cls.snapshots_client.create_snapshot(volume_id,
129 **kwargs)
130 assert 200 == resp.status
Giulio Fidente02f42982013-06-17 16:25:56 +0200131 cls.snapshots.append(snapshot)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100132 cls.snapshots_client.wait_for_snapshot_status(snapshot['id'],
133 'available')
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100134 return snapshot
135
136 #NOTE(afazekas): these create_* and clean_* could be defined
137 # only in a single location in the source, and could be more general.
138
139 @classmethod
140 def create_volume(cls, size=1, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500141 """Wrapper utility that returns a test volume."""
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100142 resp, volume = cls.volumes_client.create_volume(size, **kwargs)
143 assert 200 == resp.status
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100144 cls.volumes.append(volume)
Giulio Fidente02f42982013-06-17 16:25:56 +0200145 cls.volumes_client.wait_for_volume_status(volume['id'], 'available')
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700146 return volume
147
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100148 @classmethod
149 def clear_volumes(cls):
150 for volume in cls.volumes:
151 try:
Giulio Fidente26d16ed2013-04-30 12:05:56 +0200152 cls.volumes_client.delete_volume(volume['id'])
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100153 except Exception:
154 pass
155
156 for volume in cls.volumes:
157 try:
Giulio Fidente26d16ed2013-04-30 12:05:56 +0200158 cls.volumes_client.wait_for_resource_deletion(volume['id'])
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100159 except Exception:
160 pass
161
162 @classmethod
163 def clear_snapshots(cls):
164 for snapshot in cls.snapshots:
165 try:
166 cls.snapshots_client.delete_snapshot(snapshot['id'])
167 except Exception:
168 pass
169
170 for snapshot in cls.snapshots:
171 try:
172 cls.snapshots_client.wait_for_resource_deletion(snapshot['id'])
173 except Exception:
174 pass
175
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700176 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500177 """Repeatedly calls condition() until a timeout."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700178 start_time = int(time.time())
179 while True:
180 try:
181 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500182 except Exception:
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700183 pass
184 else:
185 return
186 if int(time.time()) - start_time >= self.build_timeout:
187 condition()
188 return
189 time.sleep(self.build_interval)
James E. Blaire6d8ee12013-01-18 21:33:45 +0000190
191
Attila Fazekas3dcdae12013-02-14 12:50:04 +0100192class BaseVolumeAdminTest(BaseVolumeTest):
193 """Base test case class for all Volume Admin API tests."""
James E. Blaire6d8ee12013-01-18 21:33:45 +0000194 @classmethod
195 def setUpClass(cls):
Attila Fazekas3dcdae12013-02-14 12:50:04 +0100196 super(BaseVolumeAdminTest, cls).setUpClass()
197 cls.adm_user = cls.config.identity.admin_username
198 cls.adm_pass = cls.config.identity.admin_password
199 cls.adm_tenant = cls.config.identity.admin_tenant_name
200 if not all((cls.adm_user, cls.adm_pass, cls.adm_tenant)):
201 msg = ("Missing Volume Admin API credentials "
202 "in configuration.")
203 raise cls.skipException(msg)
James E. Blaire6d8ee12013-01-18 21:33:45 +0000204
Attila Fazekas3dcdae12013-02-14 12:50:04 +0100205 cls.os_adm = clients.AdminManager(interface=cls._interface)
206 cls.client = cls.os_adm.volume_types_client