blob: c5a7597f4446893d4540156bdc7058fffb3bb7ef [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 )
Peter Hamiltonee389172017-01-12 16:41:45 -050068 cls.secret_client = os.secret_v1.SecretClient(service='key-manager')
Marc Kodererc832c612016-12-12 11:49:10 +010069
70 @classmethod
71 def resource_setup(cls):
72 super(BaseKeyManagerTest, cls).resource_setup()
73 for resource in RESOURCE_TYPES:
74 cls.created_objects[resource] = set()
75
76 @classmethod
77 def resource_cleanup(cls):
78 try:
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050079 for secret_uuid in list(cls.created_objects['secret']):
Marc Kodererc832c612016-12-12 11:49:10 +010080 cls.delete_secret(secret_uuid)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050081 for container_uuid in list(cls.created_objects['container']):
82 cls.delete_container(container_uuid)
Marc Kodererc832c612016-12-12 11:49:10 +010083 finally:
84 super(BaseKeyManagerTest, cls).resource_cleanup()
85
86 @classmethod
87 @creates('secret')
88 def create_secret(cls, **kwargs):
89 return cls.secret_client.create_secret(**kwargs)
90
91 @classmethod
92 def delete_secret(cls, uuid):
93 cls.created_objects['secret'].remove(uuid)
94 return cls.secret_client.delete_secret(uuid)
Peter Hamilton7be3f7e2017-01-11 15:12:21 -050095
96 @classmethod
97 @creates('container')
98 def create_container(cls, **kwargs):
99 return cls.container_client.create_container(**kwargs)
100
101 @classmethod
102 def delete_container(cls, uuid):
103 cls.created_objects['container'].remove(uuid)
104 return cls.container_client.delete_container(uuid)