blob: 76d03831edb4e51ece8c48fdbf61f7ec358dd5a2 [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
19from tempest.lib.common import api_version_utils
20from tempest.lib.common.utils import data_utils
21from tempest.lib.common.utils import test_utils
ghanshyamdae4b6d2018-01-12 07:49:15 +000022from tempest import test
23
24CONF = config.CONF
25
26
27class BaseVolumeTest(api_version_utils.BaseMicroversionTest,
28 test.BaseTestCase):
29 """Base test case class for all Cinder API tests."""
30
ghanshyamdae4b6d2018-01-12 07:49:15 +000031 credentials = ['primary']
32
33 @classmethod
34 def skip_checks(cls):
35 super(BaseVolumeTest, cls).skip_checks()
36
37 if not CONF.service_available.cinder:
38 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
39 raise cls.skipException(skip_msg)
ghanshyamdae4b6d2018-01-12 07:49:15 +000040
41 api_version_utils.check_skip_with_microversion(
42 cls.min_microversion, cls.max_microversion,
43 CONF.volume.min_microversion, CONF.volume.max_microversion)
44
45 @classmethod
46 def setup_clients(cls):
47 super(BaseVolumeTest, cls).setup_clients()
Luigi Toscano6f0b4fc2019-12-12 19:27:30 +010048 cls.backups_client = cls.os_primary.backups_client_latest
49 cls.volumes_client = cls.os_primary.volumes_client_latest
50 cls.snapshots_client = cls.os_primary.snapshots_client_latest
ghanshyamdae4b6d2018-01-12 07:49:15 +000051
52 @classmethod
53 def setup_credentials(cls):
54 cls.set_network_resources()
55 super(BaseVolumeTest, cls).setup_credentials()
56
57 def setUp(self):
58 super(BaseVolumeTest, self).setUp()
ghanshyamdae4b6d2018-01-12 07:49:15 +000059
60 @classmethod
61 def resource_setup(cls):
62 super(BaseVolumeTest, cls).resource_setup()
63 cls.request_microversion = (
64 api_version_utils.select_request_microversion(
65 cls.min_microversion,
66 CONF.volume.min_microversion))
Ghanshyam Mann89927702023-09-20 11:16:02 -070067 cls.setup_api_microversion_fixture(
68 volume_microversion=cls.request_microversion)
ghanshyamdae4b6d2018-01-12 07:49:15 +000069
70 @classmethod
71 def create_volume(cls, wait_until='available', **kwargs):
72 """Wrapper utility that returns a test volume.
73
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040074 :param wait_until: wait till volume status, None means no wait.
ghanshyamdae4b6d2018-01-12 07:49:15 +000075 """
76 if 'size' not in kwargs:
77 kwargs['size'] = CONF.volume.volume_size
78
79 if 'imageRef' in kwargs:
80 image = cls.os_primary.image_client_v2.show_image(
81 kwargs['imageRef'])
82 min_disk = image['min_disk']
83 kwargs['size'] = max(kwargs['size'], min_disk)
84
85 if 'name' not in kwargs:
86 name = data_utils.rand_name(cls.__name__ + '-Volume')
87 kwargs['name'] = name
88
89 volume = cls.volumes_client.create_volume(**kwargs)['volume']
90 cls.addClassResourceCleanup(
91 cls.volumes_client.wait_for_resource_deletion, volume['id'])
92 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
93 cls.volumes_client.delete_volume,
94 volume['id'])
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -040095 if wait_until:
96 waiters.wait_for_volume_resource_status(cls.volumes_client,
97 volume['id'], wait_until)
ghanshyamdae4b6d2018-01-12 07:49:15 +000098 return volume
99
100 @classmethod
101 def create_snapshot(cls, volume_id=1, **kwargs):
102 """Wrapper utility that returns a test snapshot."""
103 if 'name' not in kwargs:
104 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
105 kwargs['name'] = name
106
107 snapshot = cls.snapshots_client.create_snapshot(
108 volume_id=volume_id, **kwargs)['snapshot']
109 cls.addClassResourceCleanup(
110 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
111 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
112 cls.snapshots_client.delete_snapshot,
113 snapshot['id'])
114 waiters.wait_for_volume_resource_status(cls.snapshots_client,
115 snapshot['id'], 'available')
116 return snapshot
117
118 def create_backup(self, volume_id, backup_client=None, **kwargs):
119 """Wrapper utility that returns a test backup."""
120 if backup_client is None:
121 backup_client = self.backups_client
122 if 'name' not in kwargs:
123 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
124 kwargs['name'] = name
125
126 backup = backup_client.create_backup(
127 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000128 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
129 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
130 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000131 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
132 'available')
133 return backup
134
135 def create_server(self, wait_until='ACTIVE', **kwargs):
136 name = kwargs.pop(
137 'name',
138 data_utils.rand_name(self.__class__.__name__ + '-instance'))
139
Dan Smith51c56f22023-04-27 15:49:10 -0700140 if wait_until == 'SSHABLE' and not kwargs.get('validation_resources'):
141 kwargs['validation_resources'] = (
142 self.get_test_validation_resources(self.os_primary))
143 kwargs['validatable'] = True
144
ghanshyamdae4b6d2018-01-12 07:49:15 +0000145 tenant_network = self.get_tenant_network()
146 body, _ = compute.create_test_server(
147 self.os_primary,
148 tenant_network=tenant_network,
149 name=name,
150 wait_until=wait_until,
151 **kwargs)
152
153 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
154 waiters.wait_for_server_termination,
155 self.os_primary.servers_client, body['id'])
156 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
157 self.os_primary.servers_client.delete_server,
158 body['id'])
159 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300160
161
162class BaseVolumeAdminTest(BaseVolumeTest):
163 """Base test case class for all Volume Admin API tests."""
164
165 credentials = ['primary', 'admin']
166
167 @classmethod
168 def setup_clients(cls):
169 super(BaseVolumeAdminTest, cls).setup_clients()
170
171 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
Rodrigo Barbierie2eeff12020-05-14 17:40:57 -0300172 cls.admin_backups_client = cls.os_admin.backups_client_latest
Brian Rosmaitaa75d5e52020-06-22 17:12:51 -0400173 cls.admin_volumes_client = cls.os_admin.volumes_client_latest
174
175 @classmethod
176 def create_volume_type(cls, name=None, **kwargs):
177 """Create a test volume-type"""
178
179 name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
180 volume_type = cls.admin_volume_types_client.create_volume_type(
181 name=name, **kwargs)['volume_type']
182 cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
183 return volume_type
184
185 @classmethod
186 def _clear_volume_type(cls, volume_type):
187 # If image caching is enabled, we must delete the cached volume
188 # before cinder will allow us to delete the volume_type. This function
189 # solves that problem by taking the brute-force approach of deleting
190 # any volumes of this volume_type that exist *no matter what project
191 # they are in*. Since this won't happen until the teardown of the
192 # test class, that should be OK.
193 type_id = volume_type['id']
194 type_name = volume_type['name']
195
196 volumes = cls.admin_volumes_client.list_volumes(
197 detail=True, params={'all_tenants': 1})['volumes']
198 for volume in [v for v in volumes if v['volume_type'] == type_name]:
199 test_utils.call_and_ignore_notfound_exc(
200 cls.admin_volumes_client.delete_volume, volume['id'])
201 cls.admin_volumes_client.wait_for_resource_deletion(volume['id'])
202
203 test_utils.call_and_ignore_notfound_exc(
204 cls.admin_volume_types_client.delete_volume_type, type_id)
205 test_utils.call_and_ignore_notfound_exc(
206 cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
Rajat Dhasmanaf6b9cf62021-06-10 05:41:37 -0400207
208
209class CreateMultipleResourceTest(BaseVolumeTest):
210
211 def _create_multiple_resource(self, callback, repeat_count=5,
212 **kwargs):
213
214 res = []
215 for _ in range(repeat_count):
216 res.append(callback(**kwargs)['id'])
217 return res
218
219 def _wait_for_multiple_resources(self, callback, wait_list, **kwargs):
220
221 for r in wait_list:
222 callback(resource_id=r, **kwargs)