summaryrefslogtreecommitdiffstats
path: root/tosca2heat/heat-translator/translator/tests/base.py
blob: 6e93268514a101184fa984198c03c4664bf8acbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-

# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os

import fixtures
import testtools

_TRUE_VALUES = ('True', 'true', '1', 'yes')


class TestCase(testtools.TestCase):

    """Test case base class for all unit tests."""

    def setUp(self):
        """Run before each test method to initialize test environment."""

        super(TestCase, self).setUp()
        test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
        try:
            test_timeout = int(test_timeout)
        except ValueError:
            # If timeout value is invalid do not set a timeout.
            test_timeout = 0
        if test_timeout > 0:
            self.useFixture(fixtures.Timeout(test_timeout, gentle=True))

        self.useFixture(fixtures.NestedTempfile())
        self.useFixture(fixtures.TempHomeDir())

        if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
            stdout = self.useFixture(fixtures.StringStream('stdout')).stream
            self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
        if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
            stderr = self.useFixture(fixtures.StringStream('stderr')).stream
            self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))

        self.log_fixture = self.useFixture(fixtures.FakeLogger())
rder self.ci_loop = ci_loop self.description = description def add_test(self, testcase): self.tests_array.append(testcase) def skip_test(self, testcase): self.skipped_tests_array.append(testcase) def get_tests(self): array_tests = [] for test in self.tests_array: array_tests.append(test) return array_tests def get_skipped_test(self): return self.skipped_tests_array def get_test_names(self): array_tests = [] for test in self.tests_array: array_tests.append(test.get_name()) return array_tests def get_test(self, test_name): if self.is_test(test_name): for test in self.tests_array: if test.get_name() == test_name: return test return None def is_test(self, test_name): for test in self.tests_array: if test.get_name() == test_name: return True return False def get_name(self): return self.name def get_order(self): return self.order def get_ci_loop(self): return self.ci_loop def __str__(self): msg = prettytable.PrettyTable( header_style='upper', padding_width=5, field_names=['tiers', 'order', 'CI Loop', 'description', 'testcases']) msg.add_row( [self.name, self.order, self.ci_loop, textwrap.fill(self.description, width=40), textwrap.fill(' '.join([str(x.get_name( )) for x in self.get_tests()]), width=40)]) return msg.get_string() class TestCase(object): def __init__(self, name, enabled, dependency, criteria, blocking, description="", project=""): self.name = name self.enabled = enabled self.dependency = dependency self.criteria = criteria self.blocking = blocking self.description = description self.project = project @staticmethod def is_none(item): return item is None or item is "" def is_compatible(self, ci_installer, ci_scenario): try: if not self.is_none(ci_installer): if re.search(self.dependency.get_installer(), ci_installer) is None: return False if not self.is_none(ci_scenario): if re.search(self.dependency.get_scenario(), ci_scenario) is None: return False return True except TypeError: return False def get_name(self): return self.name def is_enabled(self): return self.enabled def get_criteria(self): return self.criteria def is_blocking(self): return self.blocking def get_project(self): return self.project def __str__(self): msg = prettytable.PrettyTable( header_style='upper', padding_width=5, field_names=['test case', 'description', 'criteria', 'dependency']) msg.add_row([self.name, textwrap.fill(self.description, width=40), self.criteria, self.dependency]) return msg.get_string() class Dependency(object): def __init__(self, installer, scenario): self.installer = installer self.scenario = scenario def get_installer(self): return self.installer def get_scenario(self): return self.scenario def __str__(self): delimitator = "\n" if self.get_installer( ) and self.get_scenario() else "" return "{}{}{}".format(self.get_installer(), delimitator, self.get_scenario())