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. |
| 26 | RESOURCE_TYPES = ['secret'] |
| 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']) |
| 42 | cls.created_objects[resource].add(uuid) |
| 43 | return resp |
| 44 | return wrapper |
| 45 | return decorator |
| 46 | |
| 47 | |
| 48 | class BaseKeyManagerTest(test.BaseTestCase): |
| 49 | """Base class for all api tests.""" |
| 50 | |
| 51 | # Why do I have to be an admin to create secrets? No idea... |
| 52 | credentials = ('admin', ) |
| 53 | client_manager = clients.Clients |
| 54 | created_objects = {} |
| 55 | |
| 56 | @classmethod |
| 57 | def setup_clients(cls): |
| 58 | super(BaseKeyManagerTest, cls).setup_clients() |
| 59 | os = getattr(cls, 'os_%s' % cls.credentials[0]) |
| 60 | cls.secret_client = os.secret_v1.SecretClient(service='key-manager') |
| 61 | |
| 62 | @classmethod |
| 63 | def resource_setup(cls): |
| 64 | super(BaseKeyManagerTest, cls).resource_setup() |
| 65 | for resource in RESOURCE_TYPES: |
| 66 | cls.created_objects[resource] = set() |
| 67 | |
| 68 | @classmethod |
| 69 | def resource_cleanup(cls): |
| 70 | try: |
| 71 | for secret_uuid in cls.created_objects['secret']: |
| 72 | cls.delete_secret(secret_uuid) |
| 73 | finally: |
| 74 | super(BaseKeyManagerTest, cls).resource_cleanup() |
| 75 | |
| 76 | @classmethod |
| 77 | @creates('secret') |
| 78 | def create_secret(cls, **kwargs): |
| 79 | return cls.secret_client.create_secret(**kwargs) |
| 80 | |
| 81 | @classmethod |
| 82 | def delete_secret(cls, uuid): |
| 83 | cls.created_objects['secret'].remove(uuid) |
| 84 | return cls.secret_client.delete_secret(uuid) |