1#!/usr/bin/env python3 2# 3# Copyright (C) 2016 Intel Corporation 4# 5# SPDX-License-Identifier: MIT 6# 7 8import signal 9import unittest 10 11from common import setup_sys_path, TestBase 12setup_sys_path() 13 14from oeqa.core.exception import OEQADependency 15from oeqa.core.utils.test import getCaseMethod, getSuiteCasesNames, getSuiteCasesIDs 16 17class TestTagDecorator(TestBase): 18 def _runTest(self, modules, filterfn, expect): 19 tc = self._testLoader(modules = modules, tags_filter = filterfn) 20 test_loaded = set(getSuiteCasesIDs(tc.suites)) 21 self.assertEqual(expect, test_loaded) 22 23 def test_oetag(self): 24 # get all cases without any filtering 25 self._runTest(['oetag'], None, { 26 'oetag.TagTest.testTagGood', 27 'oetag.TagTest.testTagOther', 28 'oetag.TagTest.testTagOtherMulti', 29 'oetag.TagTest.testTagNone', 30 'oetag.TagClassTest.testTagOther', 31 'oetag.TagClassTest.testTagOtherMulti', 32 'oetag.TagClassTest.testTagNone', 33 }) 34 35 # exclude any case with tags 36 self._runTest(['oetag'], lambda tags: tags, { 37 'oetag.TagTest.testTagNone', 38 }) 39 40 # exclude any case with otherTag 41 self._runTest(['oetag'], lambda tags: "otherTag" in tags, { 42 'oetag.TagTest.testTagGood', 43 'oetag.TagTest.testTagNone', 44 'oetag.TagClassTest.testTagNone', 45 }) 46 47 # exclude any case with classTag 48 self._runTest(['oetag'], lambda tags: "classTag" in tags, { 49 'oetag.TagTest.testTagGood', 50 'oetag.TagTest.testTagOther', 51 'oetag.TagTest.testTagOtherMulti', 52 'oetag.TagTest.testTagNone', 53 }) 54 55 # include any case with classTag 56 self._runTest(['oetag'], lambda tags: "classTag" not in tags, { 57 'oetag.TagClassTest.testTagOther', 58 'oetag.TagClassTest.testTagOtherMulti', 59 'oetag.TagClassTest.testTagNone', 60 }) 61 62 # include any case with classTag or no tags 63 self._runTest(['oetag'], lambda tags: tags and "classTag" not in tags, { 64 'oetag.TagTest.testTagNone', 65 'oetag.TagClassTest.testTagOther', 66 'oetag.TagClassTest.testTagOtherMulti', 67 'oetag.TagClassTest.testTagNone', 68 }) 69 70class TestDependsDecorator(TestBase): 71 modules = ['depends'] 72 73 def test_depends_order(self): 74 tests = ['depends.DependsTest.testDependsFirst', 75 'depends.DependsTest.testDependsSecond', 76 'depends.DependsTest.testDependsThird', 77 'depends.DependsTest.testDependsFourth', 78 'depends.DependsTest.testDependsFifth'] 79 tests2 = list(tests) 80 tests2[2], tests2[3] = tests[3], tests[2] 81 tc = self._testLoader(modules=self.modules, tests=tests) 82 test_loaded = getSuiteCasesIDs(tc.suites) 83 result = True if test_loaded == tests or test_loaded == tests2 else False 84 msg = 'Failed to order tests using OETestDepends decorator.\nTest order:'\ 85 ' %s.\nExpected: %s\nOr: %s' % (test_loaded, tests, tests2) 86 self.assertTrue(result, msg=msg) 87 88 def test_depends_fail_missing_dependency(self): 89 expect = "TestCase depends.DependsTest.testDependsSecond depends on "\ 90 "depends.DependsTest.testDependsFirst and isn't available" 91 tests = ['depends.DependsTest.testDependsSecond'] 92 try: 93 # Must throw OEQADependency because missing 'testDependsFirst' 94 tc = self._testLoader(modules=self.modules, tests=tests) 95 self.fail('Expected OEQADependency exception') 96 except OEQADependency as e: 97 result = True if expect in str(e) else False 98 msg = 'Expected OEQADependency exception missing testDependsFirst test' 99 self.assertTrue(result, msg=msg) 100 101 def test_depends_fail_circular_dependency(self): 102 expect = 'have a circular dependency' 103 tests = ['depends.DependsTest.testDependsCircular1', 104 'depends.DependsTest.testDependsCircular2', 105 'depends.DependsTest.testDependsCircular3'] 106 try: 107 # Must throw OEQADependency because circular dependency 108 tc = self._testLoader(modules=self.modules, tests=tests) 109 self.fail('Expected OEQADependency exception') 110 except OEQADependency as e: 111 result = True if expect in str(e) else False 112 msg = 'Expected OEQADependency exception having a circular dependency' 113 self.assertTrue(result, msg=msg) 114 115class TestTimeoutDecorator(TestBase): 116 modules = ['timeout'] 117 118 def test_timeout(self): 119 tests = ['timeout.TimeoutTest.testTimeoutPass'] 120 msg = 'Failed to run test using OETestTimeout' 121 alarm_signal = signal.getsignal(signal.SIGALRM) 122 tc = self._testLoader(modules=self.modules, tests=tests) 123 self.assertTrue(tc.runTests().wasSuccessful(), msg=msg) 124 msg = "OETestTimeout didn't restore SIGALRM" 125 self.assertIs(alarm_signal, signal.getsignal(signal.SIGALRM), msg=msg) 126 127 def test_timeout_fail(self): 128 tests = ['timeout.TimeoutTest.testTimeoutFail'] 129 msg = "OETestTimeout test didn't timeout as expected" 130 alarm_signal = signal.getsignal(signal.SIGALRM) 131 tc = self._testLoader(modules=self.modules, tests=tests) 132 self.assertFalse(tc.runTests().wasSuccessful(), msg=msg) 133 msg = "OETestTimeout didn't restore SIGALRM" 134 self.assertIs(alarm_signal, signal.getsignal(signal.SIGALRM), msg=msg) 135 136 def test_timeout_cancel(self): 137 tests = ['timeout.TimeoutTest.testTimeoutSkip', 'timeout.TimeoutTest.testTimeoutDepends', 'timeout.TimeoutTest.testTimeoutUnrelated'] 138 msg = 'Unrelated test failed to complete' 139 tc = self._testLoader(modules=self.modules, tests=tests) 140 self.assertTrue(tc.runTests().wasSuccessful(), msg=msg) 141 142if __name__ == '__main__': 143 unittest.main() 144