blob: ea6bd2e38f5a0e86fbd4d039f02e3d9821f00b2a [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
Dan Smith51c56f22023-04-27 15:49:10 -0700141 if wait_until == 'SSHABLE' and not kwargs.get('validation_resources'):
142 kwargs['validation_resources'] = (
143 self.get_test_validation_resources(self.os_primary))
144 kwargs['validatable'] = True
145
ghanshyamdae4b6d2018-01-12 07:49:15 +0000146 tenant_network = self.get_tenant_network()
147 body, _ = compute.create_test_server(
148 self.os_primary,
149 tenant_network=tenant_network,
150 name=name,
151 wait_until=wait_until,
152 **kwargs)
153
154 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
155 waiters.wait_for_server_termination,
156 self.os_primary.servers_client, body['id'])
157 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
158 self.os_primary.servers_client.delete_server,
159 body['id'])
160 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300161
162
163class BaseVolumeAdminTest(BaseVolumeTest):
164 """Base test case class for all Volume Admin API tests."""
165
166 credentials = ['primary', 'admin']
167
168 @classmethod
169 def setup_clients(cls):
170 super(BaseVolumeAdminTest, cls).setup_clients()
171
172 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300173 cls.admin_backups_client = cls.os_admin.backups_client_latest
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400174 cls.admin_volumes_client = cls.os_admin.volumes_client_latest
175
176 @classmethod
177 def create_volume_type(cls, name=None, **kwargs):
178 """Create a test volume-type"""
179
180 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
181 volume_type = cls.admin_volume_types_client.create_volume_type(
182 name=name, **kwargs)['volume_type']
183 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
184 return volume_type
185
186 @classmethod
187 def _clear_volume_type(cls, volume_type):
188 # If image caching is enabled, we must delete the cached volume
189 # before cinder will allow us to delete the volume_type. This function
190 # solves that problem by taking the brute-force approach of deleting
191 # any volumes of this volume_type that exist *no matter what project
192 # they are in*. Since this won't happen until the teardown of the
193 # test class, that should be OK.
194 type_id = volume_type['id']
195 type_name = volume_type['name']
196
197 volumes = cls.admin_volumes_client.list_volumes(
198 detail=True, params={'all_tenants': 1})['volumes']
199 for volume in [v for v in volumes if v['volume_type'] == type_name]:
200 test_utils.call_and_ignore_notfound_exc(
201 cls.admin_volumes_client.delete_volume, volume['id'])
202 cls.admin_volumes_client.wait_for_resource_deletion(volume['id'])
203
204 test_utils.call_and_ignore_notfound_exc(
205 cls.admin_volume_types_client.delete_volume_type, type_id)
206 test_utils.call_and_ignore_notfound_exc(
207 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400208
209
210class CreateMultipleResourceTest(BaseVolumeTest):
211
212 def _create_multiple_resource(self, callback, repeat_count=5,
213 **kwargs):
214
215 res = []
216 for _ in range(repeat_count):
217 res.append(callback(**kwargs)['id'])
218 return res
219
220 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
221
222 for r in wait_list:
223 callback(resource_id=r, **kwargs)