blob: 1c64973d0d23e2fb5f3cd7491a816fed1cc753b2 [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
94 volume = cls.volumes_client.create_volume(**kwargs)['volume']
95 cls.addClassResourceCleanup(
96 cls.volumes_client.wait_for_resource_deletion, volume['id'])
97 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
98 cls.volumes_client.delete_volume,
99 volume['id'])
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400100 if wait_until:
101 waiters.wait_for_volume_resource_status(cls.volumes_client,
102 volume['id'], wait_until)
ghanshyamdae4b6d2018-01-12 07:49:15 +0000103 return volume
104
105 @classmethod
106 def create_snapshot(cls, volume_id=1, **kwargs):
107 """Wrapper utility that returns a test snapshot."""
108 if 'name' not in kwargs:
109 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
110 kwargs['name'] = name
111
112 snapshot = cls.snapshots_client.create_snapshot(
113 volume_id=volume_id, **kwargs)['snapshot']
114 cls.addClassResourceCleanup(
115 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
116 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
117 cls.snapshots_client.delete_snapshot,
118 snapshot['id'])
119 waiters.wait_for_volume_resource_status(cls.snapshots_client,
120 snapshot['id'], 'available')
121 return snapshot
122
123 def create_backup(self, volume_id, backup_client=None, **kwargs):
124 """Wrapper utility that returns a test backup."""
125 if backup_client is None:
126 backup_client = self.backups_client
127 if 'name' not in kwargs:
128 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
129 kwargs['name'] = name
130
131 backup = backup_client.create_backup(
132 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000133 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
134 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
135 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000136 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
137 'available')
138 return backup
139
140 def create_server(self, wait_until='ACTIVE', **kwargs):
141 name = kwargs.pop(
142 'name',
143 data_utils.rand_name(self.__class__.__name__ + '-instance'))
144
Dan Smith51c56f22023-04-27 15:49:10 -0700145 if wait_until == 'SSHABLE' and not kwargs.get('validation_resources'):
146 kwargs['validation_resources'] = (
147 self.get_test_validation_resources(self.os_primary))
148 kwargs['validatable'] = True
149
ghanshyamdae4b6d2018-01-12 07:49:15 +0000150 tenant_network = self.get_tenant_network()
151 body, _ = compute.create_test_server(
152 self.os_primary,
153 tenant_network=tenant_network,
154 name=name,
155 wait_until=wait_until,
156 **kwargs)
157
158 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
159 waiters.wait_for_server_termination,
160 self.os_primary.servers_client, body['id'])
161 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
162 self.os_primary.servers_client.delete_server,
163 body['id'])
164 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300165
Eric Harney5a5e1bf2024-03-29 09:38:53 -0400166 @classmethod
167 def create_image_with_data(cls, **kwargs):
168 # we do this as a class method so we can use the
169 # addClassResourceCleanup functionality of tempest.test.BaseTestCase
170 images_client = cls.os_primary.image_client_v2
171 if 'min_disk' not in kwargs:
172 kwargs['min_disk'] = 1
173 response = images_client.create_image(**kwargs)
174 image_id = response['id']
175 cls.addClassResourceCleanup(
176 images_client.wait_for_resource_deletion, image_id)
177 cls.addClassResourceCleanup(
178 test_utils.call_and_ignore_notfound_exc,
179 images_client.delete_image, image_id)
180
181 # upload "data" to image
182 image_file = io.BytesIO(data_utils.random_bytes(size=1024))
183 images_client.store_image_file(image_id, image_file)
184
185 waiters.wait_for_image_status(images_client, image_id, 'active')
186 image = images_client.show_image(image_id)
187 return image
188
Miriam Yumifa8791e2018-09-04 14:42:14 -0300189
190class BaseVolumeAdminTest(BaseVolumeTest):
191 """Base test case class for all Volume Admin API tests."""
192
193 credentials = ['primary', 'admin']
194
195 @classmethod
196 def setup_clients(cls):
197 super(BaseVolumeAdminTest, cls).setup_clients()
198
199 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300200 cls.admin_backups_client = cls.os_admin.backups_client_latest
Luigi Toscano06201382022-05-06 00:23:06 +0200201 cls.admin_volume_client = cls.os_admin.volumes_client_latest
Luigi Toscano545a8872024-09-20 15:24:47 +0200202 cls.admin_consistencygroups_client = (
203 cls.os_admin.consistencygroups_v3.ConsistencyGroupsClient()
204 )
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400205
206 @classmethod
207 def create_volume_type(cls, name=None, **kwargs):
208 """Create a test volume-type"""
209
210 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
211 volume_type = cls.admin_volume_types_client.create_volume_type(
212 name=name, **kwargs)['volume_type']
213 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
214 return volume_type
215
216 @classmethod
217 def _clear_volume_type(cls, volume_type):
218 # If image caching is enabled, we must delete the cached volume
219 # before cinder will allow us to delete the volume_type. This function
220 # solves that problem by taking the brute-force approach of deleting
221 # any volumes of this volume_type that exist *no matter what project
222 # they are in*. Since this won't happen until the teardown of the
223 # test class, that should be OK.
224 type_id = volume_type['id']
225 type_name = volume_type['name']
226
Luigi Toscano06201382022-05-06 00:23:06 +0200227 volumes = cls.admin_volume_client.list_volumes(
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400228 detail=True, params={'all_tenants': 1})['volumes']
229 for volume in [v for v in volumes if v['volume_type'] == type_name]:
230 test_utils.call_and_ignore_notfound_exc(
Luigi Toscano06201382022-05-06 00:23:06 +0200231 cls.admin_volume_client.delete_volume, volume['id'])
232 cls.admin_volume_client.wait_for_resource_deletion(volume['id'])
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400233
234 test_utils.call_and_ignore_notfound_exc(
235 cls.admin_volume_types_client.delete_volume_type, type_id)
236 test_utils.call_and_ignore_notfound_exc(
237 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400238
239
240class CreateMultipleResourceTest(BaseVolumeTest):
241
242 def _create_multiple_resource(self, callback, repeat_count=5,
243 **kwargs):
244
245 res = []
246 for _ in range(repeat_count):
247 res.append(callback(**kwargs)['id'])
248 return res
249
250 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
251
252 for r in wait_list:
253 callback(resource_id=r, **kwargs)