blob: f948a93279d42edf599050add8beddb5ba3da381 [file] [log] [blame]
ghanshyamdae4b6d2018-01-12 07:49:15 +00001# Copyright 2017 NEC Corporation.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
ghanshyamdae4b6d2018-01-12 07:49:15 +000016from tempest.common import compute
17from tempest.common import waiters
18from tempest import config
whoami-rajat56ace0a2022-04-22 14:23:57 +053019from tempest.lib.common import api_microversion_fixture
ghanshyamdae4b6d2018-01-12 07:49:15 +000020from tempest.lib.common import api_version_utils
21from tempest.lib.common.utils import data_utils
22from tempest.lib.common.utils import test_utils
ghanshyamdae4b6d2018-01-12 07:49:15 +000023from tempest import test
24
25CONF = config.CONF
26
27
28class BaseVolumeTest(api_version_utils.BaseMicroversionTest,
29 test.BaseTestCase):
30 """Base test case class for all Cinder API tests."""
31
ghanshyamdae4b6d2018-01-12 07:49:15 +000032 credentials = ['primary']
33
34 @classmethod
35 def skip_checks(cls):
36 super(BaseVolumeTest, cls).skip_checks()
37
38 if not CONF.service_available.cinder:
39 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
40 raise cls.skipException(skip_msg)
ghanshyamdae4b6d2018-01-12 07:49:15 +000041
42 api_version_utils.check_skip_with_microversion(
43 cls.min_microversion, cls.max_microversion,
44 CONF.volume.min_microversion, CONF.volume.max_microversion)
45
46 @classmethod
47 def setup_clients(cls):
48 super(BaseVolumeTest, cls).setup_clients()
Luigi Toscano6f0b4fc2019-12-12 19:27:30 +010049 cls.backups_client = cls.os_primary.backups_client_latest
50 cls.volumes_client = cls.os_primary.volumes_client_latest
51 cls.snapshots_client = cls.os_primary.snapshots_client_latest
ghanshyamdae4b6d2018-01-12 07:49:15 +000052
53 @classmethod
54 def setup_credentials(cls):
55 cls.set_network_resources()
56 super(BaseVolumeTest, cls).setup_credentials()
57
58 def setUp(self):
59 super(BaseVolumeTest, self).setUp()
60 self.useFixture(api_microversion_fixture.APIMicroversionFixture(
whoami-rajat56ace0a2022-04-22 14:23:57 +053061 volume_microversion=self.request_microversion))
ghanshyamdae4b6d2018-01-12 07:49:15 +000062
63 @classmethod
64 def resource_setup(cls):
65 super(BaseVolumeTest, cls).resource_setup()
66 cls.request_microversion = (
67 api_version_utils.select_request_microversion(
68 cls.min_microversion,
69 CONF.volume.min_microversion))
70
71 @classmethod
72 def create_volume(cls, wait_until='available', **kwargs):
73 """Wrapper utility that returns a test volume.
74
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040075 :param wait_until: wait till volume status, None means no wait.
ghanshyamdae4b6d2018-01-12 07:49:15 +000076 """
77 if 'size' not in kwargs:
78 kwargs['size'] = CONF.volume.volume_size
79
80 if 'imageRef' in kwargs:
81 image = cls.os_primary.image_client_v2.show_image(
82 kwargs['imageRef'])
83 min_disk = image['min_disk']
84 kwargs['size'] = max(kwargs['size'], min_disk)
85
86 if 'name' not in kwargs:
87 name = data_utils.rand_name(cls.__name__ + '-Volume')
88 kwargs['name'] = name
89
90 volume = cls.volumes_client.create_volume(**kwargs)['volume']
91 cls.addClassResourceCleanup(
92 cls.volumes_client.wait_for_resource_deletion, volume['id'])
93 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
94 cls.volumes_client.delete_volume,
95 volume['id'])
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040096 if wait_until:
97 waiters.wait_for_volume_resource_status(cls.volumes_client,
98 volume['id'], wait_until)
ghanshyamdae4b6d2018-01-12 07:49:15 +000099 return volume
100
101 @classmethod
102 def create_snapshot(cls, volume_id=1, **kwargs):
103 """Wrapper utility that returns a test snapshot."""
104 if 'name' not in kwargs:
105 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
106 kwargs['name'] = name
107
108 snapshot = cls.snapshots_client.create_snapshot(
109 volume_id=volume_id, **kwargs)['snapshot']
110 cls.addClassResourceCleanup(
111 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
112 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
113 cls.snapshots_client.delete_snapshot,
114 snapshot['id'])
115 waiters.wait_for_volume_resource_status(cls.snapshots_client,
116 snapshot['id'], 'available')
117 return snapshot
118
119 def create_backup(self, volume_id, backup_client=None, **kwargs):
120 """Wrapper utility that returns a test backup."""
121 if backup_client is None:
122 backup_client = self.backups_client
123 if 'name' not in kwargs:
124 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
125 kwargs['name'] = name
126
127 backup = backup_client.create_backup(
128 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000129 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
130 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
131 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000132 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
133 'available')
134 return backup
135
136 def create_server(self, wait_until='ACTIVE', **kwargs):
137 name = kwargs.pop(
138 'name',
139 data_utils.rand_name(self.__class__.__name__ + '-instance'))
140
141 tenant_network = self.get_tenant_network()
142 body, _ = compute.create_test_server(
143 self.os_primary,
144 tenant_network=tenant_network,
145 name=name,
146 wait_until=wait_until,
147 **kwargs)
148
149 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
150 waiters.wait_for_server_termination,
151 self.os_primary.servers_client, body['id'])
152 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
153 self.os_primary.servers_client.delete_server,
154 body['id'])
155 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300156
157
158class BaseVolumeAdminTest(BaseVolumeTest):
159 """Base test case class for all Volume Admin API tests."""
160
161 credentials = ['primary', 'admin']
162
163 @classmethod
164 def setup_clients(cls):
165 super(BaseVolumeAdminTest, cls).setup_clients()
166
167 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300168 cls.admin_backups_client = cls.os_admin.backups_client_latest
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400169 cls.admin_volumes_client = cls.os_admin.volumes_client_latest
170
171 @classmethod
172 def create_volume_type(cls, name=None, **kwargs):
173 """Create a test volume-type"""
174
175 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
176 volume_type = cls.admin_volume_types_client.create_volume_type(
177 name=name, **kwargs)['volume_type']
178 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
179 return volume_type
180
181 @classmethod
182 def _clear_volume_type(cls, volume_type):
183 # If image caching is enabled, we must delete the cached volume
184 # before cinder will allow us to delete the volume_type. This function
185 # solves that problem by taking the brute-force approach of deleting
186 # any volumes of this volume_type that exist *no matter what project
187 # they are in*. Since this won't happen until the teardown of the
188 # test class, that should be OK.
189 type_id = volume_type['id']
190 type_name = volume_type['name']
191
192 volumes = cls.admin_volumes_client.list_volumes(
193 detail=True, params={'all_tenants': 1})['volumes']
194 for volume in [v for v in volumes if v['volume_type'] == type_name]:
195 test_utils.call_and_ignore_notfound_exc(
196 cls.admin_volumes_client.delete_volume, volume['id'])
197 cls.admin_volumes_client.wait_for_resource_deletion(volume['id'])
198
199 test_utils.call_and_ignore_notfound_exc(
200 cls.admin_volume_types_client.delete_volume_type, type_id)
201 test_utils.call_and_ignore_notfound_exc(
202 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400203
204
205class CreateMultipleResourceTest(BaseVolumeTest):
206
207 def _create_multiple_resource(self, callback, repeat_count=5,
208 **kwargs):
209
210 res = []
211 for _ in range(repeat_count):
212 res.append(callback(**kwargs)['id'])
213 return res
214
215 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
216
217 for r in wait_list:
218 callback(resource_id=r, **kwargs)