blob: 3be55c048c4d952d28f1be1c89ee68436e591db0 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2012 OpenStack Foundation
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
Matthew Treinish9e26ca82016-02-23 11:43:20 -050016import os
17
18import fixtures
19import testtools
20
Matthew Treinish9e26ca82016-02-23 11:43:20 -050021
22class BaseTestCase(testtools.testcase.WithAttributes, testtools.TestCase):
23 setUpClassCalled = False
24
25 # NOTE(sdague): log_format is defined inline here instead of using the oslo
26 # default because going through the config path recouples config to the
27 # stress tests too early, and depending on testr order will fail unit tests
28 log_format = ('%(asctime)s %(process)d %(levelname)-8s '
29 '[%(name)s] %(message)s')
30
31 @classmethod
32 def setUpClass(cls):
33 if hasattr(super(BaseTestCase, cls), 'setUpClass'):
34 super(BaseTestCase, cls).setUpClass()
35 cls.setUpClassCalled = True
36
37 @classmethod
38 def tearDownClass(cls):
39 if hasattr(super(BaseTestCase, cls), 'tearDownClass'):
40 super(BaseTestCase, cls).tearDownClass()
41
42 def setUp(self):
43 super(BaseTestCase, self).setUp()
44 if not self.setUpClassCalled:
gong yong shengd5b40eb2016-08-31 15:47:22 +080045 raise RuntimeError("setUpClass does not calls the super's "
Federico Ressi2d6bcaa2018-04-11 12:37:36 +020046 "setUpClass in {!r}".format(type(self)))
Matthew Treinish9e26ca82016-02-23 11:43:20 -050047 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
48 try:
49 test_timeout = int(test_timeout)
50 except ValueError:
51 test_timeout = 0
52 if test_timeout > 0:
53 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
54
55 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
56 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
57 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
58 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
59 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
60 os.environ.get('OS_STDERR_CAPTURE') == '1'):
61 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
62 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
63 if (os.environ.get('OS_LOG_CAPTURE') != 'False' and
Federico Ressi2d6bcaa2018-04-11 12:37:36 +020064 os.environ.get('OS_LOG_CAPTURE') != '0'):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050065 self.useFixture(fixtures.LoggerFixture(nuke_handlers=False,
66 format=self.log_format,
67 level=None))