ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 1 | # 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 Harney | 5a5e1bf | 2024-03-29 09:38:53 -0400 | [diff] [blame] | 16 | import io |
| 17 | |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 18 | from tempest.common import compute |
| 19 | from tempest.common import waiters |
| 20 | from tempest import config |
| 21 | from tempest.lib.common import api_version_utils |
| 22 | from tempest.lib.common.utils import data_utils |
| 23 | from tempest.lib.common.utils import test_utils |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 24 | from tempest import test |
| 25 | |
| 26 | CONF = config.CONF |
| 27 | |
| 28 | |
| 29 | class BaseVolumeTest(api_version_utils.BaseMicroversionTest, |
| 30 | test.BaseTestCase): |
| 31 | """Base test case class for all Cinder API tests.""" |
| 32 | |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 33 | 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) |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 42 | |
| 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 Toscano | 6f0b4fc | 2019-12-12 19:27:30 +0100 | [diff] [blame] | 50 | 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 Toscano | 545a887 | 2024-09-20 15:24:47 +0200 | [diff] [blame] | 53 | cls.volume_revert_client = ( |
| 54 | cls.os_primary.volume_revert_v3.VolumeRevertClient() |
| 55 | ) |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 56 | |
| 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() |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 64 | |
| 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 Mann | 8992770 | 2023-09-20 11:16:02 -0700 | [diff] [blame] | 72 | cls.setup_api_microversion_fixture( |
| 73 | volume_microversion=cls.request_microversion) |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 74 | |
| 75 | @classmethod |
| 76 | def create_volume(cls, wait_until='available', **kwargs): |
| 77 | """Wrapper utility that returns a test volume. |
| 78 | |
Rajat Dhasmana | f6b9cf6 | 2021-06-10 05:41:37 -0400 | [diff] [blame] | 79 | :param wait_until: wait till volume status, None means no wait. |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 80 | """ |
| 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 Dhasmana | f6b9cf6 | 2021-06-10 05:41:37 -0400 | [diff] [blame] | 100 | if wait_until: |
| 101 | waiters.wait_for_volume_resource_status(cls.volumes_client, |
| 102 | volume['id'], wait_until) |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 103 | 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 witt | c96757b | 2018-05-31 00:05:00 +0000 | [diff] [blame] | 133 | 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']) |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 136 | 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 Smith | 51c56f2 | 2023-04-27 15:49:10 -0700 | [diff] [blame] | 145 | 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 | |
ghanshyam | dae4b6d | 2018-01-12 07:49:15 +0000 | [diff] [blame] | 150 | 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 Yumi | fa8791e | 2018-09-04 14:42:14 -0300 | [diff] [blame] | 165 | |
Eric Harney | 5a5e1bf | 2024-03-29 09:38:53 -0400 | [diff] [blame] | 166 | @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 Yumi | fa8791e | 2018-09-04 14:42:14 -0300 | [diff] [blame] | 189 | |
| 190 | class 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 Barbieri | e2eeff1 | 2020-05-14 17:40:57 -0300 | [diff] [blame] | 200 | cls.admin_backups_client = cls.os_admin.backups_client_latest |
Luigi Toscano | 0620138 | 2022-05-06 00:23:06 +0200 | [diff] [blame] | 201 | cls.admin_volume_client = cls.os_admin.volumes_client_latest |
Luigi Toscano | 545a887 | 2024-09-20 15:24:47 +0200 | [diff] [blame] | 202 | cls.admin_consistencygroups_client = ( |
| 203 | cls.os_admin.consistencygroups_v3.ConsistencyGroupsClient() |
| 204 | ) |
Brian Rosmaita | a75d5e5 | 2020-06-22 17:12:51 -0400 | [diff] [blame] | 205 | |
| 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 Toscano | 0620138 | 2022-05-06 00:23:06 +0200 | [diff] [blame] | 227 | volumes = cls.admin_volume_client.list_volumes( |
Brian Rosmaita | a75d5e5 | 2020-06-22 17:12:51 -0400 | [diff] [blame] | 228 | 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 Toscano | 0620138 | 2022-05-06 00:23:06 +0200 | [diff] [blame] | 231 | cls.admin_volume_client.delete_volume, volume['id']) |
| 232 | cls.admin_volume_client.wait_for_resource_deletion(volume['id']) |
Brian Rosmaita | a75d5e5 | 2020-06-22 17:12:51 -0400 | [diff] [blame] | 233 | |
| 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 Dhasmana | f6b9cf6 | 2021-06-10 05:41:37 -0400 | [diff] [blame] | 238 | |
| 239 | |
| 240 | class 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) |