Sync upstream code
[parser.git] / tosca2heat / tosca-parser / toscaparser / tests / base.py
1 # Copyright 2010-2011 OpenStack Foundation
2 # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
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
16 import os
17
18 import fixtures
19 import testscenarios
20 import testtools
21
22 from toscaparser.tosca_template import ToscaTemplate
23
24 _TRUE_VALUES = ('True', 'true', '1', 'yes')
25
26
27 class TestCase(testscenarios.TestWithScenarios, testtools.TestCase):
28
29     """Test case base class for all unit tests."""
30
31     def setUp(self):
32         """Run before each test method to initialize test environment."""
33
34         super(TestCase, self).setUp()
35         test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
36         try:
37             test_timeout = int(test_timeout)
38         except ValueError:
39             # If timeout value is invalid do not set a timeout.
40             test_timeout = 0
41         if test_timeout > 0:
42             self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
43
44         self.useFixture(fixtures.NestedTempfile())
45         self.useFixture(fixtures.TempHomeDir())
46
47         if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
48             stdout = self.useFixture(fixtures.StringStream('stdout')).stream
49             self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
50         if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
51             stderr = self.useFixture(fixtures.StringStream('stderr')).stream
52             self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
53
54         self.log_fixture = self.useFixture(fixtures.FakeLogger())
55
56     def _load_template(self, filename):
57         """Load a Tosca template from tests data folder.
58
59         :param filename: Tosca template file name to load.
60         :return: ToscaTemplate
61         """
62         return ToscaTemplate(os.path.join(
63             os.path.dirname(os.path.abspath(__file__)),
64             'data',
65             filename))