blob: 6fc6be531eb22a68b9c143881c9527893536774c [file] [log] [blame]
Yosi Ben Shimonbbb3cd62023-03-26 17:55:04 +03001
Lance Bragstadd3fddec2021-02-16 16:27:13 +00002# 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 Shamesde601ae2023-02-07 17:34:03 +000014from tempest.common import waiters
Lance Bragstadd3fddec2021-02-16 16:27:13 +000015from tempest import config
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030016from tempest.lib.common import api_version_utils
Evelina Shamesde601ae2023-02-07 17:34:03 +000017from tempest.lib.common.utils import data_utils
18from tempest.lib.common.utils import test_utils
19from tempest.lib.decorators import cleanup_order
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030020from tempest import test
Lance Bragstadd3fddec2021-02-16 16:27:13 +000021
22CONF = config.CONF
23
24
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030025class VolumeV3RbacBaseTests(
26 api_version_utils.BaseMicroversionTest, test.BaseTestCase
27):
Lance Bragstadd3fddec2021-02-16 16:27:13 +000028 identity_version = 'v3'
29
30 @classmethod
31 def skip_checks(cls):
32 super(VolumeV3RbacBaseTests, cls).skip_checks()
33 if not CONF.enforce_scope.cinder:
34 raise cls.skipException(
35 "Tempest is not configured to enforce_scope for cinder, "
36 "skipping RBAC tests. To enable these tests set "
37 "`tempest.conf [enforce_scope] cinder=True`."
38 )
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030039 if not CONF.service_available.cinder:
40 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
41 raise cls.skipException(skip_msg)
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030042 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_credentials(cls):
48 cls.set_network_resources()
49 super(VolumeV3RbacBaseTests, cls).setup_credentials()
50
51 def setUp(self):
52 super(VolumeV3RbacBaseTests, self).setUp()
Yosi Ben Shimon490c8252023-04-24 17:16:51 +030053
54 @classmethod
55 def resource_setup(cls):
56 super(VolumeV3RbacBaseTests, cls).resource_setup()
57 cls.request_microversion = (
58 api_version_utils.select_request_microversion(
59 cls.min_microversion,
60 CONF.volume.min_microversion))
Ghanshyam Mann89927702023-09-20 11:16:02 -070061 cls.setup_api_microversion_fixture(
62 volume_microversion=cls.request_microversion)
Lance Bragstadd3fddec2021-02-16 16:27:13 +000063
64 def do_request(self, method, expected_status=200, client=None, **payload):
Evelina Shamesde601ae2023-02-07 17:34:03 +000065 """Perform API call
66
67 Args:
68 method: Name of the API call
69 expected_status: HTTP desired response code
70 client: Client object if exists, None otherwise
71 payload: API call required parameters
72
73 Returns:
74 HTTP response
75 """
Lance Bragstadd3fddec2021-02-16 16:27:13 +000076 if not client:
77 client = self.client
78 if isinstance(expected_status, type(Exception)):
79 self.assertRaises(expected_status,
80 getattr(client, method),
81 **payload)
82 else:
83 response = getattr(client, method)(**payload)
84 self.assertEqual(response.response.status, expected_status)
85 return response
Evelina Shamesde601ae2023-02-07 17:34:03 +000086
87 @cleanup_order
88 def create_volume(self, client, **kwargs):
89 """Wrapper utility that returns a test volume
90
91 Args:
92 client: Client object
93
94 Returns:
95 ID of the created volume
96 """
97 kwargs['size'] = CONF.volume.volume_size
98 kwargs['name'] = data_utils.rand_name(
99 VolumeV3RbacBaseTests.__name__ + '-Volume'
100 )
101
102 volume_id = client.create_volume(**kwargs)['volume']['id']
103 self.cleanup(
104 test_utils.call_and_ignore_notfound_exc, func=self.delete_resource,
105 client=client, volume_id=volume_id
106 )
107 waiters.wait_for_volume_resource_status(
108 client=client, resource_id=volume_id, status='available'
109 )
110
111 return volume_id
112
113 @cleanup_order
114 def create_snapshot(self, client, volume_id, cleanup=True, **kwargs):
115 """Wrapper utility that returns a test snapshot.
116
117 Args:
118 client: Client object
119 volume_id: ID of the volume
120 cleanup: Boolean if should delete the snapshot
121
122 Returns:
123 ID of the created snapshot
124 """
125 kwargs['name'] = data_utils.rand_name(
126 VolumeV3RbacBaseTests.__name__ + '-Snapshot'
127 )
128
129 snapshot_id = client.create_snapshot(
130 volume_id=volume_id, **kwargs)['snapshot']['id']
131 if cleanup:
132 self.cleanup(
133 test_utils.call_and_ignore_notfound_exc,
134 func=self.delete_resource,
135 client=client, snapshot_id=snapshot_id
136 )
137 waiters.wait_for_volume_resource_status(
138 client=client, resource_id=snapshot_id, status='available'
139 )
140
141 return snapshot_id
142
143 @classmethod
144 def delete_resource(cls, client, **kwargs):
145 """Delete a resource by a given client
146
147 Args:
148 client: Client object
149
150 Keyword Args:
151 snapshot_id: ID of a snapshot
152 volume_id: ID of a volume
153 """
154 key, resource_id = list(kwargs.items())[0]
155 resource_name = key.split('_')[0]
156
157 del_action = getattr(client, f'delete_{resource_name}')
158 test_utils.call_and_ignore_notfound_exc(del_action, resource_id)
159 test_utils.call_and_ignore_notfound_exc(
160 client.wait_for_resource_deletion, resource_id)
Yosi Ben Shimonbbb3cd62023-03-26 17:55:04 +0300161
162 @classmethod
163 def create_backup(
164 cls, volume_id, backup_client=None, add_cleanup=True, **kwargs
165 ):
166 """Wrapper utility that returns a test backup."""
167 if backup_client is None:
168 backup_client = cls.backups_client
169 if 'name' not in kwargs:
170 name = data_utils.rand_name(cls.__class__.__name__ + '-Backup')
171 kwargs['name'] = name
172
173 backup = backup_client.create_backup(
174 volume_id=volume_id, **kwargs
175 )['backup']
176 if add_cleanup:
177 cls.addClassResourceCleanup(
178 test_utils.call_and_ignore_notfound_exc,
179 cls.delete_resource,
180 client=backup_client,
181 backup_id=backup['id']
182 )
183 waiters.wait_for_volume_resource_status(
184 backup_client, backup['id'], 'available'
185 )
186 return backup