Yosi Ben Shimon | bbb3cd6 | 2023-03-26 17:55:04 +0300 | [diff] [blame] | 1 | |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 2 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | # not use this file except in compliance with the License. You may obtain |
| 4 | # a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | # License for the specific language governing permissions and limitations |
| 12 | # under the License. |
| 13 | |
Evelina Shames | de601ae | 2023-02-07 17:34:03 +0000 | [diff] [blame] | 14 | from tempest.common import waiters |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 15 | from tempest import config |
Yosi Ben Shimon | 490c825 | 2023-04-24 17:16:51 +0300 | [diff] [blame] | 16 | from tempest.lib.common import api_microversion_fixture |
| 17 | from tempest.lib.common import api_version_utils |
Evelina Shames | de601ae | 2023-02-07 17:34:03 +0000 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
| 19 | from tempest.lib.common.utils import test_utils |
| 20 | from tempest.lib.decorators import cleanup_order |
Yosi Ben Shimon | 490c825 | 2023-04-24 17:16:51 +0300 | [diff] [blame] | 21 | from tempest import test |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 22 | |
| 23 | CONF = config.CONF |
| 24 | |
| 25 | |
Yosi Ben Shimon | 490c825 | 2023-04-24 17:16:51 +0300 | [diff] [blame] | 26 | class VolumeV3RbacBaseTests( |
| 27 | api_version_utils.BaseMicroversionTest, test.BaseTestCase |
| 28 | ): |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 29 | identity_version = 'v3' |
| 30 | |
| 31 | @classmethod |
| 32 | def skip_checks(cls): |
| 33 | super(VolumeV3RbacBaseTests, cls).skip_checks() |
| 34 | if not CONF.enforce_scope.cinder: |
| 35 | raise cls.skipException( |
| 36 | "Tempest is not configured to enforce_scope for cinder, " |
| 37 | "skipping RBAC tests. To enable these tests set " |
| 38 | "`tempest.conf [enforce_scope] cinder=True`." |
| 39 | ) |
Yosi Ben Shimon | 490c825 | 2023-04-24 17:16:51 +0300 | [diff] [blame] | 40 | if not CONF.service_available.cinder: |
| 41 | skip_msg = ("%s skipped as Cinder is not available" % cls.__name__) |
| 42 | raise cls.skipException(skip_msg) |
Yosi Ben Shimon | 490c825 | 2023-04-24 17:16:51 +0300 | [diff] [blame] | 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_credentials(cls): |
| 49 | cls.set_network_resources() |
| 50 | super(VolumeV3RbacBaseTests, cls).setup_credentials() |
| 51 | |
| 52 | def setUp(self): |
| 53 | super(VolumeV3RbacBaseTests, self).setUp() |
| 54 | self.useFixture(api_microversion_fixture.APIMicroversionFixture( |
| 55 | volume_microversion=self.request_microversion)) |
| 56 | |
| 57 | @classmethod |
| 58 | def resource_setup(cls): |
| 59 | super(VolumeV3RbacBaseTests, cls).resource_setup() |
| 60 | cls.request_microversion = ( |
| 61 | api_version_utils.select_request_microversion( |
| 62 | cls.min_microversion, |
| 63 | CONF.volume.min_microversion)) |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 64 | |
| 65 | def do_request(self, method, expected_status=200, client=None, **payload): |
Evelina Shames | de601ae | 2023-02-07 17:34:03 +0000 | [diff] [blame] | 66 | """Perform API call |
| 67 | |
| 68 | Args: |
| 69 | method: Name of the API call |
| 70 | expected_status: HTTP desired response code |
| 71 | client: Client object if exists, None otherwise |
| 72 | payload: API call required parameters |
| 73 | |
| 74 | Returns: |
| 75 | HTTP response |
| 76 | """ |
Lance Bragstad | d3fddec | 2021-02-16 16:27:13 +0000 | [diff] [blame] | 77 | if not client: |
| 78 | client = self.client |
| 79 | if isinstance(expected_status, type(Exception)): |
| 80 | self.assertRaises(expected_status, |
| 81 | getattr(client, method), |
| 82 | **payload) |
| 83 | else: |
| 84 | response = getattr(client, method)(**payload) |
| 85 | self.assertEqual(response.response.status, expected_status) |
| 86 | return response |
Evelina Shames | de601ae | 2023-02-07 17:34:03 +0000 | [diff] [blame] | 87 | |
| 88 | @cleanup_order |
| 89 | def create_volume(self, client, **kwargs): |
| 90 | """Wrapper utility that returns a test volume |
| 91 | |
| 92 | Args: |
| 93 | client: Client object |
| 94 | |
| 95 | Returns: |
| 96 | ID of the created volume |
| 97 | """ |
| 98 | kwargs['size'] = CONF.volume.volume_size |
| 99 | kwargs['name'] = data_utils.rand_name( |
| 100 | VolumeV3RbacBaseTests.__name__ + '-Volume' |
| 101 | ) |
| 102 | |
| 103 | volume_id = client.create_volume(**kwargs)['volume']['id'] |
| 104 | self.cleanup( |
| 105 | test_utils.call_and_ignore_notfound_exc, func=self.delete_resource, |
| 106 | client=client, volume_id=volume_id |
| 107 | ) |
| 108 | waiters.wait_for_volume_resource_status( |
| 109 | client=client, resource_id=volume_id, status='available' |
| 110 | ) |
| 111 | |
| 112 | return volume_id |
| 113 | |
| 114 | @cleanup_order |
| 115 | def create_snapshot(self, client, volume_id, cleanup=True, **kwargs): |
| 116 | """Wrapper utility that returns a test snapshot. |
| 117 | |
| 118 | Args: |
| 119 | client: Client object |
| 120 | volume_id: ID of the volume |
| 121 | cleanup: Boolean if should delete the snapshot |
| 122 | |
| 123 | Returns: |
| 124 | ID of the created snapshot |
| 125 | """ |
| 126 | kwargs['name'] = data_utils.rand_name( |
| 127 | VolumeV3RbacBaseTests.__name__ + '-Snapshot' |
| 128 | ) |
| 129 | |
| 130 | snapshot_id = client.create_snapshot( |
| 131 | volume_id=volume_id, **kwargs)['snapshot']['id'] |
| 132 | if cleanup: |
| 133 | self.cleanup( |
| 134 | test_utils.call_and_ignore_notfound_exc, |
| 135 | func=self.delete_resource, |
| 136 | client=client, snapshot_id=snapshot_id |
| 137 | ) |
| 138 | waiters.wait_for_volume_resource_status( |
| 139 | client=client, resource_id=snapshot_id, status='available' |
| 140 | ) |
| 141 | |
| 142 | return snapshot_id |
| 143 | |
| 144 | @classmethod |
| 145 | def delete_resource(cls, client, **kwargs): |
| 146 | """Delete a resource by a given client |
| 147 | |
| 148 | Args: |
| 149 | client: Client object |
| 150 | |
| 151 | Keyword Args: |
| 152 | snapshot_id: ID of a snapshot |
| 153 | volume_id: ID of a volume |
| 154 | """ |
| 155 | key, resource_id = list(kwargs.items())[0] |
| 156 | resource_name = key.split('_')[0] |
| 157 | |
| 158 | del_action = getattr(client, f'delete_{resource_name}') |
| 159 | test_utils.call_and_ignore_notfound_exc(del_action, resource_id) |
| 160 | test_utils.call_and_ignore_notfound_exc( |
| 161 | client.wait_for_resource_deletion, resource_id) |
Yosi Ben Shimon | bbb3cd6 | 2023-03-26 17:55:04 +0300 | [diff] [blame] | 162 | |
| 163 | @classmethod |
| 164 | def create_backup( |
| 165 | cls, volume_id, backup_client=None, add_cleanup=True, **kwargs |
| 166 | ): |
| 167 | """Wrapper utility that returns a test backup.""" |
| 168 | if backup_client is None: |
| 169 | backup_client = cls.backups_client |
| 170 | if 'name' not in kwargs: |
| 171 | name = data_utils.rand_name(cls.__class__.__name__ + '-Backup') |
| 172 | kwargs['name'] = name |
| 173 | |
| 174 | backup = backup_client.create_backup( |
| 175 | volume_id=volume_id, **kwargs |
| 176 | )['backup'] |
| 177 | if add_cleanup: |
| 178 | cls.addClassResourceCleanup( |
| 179 | test_utils.call_and_ignore_notfound_exc, |
| 180 | cls.delete_resource, |
| 181 | client=backup_client, |
| 182 | backup_id=backup['id'] |
| 183 | ) |
| 184 | waiters.wait_for_volume_resource_status( |
| 185 | backup_client, backup['id'], 'available' |
| 186 | ) |
| 187 | return backup |