Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 1 | # 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 | |
| 16 | import functools |
| 17 | |
| 18 | from tempest import config |
| 19 | from tempest import test |
| 20 | |
| 21 | from barbican_tempest_plugin import clients |
| 22 | |
| 23 | CONF = config.CONF |
| 24 | |
| 25 | # NOTE(dane-fichter): We need to track resource types for cleanup. |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 26 | RESOURCE_TYPES = ['secret', 'container'] |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def _get_uuid(href): |
| 30 | return href.split('/')[-1] |
| 31 | |
| 32 | |
| 33 | def 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 Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 42 | if resource == 'container': |
| 43 | uuid = _get_uuid(resp['container_ref']) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 44 | cls.created_objects[resource].add(uuid) |
| 45 | return resp |
| 46 | return wrapper |
| 47 | return decorator |
| 48 | |
| 49 | |
| 50 | class 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 Hamilton | ee38917 | 2017-01-12 16:41:45 -0500 | [diff] [blame^] | 62 | cls.consumer_client = os.secret_v1.ConsumerClient( |
| 63 | service='key-manager' |
| 64 | ) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 65 | cls.container_client = os.secret_v1.ContainerClient( |
| 66 | service='key-manager' |
| 67 | ) |
Peter Hamilton | ee38917 | 2017-01-12 16:41:45 -0500 | [diff] [blame^] | 68 | cls.secret_client = os.secret_v1.SecretClient(service='key-manager') |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 69 | |
| 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 Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 79 | for secret_uuid in list(cls.created_objects['secret']): |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 80 | cls.delete_secret(secret_uuid) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 81 | for container_uuid in list(cls.created_objects['container']): |
| 82 | cls.delete_container(container_uuid) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 83 | 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 Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 95 | |
| 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) |