Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / common / base.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12 #
13
14 import functools
15 import unittest.case
16
17 from oslo_db.sqlalchemy import test_base
18 import testtools.testcase
19
20 from neutron.common import constants as n_const
21 from neutron.tests import base
22 from neutron.tests import tools
23
24
25 def create_resource(prefix, creation_func, *args, **kwargs):
26     """Create a new resource that does not already exist.
27
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.
30
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.
36     """
37
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)
41
42     while True:
43         name = base.get_rand_name(
44             max_length=n_const.DEVICE_NAME_MAX_LEN,
45             prefix=prefix)
46         try:
47             return creation_func(name, *args, **kwargs)
48         except RuntimeError:
49             pass
50
51
52 def no_skip_on_missing_deps(wrapped):
53     """Do not allow a method/test to skip on missing dependencies.
54
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).
58     """
59
60     @functools.wraps(wrapped)
61     def wrapper(*args, **kwargs):
62         try:
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'):
66                 tools.fail(
67                     '%s cannot be skipped because OS_FAIL_ON_MISSING_DEPS '
68                     'is enabled, skip reason: %s' % (wrapped.__name__, e))
69             raise
70     return wrapper
71
72
73 class MySQLTestCase(test_base.MySQLOpportunisticTestCase):
74     """Base test class for MySQL tests.
75
76     If the MySQL db is unavailable then this test is skipped, unless
77     OS_FAIL_ON_MISSING_DEPS is enabled.
78     """
79     SKIP_ON_UNAVAILABLE_DB = not base.bool_from_env('OS_FAIL_ON_MISSING_DEPS')
80
81
82 class PostgreSQLTestCase(test_base.PostgreSQLOpportunisticTestCase):
83     """Base test class for PostgreSQL tests.
84
85     If the PostgreSQL db is unavailable then this test is skipped, unless
86     OS_FAIL_ON_MISSING_DEPS is enabled.
87     """
88     SKIP_ON_UNAVAILABLE_DB = not base.bool_from_env('OS_FAIL_ON_MISSING_DEPS')