Finn Behrens | c25ce58 | 2020-11-23 15:15:33 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
| 4 | # A collection of tests for tools/testing/kunit/kunit.py |
| 5 | # |
| 6 | # Copyright (C) 2019, Google LLC. |
| 7 | # Author: Brendan Higgins <brendanhiggins@google.com> |
| 8 | |
| 9 | import unittest |
| 10 | from unittest import mock |
| 11 | |
| 12 | import tempfile, shutil # Handling test_tmpdir |
| 13 | |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 14 | import itertools |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 15 | import json |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 16 | import signal |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 17 | import os |
| 18 | |
| 19 | import kunit_config |
| 20 | import kunit_parser |
| 21 | import kunit_kernel |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 22 | import kunit_json |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 23 | import kunit |
| 24 | |
| 25 | test_tmpdir = '' |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 26 | abs_test_data_dir = '' |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 27 | |
| 28 | def setUpModule(): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 29 | global test_tmpdir, abs_test_data_dir |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 30 | test_tmpdir = tempfile.mkdtemp() |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 31 | abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 32 | |
| 33 | def tearDownModule(): |
| 34 | shutil.rmtree(test_tmpdir) |
| 35 | |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 36 | def test_data_path(path): |
| 37 | return os.path.join(abs_test_data_dir, path) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 38 | |
| 39 | class KconfigTest(unittest.TestCase): |
| 40 | |
| 41 | def test_is_subset_of(self): |
| 42 | kconfig0 = kunit_config.Kconfig() |
| 43 | self.assertTrue(kconfig0.is_subset_of(kconfig0)) |
| 44 | |
| 45 | kconfig1 = kunit_config.Kconfig() |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 46 | kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 47 | self.assertTrue(kconfig1.is_subset_of(kconfig1)) |
| 48 | self.assertTrue(kconfig0.is_subset_of(kconfig1)) |
| 49 | self.assertFalse(kconfig1.is_subset_of(kconfig0)) |
| 50 | |
| 51 | def test_read_from_file(self): |
| 52 | kconfig = kunit_config.Kconfig() |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 53 | kconfig_path = test_data_path('test_read_from_file.kconfig') |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 54 | |
| 55 | kconfig.read_from_file(kconfig_path) |
| 56 | |
| 57 | expected_kconfig = kunit_config.Kconfig() |
| 58 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 59 | kunit_config.KconfigEntry('UML', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 60 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 61 | kunit_config.KconfigEntry('MMU', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 62 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 63 | kunit_config.KconfigEntry('TEST', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 64 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 65 | kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 66 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 67 | kunit_config.KconfigEntry('MK8', 'n')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 68 | |
| 69 | self.assertEqual(kconfig.entries(), expected_kconfig.entries()) |
| 70 | |
| 71 | def test_write_to_file(self): |
| 72 | kconfig_path = os.path.join(test_tmpdir, '.config') |
| 73 | |
| 74 | expected_kconfig = kunit_config.Kconfig() |
| 75 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 76 | kunit_config.KconfigEntry('UML', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 77 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 78 | kunit_config.KconfigEntry('MMU', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 79 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 80 | kunit_config.KconfigEntry('TEST', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 81 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 82 | kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 83 | expected_kconfig.add_entry( |
David Gow | 97752c3 | 2020-03-23 19:43:33 -0700 | [diff] [blame] | 84 | kunit_config.KconfigEntry('MK8', 'n')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 85 | |
| 86 | expected_kconfig.write_to_file(kconfig_path) |
| 87 | |
| 88 | actual_kconfig = kunit_config.Kconfig() |
| 89 | actual_kconfig.read_from_file(kconfig_path) |
| 90 | |
| 91 | self.assertEqual(actual_kconfig.entries(), |
| 92 | expected_kconfig.entries()) |
| 93 | |
| 94 | class KUnitParserTest(unittest.TestCase): |
| 95 | |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 96 | def assertContains(self, needle: str, haystack: kunit_parser.LineStream): |
| 97 | # Clone the iterator so we can print the contents on failure. |
| 98 | copy, backup = itertools.tee(haystack) |
| 99 | for line in copy: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 100 | if needle in line: |
| 101 | return |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 102 | raise AssertionError(f'"{needle}" not found in {list(backup)}!') |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 103 | |
| 104 | def test_output_isolated_correctly(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 105 | log_path = test_data_path('test_output_isolated_correctly.log') |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 106 | with open(log_path) as file: |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 107 | result = kunit_parser.extract_tap_lines(file.readlines()) |
Daniel Latypov | 060352e | 2020-10-30 15:38:53 -0700 | [diff] [blame] | 108 | self.assertContains('TAP version 14', result) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 109 | self.assertContains(' # Subtest: example', result) |
| 110 | self.assertContains(' 1..2', result) |
| 111 | self.assertContains(' ok 1 - example_simple_test', result) |
| 112 | self.assertContains(' ok 2 - example_mock_test', result) |
| 113 | self.assertContains('ok 1 - example', result) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 114 | |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 115 | def test_output_with_prefix_isolated_correctly(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 116 | log_path = test_data_path('test_pound_sign.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 117 | with open(log_path) as file: |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 118 | result = kunit_parser.extract_tap_lines(file.readlines()) |
Daniel Latypov | 060352e | 2020-10-30 15:38:53 -0700 | [diff] [blame] | 119 | self.assertContains('TAP version 14', result) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 120 | self.assertContains(' # Subtest: kunit-resource-test', result) |
| 121 | self.assertContains(' 1..5', result) |
| 122 | self.assertContains(' ok 1 - kunit_resource_test_init_resources', result) |
| 123 | self.assertContains(' ok 2 - kunit_resource_test_alloc_resource', result) |
| 124 | self.assertContains(' ok 3 - kunit_resource_test_destroy_resource', result) |
| 125 | self.assertContains(' foo bar #', result) |
| 126 | self.assertContains(' ok 4 - kunit_resource_test_cleanup_resources', result) |
| 127 | self.assertContains(' ok 5 - kunit_resource_test_proper_free_ordering', result) |
| 128 | self.assertContains('ok 1 - kunit-resource-test', result) |
| 129 | self.assertContains(' foo bar # non-kunit output', result) |
| 130 | self.assertContains(' # Subtest: kunit-try-catch-test', result) |
| 131 | self.assertContains(' 1..2', result) |
| 132 | self.assertContains(' ok 1 - kunit_test_try_catch_successful_try_no_catch', |
| 133 | result) |
| 134 | self.assertContains(' ok 2 - kunit_test_try_catch_unsuccessful_try_does_catch', |
| 135 | result) |
| 136 | self.assertContains('ok 2 - kunit-try-catch-test', result) |
| 137 | self.assertContains(' # Subtest: string-stream-test', result) |
| 138 | self.assertContains(' 1..3', result) |
| 139 | self.assertContains(' ok 1 - string_stream_test_empty_on_creation', result) |
| 140 | self.assertContains(' ok 2 - string_stream_test_not_empty_after_add', result) |
| 141 | self.assertContains(' ok 3 - string_stream_test_get_string', result) |
| 142 | self.assertContains('ok 3 - string-stream-test', result) |
| 143 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 144 | def test_parse_successful_test_log(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 145 | all_passed_log = test_data_path('test_is_test_passed-all_passed.log') |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 146 | with open(all_passed_log) as file: |
| 147 | result = kunit_parser.parse_run_tests(file.readlines()) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 148 | self.assertEqual( |
| 149 | kunit_parser.TestStatus.SUCCESS, |
| 150 | result.status) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 151 | |
| 152 | def test_parse_failed_test_log(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 153 | failed_log = test_data_path('test_is_test_passed-failure.log') |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 154 | with open(failed_log) as file: |
| 155 | result = kunit_parser.parse_run_tests(file.readlines()) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 156 | self.assertEqual( |
| 157 | kunit_parser.TestStatus.FAILURE, |
| 158 | result.status) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 159 | |
| 160 | def test_no_tests(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 161 | empty_log = test_data_path('test_is_test_passed-no_tests_run.log') |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 162 | with open(empty_log) as file: |
| 163 | result = kunit_parser.parse_run_tests( |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 164 | kunit_parser.extract_tap_lines(file.readlines())) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 165 | self.assertEqual(0, len(result.suites)) |
| 166 | self.assertEqual( |
| 167 | kunit_parser.TestStatus.NO_TESTS, |
| 168 | result.status) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 169 | |
Uriel Guajardo | e173b8b | 2020-06-11 21:05:45 +0000 | [diff] [blame] | 170 | def test_no_kunit_output(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 171 | crash_log = test_data_path('test_insufficient_memory.log') |
Uriel Guajardo | e173b8b | 2020-06-11 21:05:45 +0000 | [diff] [blame] | 172 | print_mock = mock.patch('builtins.print').start() |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 173 | with open(crash_log) as file: |
| 174 | result = kunit_parser.parse_run_tests( |
Daniel Latypov | b29b14f | 2021-05-26 01:22:17 -0700 | [diff] [blame^] | 175 | kunit_parser.extract_tap_lines(file.readlines())) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 176 | print_mock.assert_any_call(StrContains('no tests run!')) |
Uriel Guajardo | e173b8b | 2020-06-11 21:05:45 +0000 | [diff] [blame] | 177 | print_mock.stop() |
| 178 | file.close() |
| 179 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 180 | def test_crashed_test(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 181 | crashed_log = test_data_path('test_is_test_passed-crash.log') |
Daniel Latypov | a3ece07 | 2020-12-02 11:08:23 -0800 | [diff] [blame] | 182 | with open(crashed_log) as file: |
| 183 | result = kunit_parser.parse_run_tests(file.readlines()) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 184 | self.assertEqual( |
| 185 | kunit_parser.TestStatus.TEST_CRASHED, |
| 186 | result.status) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 187 | |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 188 | def test_ignores_prefix_printk_time(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 189 | prefix_log = test_data_path('test_config_printk_time.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 190 | with open(prefix_log) as file: |
| 191 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 192 | self.assertEqual( |
| 193 | kunit_parser.TestStatus.SUCCESS, |
| 194 | result.status) |
| 195 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 196 | |
| 197 | def test_ignores_multiple_prefixes(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 198 | prefix_log = test_data_path('test_multiple_prefixes.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 199 | with open(prefix_log) as file: |
| 200 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 201 | self.assertEqual( |
| 202 | kunit_parser.TestStatus.SUCCESS, |
| 203 | result.status) |
| 204 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 205 | |
| 206 | def test_prefix_mixed_kernel_output(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 207 | mixed_prefix_log = test_data_path('test_interrupted_tap_output.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 208 | with open(mixed_prefix_log) as file: |
| 209 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 210 | self.assertEqual( |
| 211 | kunit_parser.TestStatus.SUCCESS, |
| 212 | result.status) |
| 213 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 214 | |
| 215 | def test_prefix_poundsign(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 216 | pound_log = test_data_path('test_pound_sign.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 217 | with open(pound_log) as file: |
| 218 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 219 | self.assertEqual( |
| 220 | kunit_parser.TestStatus.SUCCESS, |
| 221 | result.status) |
| 222 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 223 | |
| 224 | def test_kernel_panic_end(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 225 | panic_log = test_data_path('test_kernel_panic_interrupt.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 226 | with open(panic_log) as file: |
| 227 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 228 | self.assertEqual( |
| 229 | kunit_parser.TestStatus.TEST_CRASHED, |
| 230 | result.status) |
| 231 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 232 | |
| 233 | def test_pound_no_prefix(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 234 | pound_log = test_data_path('test_pound_no_prefix.log') |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 235 | with open(pound_log) as file: |
| 236 | result = kunit_parser.parse_run_tests(file.readlines()) |
Brendan Higgins | 0d0d245 | 2020-10-21 13:39:14 -0700 | [diff] [blame] | 237 | self.assertEqual( |
| 238 | kunit_parser.TestStatus.SUCCESS, |
| 239 | result.status) |
| 240 | self.assertEqual('kunit-resource-test', result.suites[0].name) |
Heidi Fahim | afc63da | 2020-03-16 13:21:24 -0700 | [diff] [blame] | 241 | |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 242 | class LinuxSourceTreeTest(unittest.TestCase): |
| 243 | |
| 244 | def setUp(self): |
| 245 | mock.patch.object(signal, 'signal').start() |
| 246 | self.addCleanup(mock.patch.stopall) |
| 247 | |
| 248 | def test_invalid_kunitconfig(self): |
| 249 | with self.assertRaisesRegex(kunit_kernel.ConfigError, 'nonexistent.* does not exist'): |
| 250 | kunit_kernel.LinuxSourceTree('', kunitconfig_path='/nonexistent_file') |
| 251 | |
| 252 | def test_valid_kunitconfig(self): |
| 253 | with tempfile.NamedTemporaryFile('wt') as kunitconfig: |
| 254 | tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=kunitconfig.name) |
| 255 | |
Daniel Latypov | 9854781 | 2021-02-22 14:52:41 -0800 | [diff] [blame] | 256 | def test_dir_kunitconfig(self): |
| 257 | with tempfile.TemporaryDirectory('') as dir: |
| 258 | with open(os.path.join(dir, '.kunitconfig'), 'w') as f: |
| 259 | pass |
| 260 | tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=dir) |
| 261 | |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 262 | # TODO: add more test cases. |
| 263 | |
| 264 | |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 265 | class KUnitJsonTest(unittest.TestCase): |
| 266 | |
| 267 | def _json_for(self, log_file): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 268 | with open(test_data_path(log_file)) as file: |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 269 | test_result = kunit_parser.parse_run_tests(file) |
| 270 | json_obj = kunit_json.get_json_result( |
| 271 | test_result=test_result, |
| 272 | def_config='kunit_defconfig', |
| 273 | build_dir=None, |
| 274 | json_path='stdout') |
| 275 | return json.loads(json_obj) |
| 276 | |
| 277 | def test_failed_test_json(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 278 | result = self._json_for('test_is_test_passed-failure.log') |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 279 | self.assertEqual( |
| 280 | {'name': 'example_simple_test', 'status': 'FAIL'}, |
| 281 | result["sub_groups"][1]["test_cases"][0]) |
| 282 | |
| 283 | def test_crashed_test_json(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 284 | result = self._json_for('test_is_test_passed-crash.log') |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 285 | self.assertEqual( |
| 286 | {'name': 'example_simple_test', 'status': 'ERROR'}, |
| 287 | result["sub_groups"][1]["test_cases"][0]) |
| 288 | |
| 289 | def test_no_tests_json(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 290 | result = self._json_for('test_is_test_passed-no_tests_run.log') |
Heidi Fahim | 21a6d17 | 2020-08-11 14:27:56 -0700 | [diff] [blame] | 291 | self.assertEqual(0, len(result['sub_groups'])) |
| 292 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 293 | class StrContains(str): |
| 294 | def __eq__(self, other): |
| 295 | return self in other |
| 296 | |
| 297 | class KUnitMainTest(unittest.TestCase): |
| 298 | def setUp(self): |
Daniel Latypov | cd4a9bc | 2020-12-02 11:08:24 -0800 | [diff] [blame] | 299 | path = test_data_path('test_is_test_passed-all_passed.log') |
Daniel Latypov | cfd607e | 2020-12-02 11:08:21 -0800 | [diff] [blame] | 300 | with open(path) as file: |
| 301 | all_passed_log = file.readlines() |
| 302 | |
| 303 | self.print_mock = mock.patch('builtins.print').start() |
| 304 | self.addCleanup(mock.patch.stopall) |
| 305 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 306 | self.linux_source_mock = mock.Mock() |
| 307 | self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 308 | self.linux_source_mock.build_kernel = mock.Mock(return_value=True) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 309 | self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) |
| 310 | |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 311 | def test_config_passes_args_pass(self): |
Brendan Higgins | d43c7fb | 2020-07-14 13:41:30 -0700 | [diff] [blame] | 312 | kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 313 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
| 314 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 315 | |
| 316 | def test_build_passes_args_pass(self): |
| 317 | kunit.main(['build'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 318 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 319 | self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, '.kunit', None) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 320 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 321 | |
| 322 | def test_exec_passes_args_pass(self): |
| 323 | kunit.main(['exec'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 324 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) |
| 325 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 326 | self.linux_source_mock.run_kernel.assert_called_once_with( |
| 327 | build_dir='.kunit', filter_glob='', timeout=300) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 328 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
| 329 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 330 | def test_run_passes_args_pass(self): |
| 331 | kunit.main(['run'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 332 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
| 333 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 334 | self.linux_source_mock.run_kernel.assert_called_once_with( |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 335 | build_dir='.kunit', filter_glob='', timeout=300) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 336 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
| 337 | |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 338 | def test_exec_passes_args_fail(self): |
| 339 | self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) |
| 340 | with self.assertRaises(SystemExit) as e: |
| 341 | kunit.main(['exec'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 342 | self.assertEqual(e.exception.code, 1) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 343 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 344 | def test_run_passes_args_fail(self): |
| 345 | self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) |
| 346 | with self.assertRaises(SystemExit) as e: |
| 347 | kunit.main(['run'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 348 | self.assertEqual(e.exception.code, 1) |
| 349 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
| 350 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 351 | self.print_mock.assert_any_call(StrContains(' 0 tests run')) |
| 352 | |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 353 | def test_exec_raw_output(self): |
| 354 | self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) |
| 355 | kunit.main(['exec', '--raw_output'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 356 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) |
| 357 | for call in self.print_mock.call_args_list: |
| 358 | self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) |
| 359 | self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 360 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 361 | def test_run_raw_output(self): |
| 362 | self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 363 | kunit.main(['run', '--raw_output'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 364 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
| 365 | self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) |
| 366 | for call in self.print_mock.call_args_list: |
| 367 | self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) |
| 368 | self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 369 | |
| 370 | def test_exec_timeout(self): |
| 371 | timeout = 3453 |
| 372 | kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 373 | self.linux_source_mock.run_kernel.assert_called_once_with( |
| 374 | build_dir='.kunit', filter_glob='', timeout=timeout) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 375 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 376 | |
| 377 | def test_run_timeout(self): |
| 378 | timeout = 3453 |
| 379 | kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 380 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 381 | self.linux_source_mock.run_kernel.assert_called_once_with( |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 382 | build_dir='.kunit', filter_glob='', timeout=timeout) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 383 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
| 384 | |
SeongJae Park | b1b3520 | 2019-12-20 05:14:08 +0000 | [diff] [blame] | 385 | def test_run_builddir(self): |
| 386 | build_dir = '.kunit' |
Brendan Higgins | d43c7fb | 2020-07-14 13:41:30 -0700 | [diff] [blame] | 387 | kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 388 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 389 | self.linux_source_mock.run_kernel.assert_called_once_with( |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 390 | build_dir=build_dir, filter_glob='', timeout=300) |
SeongJae Park | b1b3520 | 2019-12-20 05:14:08 +0000 | [diff] [blame] | 391 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
| 392 | |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 393 | def test_config_builddir(self): |
| 394 | build_dir = '.kunit' |
| 395 | kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock) |
Daniel Latypov | 0b3e680 | 2020-12-02 11:08:22 -0800 | [diff] [blame] | 396 | self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 397 | |
| 398 | def test_build_builddir(self): |
| 399 | build_dir = '.kunit' |
| 400 | kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 401 | self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, build_dir, None) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 402 | |
| 403 | def test_exec_builddir(self): |
| 404 | build_dir = '.kunit' |
| 405 | kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock) |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 406 | self.linux_source_mock.run_kernel.assert_called_once_with( |
| 407 | build_dir=build_dir, filter_glob='', timeout=300) |
David Gow | 45ba7a8 | 2020-04-30 21:27:01 -0700 | [diff] [blame] | 408 | self.print_mock.assert_any_call(StrContains('Testing complete.')) |
| 409 | |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 410 | @mock.patch.object(kunit_kernel, 'LinuxSourceTree') |
| 411 | def test_run_kunitconfig(self, mock_linux_init): |
| 412 | mock_linux_init.return_value = self.linux_source_mock |
| 413 | kunit.main(['run', '--kunitconfig=mykunitconfig']) |
| 414 | # Just verify that we parsed and initialized it correctly here. |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 415 | mock_linux_init.assert_called_once_with('.kunit', |
| 416 | kunitconfig_path='mykunitconfig', |
| 417 | arch='um', |
| 418 | cross_compile=None, |
| 419 | qemu_config_path=None) |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 420 | |
| 421 | @mock.patch.object(kunit_kernel, 'LinuxSourceTree') |
| 422 | def test_config_kunitconfig(self, mock_linux_init): |
| 423 | mock_linux_init.return_value = self.linux_source_mock |
| 424 | kunit.main(['config', '--kunitconfig=mykunitconfig']) |
| 425 | # Just verify that we parsed and initialized it correctly here. |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 426 | mock_linux_init.assert_called_once_with('.kunit', |
| 427 | kunitconfig_path='mykunitconfig', |
| 428 | arch='um', |
| 429 | cross_compile=None, |
| 430 | qemu_config_path=None) |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 431 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 432 | if __name__ == '__main__': |
| 433 | unittest.main() |