blob: 225a713ee25613c8a49c7f4eedb218df5459d345 [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
43data_utils = DataUtils()
Andrea Frittolicd368412017-08-14 21:37:56 +010044
45
46def get_service_list():
47 service_list = {
48 'compute': CONF.service_available.nova,
49 'image': CONF.service_available.glance,
50 'volume': CONF.service_available.cinder,
51 # NOTE(masayukig): We have two network services which are neutron and
52 # nova-network. And we have no way to know whether nova-network is
53 # available or not. After the pending removal of nova-network from
54 # nova, we can treat the network/neutron case in the same manner as
55 # the other services.
56 'network': True,
57 # NOTE(masayukig): Tempest tests always require the identity service.
58 # So we should set this True here.
59 'identity': True,
60 'object_storage': CONF.service_available.swift,
61 }
62 return service_list
63
64
65def services(*args):
66 """A decorator used to set an attr for each service used in a test case
67
68 This decorator applies a testtools attr for each service that gets
69 exercised by a test case.
70 """
71 def decorator(f):
72 known_services = get_service_list()
73
74 for service in args:
75 if service not in known_services:
76 raise InvalidServiceTag('%s is not a valid service' % service)
77 decorators.attr(type=list(args))(f)
78
79 @functools.wraps(f)
lkuchlanf5c19052017-11-23 09:26:55 +020080 def wrapper(*func_args, **func_kwargs):
Andrea Frittolicd368412017-08-14 21:37:56 +010081 service_list = get_service_list()
82
83 for service in args:
84 if not service_list[service]:
85 msg = 'Skipped because the %s service is not available' % (
86 service)
87 raise testtools.TestCase.skipException(msg)
lkuchlanf5c19052017-11-23 09:26:55 +020088 return f(*func_args, **func_kwargs)
Andrea Frittolicd368412017-08-14 21:37:56 +010089 return wrapper
90 return decorator
91
92
93def requires_ext(**kwargs):
94 """A decorator to skip tests if an extension is not enabled
95
96 @param extension
97 @param service
98 """
99 def decorator(func):
100 @functools.wraps(func)
101 def wrapper(*func_args, **func_kwargs):
102 if not is_extension_enabled(kwargs['extension'],
103 kwargs['service']):
104 msg = "Skipped because %s extension: %s is not enabled" % (
105 kwargs['service'], kwargs['extension'])
106 raise testtools.TestCase.skipException(msg)
107 return func(*func_args, **func_kwargs)
108 return wrapper
109 return decorator
110
111
112def is_extension_enabled(extension_name, service):
113 """A function that will check the list of enabled extensions from config
114
115 """
116 config_dict = {
117 'compute': CONF.compute_feature_enabled.api_extensions,
118 'volume': CONF.volume_feature_enabled.api_extensions,
119 'network': CONF.network_feature_enabled.api_extensions,
120 'object': CONF.object_storage_feature_enabled.discoverable_apis,
121 'identity': CONF.identity_feature_enabled.api_extensions
122 }
123 if not config_dict[service]:
124 return False
125 if config_dict[service][0] == 'all':
126 return True
127 if extension_name in config_dict[service]:
128 return True
129 return False