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