1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
17 from oslo_db.sqlalchemy import test_base
18 import testtools.testcase
20 from neutron.common import constants as n_const
21 from neutron.tests import base
22 from neutron.tests import tools
25 def create_resource(prefix, creation_func, *args, **kwargs):
26 """Create a new resource that does not already exist.
28 If prefix isn't 'max_length' in size, a random suffix is concatenated to
29 ensure it is random. Otherwise, 'prefix' is used as is.
31 :param prefix: The prefix for a randomly generated name
32 :param creation_func: A function taking the name of the resource
33 to be created as it's first argument. An error is assumed
34 to indicate a name collision.
35 :param *args *kwargs: These will be passed to the create function.
38 # Don't generate a random name if prefix is already full-length.
39 if len(prefix) == n_const.DEVICE_NAME_MAX_LEN:
40 return creation_func(prefix, *args, **kwargs)
43 name = base.get_rand_name(
44 max_length=n_const.DEVICE_NAME_MAX_LEN,
47 return creation_func(name, *args, **kwargs)
52 def no_skip_on_missing_deps(wrapped):
53 """Do not allow a method/test to skip on missing dependencies.
55 This decorator raises an error if a skip is raised by wrapped method when
56 OS_FAIL_ON_MISSING_DEPS is evaluated to True. This decorator should be used
57 only for missing dependencies (including missing system requirements).
60 @functools.wraps(wrapped)
61 def wrapper(*args, **kwargs):
63 return wrapped(*args, **kwargs)
64 except (testtools.TestCase.skipException, unittest.case.SkipTest) as e:
65 if base.bool_from_env('OS_FAIL_ON_MISSING_DEPS'):
67 '%s cannot be skipped because OS_FAIL_ON_MISSING_DEPS '
68 'is enabled, skip reason: %s' % (wrapped.__name__, e))
73 class MySQLTestCase(test_base.MySQLOpportunisticTestCase):
74 """Base test class for MySQL tests.
76 If the MySQL db is unavailable then this test is skipped, unless
77 OS_FAIL_ON_MISSING_DEPS is enabled.
79 SKIP_ON_UNAVAILABLE_DB = not base.bool_from_env('OS_FAIL_ON_MISSING_DEPS')
82 class PostgreSQLTestCase(test_base.PostgreSQLOpportunisticTestCase):
83 """Base test class for PostgreSQL tests.
85 If the PostgreSQL db is unavailable then this test is skipped, unless
86 OS_FAIL_ON_MISSING_DEPS is enabled.
88 SKIP_ON_UNAVAILABLE_DB = not base.bool_from_env('OS_FAIL_ON_MISSING_DEPS')