blob: 33a32eefcd1e37fd5cf31db1bc72024feb5ae06b [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 "
Matthew Treinish9e26ca82016-02-23 11:43:20 -050046 "setUpClass in the "
47 + self.__class__.__name__)
48 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
49 try:
50 test_timeout = int(test_timeout)
51 except ValueError:
52 test_timeout = 0
53 if test_timeout > 0:
54 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
55
56 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
57 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
58 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
59 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
60 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
61 os.environ.get('OS_STDERR_CAPTURE') == '1'):
62 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
63 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
64 if (os.environ.get('OS_LOG_CAPTURE') != 'False' and
65 os.environ.get('OS_LOG_CAPTURE') != '0'):
66 self.useFixture(fixtures.LoggerFixture(nuke_handlers=False,
67 format=self.log_format,
68 level=None))