blob: c0f53bd381f8a56bbe1d384513d48fa01f872dee [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
ghanshyamdae4b6d2018-01-12 07:49:15 +000053
54 @classmethod
55 def setup_credentials(cls):
56 cls.set_network_resources()
57 super(BaseVolumeTest, cls).setup_credentials()
58
59 def setUp(self):
60 super(BaseVolumeTest, self).setUp()
ghanshyamdae4b6d2018-01-12 07:49:15 +000061
62 @classmethod
63 def resource_setup(cls):
64 super(BaseVolumeTest, cls).resource_setup()
65 cls.request_microversion = (
66 api_version_utils.select_request_microversion(
67 cls.min_microversion,
68 CONF.volume.min_microversion))
Ghanshyam Mann89927702023-09-20 11:16:02 -070069 cls.setup_api_microversion_fixture(
70 volume_microversion=cls.request_microversion)
ghanshyamdae4b6d2018-01-12 07:49:15 +000071
72 @classmethod
73 def create_volume(cls, wait_until='available', **kwargs):
74 """Wrapper utility that returns a test volume.
75
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040076 :param wait_until: wait till volume status, None means no wait.
ghanshyamdae4b6d2018-01-12 07:49:15 +000077 """
78 if 'size' not in kwargs:
79 kwargs['size'] = CONF.volume.volume_size
80
81 if 'imageRef' in kwargs:
82 image = cls.os_primary.image_client_v2.show_image(
83 kwargs['imageRef'])
84 min_disk = image['min_disk']
85 kwargs['size'] = max(kwargs['size'], min_disk)
86
87 if 'name' not in kwargs:
88 name = data_utils.rand_name(cls.__name__ + '-Volume')
89 kwargs['name'] = name
90
91 volume = cls.volumes_client.create_volume(**kwargs)['volume']
92 cls.addClassResourceCleanup(
93 cls.volumes_client.wait_for_resource_deletion, volume['id'])
94 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
95 cls.volumes_client.delete_volume,
96 volume['id'])
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040097 if wait_until:
98 waiters.wait_for_volume_resource_status(cls.volumes_client,
99 volume['id'], wait_until)
ghanshyamdae4b6d2018-01-12 07:49:15 +0000100 return volume
101
102 @classmethod
103 def create_snapshot(cls, volume_id=1, **kwargs):
104 """Wrapper utility that returns a test snapshot."""
105 if 'name' not in kwargs:
106 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
107 kwargs['name'] = name
108
109 snapshot = cls.snapshots_client.create_snapshot(
110 volume_id=volume_id, **kwargs)['snapshot']
111 cls.addClassResourceCleanup(
112 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
113 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
114 cls.snapshots_client.delete_snapshot,
115 snapshot['id'])
116 waiters.wait_for_volume_resource_status(cls.snapshots_client,
117 snapshot['id'], 'available')
118 return snapshot
119
120 def create_backup(self, volume_id, backup_client=None, **kwargs):
121 """Wrapper utility that returns a test backup."""
122 if backup_client is None:
123 backup_client = self.backups_client
124 if 'name' not in kwargs:
125 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
126 kwargs['name'] = name
127
128 backup = backup_client.create_backup(
129 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000130 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
131 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
132 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000133 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
134 'available')
135 return backup
136
137 def create_server(self, wait_until='ACTIVE', **kwargs):
138 name = kwargs.pop(
139 'name',
140 data_utils.rand_name(self.__class__.__name__ + '-instance'))
141
Dan Smith51c56f22023-04-27 15:49:10 -0700142 if wait_until == 'SSHABLE' and not kwargs.get('validation_resources'):
143 kwargs['validation_resources'] = (
144 self.get_test_validation_resources(self.os_primary))
145 kwargs['validatable'] = True
146
ghanshyamdae4b6d2018-01-12 07:49:15 +0000147 tenant_network = self.get_tenant_network()
148 body, _ = compute.create_test_server(
149 self.os_primary,
150 tenant_network=tenant_network,
151 name=name,
152 wait_until=wait_until,
153 **kwargs)
154
155 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
156 waiters.wait_for_server_termination,
157 self.os_primary.servers_client, body['id'])
158 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
159 self.os_primary.servers_client.delete_server,
160 body['id'])
161 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300162
Eric Harney5a5e1bf2024-03-29 09:38:53 -0400163 @classmethod
164 def create_image_with_data(cls, **kwargs):
165 # we do this as a class method so we can use the
166 # addClassResourceCleanup functionality of tempest.test.BaseTestCase
167 images_client = cls.os_primary.image_client_v2
168 if 'min_disk' not in kwargs:
169 kwargs['min_disk'] = 1
170 response = images_client.create_image(**kwargs)
171 image_id = response['id']
172 cls.addClassResourceCleanup(
173 images_client.wait_for_resource_deletion, image_id)
174 cls.addClassResourceCleanup(
175 test_utils.call_and_ignore_notfound_exc,
176 images_client.delete_image, image_id)
177
178 # upload "data" to image
179 image_file = io.BytesIO(data_utils.random_bytes(size=1024))
180 images_client.store_image_file(image_id, image_file)
181
182 waiters.wait_for_image_status(images_client, image_id, 'active')
183 image = images_client.show_image(image_id)
184 return image
185
Miriam Yumifa8791e2018-09-04 14:42:14 -0300186
187class BaseVolumeAdminTest(BaseVolumeTest):
188 """Base test case class for all Volume Admin API tests."""
189
190 credentials = ['primary', 'admin']
191
192 @classmethod
193 def setup_clients(cls):
194 super(BaseVolumeAdminTest, cls).setup_clients()
195
196 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300197 cls.admin_backups_client = cls.os_admin.backups_client_latest
Luigi Toscano06201382022-05-06 00:23:06 +0200198 cls.admin_volume_client = cls.os_admin.volumes_client_latest
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400199
200 @classmethod
201 def create_volume_type(cls, name=None, **kwargs):
202 """Create a test volume-type"""
203
204 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
205 volume_type = cls.admin_volume_types_client.create_volume_type(
206 name=name, **kwargs)['volume_type']
207 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
208 return volume_type
209
210 @classmethod
211 def _clear_volume_type(cls, volume_type):
212 # If image caching is enabled, we must delete the cached volume
213 # before cinder will allow us to delete the volume_type. This function
214 # solves that problem by taking the brute-force approach of deleting
215 # any volumes of this volume_type that exist *no matter what project
216 # they are in*. Since this won't happen until the teardown of the
217 # test class, that should be OK.
218 type_id = volume_type['id']
219 type_name = volume_type['name']
220
Luigi Toscano06201382022-05-06 00:23:06 +0200221 volumes = cls.admin_volume_client.list_volumes(
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400222 detail=True, params={'all_tenants': 1})['volumes']
223 for volume in [v for v in volumes if v['volume_type'] == type_name]:
224 test_utils.call_and_ignore_notfound_exc(
Luigi Toscano06201382022-05-06 00:23:06 +0200225 cls.admin_volume_client.delete_volume, volume['id'])
226 cls.admin_volume_client.wait_for_resource_deletion(volume['id'])
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400227
228 test_utils.call_and_ignore_notfound_exc(
229 cls.admin_volume_types_client.delete_volume_type, type_id)
230 test_utils.call_and_ignore_notfound_exc(
231 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400232
233
234class CreateMultipleResourceTest(BaseVolumeTest):
235
236 def _create_multiple_resource(self, callback, repeat_count=5,
237 **kwargs):
238
239 res = []
240 for _ in range(repeat_count):
241 res.append(callback(**kwargs)['id'])
242 return res
243
244 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
245
246 for r in wait_list:
247 callback(resource_id=r, **kwargs)