blob: 58f6bbf65d8bfac82b04095df54e952d3238d661 [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 Hamilton7be3f7e2017-01-11 15:12:21 -050026RESOURCE_TYPES = ['secret', 'container']
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)
40 if resource == 'secret':
41 uuid = _get_uuid(resp['secret_ref'])
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050042 if resource == 'container':
43 uuid = _get_uuid(resp['container_ref'])
Marc Kodererc832c612016-12-12 11:49:10 +010044 cls.created_objects[resource].add(uuid)
45 return resp
46 return wrapper
47 return decorator
48
49
50class BaseKeyManagerTest(test.BaseTestCase):
51 """Base class for all api tests."""
52
53 # Why do I have to be an admin to create secrets? No idea...
54 credentials = ('admin', )
55 client_manager = clients.Clients
56 created_objects = {}
57
58 @classmethod
59 def setup_clients(cls):
60 super(BaseKeyManagerTest, cls).setup_clients()
61 os = getattr(cls, 'os_%s' % cls.credentials[0])
Peter Hamiltonee389172017-01-12 16:41:45 -050062 cls.consumer_client = os.secret_v1.ConsumerClient(
63 service='key-manager'
64 )
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050065 cls.container_client = os.secret_v1.ContainerClient(
66 service='key-manager'
67 )
dane-fichter2aff5072017-01-11 17:20:42 -080068 cls.secret_metadata_client = os.secret_v1.SecretMetadataClient(
69 service='key-manager'
70 )
Peter Hamiltonee389172017-01-12 16:41:45 -050071 cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
Marc Kodererc832c612016-12-12 11:49:10 +010072
73 @classmethod
74 def resource_setup(cls):
75 super(BaseKeyManagerTest, cls).resource_setup()
76 for resource in RESOURCE_TYPES:
77 cls.created_objects[resource] = set()
78
79 @classmethod
80 def resource_cleanup(cls):
81 try:
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050082 for secret_uuid in list(cls.created_objects['secret']):
Marc Kodererc832c612016-12-12 11:49:10 +010083 cls.delete_secret(secret_uuid)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050084 for container_uuid in list(cls.created_objects['container']):
85 cls.delete_container(container_uuid)
Marc Kodererc832c612016-12-12 11:49:10 +010086 finally:
87 super(BaseKeyManagerTest, cls).resource_cleanup()
88
89 @classmethod
90 @creates('secret')
91 def create_secret(cls, **kwargs):
92 return cls.secret_client.create_secret(**kwargs)
93
94 @classmethod
95 def delete_secret(cls, uuid):
96 cls.created_objects['secret'].remove(uuid)
97 return cls.secret_client.delete_secret(uuid)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050098
99 @classmethod
100 @creates('container')
101 def create_container(cls, **kwargs):
102 return cls.container_client.create_container(**kwargs)
103
104 @classmethod
105 def delete_container(cls, uuid):
106 cls.created_objects['container'].remove(uuid)
107 return cls.container_client.delete_container(uuid)