blob: 167bf5b5a8d8bf63eb98d75bc168bdb53f89e419 [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 +000016from functools import partial
17
Andrea Frittolicd368412017-08-14 21:37:56 +010018import testtools
19
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000020from tempest import config
Andrea Frittolicd368412017-08-14 21:37:56 +010021from tempest.exceptions import InvalidServiceTag
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000022from tempest.lib.common.utils import data_utils as lib_data_utils
Andrea Frittolicd368412017-08-14 21:37:56 +010023from tempest.lib import decorators
24
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000025
26CONF = config.CONF
27
28
29class 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
ghanshyamb20f7e62017-12-10 07:10:22 +030034 # includes a random number and a prefix 'tempest'
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000035 attr_obj = partial(lib_data_utils.rand_name,
ghanshyamb20f7e62017-12-10 07:10:22 +030036 prefix='tempest')
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000037 else:
38 attr_obj = getattr(lib_data_utils, attr)
39
40 self.__dict__[attr] = attr_obj
41 return attr_obj
42
Stephen Finucane7f4a6212018-07-06 13:58:21 +010043
Rabi Mishra8e87ccb2017-07-25 04:30:21 +000044data_utils = DataUtils()
Andrea Frittolicd368412017-08-14 21:37:56 +010045
46
47def 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
66def 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)
lkuchlanf5c19052017-11-23 09:26:55 +020081 def wrapper(*func_args, **func_kwargs):
Andrea Frittolicd368412017-08-14 21:37:56 +010082 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)
lkuchlanf5c19052017-11-23 09:26:55 +020089 return f(*func_args, **func_kwargs)
Andrea Frittolicd368412017-08-14 21:37:56 +010090 return wrapper
91 return decorator
92
93
94def 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
113def 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