blob: 7a34419b0bfd6cc6ef266d7a0d5480d07f9320de [file] [log] [blame]
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +01001# 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.
14from tempest import test
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010015from tempest import config
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +020016from tempest.lib.common.utils import test_utils as utils
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010017
Michael Johnsoncc8f89b2021-11-30 00:31:24 +000018from designate_tempest_plugin.services.dns.query.query_client import (
19 QueryClient)
Michael Johnsonbf2379b2021-08-27 00:04:50 +000020from designate_tempest_plugin.tests import rbac_utils
Michael Johnsondf9fda12021-07-09 16:39:08 +000021
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010022
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010023CONF = config.CONF
24
25
Dmitry Galkin9a0a3602018-11-13 19:42:29 +000026class AssertRaisesDns(test.BaseTestCase):
Kiall Mac Innes4a376792016-05-19 18:05:54 +010027 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 Johnsonbf2379b2021-08-27 00:04:50 +000058class BaseDnsTest(rbac_utils.RBACTestsMixin, test.BaseTestCase):
Kiall Mac Innes560c89b2016-04-13 11:21:56 +010059 """Base class for DNS tests."""
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010060
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010061 # 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 Hayesbbc01e32018-02-15 14:40:54 +000065 # 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 Johnsonbf2379b2021-08-27 00:04:50 +000067 # NOTE(johnsom) We will allocate most credentials here so that each test
68 # can test for allowed and disallowed RBAC policies.
Michael Johnson3ff84af2021-09-03 20:16:34 +000069 credentials = ['admin', 'primary', 'alt']
Michael Johnsonbf2379b2021-08-27 00:04:50 +000070 if CONF.dns_feature_enabled.enforce_new_defaults:
Ghanshyam Mannbb1996d2024-08-15 18:52:31 -070071 credentials.extend(['project_member', 'project_reader'])
Michael Johnsonbf2379b2021-08-27 00:04:50 +000072
73 # A tuple of credentials that will be allocated by tempest using the
74 # 'credentials' list above. These are used to build RBAC test lists.
75 allocated_creds = []
76 for cred in credentials:
77 if isinstance(cred, list):
78 allocated_creds.append('os_roles_' + cred[0])
79 else:
80 allocated_creds.append('os_' + cred)
81 # Tests shall not mess with the list of allocated credentials
82 allocated_credentials = tuple(allocated_creds)
sonu.kumard8471de2016-04-14 19:20:17 +090083
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010084 @classmethod
85 def skip_checks(cls):
86 super(BaseDnsTest, cls).skip_checks()
87
88 if not CONF.service_available.designate:
89 skip_msg = ("%s skipped as designate is not available"
90 % cls.__name__)
91 raise cls.skipException(skip_msg)
92
Michael Johnsondf9fda12021-07-09 16:39:08 +000093 @classmethod
94 def setup_clients(cls):
95 super(BaseDnsTest, cls).setup_clients()
96 # The Query Client is not an OpenStack client which means
97 # we should not set it up through the tempest client manager.
98 # Set it up here so all tests have access to it.
99 cls.query_client = QueryClient(
100 nameservers=CONF.dns.nameservers,
101 query_timeout=CONF.dns.query_timeout,
102 build_interval=CONF.dns.build_interval,
103 build_timeout=CONF.dns.build_timeout,
104 )
Michael Johnson73065cd2023-02-09 20:49:50 +0000105 # Most tests need a "primary" zones client and we need it for the
106 # API version check, so create one instance here.
107 cls.zones_client = cls.os_primary.dns_v2.ZonesClient()
108
109 @classmethod
110 def resource_setup(cls):
111 """Setup resources needed by the tests."""
112 super(BaseDnsTest, cls).resource_setup()
113
114 # The credential does not matter here.
115 cls.api_version = cls.zones_client.get_max_api_version()
Michael Johnsondf9fda12021-07-09 16:39:08 +0000116
sonu.kumard8471de2016-04-14 19:20:17 +0900117 def assertExpected(self, expected, actual, excluded_keys):
Takashi Kajinamic1362302022-05-10 16:50:09 +0900118 for key, value in expected.items():
sonu.kumard8471de2016-04-14 19:20:17 +0900119 if key not in excluded_keys:
120 self.assertIn(key, actual)
121 self.assertEqual(value, actual[key], key)
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100122
Kiall Mac Innes4a376792016-05-19 18:05:54 +0100123 def assertRaisesDns(self, exc, type_, code, callable_=None, *args,
124 **kwargs):
125 """
126 Checks the response that a api call with a exception contains the
127 expected data
128
129 Usable as both a ordinary function, and a context manager
130 """
131 context = AssertRaisesDns(self, exc, type_, code)
132
133 if callable_ is None:
134 return context
135
136 with context:
137 callable_(*args, **kwargs)
138
Erik Olof Gunnar Anderssonfa6f78c2021-06-19 00:05:51 -0700139 def transfer_request_delete(self, transfer_client, transfer_request_id):
140 return utils.call_and_ignore_notfound_exc(
141 transfer_client.delete_transfer_request, transfer_request_id)
142
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200143 def wait_zone_delete(self, zone_client, zone_id, **kwargs):
Erik Olof Gunnar Anderssone8ba5cc2021-06-14 23:02:17 -0700144 self._delete_zone(zone_client, zone_id, **kwargs)
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200145 utils.call_until_true(self._check_zone_deleted,
146 CONF.dns.build_timeout,
147 CONF.dns.build_interval,
148 zone_client,
149 zone_id)
150
zahlabut52602272021-08-04 17:07:29 +0300151 def wait_recordset_delete(self, recordset_client, zone_id,
152 recordset_id, **kwargs):
153 self._delete_recordset(
154 recordset_client, zone_id, recordset_id, **kwargs)
155 utils.call_until_true(self._check_recordset_deleted,
156 CONF.dns.build_timeout,
157 CONF.dns.build_interval,
158 recordset_client,
159 zone_id,
160 recordset_id)
161
Erik Olof Gunnar Andersson3bfce9b2022-01-08 23:11:13 -0800162 def unset_ptr(self, ptr_client, fip_id, **kwargs):
163 return utils.call_and_ignore_notfound_exc(
164 ptr_client.unset_ptr_record, fip_id, **kwargs)
165
Erik Olof Gunnar Anderssone8ba5cc2021-06-14 23:02:17 -0700166 def _delete_zone(self, zone_client, zone_id, **kwargs):
167 return utils.call_and_ignore_notfound_exc(zone_client.delete_zone,
168 zone_id, **kwargs)
169
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200170 def _check_zone_deleted(self, zone_client, zone_id):
171 return utils.call_and_ignore_notfound_exc(zone_client.show_zone,
172 zone_id) is None
173
zahlabut52602272021-08-04 17:07:29 +0300174 def _delete_recordset(self, recordset_client, zone_id,
175 recordset_id, **kwargs):
176 return utils.call_and_ignore_notfound_exc(
177 recordset_client.delete_recordset,
178 zone_id, recordset_id, **kwargs)
179
180 def _check_recordset_deleted(
181 self, recordset_client, zone_id, recordset_id):
182 return utils.call_and_ignore_notfound_exc(
183 recordset_client.show_recordset, zone_id, recordset_id) is None
184
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100185
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100186class BaseDnsV2Test(BaseDnsTest):
187 """Base class for DNS V2 API tests."""
188
Michael Johnsona3a23632021-07-21 21:55:32 +0000189 all_projects_header = {'X-Auth-All-Projects': True}
Arkady Shtemplera2b08ee2022-04-12 18:02:24 +0300190 managed_records = {'x-designate-edit-managed-records': True}
Michael Johnsona3a23632021-07-21 21:55:32 +0000191
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100192 @classmethod
193 def skip_checks(cls):
194 super(BaseDnsV2Test, cls).skip_checks()
195
196 if not CONF.dns_feature_enabled.api_v2:
197 skip_msg = ("%s skipped as designate v2 API is not available"
198 % cls.__name__)
199 raise cls.skipException(skip_msg)
200
201
202class BaseDnsAdminTest(BaseDnsTest):
203 """Base class for DNS Admin API tests."""
204
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100205 @classmethod
206 def skip_checks(cls):
207 super(BaseDnsAdminTest, cls).skip_checks()
208 if not CONF.dns_feature_enabled.api_admin:
209 skip_msg = ("%s skipped as designate admin API is not available"
210 % cls.__name__)
211 raise cls.skipException(skip_msg)