blob: 83a157391a736d9a9448be58eef749430e2b51c5 [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
19from tempest import test
20
21from barbican_tempest_plugin import clients
22
23CONF = config.CONF
24
25# NOTE(dane-fichter): We need to track resource types for cleanup.
Peter Hamilton2adeed62017-02-01 12:23:45 -050026RESOURCE_TYPES = ['container', 'order', 'quota', 'secret']
Marc Kodererc832c612016-12-12 11:49:10 +010027
28
29def _get_uuid(href):
30 return href.split('/')[-1]
31
32
33def creates(resource):
34 """Decorator that adds resource UUIDs to queue for cleanup"""
35
36 def decorator(f):
37 @functools.wraps(f)
38 def wrapper(cls, *args, **kwargs):
39 resp = f(cls, *args, **kwargs)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050040 if resource == 'container':
41 uuid = _get_uuid(resp['container_ref'])
Peter Hamiltonf4166be2017-01-20 10:54:33 -050042 if resource == 'order':
43 uuid = _get_uuid(resp.get('order_ref'))
44 order_metadata = cls.get_order(uuid)
45 secret_ref = order_metadata.get('secret_ref')
46 if secret_ref:
47 cls.created_objects['secret'].add(_get_uuid(secret_ref))
Peter Hamilton2adeed62017-02-01 12:23:45 -050048 uuid = _get_uuid(resp['order_ref'])
49 if resource == 'quota':
50 uuid = _get_uuid(args[0])
Peter Hamiltonf4166be2017-01-20 10:54:33 -050051 if resource == 'secret':
52 uuid = _get_uuid(resp['secret_ref'])
Marc Kodererc832c612016-12-12 11:49:10 +010053 cls.created_objects[resource].add(uuid)
54 return resp
55 return wrapper
56 return decorator
57
58
59class BaseKeyManagerTest(test.BaseTestCase):
60 """Base class for all api tests."""
61
62 # Why do I have to be an admin to create secrets? No idea...
Peter Hamilton2adeed62017-02-01 12:23:45 -050063 credentials = ('admin', ['service_admin', 'key-manager:service-admin'])
Marc Kodererc832c612016-12-12 11:49:10 +010064 client_manager = clients.Clients
65 created_objects = {}
66
67 @classmethod
sandriichenko5f431042018-03-05 14:52:33 +020068 def skip_checks(cls):
69 super(BaseKeyManagerTest, cls).skip_checks()
70 if not CONF.service_available.barbican:
71 raise cls.skipException('Barbican is not enabled.')
72
73 @classmethod
Marc Kodererc832c612016-12-12 11:49:10 +010074 def setup_clients(cls):
75 super(BaseKeyManagerTest, cls).setup_clients()
76 os = getattr(cls, 'os_%s' % cls.credentials[0])
Peter Hamiltonee389172017-01-12 16:41:45 -050077 cls.consumer_client = os.secret_v1.ConsumerClient(
78 service='key-manager'
79 )
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050080 cls.container_client = os.secret_v1.ContainerClient(
81 service='key-manager'
82 )
Peter Hamiltonf4166be2017-01-20 10:54:33 -050083 cls.order_client = os.secret_v1.OrderClient(service='key-manager')
84 cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
dane-fichter2aff5072017-01-11 17:20:42 -080085 cls.secret_metadata_client = os.secret_v1.SecretMetadataClient(
86 service='key-manager'
87 )
Marc Kodererc832c612016-12-12 11:49:10 +010088
Peter Hamilton2adeed62017-02-01 12:23:45 -050089 os = getattr(cls, 'os_roles_%s' % cls.credentials[1][0])
90 cls.quota_client = os.secret_v1.QuotaClient(service='key-manager')
91
Marc Kodererc832c612016-12-12 11:49:10 +010092 @classmethod
Ade Leec80b00a2021-02-09 14:49:41 -050093 def setup_credentials(cls):
94 super().setup_credentials()
95 cls.os_primary = getattr(cls, f'os_{cls.credentials[0]}')
96
97 @classmethod
Marc Kodererc832c612016-12-12 11:49:10 +010098 def resource_setup(cls):
99 super(BaseKeyManagerTest, cls).resource_setup()
100 for resource in RESOURCE_TYPES:
101 cls.created_objects[resource] = set()
102
103 @classmethod
104 def resource_cleanup(cls):
105 try:
Peter Hamilton7be3f7e2017-01-11 15:12:21 -0500106 for container_uuid in list(cls.created_objects['container']):
107 cls.delete_container(container_uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500108 for order_uuid in list(cls.created_objects['order']):
109 cls.delete_order(order_uuid)
Peter Hamilton2adeed62017-02-01 12:23:45 -0500110 for project_quota_uuid in list(cls.created_objects['quota']):
111 cls.delete_project_quota(project_quota_uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500112 for secret_uuid in list(cls.created_objects['secret']):
113 cls.delete_secret(secret_uuid)
Marc Kodererc832c612016-12-12 11:49:10 +0100114 finally:
115 super(BaseKeyManagerTest, cls).resource_cleanup()
116
117 @classmethod
Peter Hamilton7be3f7e2017-01-11 15:12:21 -0500118 @creates('container')
119 def create_container(cls, **kwargs):
120 return cls.container_client.create_container(**kwargs)
121
122 @classmethod
123 def delete_container(cls, uuid):
124 cls.created_objects['container'].remove(uuid)
125 return cls.container_client.delete_container(uuid)
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500126
127 @classmethod
128 @creates('order')
129 def create_order(cls, **kwargs):
130 return cls.order_client.create_order(**kwargs)
131
132 @classmethod
133 def get_order(cls, uuid):
134 return cls.order_client.get_order(uuid)
135
136 @classmethod
137 def delete_order(cls, uuid):
138 cls.created_objects['order'].remove(uuid)
139 return cls.order_client.delete_order(uuid)
140
141 @classmethod
Peter Hamilton2adeed62017-02-01 12:23:45 -0500142 @creates('quota')
143 def create_project_quota(cls, project_id, **kwargs):
144 return cls.quota_client.create_project_quota(project_id, **kwargs)
145
146 @classmethod
147 def delete_project_quota(cls, project_id):
148 cls.created_objects['quota'].remove(project_id)
149 return cls.quota_client.delete_project_quota(project_id)
150
151 @classmethod
Peter Hamiltonf4166be2017-01-20 10:54:33 -0500152 @creates('secret')
153 def create_secret(cls, **kwargs):
154 return cls.secret_client.create_secret(**kwargs)
155
156 @classmethod
157 def delete_secret(cls, uuid):
158 cls.created_objects['secret'].remove(uuid)
159 return cls.secret_client.delete_secret(uuid)