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 | from functools import partial |
| 17 | |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 18 | import testtools |
| 19 | |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 20 | from tempest import config |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 21 | from tempest.exceptions import InvalidServiceTag |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 22 | from tempest.lib.common.utils import data_utils as lib_data_utils |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 23 | from tempest.lib import decorators |
| 24 | |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 25 | |
| 26 | CONF = config.CONF |
| 27 | |
| 28 | |
| 29 | class DataUtils(object): |
| 30 | def __getattr__(self, attr): |
| 31 | |
| 32 | if attr == 'rand_name': |
| 33 | # NOTE(flwang): This is a proxy to generate a random name that |
ghanshyam | b20f7e6 | 2017-12-10 07:10:22 +0300 | [diff] [blame] | 34 | # includes a random number and a prefix 'tempest' |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 35 | attr_obj = partial(lib_data_utils.rand_name, |
ghanshyam | b20f7e6 | 2017-12-10 07:10:22 +0300 | [diff] [blame] | 36 | prefix='tempest') |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 37 | else: |
| 38 | attr_obj = getattr(lib_data_utils, attr) |
| 39 | |
| 40 | self.__dict__[attr] = attr_obj |
| 41 | return attr_obj |
| 42 | |
Stephen Finucane | 7f4a621 | 2018-07-06 13:58:21 +0100 | [diff] [blame] | 43 | |
Rabi Mishra | 8e87ccb | 2017-07-25 04:30:21 +0000 | [diff] [blame] | 44 | data_utils = DataUtils() |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def get_service_list(): |
| 48 | service_list = { |
| 49 | 'compute': CONF.service_available.nova, |
| 50 | 'image': CONF.service_available.glance, |
| 51 | 'volume': CONF.service_available.cinder, |
| 52 | # NOTE(masayukig): We have two network services which are neutron and |
| 53 | # nova-network. And we have no way to know whether nova-network is |
| 54 | # available or not. After the pending removal of nova-network from |
| 55 | # nova, we can treat the network/neutron case in the same manner as |
| 56 | # the other services. |
| 57 | 'network': True, |
| 58 | # NOTE(masayukig): Tempest tests always require the identity service. |
| 59 | # So we should set this True here. |
| 60 | 'identity': True, |
| 61 | 'object_storage': CONF.service_available.swift, |
| 62 | } |
| 63 | return service_list |
| 64 | |
| 65 | |
| 66 | def services(*args): |
| 67 | """A decorator used to set an attr for each service used in a test case |
| 68 | |
| 69 | This decorator applies a testtools attr for each service that gets |
| 70 | exercised by a test case. |
| 71 | """ |
| 72 | def decorator(f): |
| 73 | known_services = get_service_list() |
| 74 | |
| 75 | for service in args: |
| 76 | if service not in known_services: |
| 77 | raise InvalidServiceTag('%s is not a valid service' % service) |
| 78 | decorators.attr(type=list(args))(f) |
| 79 | |
| 80 | @functools.wraps(f) |
lkuchlan | f5c1905 | 2017-11-23 09:26:55 +0200 | [diff] [blame] | 81 | def wrapper(*func_args, **func_kwargs): |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 82 | service_list = get_service_list() |
| 83 | |
| 84 | for service in args: |
| 85 | if not service_list[service]: |
| 86 | msg = 'Skipped because the %s service is not available' % ( |
| 87 | service) |
| 88 | raise testtools.TestCase.skipException(msg) |
lkuchlan | f5c1905 | 2017-11-23 09:26:55 +0200 | [diff] [blame] | 89 | return f(*func_args, **func_kwargs) |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 90 | return wrapper |
| 91 | return decorator |
| 92 | |
| 93 | |
| 94 | def requires_ext(**kwargs): |
| 95 | """A decorator to skip tests if an extension is not enabled |
| 96 | |
| 97 | @param extension |
| 98 | @param service |
| 99 | """ |
| 100 | def decorator(func): |
| 101 | @functools.wraps(func) |
| 102 | def wrapper(*func_args, **func_kwargs): |
| 103 | if not is_extension_enabled(kwargs['extension'], |
| 104 | kwargs['service']): |
| 105 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 106 | kwargs['service'], kwargs['extension']) |
| 107 | raise testtools.TestCase.skipException(msg) |
| 108 | return func(*func_args, **func_kwargs) |
| 109 | return wrapper |
| 110 | return decorator |
| 111 | |
| 112 | |
| 113 | def is_extension_enabled(extension_name, service): |
| 114 | """A function that will check the list of enabled extensions from config |
| 115 | |
| 116 | """ |
| 117 | config_dict = { |
| 118 | 'compute': CONF.compute_feature_enabled.api_extensions, |
| 119 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 120 | 'network': CONF.network_feature_enabled.api_extensions, |
| 121 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
| 122 | 'identity': CONF.identity_feature_enabled.api_extensions |
| 123 | } |
| 124 | if not config_dict[service]: |
| 125 | return False |
| 126 | if config_dict[service][0] == 'all': |
| 127 | return True |
| 128 | if extension_name in config_dict[service]: |
| 129 | return True |
| 130 | return False |