Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 15 | import functools |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 16 | |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 17 | import testtools |
| 18 | |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 19 | from tempest import config |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 20 | from tempest.exceptions import InvalidServiceTag |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 21 | from tempest.lib import decorators |
| 22 | |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 23 | |
| 24 | CONF = config.CONF |
| 25 | |
| 26 | |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 27 | def get_service_list(): |
| 28 | service_list = { |
| 29 | 'compute': CONF.service_available.nova, |
| 30 | 'image': CONF.service_available.glance, |
| 31 | 'volume': CONF.service_available.cinder, |
| 32 | # NOTE(masayukig): We have two network services which are neutron and |
| 33 | # nova-network. And we have no way to know whether nova-network is |
| 34 | # available or not. After the pending removal of nova-network from |
| 35 | # nova, we can treat the network/neutron case in the same manner as |
| 36 | # the other services. |
| 37 | 'network': True, |
| 38 | # NOTE(masayukig): Tempest tests always require the identity service. |
| 39 | # So we should set this True here. |
| 40 | 'identity': True, |
| 41 | 'object_storage': CONF.service_available.swift, |
Ghanshyam Mann | eb3c7e3 | 2021-01-20 15:27:16 -0600 | [diff] [blame] | 42 | 'dashboard': CONF.service_available.horizon, |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 43 | } |
| 44 | return service_list |
| 45 | |
| 46 | |
| 47 | def services(*args): |
| 48 | """A decorator used to set an attr for each service used in a test case |
| 49 | |
| 50 | This decorator applies a testtools attr for each service that gets |
| 51 | exercised by a test case. |
| 52 | """ |
| 53 | def decorator(f): |
| 54 | known_services = get_service_list() |
| 55 | |
| 56 | for service in args: |
| 57 | if service not in known_services: |
| 58 | raise InvalidServiceTag('%s is not a valid service' % service) |
| 59 | decorators.attr(type=list(args))(f) |
| 60 | |
| 61 | @functools.wraps(f) |
lkuchlan | f5c1905 | 2017-11-23 09:26:55 +0200 | [diff] [blame] | 62 | def wrapper(*func_args, **func_kwargs): |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 63 | service_list = get_service_list() |
| 64 | |
| 65 | for service in args: |
| 66 | if not service_list[service]: |
| 67 | msg = 'Skipped because the %s service is not available' % ( |
| 68 | service) |
| 69 | raise testtools.TestCase.skipException(msg) |
lkuchlan | f5c1905 | 2017-11-23 09:26:55 +0200 | [diff] [blame] | 70 | return f(*func_args, **func_kwargs) |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 71 | return wrapper |
| 72 | return decorator |
| 73 | |
| 74 | |
| 75 | def requires_ext(**kwargs): |
| 76 | """A decorator to skip tests if an extension is not enabled |
| 77 | |
| 78 | @param extension |
| 79 | @param service |
| 80 | """ |
| 81 | def decorator(func): |
| 82 | @functools.wraps(func) |
| 83 | def wrapper(*func_args, **func_kwargs): |
| 84 | if not is_extension_enabled(kwargs['extension'], |
| 85 | kwargs['service']): |
| 86 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 87 | kwargs['service'], kwargs['extension']) |
| 88 | raise testtools.TestCase.skipException(msg) |
| 89 | return func(*func_args, **func_kwargs) |
| 90 | return wrapper |
| 91 | return decorator |
| 92 | |
| 93 | |
| 94 | def is_extension_enabled(extension_name, service): |
| 95 | """A function that will check the list of enabled extensions from config |
| 96 | |
| 97 | """ |
| 98 | config_dict = { |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 99 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 100 | 'network': CONF.network_feature_enabled.api_extensions, |
| 101 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
| 102 | 'identity': CONF.identity_feature_enabled.api_extensions |
| 103 | } |
| 104 | if not config_dict[service]: |
| 105 | return False |
| 106 | if config_dict[service][0] == 'all': |
| 107 | return True |
| 108 | if extension_name in config_dict[service]: |
| 109 | return True |
| 110 | return False |
elajkat | f9bb8b8 | 2020-11-02 13:41:06 +0100 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def is_network_feature_enabled(feature_name): |
| 114 | """A function that will check the list of available network features |
| 115 | |
| 116 | """ |
| 117 | list_of_features = CONF.network_feature_enabled.available_features |
| 118 | |
| 119 | if not list_of_features: |
| 120 | return False |
| 121 | if list_of_features[0] == 'all': |
| 122 | return True |
| 123 | if feature_name in list_of_features: |
| 124 | return True |
| 125 | return False |