blob: e049ff3b6e3f543632dffab1cd1aca46ac4b7f2a [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
Eric Harney5a5e1bf2024-03-29 09:38:53 -040016import io
17
ghanshyamdae4b6d2018-01-12 07:49:15 +000018from tempest.common import compute
19from tempest.common import waiters
20from tempest import config
21from tempest.lib.common import api_version_utils
22from tempest.lib.common.utils import data_utils
23from tempest.lib.common.utils import test_utils
ghanshyamdae4b6d2018-01-12 07:49:15 +000024from tempest import test
25
26CONF = config.CONF
27
28
29class BaseVolumeTest(api_version_utils.BaseMicroversionTest,
30 test.BaseTestCase):
31 """Base test case class for all Cinder API tests."""
32
ghanshyamdae4b6d2018-01-12 07:49:15 +000033 credentials = ['primary']
34
35 @classmethod
36 def skip_checks(cls):
37 super(BaseVolumeTest, cls).skip_checks()
38
39 if not CONF.service_available.cinder:
40 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
41 raise cls.skipException(skip_msg)
ghanshyamdae4b6d2018-01-12 07:49:15 +000042
43 api_version_utils.check_skip_with_microversion(
44 cls.min_microversion, cls.max_microversion,
45 CONF.volume.min_microversion, CONF.volume.max_microversion)
46
47 @classmethod
48 def setup_clients(cls):
49 super(BaseVolumeTest, cls).setup_clients()
Luigi Toscano6f0b4fc2019-12-12 19:27:30 +010050 cls.backups_client = cls.os_primary.backups_client_latest
51 cls.volumes_client = cls.os_primary.volumes_client_latest
52 cls.snapshots_client = cls.os_primary.snapshots_client_latest
Luigi Toscano545a8872024-09-20 15:24:47 +020053 cls.volume_revert_client = (
54 cls.os_primary.volume_revert_v3.VolumeRevertClient()
55 )
ghanshyamdae4b6d2018-01-12 07:49:15 +000056
57 @classmethod
58 def setup_credentials(cls):
59 cls.set_network_resources()
60 super(BaseVolumeTest, cls).setup_credentials()
61
62 def setUp(self):
63 super(BaseVolumeTest, self).setUp()
ghanshyamdae4b6d2018-01-12 07:49:15 +000064
65 @classmethod
66 def resource_setup(cls):
67 super(BaseVolumeTest, cls).resource_setup()
68 cls.request_microversion = (
69 api_version_utils.select_request_microversion(
70 cls.min_microversion,
71 CONF.volume.min_microversion))
Ghanshyam Mann89927702023-09-20 11:16:02 -070072 cls.setup_api_microversion_fixture(
73 volume_microversion=cls.request_microversion)
ghanshyamdae4b6d2018-01-12 07:49:15 +000074
75 @classmethod
76 def create_volume(cls, wait_until='available', **kwargs):
77 """Wrapper utility that returns a test volume.
78
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040079 :param wait_until: wait till volume status, None means no wait.
ghanshyamdae4b6d2018-01-12 07:49:15 +000080 """
81 if 'size' not in kwargs:
82 kwargs['size'] = CONF.volume.volume_size
83
84 if 'imageRef' in kwargs:
85 image = cls.os_primary.image_client_v2.show_image(
86 kwargs['imageRef'])
87 min_disk = image['min_disk']
88 kwargs['size'] = max(kwargs['size'], min_disk)
89
90 if 'name' not in kwargs:
91 name = data_utils.rand_name(cls.__name__ + '-Volume')
92 kwargs['name'] = name
93
lkuchlan2be6e102024-11-27 14:43:25 +020094 if CONF.compute.compute_volume_common_az:
95 kwargs.setdefault('availability_zone',
96 CONF.compute.compute_volume_common_az)
97
ghanshyamdae4b6d2018-01-12 07:49:15 +000098 volume = cls.volumes_client.create_volume(**kwargs)['volume']
99 cls.addClassResourceCleanup(
100 cls.volumes_client.wait_for_resource_deletion, volume['id'])
101 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
102 cls.volumes_client.delete_volume,
103 volume['id'])
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400104 if wait_until:
105 waiters.wait_for_volume_resource_status(cls.volumes_client,
106 volume['id'], wait_until)
ghanshyamdae4b6d2018-01-12 07:49:15 +0000107 return volume
108
109 @classmethod
110 def create_snapshot(cls, volume_id=1, **kwargs):
111 """Wrapper utility that returns a test snapshot."""
112 if 'name' not in kwargs:
113 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
114 kwargs['name'] = name
115
116 snapshot = cls.snapshots_client.create_snapshot(
117 volume_id=volume_id, **kwargs)['snapshot']
118 cls.addClassResourceCleanup(
119 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
120 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
121 cls.snapshots_client.delete_snapshot,
122 snapshot['id'])
123 waiters.wait_for_volume_resource_status(cls.snapshots_client,
124 snapshot['id'], 'available')
125 return snapshot
126
127 def create_backup(self, volume_id, backup_client=None, **kwargs):
128 """Wrapper utility that returns a test backup."""
129 if backup_client is None:
130 backup_client = self.backups_client
131 if 'name' not in kwargs:
132 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
133 kwargs['name'] = name
134
135 backup = backup_client.create_backup(
136 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000137 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
138 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
139 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000140 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
141 'available')
142 return backup
143
144 def create_server(self, wait_until='ACTIVE', **kwargs):
145 name = kwargs.pop(
146 'name',
147 data_utils.rand_name(self.__class__.__name__ + '-instance'))
148
Dan Smith51c56f22023-04-27 15:49:10 -0700149 if wait_until == 'SSHABLE' and not kwargs.get('validation_resources'):
150 kwargs['validation_resources'] = (
151 self.get_test_validation_resources(self.os_primary))
152 kwargs['validatable'] = True
153
ghanshyamdae4b6d2018-01-12 07:49:15 +0000154 tenant_network = self.get_tenant_network()
155 body, _ = compute.create_test_server(
156 self.os_primary,
157 tenant_network=tenant_network,
158 name=name,
159 wait_until=wait_until,
160 **kwargs)
161
162 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
163 waiters.wait_for_server_termination,
164 self.os_primary.servers_client, body['id'])
165 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
166 self.os_primary.servers_client.delete_server,
167 body['id'])
168 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300169
Eric Harney5a5e1bf2024-03-29 09:38:53 -0400170 @classmethod
171 def create_image_with_data(cls, **kwargs):
172 # we do this as a class method so we can use the
173 # addClassResourceCleanup functionality of tempest.test.BaseTestCase
174 images_client = cls.os_primary.image_client_v2
175 if 'min_disk' not in kwargs:
176 kwargs['min_disk'] = 1
177 response = images_client.create_image(**kwargs)
178 image_id = response['id']
179 cls.addClassResourceCleanup(
180 images_client.wait_for_resource_deletion, image_id)
181 cls.addClassResourceCleanup(
182 test_utils.call_and_ignore_notfound_exc,
183 images_client.delete_image, image_id)
184
185 # upload "data" to image
186 image_file = io.BytesIO(data_utils.random_bytes(size=1024))
187 images_client.store_image_file(image_id, image_file)
188
189 waiters.wait_for_image_status(images_client, image_id, 'active')
190 image = images_client.show_image(image_id)
191 return image
192
Miriam Yumifa8791e2018-09-04 14:42:14 -0300193
194class BaseVolumeAdminTest(BaseVolumeTest):
195 """Base test case class for all Volume Admin API tests."""
196
197 credentials = ['primary', 'admin']
198
199 @classmethod
200 def setup_clients(cls):
201 super(BaseVolumeAdminTest, cls).setup_clients()
202
203 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300204 cls.admin_backups_client = cls.os_admin.backups_client_latest
Luigi Toscano06201382022-05-06 00:23:06 +0200205 cls.admin_volume_client = cls.os_admin.volumes_client_latest
Luigi Toscano545a8872024-09-20 15:24:47 +0200206 cls.admin_consistencygroups_client = (
207 cls.os_admin.consistencygroups_v3.ConsistencyGroupsClient()
208 )
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400209
210 @classmethod
211 def create_volume_type(cls, name=None, **kwargs):
212 """Create a test volume-type"""
213
214 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
215 volume_type = cls.admin_volume_types_client.create_volume_type(
216 name=name, **kwargs)['volume_type']
217 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
218 return volume_type
219
220 @classmethod
221 def _clear_volume_type(cls, volume_type):
222 # If image caching is enabled, we must delete the cached volume
223 # before cinder will allow us to delete the volume_type. This function
224 # solves that problem by taking the brute-force approach of deleting
225 # any volumes of this volume_type that exist *no matter what project
226 # they are in*. Since this won't happen until the teardown of the
227 # test class, that should be OK.
228 type_id = volume_type['id']
229 type_name = volume_type['name']
230
Luigi Toscano06201382022-05-06 00:23:06 +0200231 volumes = cls.admin_volume_client.list_volumes(
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400232 detail=True, params={'all_tenants': 1})['volumes']
233 for volume in [v for v in volumes if v['volume_type'] == type_name]:
234 test_utils.call_and_ignore_notfound_exc(
Luigi Toscano06201382022-05-06 00:23:06 +0200235 cls.admin_volume_client.delete_volume, volume['id'])
236 cls.admin_volume_client.wait_for_resource_deletion(volume['id'])
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400237
238 test_utils.call_and_ignore_notfound_exc(
239 cls.admin_volume_types_client.delete_volume_type, type_id)
240 test_utils.call_and_ignore_notfound_exc(
241 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400242
243
244class CreateMultipleResourceTest(BaseVolumeTest):
245
246 def _create_multiple_resource(self, callback, repeat_count=5,
247 **kwargs):
248
249 res = []
250 for _ in range(repeat_count):
251 res.append(callback(**kwargs)['id'])
252 return res
253
254 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
255
256 for r in wait_list:
257 callback(resource_id=r, **kwargs)