blob: e581e0a08f7419fa51a39326237896cca375f990 [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.
sonu.kumard8471de2016-04-14 19:20:17 +090014import six
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010015from tempest import test
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010016from tempest import config
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +020017from tempest.lib.common.utils import test_utils as utils
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010018
Michael Johnsondf9fda12021-07-09 16:39:08 +000019from designate_tempest_plugin.services.dns.query.query_client import \
20 QueryClient
Michael Johnsonbf2379b2021-08-27 00:04:50 +000021from designate_tempest_plugin.tests import rbac_utils
Michael Johnsondf9fda12021-07-09 16:39:08 +000022
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010023
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010024CONF = config.CONF
25
26
Dmitry Galkin9a0a3602018-11-13 19:42:29 +000027class AssertRaisesDns(test.BaseTestCase):
Kiall Mac Innes4a376792016-05-19 18:05:54 +010028 def __init__(self, test_class, exc, type_, code):
29 self.test_class = test_class
30 self.exc = exc
31 self.type_ = type_
32 self.code = code
33
34 def __enter__(self):
35 pass
36
37 def __exit__(self, exc_type, exc_value, traceback):
38 if exc_type is None:
39 try:
40 exc_name = self.exc.__name__
41 except AttributeError:
42 exc_name = str(self.exc)
43 raise self.failureException(
44 "{0} not raised".format(exc_name))
45
46 if issubclass(exc_type, self.exc):
47 self.test_class.assertEqual(
48 self.code, exc_value.resp_body['code'])
49
50 self.test_class.assertEqual(
51 self.type_, exc_value.resp_body['type'])
52
53 return True
54
55 # Unexpected exceptions will be reraised
56 return False
57
58
Michael Johnsonbf2379b2021-08-27 00:04:50 +000059class BaseDnsTest(rbac_utils.RBACTestsMixin, test.BaseTestCase):
Kiall Mac Innes560c89b2016-04-13 11:21:56 +010060 """Base class for DNS tests."""
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010061
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010062 # NOTE(andreaf) credentials holds a list of the credentials to be allocated
63 # at class setup time. Credential types can be 'primary', 'alt', 'admin' or
64 # a list of roles - the first element of the list being a label, and the
65 # rest the actual roles.
Graham Hayesbbc01e32018-02-15 14:40:54 +000066 # NOTE(kiall) primary will result in a manager @ cls.os_primary, alt will
67 # have cls.os_alt, and admin will have cls.os_admin.
Michael Johnsonbf2379b2021-08-27 00:04:50 +000068 # NOTE(johnsom) We will allocate most credentials here so that each test
69 # can test for allowed and disallowed RBAC policies.
Michael Johnson3ff84af2021-09-03 20:16:34 +000070 credentials = ['admin', 'primary', 'alt']
Michael Johnsonbf2379b2021-08-27 00:04:50 +000071 if CONF.dns_feature_enabled.enforce_new_defaults:
Michael Johnson3ff84af2021-09-03 20:16:34 +000072 credentials.extend(['system_admin', 'system_reader',
73 'project_member', 'project_reader'])
Michael Johnsonbf2379b2021-08-27 00:04:50 +000074
75 # A tuple of credentials that will be allocated by tempest using the
76 # 'credentials' list above. These are used to build RBAC test lists.
77 allocated_creds = []
78 for cred in credentials:
79 if isinstance(cred, list):
80 allocated_creds.append('os_roles_' + cred[0])
81 else:
82 allocated_creds.append('os_' + cred)
83 # Tests shall not mess with the list of allocated credentials
84 allocated_credentials = tuple(allocated_creds)
sonu.kumard8471de2016-04-14 19:20:17 +090085
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +010086 @classmethod
87 def skip_checks(cls):
88 super(BaseDnsTest, cls).skip_checks()
89
90 if not CONF.service_available.designate:
91 skip_msg = ("%s skipped as designate is not available"
92 % cls.__name__)
93 raise cls.skipException(skip_msg)
94
Michael Johnsondf9fda12021-07-09 16:39:08 +000095 @classmethod
96 def setup_clients(cls):
97 super(BaseDnsTest, cls).setup_clients()
98 # The Query Client is not an OpenStack client which means
99 # we should not set it up through the tempest client manager.
100 # Set it up here so all tests have access to it.
101 cls.query_client = QueryClient(
102 nameservers=CONF.dns.nameservers,
103 query_timeout=CONF.dns.query_timeout,
104 build_interval=CONF.dns.build_interval,
105 build_timeout=CONF.dns.build_timeout,
106 )
107
sonu.kumard8471de2016-04-14 19:20:17 +0900108 def assertExpected(self, expected, actual, excluded_keys):
109 for key, value in six.iteritems(expected):
110 if key not in excluded_keys:
111 self.assertIn(key, actual)
112 self.assertEqual(value, actual[key], key)
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100113
Kiall Mac Innes4a376792016-05-19 18:05:54 +0100114 def assertRaisesDns(self, exc, type_, code, callable_=None, *args,
115 **kwargs):
116 """
117 Checks the response that a api call with a exception contains the
118 expected data
119
120 Usable as both a ordinary function, and a context manager
121 """
122 context = AssertRaisesDns(self, exc, type_, code)
123
124 if callable_ is None:
125 return context
126
127 with context:
128 callable_(*args, **kwargs)
129
Erik Olof Gunnar Anderssonfa6f78c2021-06-19 00:05:51 -0700130 def transfer_request_delete(self, transfer_client, transfer_request_id):
131 return utils.call_and_ignore_notfound_exc(
132 transfer_client.delete_transfer_request, transfer_request_id)
133
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200134 def wait_zone_delete(self, zone_client, zone_id, **kwargs):
Erik Olof Gunnar Anderssone8ba5cc2021-06-14 23:02:17 -0700135 self._delete_zone(zone_client, zone_id, **kwargs)
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200136 utils.call_until_true(self._check_zone_deleted,
137 CONF.dns.build_timeout,
138 CONF.dns.build_interval,
139 zone_client,
140 zone_id)
141
zahlabut52602272021-08-04 17:07:29 +0300142 def wait_recordset_delete(self, recordset_client, zone_id,
143 recordset_id, **kwargs):
144 self._delete_recordset(
145 recordset_client, zone_id, recordset_id, **kwargs)
146 utils.call_until_true(self._check_recordset_deleted,
147 CONF.dns.build_timeout,
148 CONF.dns.build_interval,
149 recordset_client,
150 zone_id,
151 recordset_id)
152
Erik Olof Gunnar Andersson3bfce9b2022-01-08 23:11:13 -0800153 def unset_ptr(self, ptr_client, fip_id, **kwargs):
154 return utils.call_and_ignore_notfound_exc(
155 ptr_client.unset_ptr_record, fip_id, **kwargs)
156
Erik Olof Gunnar Anderssone8ba5cc2021-06-14 23:02:17 -0700157 def _delete_zone(self, zone_client, zone_id, **kwargs):
158 return utils.call_and_ignore_notfound_exc(zone_client.delete_zone,
159 zone_id, **kwargs)
160
Maksym Shalamov2da1d6e2018-12-05 17:17:58 +0200161 def _check_zone_deleted(self, zone_client, zone_id):
162 return utils.call_and_ignore_notfound_exc(zone_client.show_zone,
163 zone_id) is None
164
zahlabut52602272021-08-04 17:07:29 +0300165 def _delete_recordset(self, recordset_client, zone_id,
166 recordset_id, **kwargs):
167 return utils.call_and_ignore_notfound_exc(
168 recordset_client.delete_recordset,
169 zone_id, recordset_id, **kwargs)
170
171 def _check_recordset_deleted(
172 self, recordset_client, zone_id, recordset_id):
173 return utils.call_and_ignore_notfound_exc(
174 recordset_client.show_recordset, zone_id, recordset_id) is None
175
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100176
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100177class BaseDnsV2Test(BaseDnsTest):
178 """Base class for DNS V2 API tests."""
179
Michael Johnsona3a23632021-07-21 21:55:32 +0000180 all_projects_header = {'X-Auth-All-Projects': True}
181
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100182 @classmethod
183 def skip_checks(cls):
184 super(BaseDnsV2Test, cls).skip_checks()
185
186 if not CONF.dns_feature_enabled.api_v2:
187 skip_msg = ("%s skipped as designate v2 API is not available"
188 % cls.__name__)
189 raise cls.skipException(skip_msg)
190
191
192class BaseDnsAdminTest(BaseDnsTest):
193 """Base class for DNS Admin API tests."""
194
Kiall Mac Innes8ae796c2016-04-21 13:47:25 +0100195 @classmethod
196 def skip_checks(cls):
197 super(BaseDnsAdminTest, cls).skip_checks()
198 if not CONF.dns_feature_enabled.api_admin:
199 skip_msg = ("%s skipped as designate admin API is not available"
200 % cls.__name__)
201 raise cls.skipException(skip_msg)