blob: 0fa5ce42fb58bd668f32143ba6e9dc0c2623ca4a [file] [log] [blame]
Rabi Mishra8e87ccb2017-07-25 04:30:21 +00001# 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 Frittolicd368412017-08-14 21:37:56 +010015import functools
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000016
Andrea Frittolicd368412017-08-14 21:37:56 +010017import testtools
18
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000019from tempest import config
Andrea Frittolicd368412017-08-14 21:37:56 +010020from tempest.exceptions import InvalidServiceTag
Andrea Frittolicd368412017-08-14 21:37:56 +010021from tempest.lib import decorators
22
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000023
24CONF = config.CONF
25
26
Andrea Frittolicd368412017-08-14 21:37:56 +010027def 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 Manneb3c7e32021-01-20 15:27:16 -060042 'dashboard': CONF.service_available.horizon,
Andrea Frittolicd368412017-08-14 21:37:56 +010043 }
44 return service_list
45
46
47def 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)
lkuchlanf5c19052017-11-23 09:26:55 +020062 def wrapper(*func_args, **func_kwargs):
Andrea Frittolicd368412017-08-14 21:37:56 +010063 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)
lkuchlanf5c19052017-11-23 09:26:55 +020070 return f(*func_args, **func_kwargs)
Andrea Frittolicd368412017-08-14 21:37:56 +010071 return wrapper
72 return decorator
73
74
75def 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
94def 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 Frittolicd368412017-08-14 21:37:56 +010099 '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
elajkatf9bb8b82020-11-02 13:41:06 +0100111
112
113def 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