Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 1 | # Copyright 2016 Hewlett Packard Enterprise Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | from tempest import test |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 15 | from tempest import config |
Maksym Shalamov | 2da1d6e | 2018-12-05 17:17:58 +0200 | [diff] [blame] | 16 | from tempest.lib.common.utils import test_utils as utils |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 17 | |
Michael Johnson | cc8f89b | 2021-11-30 00:31:24 +0000 | [diff] [blame] | 18 | from designate_tempest_plugin.services.dns.query.query_client import ( |
| 19 | QueryClient) |
Michael Johnson | bf2379b | 2021-08-27 00:04:50 +0000 | [diff] [blame] | 20 | from designate_tempest_plugin.tests import rbac_utils |
Michael Johnson | df9fda1 | 2021-07-09 16:39:08 +0000 | [diff] [blame] | 21 | |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 22 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
| 25 | |
Dmitry Galkin | 9a0a360 | 2018-11-13 19:42:29 +0000 | [diff] [blame] | 26 | class AssertRaisesDns(test.BaseTestCase): |
Kiall Mac Innes | 4a37679 | 2016-05-19 18:05:54 +0100 | [diff] [blame] | 27 | def __init__(self, test_class, exc, type_, code): |
| 28 | self.test_class = test_class |
| 29 | self.exc = exc |
| 30 | self.type_ = type_ |
| 31 | self.code = code |
| 32 | |
| 33 | def __enter__(self): |
| 34 | pass |
| 35 | |
| 36 | def __exit__(self, exc_type, exc_value, traceback): |
| 37 | if exc_type is None: |
| 38 | try: |
| 39 | exc_name = self.exc.__name__ |
| 40 | except AttributeError: |
| 41 | exc_name = str(self.exc) |
| 42 | raise self.failureException( |
| 43 | "{0} not raised".format(exc_name)) |
| 44 | |
| 45 | if issubclass(exc_type, self.exc): |
| 46 | self.test_class.assertEqual( |
| 47 | self.code, exc_value.resp_body['code']) |
| 48 | |
| 49 | self.test_class.assertEqual( |
| 50 | self.type_, exc_value.resp_body['type']) |
| 51 | |
| 52 | return True |
| 53 | |
| 54 | # Unexpected exceptions will be reraised |
| 55 | return False |
| 56 | |
| 57 | |
Michael Johnson | bf2379b | 2021-08-27 00:04:50 +0000 | [diff] [blame] | 58 | class BaseDnsTest(rbac_utils.RBACTestsMixin, test.BaseTestCase): |
Kiall Mac Innes | 560c89b | 2016-04-13 11:21:56 +0100 | [diff] [blame] | 59 | """Base class for DNS tests.""" |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 60 | |
Kiall Mac Innes | 25fb29e | 2016-04-07 08:07:04 +0100 | [diff] [blame] | 61 | # NOTE(andreaf) credentials holds a list of the credentials to be allocated |
| 62 | # at class setup time. Credential types can be 'primary', 'alt', 'admin' or |
| 63 | # a list of roles - the first element of the list being a label, and the |
| 64 | # rest the actual roles. |
Graham Hayes | bbc01e3 | 2018-02-15 14:40:54 +0000 | [diff] [blame] | 65 | # NOTE(kiall) primary will result in a manager @ cls.os_primary, alt will |
| 66 | # have cls.os_alt, and admin will have cls.os_admin. |
Michael Johnson | bf2379b | 2021-08-27 00:04:50 +0000 | [diff] [blame] | 67 | # NOTE(johnsom) We will allocate most credentials here so that each test |
| 68 | # can test for allowed and disallowed RBAC policies. |
Michael Johnson | 3ff84af | 2021-09-03 20:16:34 +0000 | [diff] [blame] | 69 | credentials = ['admin', 'primary', 'alt'] |
Michael Johnson | bf2379b | 2021-08-27 00:04:50 +0000 | [diff] [blame] | 70 | if CONF.dns_feature_enabled.enforce_new_defaults: |
Michael Johnson | 3ff84af | 2021-09-03 20:16:34 +0000 | [diff] [blame] | 71 | credentials.extend(['system_admin', 'system_reader', |
| 72 | 'project_member', 'project_reader']) |
Michael Johnson | bf2379b | 2021-08-27 00:04:50 +0000 | [diff] [blame] | 73 | |
| 74 | # A tuple of credentials that will be allocated by tempest using the |
| 75 | # 'credentials' list above. These are used to build RBAC test lists. |
| 76 | allocated_creds = [] |
| 77 | for cred in credentials: |
| 78 | if isinstance(cred, list): |
| 79 | allocated_creds.append('os_roles_' + cred[0]) |
| 80 | else: |
| 81 | allocated_creds.append('os_' + cred) |
| 82 | # Tests shall not mess with the list of allocated credentials |
| 83 | allocated_credentials = tuple(allocated_creds) |
sonu.kumar | d8471de | 2016-04-14 19:20:17 +0900 | [diff] [blame] | 84 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 85 | @classmethod |
| 86 | def skip_checks(cls): |
| 87 | super(BaseDnsTest, cls).skip_checks() |
| 88 | |
| 89 | if not CONF.service_available.designate: |
| 90 | skip_msg = ("%s skipped as designate is not available" |
| 91 | % cls.__name__) |
| 92 | raise cls.skipException(skip_msg) |
| 93 | |
Michael Johnson | df9fda1 | 2021-07-09 16:39:08 +0000 | [diff] [blame] | 94 | @classmethod |
| 95 | def setup_clients(cls): |
| 96 | super(BaseDnsTest, cls).setup_clients() |
| 97 | # The Query Client is not an OpenStack client which means |
| 98 | # we should not set it up through the tempest client manager. |
| 99 | # Set it up here so all tests have access to it. |
| 100 | cls.query_client = QueryClient( |
| 101 | nameservers=CONF.dns.nameservers, |
| 102 | query_timeout=CONF.dns.query_timeout, |
| 103 | build_interval=CONF.dns.build_interval, |
| 104 | build_timeout=CONF.dns.build_timeout, |
| 105 | ) |
Michael Johnson | 73065cd | 2023-02-09 20:49:50 +0000 | [diff] [blame] | 106 | # Most tests need a "primary" zones client and we need it for the |
| 107 | # API version check, so create one instance here. |
| 108 | cls.zones_client = cls.os_primary.dns_v2.ZonesClient() |
| 109 | |
| 110 | @classmethod |
| 111 | def resource_setup(cls): |
| 112 | """Setup resources needed by the tests.""" |
| 113 | super(BaseDnsTest, cls).resource_setup() |
| 114 | |
| 115 | # The credential does not matter here. |
| 116 | cls.api_version = cls.zones_client.get_max_api_version() |
Michael Johnson | df9fda1 | 2021-07-09 16:39:08 +0000 | [diff] [blame] | 117 | |
sonu.kumar | d8471de | 2016-04-14 19:20:17 +0900 | [diff] [blame] | 118 | def assertExpected(self, expected, actual, excluded_keys): |
Takashi Kajinami | c136230 | 2022-05-10 16:50:09 +0900 | [diff] [blame] | 119 | for key, value in expected.items(): |
sonu.kumar | d8471de | 2016-04-14 19:20:17 +0900 | [diff] [blame] | 120 | if key not in excluded_keys: |
| 121 | self.assertIn(key, actual) |
| 122 | self.assertEqual(value, actual[key], key) |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 123 | |
Kiall Mac Innes | 4a37679 | 2016-05-19 18:05:54 +0100 | [diff] [blame] | 124 | def assertRaisesDns(self, exc, type_, code, callable_=None, *args, |
| 125 | **kwargs): |
| 126 | """ |
| 127 | Checks the response that a api call with a exception contains the |
| 128 | expected data |
| 129 | |
| 130 | Usable as both a ordinary function, and a context manager |
| 131 | """ |
| 132 | context = AssertRaisesDns(self, exc, type_, code) |
| 133 | |
| 134 | if callable_ is None: |
| 135 | return context |
| 136 | |
| 137 | with context: |
| 138 | callable_(*args, **kwargs) |
| 139 | |
Erik Olof Gunnar Andersson | fa6f78c | 2021-06-19 00:05:51 -0700 | [diff] [blame] | 140 | def transfer_request_delete(self, transfer_client, transfer_request_id): |
| 141 | return utils.call_and_ignore_notfound_exc( |
| 142 | transfer_client.delete_transfer_request, transfer_request_id) |
| 143 | |
Maksym Shalamov | 2da1d6e | 2018-12-05 17:17:58 +0200 | [diff] [blame] | 144 | def wait_zone_delete(self, zone_client, zone_id, **kwargs): |
Erik Olof Gunnar Andersson | e8ba5cc | 2021-06-14 23:02:17 -0700 | [diff] [blame] | 145 | self._delete_zone(zone_client, zone_id, **kwargs) |
Maksym Shalamov | 2da1d6e | 2018-12-05 17:17:58 +0200 | [diff] [blame] | 146 | utils.call_until_true(self._check_zone_deleted, |
| 147 | CONF.dns.build_timeout, |
| 148 | CONF.dns.build_interval, |
| 149 | zone_client, |
| 150 | zone_id) |
| 151 | |
zahlabut | 5260227 | 2021-08-04 17:07:29 +0300 | [diff] [blame] | 152 | def wait_recordset_delete(self, recordset_client, zone_id, |
| 153 | recordset_id, **kwargs): |
| 154 | self._delete_recordset( |
| 155 | recordset_client, zone_id, recordset_id, **kwargs) |
| 156 | utils.call_until_true(self._check_recordset_deleted, |
| 157 | CONF.dns.build_timeout, |
| 158 | CONF.dns.build_interval, |
| 159 | recordset_client, |
| 160 | zone_id, |
| 161 | recordset_id) |
| 162 | |
Erik Olof Gunnar Andersson | 3bfce9b | 2022-01-08 23:11:13 -0800 | [diff] [blame] | 163 | def unset_ptr(self, ptr_client, fip_id, **kwargs): |
| 164 | return utils.call_and_ignore_notfound_exc( |
| 165 | ptr_client.unset_ptr_record, fip_id, **kwargs) |
| 166 | |
Erik Olof Gunnar Andersson | e8ba5cc | 2021-06-14 23:02:17 -0700 | [diff] [blame] | 167 | def _delete_zone(self, zone_client, zone_id, **kwargs): |
| 168 | return utils.call_and_ignore_notfound_exc(zone_client.delete_zone, |
| 169 | zone_id, **kwargs) |
| 170 | |
Maksym Shalamov | 2da1d6e | 2018-12-05 17:17:58 +0200 | [diff] [blame] | 171 | def _check_zone_deleted(self, zone_client, zone_id): |
| 172 | return utils.call_and_ignore_notfound_exc(zone_client.show_zone, |
| 173 | zone_id) is None |
| 174 | |
zahlabut | 5260227 | 2021-08-04 17:07:29 +0300 | [diff] [blame] | 175 | def _delete_recordset(self, recordset_client, zone_id, |
| 176 | recordset_id, **kwargs): |
| 177 | return utils.call_and_ignore_notfound_exc( |
| 178 | recordset_client.delete_recordset, |
| 179 | zone_id, recordset_id, **kwargs) |
| 180 | |
| 181 | def _check_recordset_deleted( |
| 182 | self, recordset_client, zone_id, recordset_id): |
| 183 | return utils.call_and_ignore_notfound_exc( |
| 184 | recordset_client.show_recordset, zone_id, recordset_id) is None |
| 185 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 186 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 187 | class BaseDnsV2Test(BaseDnsTest): |
| 188 | """Base class for DNS V2 API tests.""" |
| 189 | |
Michael Johnson | a3a2363 | 2021-07-21 21:55:32 +0000 | [diff] [blame] | 190 | all_projects_header = {'X-Auth-All-Projects': True} |
Arkady Shtempler | a2b08ee | 2022-04-12 18:02:24 +0300 | [diff] [blame] | 191 | managed_records = {'x-designate-edit-managed-records': True} |
Michael Johnson | a3a2363 | 2021-07-21 21:55:32 +0000 | [diff] [blame] | 192 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 193 | @classmethod |
| 194 | def skip_checks(cls): |
| 195 | super(BaseDnsV2Test, cls).skip_checks() |
| 196 | |
| 197 | if not CONF.dns_feature_enabled.api_v2: |
| 198 | skip_msg = ("%s skipped as designate v2 API is not available" |
| 199 | % cls.__name__) |
| 200 | raise cls.skipException(skip_msg) |
| 201 | |
| 202 | |
| 203 | class BaseDnsAdminTest(BaseDnsTest): |
| 204 | """Base class for DNS Admin API tests.""" |
| 205 | |
Kiall Mac Innes | 8ae796c | 2016-04-21 13:47:25 +0100 | [diff] [blame] | 206 | @classmethod |
| 207 | def skip_checks(cls): |
| 208 | super(BaseDnsAdminTest, cls).skip_checks() |
| 209 | if not CONF.dns_feature_enabled.api_admin: |
| 210 | skip_msg = ("%s skipped as designate admin API is not available" |
| 211 | % cls.__name__) |
| 212 | raise cls.skipException(skip_msg) |