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 |
Ade Lee | 519aa80 | 2022-05-06 12:33:06 -0400 | [diff] [blame] | 19 | from tempest.lib.common import api_version_utils |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 20 | from tempest import test |
| 21 | |
| 22 | from barbican_tempest_plugin import clients |
| 23 | |
| 24 | CONF = config.CONF |
| 25 | |
| 26 | # NOTE(dane-fichter): We need to track resource types for cleanup. |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 27 | RESOURCE_TYPES = ['container', 'order', 'quota', 'secret'] |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def _get_uuid(href): |
| 31 | return href.split('/')[-1] |
| 32 | |
| 33 | |
| 34 | def creates(resource): |
| 35 | """Decorator that adds resource UUIDs to queue for cleanup""" |
| 36 | |
| 37 | def decorator(f): |
| 38 | @functools.wraps(f) |
| 39 | def wrapper(cls, *args, **kwargs): |
| 40 | resp = f(cls, *args, **kwargs) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 41 | if resource == 'container': |
| 42 | uuid = _get_uuid(resp['container_ref']) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 43 | if resource == 'order': |
| 44 | uuid = _get_uuid(resp.get('order_ref')) |
| 45 | order_metadata = cls.get_order(uuid) |
| 46 | secret_ref = order_metadata.get('secret_ref') |
| 47 | if secret_ref: |
| 48 | cls.created_objects['secret'].add(_get_uuid(secret_ref)) |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 49 | uuid = _get_uuid(resp['order_ref']) |
| 50 | if resource == 'quota': |
| 51 | uuid = _get_uuid(args[0]) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 52 | if resource == 'secret': |
| 53 | uuid = _get_uuid(resp['secret_ref']) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 54 | cls.created_objects[resource].add(uuid) |
| 55 | return resp |
| 56 | return wrapper |
| 57 | return decorator |
| 58 | |
| 59 | |
Ade Lee | 519aa80 | 2022-05-06 12:33:06 -0400 | [diff] [blame] | 60 | class BaseKeyManagerTest(test.BaseTestCase, |
| 61 | api_version_utils.BaseMicroversionTest): |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 62 | """Base class for all api tests.""" |
| 63 | |
| 64 | # 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] | 65 | credentials = ('admin', ['service_admin', 'key-manager:service-admin']) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 66 | client_manager = clients.Clients |
| 67 | created_objects = {} |
| 68 | |
| 69 | @classmethod |
Ade Lee | 519aa80 | 2022-05-06 12:33:06 -0400 | [diff] [blame] | 70 | def skip_checks(cls): |
| 71 | super().skip_checks() |
| 72 | api_version_utils.check_skip_with_microversion( |
| 73 | cls.min_microversion, |
| 74 | cls.max_microversion, |
| 75 | CONF.key_manager.min_microversion, |
| 76 | CONF.key_manager.max_microversion) |
| 77 | |
| 78 | @classmethod |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 79 | def setup_clients(cls): |
| 80 | super(BaseKeyManagerTest, cls).setup_clients() |
| 81 | os = getattr(cls, 'os_%s' % cls.credentials[0]) |
Peter Hamilton | ee38917 | 2017-01-12 16:41:45 -0500 | [diff] [blame] | 82 | cls.consumer_client = os.secret_v1.ConsumerClient( |
| 83 | service='key-manager' |
| 84 | ) |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 85 | cls.container_client = os.secret_v1.ContainerClient( |
| 86 | service='key-manager' |
| 87 | ) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 88 | cls.order_client = os.secret_v1.OrderClient(service='key-manager') |
| 89 | cls.secret_client = os.secret_v1.SecretClient(service='key-manager') |
Ade Lee | 519aa80 | 2022-05-06 12:33:06 -0400 | [diff] [blame] | 90 | cls.secret_consumer_client = os.secret_v1_1.SecretConsumerClient() |
dane-fichter | 2aff507 | 2017-01-11 17:20:42 -0800 | [diff] [blame] | 91 | cls.secret_metadata_client = os.secret_v1.SecretMetadataClient( |
| 92 | service='key-manager' |
| 93 | ) |
Ade Lee | 519aa80 | 2022-05-06 12:33:06 -0400 | [diff] [blame] | 94 | cls.version_client = os.secret_v1_1.VersionClient() |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 95 | |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 96 | os = getattr(cls, 'os_roles_%s' % cls.credentials[1][0]) |
| 97 | cls.quota_client = os.secret_v1.QuotaClient(service='key-manager') |
| 98 | |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 99 | @classmethod |
Ade Lee | c80b00a | 2021-02-09 14:49:41 -0500 | [diff] [blame] | 100 | def setup_credentials(cls): |
| 101 | super().setup_credentials() |
| 102 | cls.os_primary = getattr(cls, f'os_{cls.credentials[0]}') |
| 103 | |
| 104 | @classmethod |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 105 | def resource_setup(cls): |
| 106 | super(BaseKeyManagerTest, cls).resource_setup() |
| 107 | for resource in RESOURCE_TYPES: |
| 108 | cls.created_objects[resource] = set() |
| 109 | |
| 110 | @classmethod |
| 111 | def resource_cleanup(cls): |
| 112 | try: |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 113 | for container_uuid in list(cls.created_objects['container']): |
| 114 | cls.delete_container(container_uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 115 | for order_uuid in list(cls.created_objects['order']): |
| 116 | cls.delete_order(order_uuid) |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 117 | for project_quota_uuid in list(cls.created_objects['quota']): |
| 118 | cls.delete_project_quota(project_quota_uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 119 | for secret_uuid in list(cls.created_objects['secret']): |
| 120 | cls.delete_secret(secret_uuid) |
Marc Koderer | c832c61 | 2016-12-12 11:49:10 +0100 | [diff] [blame] | 121 | finally: |
| 122 | super(BaseKeyManagerTest, cls).resource_cleanup() |
| 123 | |
| 124 | @classmethod |
Peter Hamilton | 7be3f7e | 2017-01-11 15:12:21 -0500 | [diff] [blame] | 125 | @creates('container') |
| 126 | def create_container(cls, **kwargs): |
| 127 | return cls.container_client.create_container(**kwargs) |
| 128 | |
| 129 | @classmethod |
| 130 | def delete_container(cls, uuid): |
| 131 | cls.created_objects['container'].remove(uuid) |
| 132 | return cls.container_client.delete_container(uuid) |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 133 | |
| 134 | @classmethod |
| 135 | @creates('order') |
| 136 | def create_order(cls, **kwargs): |
| 137 | return cls.order_client.create_order(**kwargs) |
| 138 | |
| 139 | @classmethod |
| 140 | def get_order(cls, uuid): |
| 141 | return cls.order_client.get_order(uuid) |
| 142 | |
| 143 | @classmethod |
| 144 | def delete_order(cls, uuid): |
| 145 | cls.created_objects['order'].remove(uuid) |
| 146 | return cls.order_client.delete_order(uuid) |
| 147 | |
| 148 | @classmethod |
Peter Hamilton | 2adeed6 | 2017-02-01 12:23:45 -0500 | [diff] [blame] | 149 | @creates('quota') |
| 150 | def create_project_quota(cls, project_id, **kwargs): |
| 151 | return cls.quota_client.create_project_quota(project_id, **kwargs) |
| 152 | |
| 153 | @classmethod |
| 154 | def delete_project_quota(cls, project_id): |
| 155 | cls.created_objects['quota'].remove(project_id) |
| 156 | return cls.quota_client.delete_project_quota(project_id) |
| 157 | |
| 158 | @classmethod |
Peter Hamilton | f4166be | 2017-01-20 10:54:33 -0500 | [diff] [blame] | 159 | @creates('secret') |
| 160 | def create_secret(cls, **kwargs): |
| 161 | return cls.secret_client.create_secret(**kwargs) |
| 162 | |
| 163 | @classmethod |
| 164 | def delete_secret(cls, uuid): |
| 165 | cls.created_objects['secret'].remove(uuid) |
| 166 | return cls.secret_client.delete_secret(uuid) |