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 | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 26 | RESOURCE_TYPES = ['container', 'order', 'quota', 'secret'] |
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) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 40 | if resource == 'container': |
| 41 | uuid = _get_uuid(resp['container_ref']) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 42 | 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 Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 48 | uuid = _get_uuid(resp['order_ref']) |
| 49 | if resource == 'quota': |
| 50 | uuid = _get_uuid(args[0]) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 51 | if resource == 'secret': |
| 52 | uuid = _get_uuid(resp['secret_ref']) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 53 | cls.created_objects[resource].add(uuid) |
| 54 | return resp |
| 55 | return wrapper |
| 56 | return decorator |
| 57 | |
| 58 | |
| 59 | class 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 Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 63 | credentials = ('admin', ['service_admin', 'key-manager:service-admin']) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 64 | client_manager = clients.Clients |
| 65 | created_objects = {} |
| 66 | |
| 67 | @classmethod |
| 68 | def setup_clients(cls): |
| 69 | super(BaseKeyManagerTest, cls).setup_clients() |
| 70 | os = getattr(cls, 'os_%s' % cls.credentials[0]) |
Peter Hamilton | ee38917 | 2017-01-12 16:41:45 -0500 | [diff] [blame] | 71 | cls.consumer_client = os.secret_v1.ConsumerClient( |
| 72 | service='key-manager' |
| 73 | ) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 74 | cls.container_client = os.secret_v1.ContainerClient( |
| 75 | service='key-manager' |
| 76 | ) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 77 | cls.order_client = os.secret_v1.OrderClient(service='key-manager') |
| 78 | cls.secret_client = os.secret_v1.SecretClient(service='key-manager') |
dane-fichter | 2aff507 | 2017-01-11 17:20:42 -0800 | [diff] [blame] | 79 | cls.secret_metadata_client = os.secret_v1.SecretMetadataClient( |
| 80 | service='key-manager' |
| 81 | ) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 82 | |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 83 | os = getattr(cls, 'os_roles_%s' % cls.credentials[1][0]) |
| 84 | cls.quota_client = os.secret_v1.QuotaClient(service='key-manager') |
| 85 | |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 86 | @classmethod |
Ade Lee | c80b00a | 2021-02-09 14:49:41 -0500 | [diff] [blame] | 87 | def setup_credentials(cls): |
| 88 | super().setup_credentials() |
| 89 | cls.os_primary = getattr(cls, f'os_{cls.credentials[0]}') |
| 90 | |
| 91 | @classmethod |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 92 | def resource_setup(cls): |
| 93 | super(BaseKeyManagerTest, cls).resource_setup() |
| 94 | for resource in RESOURCE_TYPES: |
| 95 | cls.created_objects[resource] = set() |
| 96 | |
| 97 | @classmethod |
| 98 | def resource_cleanup(cls): |
| 99 | try: |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 100 | for container_uuid in list(cls.created_objects['container']): |
| 101 | cls.delete_container(container_uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 102 | for order_uuid in list(cls.created_objects['order']): |
| 103 | cls.delete_order(order_uuid) |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 104 | for project_quota_uuid in list(cls.created_objects['quota']): |
| 105 | cls.delete_project_quota(project_quota_uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 106 | for secret_uuid in list(cls.created_objects['secret']): |
| 107 | cls.delete_secret(secret_uuid) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 108 | finally: |
| 109 | super(BaseKeyManagerTest, cls).resource_cleanup() |
| 110 | |
| 111 | @classmethod |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 112 | @creates('container') |
| 113 | def create_container(cls, **kwargs): |
| 114 | return cls.container_client.create_container(**kwargs) |
| 115 | |
| 116 | @classmethod |
| 117 | def delete_container(cls, uuid): |
| 118 | cls.created_objects['container'].remove(uuid) |
| 119 | return cls.container_client.delete_container(uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 120 | |
| 121 | @classmethod |
| 122 | @creates('order') |
| 123 | def create_order(cls, **kwargs): |
| 124 | return cls.order_client.create_order(**kwargs) |
| 125 | |
| 126 | @classmethod |
| 127 | def get_order(cls, uuid): |
| 128 | return cls.order_client.get_order(uuid) |
| 129 | |
| 130 | @classmethod |
| 131 | def delete_order(cls, uuid): |
| 132 | cls.created_objects['order'].remove(uuid) |
| 133 | return cls.order_client.delete_order(uuid) |
| 134 | |
| 135 | @classmethod |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 136 | @creates('quota') |
| 137 | def create_project_quota(cls, project_id, **kwargs): |
| 138 | return cls.quota_client.create_project_quota(project_id, **kwargs) |
| 139 | |
| 140 | @classmethod |
| 141 | def delete_project_quota(cls, project_id): |
| 142 | cls.created_objects['quota'].remove(project_id) |
| 143 | return cls.quota_client.delete_project_quota(project_id) |
| 144 | |
| 145 | @classmethod |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 146 | @creates('secret') |
| 147 | def create_secret(cls, **kwargs): |
| 148 | return cls.secret_client.create_secret(**kwargs) |
| 149 | |
| 150 | @classmethod |
| 151 | def delete_secret(cls, uuid): |
| 152 | cls.created_objects['secret'].remove(uuid) |
| 153 | return cls.secret_client.delete_secret(uuid) |