Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 3 | # Copyright (C) 2016 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: MIT |
| 6 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | |
| 8 | import signal |
| 9 | import unittest |
| 10 | |
| 11 | from common import setup_sys_path, TestBase |
| 12 | setup_sys_path() |
| 13 | |
| 14 | from oeqa.core.exception import OEQADependency |
| 15 | from oeqa.core.utils.test import getCaseMethod, getSuiteCasesNames, getSuiteCasesIDs |
| 16 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 17 | class 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | |
| 23 | def test_oetag(self): |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 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 | }) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 35 | # exclude any case with tags |
| 36 | self._runTest(['oetag'], lambda tags: tags, { |
| 37 | 'oetag.TagTest.testTagNone', |
| 38 | }) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 39 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 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 | }) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 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 | }) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 54 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 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 | }) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 69 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 70 | class 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 | |
| 115 | class 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 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 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 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 142 | if __name__ == '__main__': |
| 143 | unittest.main() |