ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 16 | import atexit |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 17 | import functools |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 18 | import os |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 19 | import re |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 20 | import sys |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 21 | import time |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 22 | import uuid |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 23 | |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 24 | import fixtures |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 25 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 26 | from oslo_serialization import jsonutils as json |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 27 | from oslo_utils import importutils |
Chris Hoge | 296558c | 2015-02-19 00:29:49 -0600 | [diff] [blame] | 28 | import six |
Harshada Mangesh Kakad | 71f6b97 | 2015-12-22 09:46:48 -0800 | [diff] [blame] | 29 | from six.moves import urllib |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 30 | import testscenarios |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 31 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 32 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 33 | from tempest import clients |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 34 | from tempest.common import cred_client |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 35 | from tempest.common import credentials_factory as credentials |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 36 | from tempest.common import fixed_network |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 37 | import tempest.common.generator.valid_generator as valid |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 38 | import tempest.common.validation_resources as vresources |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 39 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 40 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame^] | 41 | from tempest.lib import decorators |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 42 | |
| 43 | LOG = logging.getLogger(__name__) |
| 44 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 45 | CONF = config.CONF |
| 46 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 47 | idempotent_id = decorators.idempotent_id |
| 48 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 49 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 50 | def attr(**kwargs): |
liuchenhong | 00caec5 | 2015-07-19 22:40:28 +0800 | [diff] [blame] | 51 | """A decorator which applies the testtools attr decorator |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 52 | |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 53 | This decorator applies the testtools.testcase.attr if it is in the list of |
| 54 | attributes to testtools we want to apply. |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 55 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 56 | |
| 57 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 58 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 59 | f = testtools.testcase.attr(kwargs['type'])(f) |
| 60 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 61 | for attr in kwargs['type']: |
| 62 | f = testtools.testcase.attr(attr)(f) |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 63 | return f |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 64 | |
| 65 | return decorator |
| 66 | |
| 67 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 68 | def get_service_list(): |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 69 | service_list = { |
| 70 | 'compute': CONF.service_available.nova, |
| 71 | 'image': CONF.service_available.glance, |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 72 | 'baremetal': CONF.service_available.ironic, |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 73 | 'volume': CONF.service_available.cinder, |
| 74 | 'orchestration': CONF.service_available.heat, |
| 75 | # NOTE(mtreinish) nova-network will provide networking functionality |
| 76 | # if neutron isn't available, so always set to True. |
| 77 | 'network': True, |
| 78 | 'identity': True, |
| 79 | 'object_storage': CONF.service_available.swift, |
| 80 | 'dashboard': CONF.service_available.horizon, |
ekhugen | 7aff099 | 2014-08-04 19:01:57 +0000 | [diff] [blame] | 81 | 'telemetry': CONF.service_available.ceilometer, |
Matthew Treinish | b66c94e | 2015-03-11 13:00:48 -0400 | [diff] [blame] | 82 | 'data_processing': CONF.service_available.sahara, |
| 83 | 'database': CONF.service_available.trove |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 84 | } |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 85 | return service_list |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 86 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 87 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 88 | def services(*args): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 89 | """A decorator used to set an attr for each service used in a test case |
| 90 | |
| 91 | This decorator applies a testtools attr for each service that gets |
| 92 | exercised by a test case. |
| 93 | """ |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 94 | def decorator(f): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 95 | services = ['compute', 'image', 'baremetal', 'volume', 'orchestration', |
| 96 | 'network', 'identity', 'object_storage', 'dashboard', |
Matthew Treinish | b66c94e | 2015-03-11 13:00:48 -0400 | [diff] [blame] | 97 | 'telemetry', 'data_processing', 'database'] |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 98 | for service in args: |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 99 | if service not in services: |
| 100 | raise exceptions.InvalidServiceTag('%s is not a valid ' |
| 101 | 'service' % service) |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 102 | attr(type=list(args))(f) |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 103 | |
| 104 | @functools.wraps(f) |
| 105 | def wrapper(self, *func_args, **func_kwargs): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 106 | service_list = get_service_list() |
| 107 | |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 108 | for service in args: |
| 109 | if not service_list[service]: |
| 110 | msg = 'Skipped because the %s service is not available' % ( |
| 111 | service) |
| 112 | raise testtools.TestCase.skipException(msg) |
| 113 | return f(self, *func_args, **func_kwargs) |
| 114 | return wrapper |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 115 | return decorator |
| 116 | |
| 117 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 118 | def stresstest(**kwargs): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 119 | """Add stress test decorator |
| 120 | |
| 121 | For all functions with this decorator a attr stress will be |
| 122 | set automatically. |
| 123 | |
| 124 | @param class_setup_per: allowed values are application, process, action |
| 125 | ``application``: once in the stress job lifetime |
| 126 | ``process``: once in the worker process lifetime |
| 127 | ``action``: on each action |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 128 | @param allow_inheritance: allows inheritance of this attribute |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 129 | """ |
| 130 | def decorator(f): |
| 131 | if 'class_setup_per' in kwargs: |
| 132 | setattr(f, "st_class_setup_per", kwargs['class_setup_per']) |
| 133 | else: |
| 134 | setattr(f, "st_class_setup_per", 'process') |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 135 | if 'allow_inheritance' in kwargs: |
| 136 | setattr(f, "st_allow_inheritance", kwargs['allow_inheritance']) |
| 137 | else: |
| 138 | setattr(f, "st_allow_inheritance", False) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 139 | attr(type='stress')(f) |
| 140 | return f |
| 141 | return decorator |
| 142 | |
| 143 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 144 | def requires_ext(**kwargs): |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 145 | """A decorator to skip tests if an extension is not enabled |
| 146 | |
| 147 | @param extension |
| 148 | @param service |
| 149 | """ |
| 150 | def decorator(func): |
| 151 | @functools.wraps(func) |
| 152 | def wrapper(*func_args, **func_kwargs): |
| 153 | if not is_extension_enabled(kwargs['extension'], |
| 154 | kwargs['service']): |
| 155 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 156 | kwargs['service'], kwargs['extension']) |
| 157 | raise testtools.TestCase.skipException(msg) |
| 158 | return func(*func_args, **func_kwargs) |
| 159 | return wrapper |
| 160 | return decorator |
| 161 | |
| 162 | |
| 163 | def is_extension_enabled(extension_name, service): |
| 164 | """A function that will check the list of enabled extensions from config |
| 165 | |
| 166 | """ |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 167 | config_dict = { |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 168 | 'compute': CONF.compute_feature_enabled.api_extensions, |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 169 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 170 | 'network': CONF.network_feature_enabled.api_extensions, |
| 171 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
Jane Zadorozhna | 121576d | 2015-06-23 12:57:13 +0300 | [diff] [blame] | 172 | 'identity': CONF.identity_feature_enabled.api_extensions |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 173 | } |
Simeon Monov | 5d7effe | 2014-07-16 07:32:38 +0300 | [diff] [blame] | 174 | if len(config_dict[service]) == 0: |
| 175 | return False |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 176 | if config_dict[service][0] == 'all': |
| 177 | return True |
| 178 | if extension_name in config_dict[service]: |
| 179 | return True |
| 180 | return False |
| 181 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 182 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 183 | at_exit_set = set() |
| 184 | |
| 185 | |
| 186 | def validate_tearDownClass(): |
| 187 | if at_exit_set: |
Sean Dague | eb1523b | 2014-03-10 10:17:44 -0400 | [diff] [blame] | 188 | LOG.error( |
| 189 | "tearDownClass does not call the super's " |
| 190 | "tearDownClass in these classes: \n" |
| 191 | + str(at_exit_set)) |
| 192 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 193 | |
| 194 | atexit.register(validate_tearDownClass) |
| 195 | |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 196 | |
Matthew Treinish | 2474f41 | 2014-11-17 18:11:56 -0500 | [diff] [blame] | 197 | class BaseTestCase(testtools.testcase.WithAttributes, |
| 198 | testtools.TestCase): |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 199 | """The test base class defines Tempest framework for class level fixtures. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 200 | |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 201 | `setUpClass` and `tearDownClass` are defined here and cannot be overwritten |
| 202 | by subclasses (enforced via hacking rule T105). |
| 203 | |
| 204 | Set-up is split in a series of steps (setup stages), which can be |
| 205 | overwritten by test classes. Set-up stages are: |
| 206 | - skip_checks |
| 207 | - setup_credentials |
| 208 | - setup_clients |
| 209 | - resource_setup |
| 210 | |
| 211 | Tear-down is also split in a series of steps (teardown stages), which are |
| 212 | stacked for execution only if the corresponding setup stage had been |
| 213 | reached during the setup phase. Tear-down stages are: |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 214 | - clear_credentials (defined in the base test class) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 215 | - resource_cleanup |
| 216 | """ |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 217 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 218 | setUpClassCalled = False |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 219 | _service = None |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 220 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 221 | # NOTE(andreaf) credentials holds a list of the credentials to be allocated |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 222 | # at class setup time. Credential types can be 'primary', 'alt', 'admin' or |
| 223 | # a list of roles - the first element of the list being a label, and the |
| 224 | # rest the actual roles |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 225 | credentials = [] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 226 | # Resources required to validate a server using ssh |
| 227 | validation_resources = {} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 228 | network_resources = {} |
| 229 | |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 230 | # NOTE(sdague): log_format is defined inline here instead of using the oslo |
| 231 | # default because going through the config path recouples config to the |
| 232 | # stress tests too early, and depending on testr order will fail unit tests |
| 233 | log_format = ('%(asctime)s %(process)d %(levelname)-8s ' |
| 234 | '[%(name)s] %(message)s') |
| 235 | |
Ryota MIBU | 60687e5 | 2015-12-09 18:37:39 +0900 | [diff] [blame] | 236 | # Client manager class to use in this test case. |
| 237 | client_manager = clients.Manager |
| 238 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 239 | @classmethod |
| 240 | def setUpClass(cls): |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 241 | # It should never be overridden by descendants |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 242 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 243 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 244 | cls.setUpClassCalled = True |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 245 | # Stack of (name, callable) to be invoked in reverse order at teardown |
| 246 | cls.teardowns = [] |
| 247 | # All the configuration checks that may generate a skip |
| 248 | cls.skip_checks() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 249 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 250 | # Allocation of all required credentials and client managers |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 251 | cls.teardowns.append(('credentials', cls.clear_credentials)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 252 | cls.setup_credentials() |
| 253 | # Shortcuts to clients |
| 254 | cls.setup_clients() |
| 255 | # Additional class-wide test resources |
| 256 | cls.teardowns.append(('resources', cls.resource_cleanup)) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 257 | cls.resource_setup() |
| 258 | except Exception: |
| 259 | etype, value, trace = sys.exc_info() |
Matthew Treinish | ed2ad4f | 2014-12-23 15:18:32 -0500 | [diff] [blame] | 260 | LOG.info("%s raised in %s.setUpClass. Invoking tearDownClass." % ( |
| 261 | etype, cls.__name__)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 262 | cls.tearDownClass() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 263 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 264 | six.reraise(etype, value, trace) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 265 | finally: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 266 | del trace # to avoid circular refs |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 267 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 268 | @classmethod |
| 269 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 270 | at_exit_set.discard(cls) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 271 | # It should never be overridden by descendants |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 272 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 273 | super(BaseTestCase, cls).tearDownClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 274 | # Save any existing exception, we always want to re-raise the original |
| 275 | # exception only |
| 276 | etype, value, trace = sys.exc_info() |
| 277 | # If there was no exception during setup we shall re-raise the first |
| 278 | # exception in teardown |
| 279 | re_raise = (etype is None) |
| 280 | while cls.teardowns: |
| 281 | name, teardown = cls.teardowns.pop() |
| 282 | # Catch any exception in tearDown so we can re-raise the original |
| 283 | # exception at the end |
| 284 | try: |
| 285 | teardown() |
| 286 | except Exception as te: |
| 287 | sys_exec_info = sys.exc_info() |
| 288 | tetype = sys_exec_info[0] |
| 289 | # TODO(andreaf): Till we have the ability to cleanup only |
| 290 | # resources that were successfully setup in resource_cleanup, |
| 291 | # log AttributeError as info instead of exception. |
| 292 | if tetype is AttributeError and name == 'resources': |
| 293 | LOG.info("tearDownClass of %s failed: %s" % (name, te)) |
| 294 | else: |
| 295 | LOG.exception("teardown of %s failed: %s" % (name, te)) |
| 296 | if not etype: |
| 297 | etype, value, trace = sys_exec_info |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 298 | # If exceptions were raised during teardown, and not before, re-raise |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 299 | # the first one |
| 300 | if re_raise and etype is not None: |
| 301 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 302 | six.reraise(etype, value, trace) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 303 | finally: |
| 304 | del trace # to avoid circular refs |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 305 | |
| 306 | @classmethod |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 307 | def skip_checks(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 308 | """Class level skip checks. |
| 309 | |
| 310 | Subclasses verify in here all conditions that might prevent the |
| 311 | execution of the entire test class. |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 312 | Checks implemented here may not make use API calls, and should rely on |
| 313 | configuration alone. |
| 314 | In general skip checks that require an API call are discouraged. |
| 315 | If one is really needed it may be implemented either in the |
| 316 | resource_setup or at test level. |
| 317 | """ |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 318 | identity_version = cls.get_identity_version() |
| 319 | if 'admin' in cls.credentials and not credentials.is_admin_available( |
| 320 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 321 | msg = "Missing Identity Admin API credentials in configuration." |
| 322 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 323 | if 'alt' in cls.credentials and not credentials.is_alt_available( |
| 324 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 325 | msg = "Missing a 2nd set of API credentials in configuration." |
| 326 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 327 | if hasattr(cls, 'identity_version'): |
| 328 | if cls.identity_version == 'v2': |
| 329 | if not CONF.identity_feature_enabled.api_v2: |
| 330 | raise cls.skipException("Identity api v2 is not enabled") |
| 331 | elif cls.identity_version == 'v3': |
| 332 | if not CONF.identity_feature_enabled.api_v3: |
| 333 | raise cls.skipException("Identity api v3 is not enabled") |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 334 | |
| 335 | @classmethod |
| 336 | def setup_credentials(cls): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 337 | """Allocate credentials and the client managers from them. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 338 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 339 | A test class that requires network resources must override |
| 340 | setup_credentials and defined the required resources before super |
| 341 | is invoked. |
| 342 | """ |
| 343 | for credentials_type in cls.credentials: |
| 344 | # This may raise an exception in case credentials are not available |
| 345 | # In that case we want to let the exception through and the test |
| 346 | # fail accordingly |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 347 | if isinstance(credentials_type, six.string_types): |
| 348 | manager = cls.get_client_manager( |
| 349 | credential_type=credentials_type) |
| 350 | setattr(cls, 'os_%s' % credentials_type, manager) |
| 351 | # Setup some common aliases |
| 352 | # TODO(andreaf) The aliases below are a temporary hack |
| 353 | # to avoid changing too much code in one patch. They should |
| 354 | # be removed eventually |
| 355 | if credentials_type == 'primary': |
| 356 | cls.os = cls.manager = cls.os_primary |
| 357 | if credentials_type == 'admin': |
| 358 | cls.os_adm = cls.admin_manager = cls.os_admin |
| 359 | if credentials_type == 'alt': |
| 360 | cls.alt_manager = cls.os_alt |
| 361 | elif isinstance(credentials_type, list): |
| 362 | manager = cls.get_client_manager(roles=credentials_type[1:], |
| 363 | force_new=True) |
| 364 | setattr(cls, 'os_roles_%s' % credentials_type[0], manager) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 365 | |
| 366 | @classmethod |
| 367 | def setup_clients(cls): |
| 368 | """Create links to the clients into the test object.""" |
| 369 | # TODO(andreaf) There is a fair amount of code that could me moved from |
| 370 | # base / test classes in here. Ideally tests should be able to only |
| 371 | # specify which client is `client` and nothing else. |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 372 | pass |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 373 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 374 | @classmethod |
| 375 | def resource_setup(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 376 | """Class level resource setup for test cases.""" |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 377 | if hasattr(cls, "os"): |
| 378 | cls.validation_resources = vresources.create_validation_resources( |
| 379 | cls.os, cls.validation_resources) |
| 380 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 381 | LOG.warning("Client manager not found, validation resources not" |
| 382 | " created") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 383 | |
| 384 | @classmethod |
| 385 | def resource_cleanup(cls): |
| 386 | """Class level resource cleanup for test cases. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 387 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 388 | Resource cleanup must be able to handle the case of partially setup |
| 389 | resources, in case a failure during `resource_setup` should happen. |
| 390 | """ |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 391 | if cls.validation_resources: |
| 392 | if hasattr(cls, "os"): |
| 393 | vresources.clear_validation_resources(cls.os, |
| 394 | cls.validation_resources) |
| 395 | cls.validation_resources = {} |
| 396 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 397 | LOG.warning("Client manager not found, validation resources " |
| 398 | "not deleted") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 399 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 400 | def setUp(self): |
| 401 | super(BaseTestCase, self).setUp() |
| 402 | if not self.setUpClassCalled: |
| 403 | raise RuntimeError("setUpClass does not calls the super's" |
| 404 | "setUpClass in the " |
| 405 | + self.__class__.__name__) |
| 406 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 407 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 408 | try: |
| 409 | test_timeout = int(test_timeout) |
| 410 | except ValueError: |
| 411 | test_timeout = 0 |
| 412 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 413 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 414 | |
| 415 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 416 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 417 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 418 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 419 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 420 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 421 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 422 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 423 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 424 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 425 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 426 | format=self.log_format, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 427 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 428 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 429 | @property |
| 430 | def credentials_provider(self): |
| 431 | return self._get_credentials_provider() |
| 432 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 433 | @property |
| 434 | def identity_utils(self): |
| 435 | """A client that abstracts v2 and v3 identity operations. |
| 436 | |
| 437 | This can be used for creating and tearing down projects in tests. It |
| 438 | should not be used for testing identity features. |
| 439 | """ |
| 440 | if CONF.identity.auth_version == 'v2': |
| 441 | client = self.os_admin.identity_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 442 | users_client = self.os_admin.users_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 443 | project_client = self.os_admin.tenants_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 444 | roles_client = self.os_admin.roles_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 445 | domains_client = None |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 446 | else: |
| 447 | client = self.os_admin.identity_v3_client |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 448 | project_client = self.os_adm.projects_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 449 | users_client = self.os_admin.users_v3_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 450 | roles_client = None |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 451 | domains_client = self.os_admin.domains_client |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 452 | |
| 453 | try: |
| 454 | domain = client.auth_provider.credentials.project_domain_name |
| 455 | except AttributeError: |
| 456 | domain = 'Default' |
| 457 | |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 458 | return cred_client.get_creds_client(client, project_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 459 | users_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 460 | roles_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 461 | domains_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 462 | project_domain_name=domain) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 463 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 464 | @classmethod |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 465 | def get_identity_version(cls): |
| 466 | """Returns the identity version used by the test class""" |
| 467 | identity_version = getattr(cls, 'identity_version', None) |
| 468 | return identity_version or CONF.identity.auth_version |
| 469 | |
| 470 | @classmethod |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 471 | def _get_credentials_provider(cls): |
| 472 | """Returns a credentials provider |
| 473 | |
| 474 | If no credential provider exists yet creates one. |
| 475 | It uses self.identity_version if defined, or the configuration value |
| 476 | """ |
| 477 | if (not hasattr(cls, '_creds_provider') or not cls._creds_provider or |
| 478 | not cls._creds_provider.name == cls.__name__): |
| 479 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', |
| 480 | False) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 481 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 482 | cls._creds_provider = credentials.get_credentials_provider( |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 483 | name=cls.__name__, network_resources=cls.network_resources, |
| 484 | force_tenant_isolation=force_tenant_isolation, |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 485 | identity_version=cls.get_identity_version()) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 486 | return cls._creds_provider |
| 487 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 488 | @classmethod |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 489 | def get_client_manager(cls, credential_type=None, roles=None, |
| 490 | force_new=None): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 491 | """Returns an OpenStack client manager |
| 492 | |
| 493 | Returns an OpenStack client manager based on either credential_type |
| 494 | or a list of roles. If neither is specified, it defaults to |
| 495 | credential_type 'primary' |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 496 | :param credential_type: string - primary, alt or admin |
| 497 | :param roles: list of roles |
| 498 | |
lei zhang | dd552b2 | 2015-11-25 20:41:48 +0800 | [diff] [blame] | 499 | :returns: the created client manager |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 500 | :raises skipException: if the requested credentials are not available |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 501 | """ |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 502 | if all([roles, credential_type]): |
| 503 | msg = "Cannot get credentials by type and roles at the same time" |
| 504 | raise ValueError(msg) |
| 505 | if not any([roles, credential_type]): |
| 506 | credential_type = 'primary' |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 507 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 508 | if roles: |
| 509 | for role in roles: |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 510 | if not cred_provider.is_role_available(role): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 511 | skip_msg = ( |
| 512 | "%s skipped because the configured credential provider" |
| 513 | " is not able to provide credentials with the %s role " |
| 514 | "assigned." % (cls.__name__, role)) |
| 515 | raise cls.skipException(skip_msg) |
| 516 | params = dict(roles=roles) |
| 517 | if force_new is not None: |
| 518 | params.update(force_new=force_new) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 519 | creds = cred_provider.get_creds_by_roles(**params) |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 520 | else: |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 521 | credentials_method = 'get_%s_creds' % credential_type |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 522 | if hasattr(cred_provider, credentials_method): |
| 523 | creds = getattr(cred_provider, credentials_method)() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 524 | else: |
| 525 | raise exceptions.InvalidCredentials( |
| 526 | "Invalid credentials type %s" % credential_type) |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 527 | return cls.client_manager(credentials=creds, service=cls._service) |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 528 | |
| 529 | @classmethod |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 530 | def clear_credentials(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 531 | """Clears creds if set""" |
Attila Fazekas | 5b0d926 | 2015-05-20 10:17:39 +0200 | [diff] [blame] | 532 | if hasattr(cls, '_creds_provider'): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 533 | cls._creds_provider.clear_creds() |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 534 | |
| 535 | @classmethod |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 536 | def set_validation_resources(cls, keypair=None, floating_ip=None, |
| 537 | security_group=None, |
| 538 | security_group_rules=None): |
| 539 | """Specify which ssh server validation resources should be created. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 540 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 541 | Each of the argument must be set to either None, True or False, with |
| 542 | None - use default from config (security groups and security group |
| 543 | rules get created when set to None) |
| 544 | False - Do not create the validation resource |
| 545 | True - create the validation resource |
| 546 | |
| 547 | @param keypair |
| 548 | @param security_group |
| 549 | @param security_group_rules |
| 550 | @param floating_ip |
| 551 | """ |
Matthew Treinish | e5cca00 | 2015-05-11 15:36:50 -0400 | [diff] [blame] | 552 | if not CONF.validation.run_validation: |
| 553 | return |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 554 | if keypair is None: |
| 555 | if CONF.validation.auth_method.lower() == "keypair": |
| 556 | keypair = True |
| 557 | else: |
| 558 | keypair = False |
| 559 | if floating_ip is None: |
| 560 | if CONF.validation.connect_method.lower() == "floating": |
| 561 | floating_ip = True |
| 562 | else: |
| 563 | floating_ip = False |
| 564 | if security_group is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 565 | security_group = CONF.validation.security_group |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 566 | if security_group_rules is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 567 | security_group_rules = CONF.validation.security_group_rules |
| 568 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 569 | if not cls.validation_resources: |
| 570 | cls.validation_resources = { |
| 571 | 'keypair': keypair, |
| 572 | 'security_group': security_group, |
| 573 | 'security_group_rules': security_group_rules, |
| 574 | 'floating_ip': floating_ip} |
| 575 | |
| 576 | @classmethod |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 577 | def set_network_resources(cls, network=False, router=False, subnet=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 578 | dhcp=False): |
| 579 | """Specify which network resources should be created |
| 580 | |
| 581 | @param network |
| 582 | @param router |
| 583 | @param subnet |
| 584 | @param dhcp |
| 585 | """ |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 586 | # network resources should be set only once from callers |
| 587 | # in order to ensure that even if it's called multiple times in |
| 588 | # a chain of overloaded methods, the attribute is set only |
| 589 | # in the leaf class |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 590 | if not cls.network_resources: |
| 591 | cls.network_resources = { |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 592 | 'network': network, |
| 593 | 'router': router, |
| 594 | 'subnet': subnet, |
| 595 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 596 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 597 | @classmethod |
| 598 | def get_tenant_network(cls): |
| 599 | """Get the network to be used in testing |
| 600 | |
| 601 | :return: network dict including 'id' and 'name' |
| 602 | """ |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 603 | # Make sure cred_provider exists and get a network client |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 604 | networks_client = cls.get_client_manager().compute_networks_client |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 605 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 606 | # In case of nova network, isolated tenants are not able to list the |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 607 | # network configured in fixed_network_name, even if they can use it |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 608 | # for their servers, so using an admin network client to validate |
| 609 | # the network name |
| 610 | if (not CONF.service_available.neutron and |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 611 | credentials.is_admin_available( |
| 612 | identity_version=cls.get_identity_version())): |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 613 | admin_creds = cred_provider.get_admin_creds() |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 614 | admin_manager = clients.Manager(admin_creds) |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 615 | networks_client = admin_manager.compute_networks_client |
Andrea Frittoli (andreaf) | 940f8c6 | 2015-10-30 16:39:24 +0900 | [diff] [blame] | 616 | return fixed_network.get_tenant_network( |
| 617 | cred_provider, networks_client, CONF.compute.fixed_network_name) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 618 | |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 619 | def assertEmpty(self, list, msg=None): |
| 620 | self.assertTrue(len(list) == 0, msg) |
| 621 | |
| 622 | def assertNotEmpty(self, list, msg=None): |
| 623 | self.assertTrue(len(list) > 0, msg) |
| 624 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 625 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 626 | class NegativeAutoTest(BaseTestCase): |
| 627 | |
| 628 | _resources = {} |
| 629 | |
| 630 | @classmethod |
| 631 | def setUpClass(cls): |
| 632 | super(NegativeAutoTest, cls).setUpClass() |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 633 | os = cls.get_client_manager(credential_type='primary') |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 634 | cls.client = os.negative_client |
| 635 | |
| 636 | @staticmethod |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 637 | def load_tests(*args): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 638 | """Wrapper for testscenarios |
| 639 | |
| 640 | To set the mandatory scenarios variable only in case a real test |
| 641 | loader is in place. Will be automatically called in case the variable |
| 642 | "load_tests" is set. |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 643 | """ |
| 644 | if getattr(args[0], 'suiteClass', None) is not None: |
| 645 | loader, standard_tests, pattern = args |
| 646 | else: |
| 647 | standard_tests, module, loader = args |
| 648 | for test in testtools.iterate_tests(standard_tests): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 649 | schema = getattr(test, '_schema', None) |
Marc Koderer | 3dd3105 | 2014-11-27 09:31:00 +0100 | [diff] [blame] | 650 | if schema is not None: |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 651 | setattr(test, 'scenarios', |
| 652 | NegativeAutoTest.generate_scenario(schema)) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 653 | return testscenarios.load_tests_apply_scenarios(*args) |
| 654 | |
| 655 | @staticmethod |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 656 | def generate_scenario(description): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 657 | """Generates the test scenario list for a given description. |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 658 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 659 | :param description: A file or dictionary with the following entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 660 | name (required) name for the api |
| 661 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 662 | url (required) the url to be appended to the catalog url with '%s' |
| 663 | for each resource mentioned |
| 664 | resources: (optional) A list of resource names such as "server", |
| 665 | "flavor", etc. with an element for each '%s' in the url. This |
| 666 | method will call self.get_resource for each element when |
| 667 | constructing the positive test case template so negative |
| 668 | subclasses are expected to return valid resource ids when |
| 669 | appropriate. |
| 670 | json-schema (optional) A valid json schema that will be used to |
| 671 | create invalid data for the api calls. For "GET" and "HEAD", |
| 672 | the data is used to generate query strings appended to the url, |
| 673 | otherwise for the body of the http call. |
| 674 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 675 | LOG.debug(description) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 676 | generator = importutils.import_class( |
| 677 | CONF.negative.test_generator)() |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 678 | generator.validate_schema(description) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 679 | schema = description.get("json-schema", None) |
| 680 | resources = description.get("resources", []) |
| 681 | scenario_list = [] |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 682 | expected_result = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 683 | for resource in resources: |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 684 | if isinstance(resource, dict): |
| 685 | expected_result = resource['expected_result'] |
| 686 | resource = resource['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 687 | LOG.debug("Add resource to test %s" % resource) |
| 688 | scn_name = "inv_res_%s" % (resource) |
| 689 | scenario_list.append((scn_name, {"resource": (resource, |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 690 | str(uuid.uuid4())), |
| 691 | "expected_result": expected_result |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 692 | })) |
| 693 | if schema is not None: |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 694 | for scenario in generator.generate_scenarios(schema): |
| 695 | scenario_list.append((scenario['_negtest_name'], |
| 696 | scenario)) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 697 | LOG.debug(scenario_list) |
| 698 | return scenario_list |
| 699 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 700 | def execute(self, description): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 701 | """Execute a http call |
| 702 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 703 | Execute a http call on an api that are expected to |
| 704 | result in client errors. First it uses invalid resources that are part |
| 705 | of the url, and then invalid data for queries and http request bodies. |
| 706 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 707 | :param description: A json file or dictionary with the following |
| 708 | entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 709 | name (required) name for the api |
| 710 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 711 | url (required) the url to be appended to the catalog url with '%s' |
| 712 | for each resource mentioned |
| 713 | resources: (optional) A list of resource names such as "server", |
| 714 | "flavor", etc. with an element for each '%s' in the url. This |
| 715 | method will call self.get_resource for each element when |
| 716 | constructing the positive test case template so negative |
| 717 | subclasses are expected to return valid resource ids when |
| 718 | appropriate. |
| 719 | json-schema (optional) A valid json schema that will be used to |
| 720 | create invalid data for the api calls. For "GET" and "HEAD", |
| 721 | the data is used to generate query strings appended to the url, |
| 722 | otherwise for the body of the http call. |
| 723 | |
| 724 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 725 | LOG.info("Executing %s" % description["name"]) |
| 726 | LOG.debug(description) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 727 | generator = importutils.import_class( |
| 728 | CONF.negative.test_generator)() |
| 729 | schema = description.get("json-schema", None) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 730 | method = description["http-method"] |
| 731 | url = description["url"] |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 732 | expected_result = None |
| 733 | if "default_result_code" in description: |
| 734 | expected_result = description["default_result_code"] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 735 | |
| 736 | resources = [self.get_resource(r) for |
| 737 | r in description.get("resources", [])] |
| 738 | |
| 739 | if hasattr(self, "resource"): |
| 740 | # Note(mkoderer): The resources list already contains an invalid |
| 741 | # entry (see get_resource). |
| 742 | # We just send a valid json-schema with it |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 743 | valid_schema = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 744 | if schema: |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 745 | valid_schema = \ |
| 746 | valid.ValidTestGenerator().generate_valid(schema) |
| 747 | new_url, body = self._http_arguments(valid_schema, url, method) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 748 | elif hasattr(self, "_negtest_name"): |
| 749 | schema_under_test = \ |
| 750 | valid.ValidTestGenerator().generate_valid(schema) |
| 751 | local_expected_result = \ |
| 752 | generator.generate_payload(self, schema_under_test) |
| 753 | if local_expected_result is not None: |
| 754 | expected_result = local_expected_result |
| 755 | new_url, body = \ |
| 756 | self._http_arguments(schema_under_test, url, method) |
Marc Koderer | 1c247c8 | 2014-03-20 08:24:38 +0100 | [diff] [blame] | 757 | else: |
| 758 | raise Exception("testscenarios are not active. Please make sure " |
| 759 | "that your test runner supports the load_tests " |
| 760 | "mechanism") |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 761 | |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 762 | if "admin_client" in description and description["admin_client"]: |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 763 | if not credentials.is_admin_available( |
| 764 | identity_version=self.get_identity_version()): |
David Kranz | afecec0 | 2015-03-23 14:27:15 -0400 | [diff] [blame] | 765 | msg = ("Missing Identity Admin API credentials in" |
| 766 | "configuration.") |
| 767 | raise self.skipException(msg) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 768 | creds = self.credentials_provider.get_admin_creds() |
David Kranz | afecec0 | 2015-03-23 14:27:15 -0400 | [diff] [blame] | 769 | os_adm = clients.Manager(credentials=creds) |
| 770 | client = os_adm.negative_client |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 771 | else: |
| 772 | client = self.client |
| 773 | resp, resp_body = client.send_request(method, new_url, |
| 774 | resources, body=body) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 775 | self._check_negative_response(expected_result, resp.status, resp_body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 776 | |
| 777 | def _http_arguments(self, json_dict, url, method): |
| 778 | LOG.debug("dict: %s url: %s method: %s" % (json_dict, url, method)) |
| 779 | if not json_dict: |
| 780 | return url, None |
| 781 | elif method in ["GET", "HEAD", "PUT", "DELETE"]: |
Harshada Mangesh Kakad | 71f6b97 | 2015-12-22 09:46:48 -0800 | [diff] [blame] | 782 | return "%s?%s" % (url, urllib.parse.urlencode(json_dict)), None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 783 | else: |
| 784 | return url, json.dumps(json_dict) |
| 785 | |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 786 | def _check_negative_response(self, expected_result, result, body): |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 787 | self.assertTrue(result >= 400 and result < 500 and result != 413, |
| 788 | "Expected client error, got %s:%s" % |
| 789 | (result, body)) |
| 790 | self.assertTrue(expected_result is None or expected_result == result, |
| 791 | "Expected %s, got %s:%s" % |
| 792 | (expected_result, result, body)) |
| 793 | |
| 794 | @classmethod |
| 795 | def set_resource(cls, name, resource): |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 796 | """Register a resource for a test |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 797 | |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 798 | This function can be used in setUpClass context to register a resource |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 799 | for a test. |
| 800 | |
| 801 | :param name: The name of the kind of resource such as "flavor", "role", |
| 802 | etc. |
| 803 | :resource: The id of the resource |
| 804 | """ |
| 805 | cls._resources[name] = resource |
| 806 | |
| 807 | def get_resource(self, name): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 808 | """Return a valid uuid for a type of resource. |
| 809 | |
| 810 | If a real resource is needed as part of a url then this method should |
| 811 | return one. Otherwise it can return None. |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 812 | |
| 813 | :param name: The name of the kind of resource such as "flavor", "role", |
| 814 | etc. |
| 815 | """ |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 816 | if isinstance(name, dict): |
| 817 | name = name['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 818 | if hasattr(self, "resource") and self.resource[0] == name: |
| 819 | LOG.debug("Return invalid resource (%s) value: %s" % |
| 820 | (self.resource[0], self.resource[1])) |
| 821 | return self.resource[1] |
| 822 | if name in self._resources: |
| 823 | return self._resources[name] |
| 824 | return None |
| 825 | |
| 826 | |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 827 | def SimpleNegativeAutoTest(klass): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 828 | """This decorator registers a test function on basis of the class name.""" |
Sean Dague | 5e1bcd9 | 2015-04-27 09:08:36 -0400 | [diff] [blame] | 829 | @attr(type=['negative']) |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 830 | def generic_test(self): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 831 | if hasattr(self, '_schema'): |
| 832 | self.execute(self._schema) |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 833 | |
| 834 | cn = klass.__name__ |
| 835 | cn = cn.replace('JSON', '') |
| 836 | cn = cn.replace('Test', '') |
| 837 | # NOTE(mkoderer): replaces uppercase chars inside the class name with '_' |
| 838 | lower_cn = re.sub('(?<!^)(?=[A-Z])', '_', cn).lower() |
| 839 | func_name = 'test_%s' % lower_cn |
| 840 | setattr(klass, func_name, generic_test) |
| 841 | return klass |
| 842 | |
| 843 | |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 844 | def call_until_true(func, duration, sleep_for): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 845 | """Call the given function until it returns True (and return True) |
| 846 | |
| 847 | or until the specified duration (in seconds) elapses (and return False). |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 848 | |
| 849 | :param func: A zero argument callable that returns True on success. |
| 850 | :param duration: The number of seconds for which to attempt a |
| 851 | successful call of the function. |
| 852 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 853 | invocation of the function. |
| 854 | """ |
| 855 | now = time.time() |
| 856 | timeout = now + duration |
| 857 | while now < timeout: |
| 858 | if func(): |
| 859 | return True |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 860 | time.sleep(sleep_for) |
| 861 | now = time.time() |
| 862 | return False |