blob: 566f36365f8b5d3849e46425e3314136d2734790 [file] [log] [blame]
Marc Kodererc832c612016-12-12 11:49:10 +01001# Copyright 2016 SAP SE
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import functools
17
18from tempest import config
Ade Lee519aa802022-05-06 12:33:06 -040019from tempest.lib.common import api_version_utils
Marc Kodererc832c612016-12-12 11:49:10 +010020from tempest import test
21
22from barbican_tempest_plugin import clients
23
24CONF = config.CONF
25
26# NOTE(dane-fichter): We need to track resource types for cleanup.
Peter Hamilton2adeed62017-02-01 12:23:45 -050027RESOURCE_TYPES = ['container', 'order', 'quota', 'secret']
Marc Kodererc832c612016-12-12 11:49:10 +010028
29
30def _get_uuid(href):
31 return href.split('/')[-1]
32
33
34def creates(resource):
35 """Decorator that adds resource UUIDs to queue for cleanup"""
36
37 def decorator(f):
38 @functools.wraps(f)
39 def wrapper(cls, *args, **kwargs):
40 resp = f(cls, *args, **kwargs)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050041 if resource == 'container':
42 uuid = _get_uuid(resp['container_ref'])
Peter Hamiltonf4166be2017-01-20 10:54:33 -050043 if resource == 'order':
44 uuid = _get_uuid(resp.get('order_ref'))
45 order_metadata = cls.get_order(uuid)
46 secret_ref = order_metadata.get('secret_ref')
47 if secret_ref:
48 cls.created_objects['secret'].add(_get_uuid(secret_ref))
Peter Hamilton2adeed62017-02-01 12:23:45 -050049 uuid = _get_uuid(resp['order_ref'])
50 if resource == 'quota':
51 uuid = _get_uuid(args[0])
Peter Hamiltonf4166be2017-01-20 10:54:33 -050052 if resource == 'secret':
53 uuid = _get_uuid(resp['secret_ref'])
Marc Kodererc832c612016-12-12 11:49:10 +010054 cls.created_objects[resource].add(uuid)
55 return resp
56 return wrapper
57 return decorator
58
59
Ade Lee519aa802022-05-06 12:33:06 -040060class BaseKeyManagerTest(test.BaseTestCase,
61 api_version_utils.BaseMicroversionTest):
Marc Kodererc832c612016-12-12 11:49:10 +010062 """Base class for all api tests."""
63
64 # Why do I have to be an admin to create secrets? No idea...
Peter Hamilton2adeed62017-02-01 12:23:45 -050065 credentials = ('admin', ['service_admin', 'key-manager:service-admin'])
Marc Kodererc832c612016-12-12 11:49:10 +010066 client_manager = clients.Clients
67 created_objects = {}
68
69 @classmethod
Ade Lee519aa802022-05-06 12:33:06 -040070 def skip_checks(cls):
71 super().skip_checks()
72 api_version_utils.check_skip_with_microversion(
73 cls.min_microversion,
74 cls.max_microversion,
75 CONF.key_manager.min_microversion,
76 CONF.key_manager.max_microversion)
77
78 @classmethod
Marc Kodererc832c612016-12-12 11:49:10 +010079 def setup_clients(cls):
80 super(BaseKeyManagerTest, cls).setup_clients()
81 os = getattr(cls, 'os_%s' % cls.credentials[0])
Peter Hamiltonee389172017-01-12 16:41:45 -050082 cls.consumer_client = os.secret_v1.ConsumerClient(
83 service='key-manager'
84 )
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050085 cls.container_client = os.secret_v1.ContainerClient(
86 service='key-manager'
87 )
Peter Hamiltonf4166be2017-01-20 10:54:33 -050088 cls.order_client = os.secret_v1.OrderClient(service='key-manager')
89 cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
Ade Lee519aa802022-05-06 12:33:06 -040090 cls.secret_consumer_client = os.secret_v1_1.SecretConsumerClient()
dane-fichter2aff5072017-01-11 17:20:42 -080091 cls.secret_metadata_client = os.secret_v1.SecretMetadataClient(
92 service='key-manager'
93 )
Ade Lee519aa802022-05-06 12:33:06 -040094 cls.version_client = os.secret_v1_1.VersionClient()
Marc Kodererc832c612016-12-12 11:49:10 +010095
Peter Hamilton2adeed62017-02-01 12:23:45 -050096 os = getattr(cls, 'os_roles_%s' % cls.credentials[1][0])
97 cls.quota_client = os.secret_v1.QuotaClient(service='key-manager')
98
Marc Kodererc832c612016-12-12 11:49:10 +010099 @classmethod
Ade Leec80b00a2021-02-09 14:49:41 -0500100 def setup_credentials(cls):
101 super().setup_credentials()
102 cls.os_primary = getattr(cls, f'os_{cls.credentials[0]}')
103
104 @classmethod
Marc Kodererc832c612016-12-12 11:49:10 +0100105 def resource_setup(cls):
106 super(BaseKeyManagerTest, cls).resource_setup()
107 for resource in RESOURCE_TYPES:
108 cls.created_objects[resource] = set()
109
110 @classmethod
111 def resource_cleanup(cls):
112 try:
Peter Hamilton7be3f7e2017-01-11 15:12:21 -0500113 for container_uuid in list(cls.created_objects['container']):
114 cls.delete_container(container_uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500115 for order_uuid in list(cls.created_objects['order']):
116 cls.delete_order(order_uuid)
Peter Hamilton2adeed62017-02-01 12:23:45 -0500117 for project_quota_uuid in list(cls.created_objects['quota']):
118 cls.delete_project_quota(project_quota_uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500119 for secret_uuid in list(cls.created_objects['secret']):
120 cls.delete_secret(secret_uuid)
Marc Kodererc832c612016-12-12 11:49:10 +0100121 finally:
122 super(BaseKeyManagerTest, cls).resource_cleanup()
123
124 @classmethod
Peter Hamilton7be3f7e2017-01-11 15:12:21 -0500125 @creates('container')
126 def create_container(cls, **kwargs):
127 return cls.container_client.create_container(**kwargs)
128
129 @classmethod
130 def delete_container(cls, uuid):
131 cls.created_objects['container'].remove(uuid)
132 return cls.container_client.delete_container(uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500133
134 @classmethod
135 @creates('order')
136 def create_order(cls, **kwargs):
137 return cls.order_client.create_order(**kwargs)
138
139 @classmethod
140 def get_order(cls, uuid):
141 return cls.order_client.get_order(uuid)
142
143 @classmethod
144 def delete_order(cls, uuid):
145 cls.created_objects['order'].remove(uuid)
146 return cls.order_client.delete_order(uuid)
147
148 @classmethod
Peter Hamilton2adeed62017-02-01 12:23:45 -0500149 @creates('quota')
150 def create_project_quota(cls, project_id, **kwargs):
151 return cls.quota_client.create_project_quota(project_id, **kwargs)
152
153 @classmethod
154 def delete_project_quota(cls, project_id):
155 cls.created_objects['quota'].remove(project_id)
156 return cls.quota_client.delete_project_quota(project_id)
157
158 @classmethod
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500159 @creates('secret')
160 def create_secret(cls, **kwargs):
161 return cls.secret_client.create_secret(**kwargs)
162
163 @classmethod
164 def delete_secret(cls, uuid):
165 cls.created_objects['secret'].remove(uuid)
166 return cls.secret_client.delete_secret(uuid)