1 #!/usr/bin/env python3 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 # 3 # 4 # A collection of tests for tools/testing/kuni 4 # A collection of tests for tools/testing/kunit/kunit.py 5 # 5 # 6 # Copyright (C) 2019, Google LLC. 6 # Copyright (C) 2019, Google LLC. 7 # Author: Brendan Higgins <brendanhiggins@googl 7 # Author: Brendan Higgins <brendanhiggins@google.com> 8 8 9 import unittest 9 import unittest 10 from unittest import mock 10 from unittest import mock 11 11 12 import tempfile, shutil # Handling test_tmpdir 12 import tempfile, shutil # Handling test_tmpdir 13 13 14 import itertools 14 import itertools 15 import json 15 import json 16 import os 16 import os 17 import signal 17 import signal 18 import subprocess 18 import subprocess 19 from typing import Iterable 19 from typing import Iterable 20 20 21 import kunit_config 21 import kunit_config 22 import kunit_parser 22 import kunit_parser 23 import kunit_kernel 23 import kunit_kernel 24 import kunit_json 24 import kunit_json 25 import kunit 25 import kunit 26 26 27 test_tmpdir = '' 27 test_tmpdir = '' 28 abs_test_data_dir = '' 28 abs_test_data_dir = '' 29 29 30 def setUpModule(): 30 def setUpModule(): 31 global test_tmpdir, abs_test_data_dir 31 global test_tmpdir, abs_test_data_dir 32 test_tmpdir = tempfile.mkdtemp() 32 test_tmpdir = tempfile.mkdtemp() 33 abs_test_data_dir = os.path.abspath(os 33 abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data')) 34 34 35 def tearDownModule(): 35 def tearDownModule(): 36 shutil.rmtree(test_tmpdir) 36 shutil.rmtree(test_tmpdir) 37 37 38 def test_data_path(path): 38 def test_data_path(path): 39 return os.path.join(abs_test_data_dir, 39 return os.path.join(abs_test_data_dir, path) 40 40 41 class KconfigTest(unittest.TestCase): 41 class KconfigTest(unittest.TestCase): 42 42 43 def test_is_subset_of(self): 43 def test_is_subset_of(self): 44 kconfig0 = kunit_config.Kconfi 44 kconfig0 = kunit_config.Kconfig() 45 self.assertTrue(kconfig0.is_su 45 self.assertTrue(kconfig0.is_subset_of(kconfig0)) 46 46 47 kconfig1 = kunit_config.Kconfi 47 kconfig1 = kunit_config.Kconfig() 48 kconfig1.add_entry('TEST', 'y' !! 48 kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y')) 49 self.assertTrue(kconfig1.is_su 49 self.assertTrue(kconfig1.is_subset_of(kconfig1)) 50 self.assertTrue(kconfig0.is_su 50 self.assertTrue(kconfig0.is_subset_of(kconfig1)) 51 self.assertFalse(kconfig1.is_s 51 self.assertFalse(kconfig1.is_subset_of(kconfig0)) 52 52 53 def test_read_from_file(self): 53 def test_read_from_file(self): 54 kconfig_path = test_data_path( 54 kconfig_path = test_data_path('test_read_from_file.kconfig') 55 55 56 kconfig = kunit_config.parse_f 56 kconfig = kunit_config.parse_file(kconfig_path) 57 57 58 expected_kconfig = kunit_confi 58 expected_kconfig = kunit_config.Kconfig() 59 expected_kconfig.add_entry('UM !! 59 expected_kconfig.add_entry( 60 expected_kconfig.add_entry('MM !! 60 kunit_config.KconfigEntry('UML', 'y')) 61 expected_kconfig.add_entry('TE !! 61 expected_kconfig.add_entry( 62 expected_kconfig.add_entry('EX !! 62 kunit_config.KconfigEntry('MMU', 'y')) 63 expected_kconfig.add_entry('MK !! 63 expected_kconfig.add_entry( >> 64 kunit_config.KconfigEntry('TEST', 'y')) >> 65 expected_kconfig.add_entry( >> 66 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) >> 67 expected_kconfig.add_entry( >> 68 kunit_config.KconfigEntry('MK8', 'n')) 64 69 65 self.assertEqual(kconfig, expe !! 70 self.assertEqual(kconfig.entries(), expected_kconfig.entries()) 66 71 67 def test_write_to_file(self): 72 def test_write_to_file(self): 68 kconfig_path = os.path.join(te 73 kconfig_path = os.path.join(test_tmpdir, '.config') 69 74 70 expected_kconfig = kunit_confi 75 expected_kconfig = kunit_config.Kconfig() 71 expected_kconfig.add_entry('UM !! 76 expected_kconfig.add_entry( 72 expected_kconfig.add_entry('MM !! 77 kunit_config.KconfigEntry('UML', 'y')) 73 expected_kconfig.add_entry('TE !! 78 expected_kconfig.add_entry( 74 expected_kconfig.add_entry('EX !! 79 kunit_config.KconfigEntry('MMU', 'y')) 75 expected_kconfig.add_entry('MK !! 80 expected_kconfig.add_entry( >> 81 kunit_config.KconfigEntry('TEST', 'y')) >> 82 expected_kconfig.add_entry( >> 83 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) >> 84 expected_kconfig.add_entry( >> 85 kunit_config.KconfigEntry('MK8', 'n')) 76 86 77 expected_kconfig.write_to_file 87 expected_kconfig.write_to_file(kconfig_path) 78 88 79 actual_kconfig = kunit_config. 89 actual_kconfig = kunit_config.parse_file(kconfig_path) 80 self.assertEqual(actual_kconfi << 81 90 82 class KUnitParserTest(unittest.TestCase): !! 91 self.assertEqual(actual_kconfig.entries(), 83 def setUp(self): !! 92 expected_kconfig.entries()) 84 self.print_mock = mock.patch(' << 85 self.addCleanup(mock.patch.sto << 86 93 87 def noPrintCallContains(self, substr: !! 94 class KUnitParserTest(unittest.TestCase): 88 for call in self.print_mock.mo << 89 self.assertNotIn(subst << 90 95 91 def assertContains(self, needle: str, 96 def assertContains(self, needle: str, haystack: kunit_parser.LineStream): 92 # Clone the iterator so we can 97 # Clone the iterator so we can print the contents on failure. 93 copy, backup = itertools.tee(h 98 copy, backup = itertools.tee(haystack) 94 for line in copy: 99 for line in copy: 95 if needle in line: 100 if needle in line: 96 return 101 return 97 raise AssertionError(f'"{needl 102 raise AssertionError(f'"{needle}" not found in {list(backup)}!') 98 103 99 def test_output_isolated_correctly(sel 104 def test_output_isolated_correctly(self): 100 log_path = test_data_path('tes 105 log_path = test_data_path('test_output_isolated_correctly.log') 101 with open(log_path) as file: 106 with open(log_path) as file: 102 result = kunit_parser. 107 result = kunit_parser.extract_tap_lines(file.readlines()) 103 self.assertContains('TAP versi 108 self.assertContains('TAP version 14', result) 104 self.assertContains('# Subtest 109 self.assertContains('# Subtest: example', result) 105 self.assertContains('1..2', re 110 self.assertContains('1..2', result) 106 self.assertContains('ok 1 - ex 111 self.assertContains('ok 1 - example_simple_test', result) 107 self.assertContains('ok 2 - ex 112 self.assertContains('ok 2 - example_mock_test', result) 108 self.assertContains('ok 1 - ex 113 self.assertContains('ok 1 - example', result) 109 114 110 def test_output_with_prefix_isolated_c 115 def test_output_with_prefix_isolated_correctly(self): 111 log_path = test_data_path('tes 116 log_path = test_data_path('test_pound_sign.log') 112 with open(log_path) as file: 117 with open(log_path) as file: 113 result = kunit_parser. 118 result = kunit_parser.extract_tap_lines(file.readlines()) 114 self.assertContains('TAP versi 119 self.assertContains('TAP version 14', result) 115 self.assertContains('# Subtest 120 self.assertContains('# Subtest: kunit-resource-test', result) 116 self.assertContains('1..5', re 121 self.assertContains('1..5', result) 117 self.assertContains('ok 1 - ku 122 self.assertContains('ok 1 - kunit_resource_test_init_resources', result) 118 self.assertContains('ok 2 - ku 123 self.assertContains('ok 2 - kunit_resource_test_alloc_resource', result) 119 self.assertContains('ok 3 - ku 124 self.assertContains('ok 3 - kunit_resource_test_destroy_resource', result) 120 self.assertContains('foo bar 125 self.assertContains('foo bar #', result) 121 self.assertContains('ok 4 - ku 126 self.assertContains('ok 4 - kunit_resource_test_cleanup_resources', result) 122 self.assertContains('ok 5 - ku 127 self.assertContains('ok 5 - kunit_resource_test_proper_free_ordering', result) 123 self.assertContains('ok 1 - ku 128 self.assertContains('ok 1 - kunit-resource-test', result) 124 self.assertContains('foo bar 129 self.assertContains('foo bar # non-kunit output', result) 125 self.assertContains('# Subtest 130 self.assertContains('# Subtest: kunit-try-catch-test', result) 126 self.assertContains('1..2', re 131 self.assertContains('1..2', result) 127 self.assertContains('ok 1 - ku 132 self.assertContains('ok 1 - kunit_test_try_catch_successful_try_no_catch', 128 result) 133 result) 129 self.assertContains('ok 2 - ku 134 self.assertContains('ok 2 - kunit_test_try_catch_unsuccessful_try_does_catch', 130 result) 135 result) 131 self.assertContains('ok 2 - ku 136 self.assertContains('ok 2 - kunit-try-catch-test', result) 132 self.assertContains('# Subtest 137 self.assertContains('# Subtest: string-stream-test', result) 133 self.assertContains('1..3', re 138 self.assertContains('1..3', result) 134 self.assertContains('ok 1 - st 139 self.assertContains('ok 1 - string_stream_test_empty_on_creation', result) 135 self.assertContains('ok 2 - st 140 self.assertContains('ok 2 - string_stream_test_not_empty_after_add', result) 136 self.assertContains('ok 3 - st 141 self.assertContains('ok 3 - string_stream_test_get_string', result) 137 self.assertContains('ok 3 - st 142 self.assertContains('ok 3 - string-stream-test', result) 138 143 139 def test_parse_successful_test_log(sel 144 def test_parse_successful_test_log(self): 140 all_passed_log = test_data_pat 145 all_passed_log = test_data_path('test_is_test_passed-all_passed.log') 141 with open(all_passed_log) as f 146 with open(all_passed_log) as file: 142 result = kunit_parser. 147 result = kunit_parser.parse_run_tests(file.readlines()) 143 self.assertEqual(kunit_parser. !! 148 self.assertEqual( 144 self.assertEqual(result.counts !! 149 kunit_parser.TestStatus.SUCCESS, >> 150 result.status) 145 151 146 def test_parse_successful_nested_tests 152 def test_parse_successful_nested_tests_log(self): 147 all_passed_log = test_data_pat 153 all_passed_log = test_data_path('test_is_test_passed-all_passed_nested.log') 148 with open(all_passed_log) as f 154 with open(all_passed_log) as file: 149 result = kunit_parser. 155 result = kunit_parser.parse_run_tests(file.readlines()) 150 self.assertEqual(kunit_parser. !! 156 self.assertEqual( 151 self.assertEqual(result.counts !! 157 kunit_parser.TestStatus.SUCCESS, >> 158 result.status) 152 159 153 def test_kselftest_nested(self): 160 def test_kselftest_nested(self): 154 kselftest_log = test_data_path 161 kselftest_log = test_data_path('test_is_test_passed-kselftest.log') 155 with open(kselftest_log) as fi 162 with open(kselftest_log) as file: 156 result = kunit_parser. 163 result = kunit_parser.parse_run_tests(file.readlines()) 157 self.assertEqual(kunit_parser. !! 164 self.assertEqual( 158 self.assertEqual(result.counts !! 165 kunit_parser.TestStatus.SUCCESS, >> 166 result.status) 159 167 160 def test_parse_failed_test_log(self): 168 def test_parse_failed_test_log(self): 161 failed_log = test_data_path('t 169 failed_log = test_data_path('test_is_test_passed-failure.log') 162 with open(failed_log) as file: 170 with open(failed_log) as file: 163 result = kunit_parser. 171 result = kunit_parser.parse_run_tests(file.readlines()) 164 self.assertEqual(kunit_parser. !! 172 self.assertEqual( 165 self.assertEqual(result.counts !! 173 kunit_parser.TestStatus.FAILURE, >> 174 result.status) 166 175 167 def test_no_header(self): 176 def test_no_header(self): 168 empty_log = test_data_path('te 177 empty_log = test_data_path('test_is_test_passed-no_tests_run_no_header.log') 169 with open(empty_log) as file: 178 with open(empty_log) as file: 170 result = kunit_parser. 179 result = kunit_parser.parse_run_tests( 171 kunit_parser.e 180 kunit_parser.extract_tap_lines(file.readlines())) 172 self.assertEqual(0, len(result 181 self.assertEqual(0, len(result.subtests)) 173 self.assertEqual(kunit_parser. !! 182 self.assertEqual( 174 self.assertEqual(result.counts !! 183 kunit_parser.TestStatus.FAILURE_TO_PARSE_TESTS, >> 184 result.status) 175 185 176 def test_missing_test_plan(self): 186 def test_missing_test_plan(self): 177 missing_plan_log = test_data_p 187 missing_plan_log = test_data_path('test_is_test_passed-' 178 'missing_plan.log') 188 'missing_plan.log') 179 with open(missing_plan_log) as 189 with open(missing_plan_log) as file: 180 result = kunit_parser. 190 result = kunit_parser.parse_run_tests( 181 kunit_parser.e 191 kunit_parser.extract_tap_lines( 182 file.readlines 192 file.readlines())) 183 # A missing test plan is not a 193 # A missing test plan is not an error. 184 self.assertEqual(result.counts !! 194 self.assertEqual(0, result.counts.errors) 185 self.assertEqual(kunit_parser. !! 195 # All tests should be accounted for. >> 196 self.assertEqual(10, result.counts.total()) >> 197 self.assertEqual( >> 198 kunit_parser.TestStatus.SUCCESS, >> 199 result.status) 186 200 187 def test_no_tests(self): 201 def test_no_tests(self): 188 header_log = test_data_path('t 202 header_log = test_data_path('test_is_test_passed-no_tests_run_with_header.log') 189 with open(header_log) as file: 203 with open(header_log) as file: 190 result = kunit_parser. 204 result = kunit_parser.parse_run_tests( 191 kunit_parser.e 205 kunit_parser.extract_tap_lines(file.readlines())) 192 self.assertEqual(0, len(result 206 self.assertEqual(0, len(result.subtests)) 193 self.assertEqual(kunit_parser. !! 207 self.assertEqual( 194 self.assertEqual(result.counts !! 208 kunit_parser.TestStatus.NO_TESTS, >> 209 result.status) 195 210 196 def test_no_tests_no_plan(self): 211 def test_no_tests_no_plan(self): 197 no_plan_log = test_data_path(' 212 no_plan_log = test_data_path('test_is_test_passed-no_tests_no_plan.log') 198 with open(no_plan_log) as file 213 with open(no_plan_log) as file: 199 result = kunit_parser. 214 result = kunit_parser.parse_run_tests( 200 kunit_parser.e 215 kunit_parser.extract_tap_lines(file.readlines())) 201 self.assertEqual(0, len(result 216 self.assertEqual(0, len(result.subtests[0].subtests[0].subtests)) 202 self.assertEqual( 217 self.assertEqual( 203 kunit_parser.TestStatu 218 kunit_parser.TestStatus.NO_TESTS, 204 result.subtests[0].sub 219 result.subtests[0].subtests[0].status) 205 self.assertEqual(result.counts !! 220 self.assertEqual(1, result.counts.errors) 206 221 207 222 208 def test_no_kunit_output(self): 223 def test_no_kunit_output(self): 209 crash_log = test_data_path('te 224 crash_log = test_data_path('test_insufficient_memory.log') 210 print_mock = mock.patch('kunit !! 225 print_mock = mock.patch('builtins.print').start() 211 with open(crash_log) as file: 226 with open(crash_log) as file: 212 result = kunit_parser. 227 result = kunit_parser.parse_run_tests( 213 kunit_parser.e 228 kunit_parser.extract_tap_lines(file.readlines())) 214 print_mock.assert_any_call(Str !! 229 print_mock.assert_any_call(StrContains('invalid KTAP input!')) 215 print_mock.stop() 230 print_mock.stop() 216 self.assertEqual(0, len(result 231 self.assertEqual(0, len(result.subtests)) 217 self.assertEqual(result.counts !! 232 >> 233 def test_crashed_test(self): >> 234 crashed_log = test_data_path('test_is_test_passed-crash.log') >> 235 with open(crashed_log) as file: >> 236 result = kunit_parser.parse_run_tests( >> 237 file.readlines()) >> 238 self.assertEqual( >> 239 kunit_parser.TestStatus.TEST_CRASHED, >> 240 result.status) 218 241 219 def test_skipped_test(self): 242 def test_skipped_test(self): 220 skipped_log = test_data_path(' 243 skipped_log = test_data_path('test_skip_tests.log') 221 with open(skipped_log) as file 244 with open(skipped_log) as file: 222 result = kunit_parser. 245 result = kunit_parser.parse_run_tests(file.readlines()) 223 246 224 # A skipped test does not fail 247 # A skipped test does not fail the whole suite. 225 self.assertEqual(kunit_parser. !! 248 self.assertEqual( 226 self.assertEqual(result.counts !! 249 kunit_parser.TestStatus.SUCCESS, >> 250 result.status) 227 251 228 def test_skipped_all_tests(self): 252 def test_skipped_all_tests(self): 229 skipped_log = test_data_path(' 253 skipped_log = test_data_path('test_skip_all_tests.log') 230 with open(skipped_log) as file 254 with open(skipped_log) as file: 231 result = kunit_parser. 255 result = kunit_parser.parse_run_tests(file.readlines()) 232 256 233 self.assertEqual(kunit_parser. !! 257 self.assertEqual( 234 self.assertEqual(result.counts !! 258 kunit_parser.TestStatus.SKIPPED, >> 259 result.status) 235 260 236 def test_ignores_hyphen(self): 261 def test_ignores_hyphen(self): 237 hyphen_log = test_data_path('t 262 hyphen_log = test_data_path('test_strip_hyphen.log') 238 with open(hyphen_log) as file: !! 263 file = open(hyphen_log) 239 result = kunit_parser. !! 264 result = kunit_parser.parse_run_tests(file.readlines()) 240 265 241 # A skipped test does not fail 266 # A skipped test does not fail the whole suite. 242 self.assertEqual(kunit_parser. !! 267 self.assertEqual( >> 268 kunit_parser.TestStatus.SUCCESS, >> 269 result.status) 243 self.assertEqual( 270 self.assertEqual( 244 "sysctl_test", 271 "sysctl_test", 245 result.subtests[0].nam 272 result.subtests[0].name) 246 self.assertEqual( 273 self.assertEqual( 247 "example", 274 "example", 248 result.subtests[1].nam 275 result.subtests[1].name) >> 276 file.close() >> 277 249 278 250 def test_ignores_prefix_printk_time(se 279 def test_ignores_prefix_printk_time(self): 251 prefix_log = test_data_path('t 280 prefix_log = test_data_path('test_config_printk_time.log') 252 with open(prefix_log) as file: 281 with open(prefix_log) as file: 253 result = kunit_parser. 282 result = kunit_parser.parse_run_tests(file.readlines()) 254 self.assertEqual(kunit_parser. !! 283 self.assertEqual( 255 self.assertEqual('kunit-resour !! 284 kunit_parser.TestStatus.SUCCESS, 256 self.assertEqual(result.counts !! 285 result.status) >> 286 self.assertEqual('kunit-resource-test', result.subtests[0].name) 257 287 258 def test_ignores_multiple_prefixes(sel 288 def test_ignores_multiple_prefixes(self): 259 prefix_log = test_data_path('t 289 prefix_log = test_data_path('test_multiple_prefixes.log') 260 with open(prefix_log) as file: 290 with open(prefix_log) as file: 261 result = kunit_parser. 291 result = kunit_parser.parse_run_tests(file.readlines()) 262 self.assertEqual(kunit_parser. !! 292 self.assertEqual( 263 self.assertEqual('kunit-resour !! 293 kunit_parser.TestStatus.SUCCESS, 264 self.assertEqual(result.counts !! 294 result.status) >> 295 self.assertEqual('kunit-resource-test', result.subtests[0].name) 265 296 266 def test_prefix_mixed_kernel_output(se 297 def test_prefix_mixed_kernel_output(self): 267 mixed_prefix_log = test_data_p 298 mixed_prefix_log = test_data_path('test_interrupted_tap_output.log') 268 with open(mixed_prefix_log) as 299 with open(mixed_prefix_log) as file: 269 result = kunit_parser. 300 result = kunit_parser.parse_run_tests(file.readlines()) 270 self.assertEqual(kunit_parser. !! 301 self.assertEqual( 271 self.assertEqual('kunit-resour !! 302 kunit_parser.TestStatus.SUCCESS, 272 self.assertEqual(result.counts !! 303 result.status) >> 304 self.assertEqual('kunit-resource-test', result.subtests[0].name) 273 305 274 def test_prefix_poundsign(self): 306 def test_prefix_poundsign(self): 275 pound_log = test_data_path('te 307 pound_log = test_data_path('test_pound_sign.log') 276 with open(pound_log) as file: 308 with open(pound_log) as file: 277 result = kunit_parser. 309 result = kunit_parser.parse_run_tests(file.readlines()) 278 self.assertEqual(kunit_parser. !! 310 self.assertEqual( 279 self.assertEqual('kunit-resour !! 311 kunit_parser.TestStatus.SUCCESS, 280 self.assertEqual(result.counts !! 312 result.status) >> 313 self.assertEqual('kunit-resource-test', result.subtests[0].name) 281 314 282 def test_kernel_panic_end(self): 315 def test_kernel_panic_end(self): 283 panic_log = test_data_path('te 316 panic_log = test_data_path('test_kernel_panic_interrupt.log') 284 with open(panic_log) as file: 317 with open(panic_log) as file: 285 result = kunit_parser. 318 result = kunit_parser.parse_run_tests(file.readlines()) 286 self.assertEqual(kunit_parser. !! 319 self.assertEqual( 287 self.assertEqual('kunit-resour !! 320 kunit_parser.TestStatus.TEST_CRASHED, 288 self.assertGreaterEqual(result !! 321 result.status) >> 322 self.assertEqual('kunit-resource-test', result.subtests[0].name) 289 323 290 def test_pound_no_prefix(self): 324 def test_pound_no_prefix(self): 291 pound_log = test_data_path('te 325 pound_log = test_data_path('test_pound_no_prefix.log') 292 with open(pound_log) as file: 326 with open(pound_log) as file: 293 result = kunit_parser. 327 result = kunit_parser.parse_run_tests(file.readlines()) 294 self.assertEqual(kunit_parser. !! 328 self.assertEqual( 295 self.assertEqual('kunit-resour !! 329 kunit_parser.TestStatus.SUCCESS, 296 self.assertEqual(result.counts !! 330 result.status) 297 !! 331 self.assertEqual('kunit-resource-test', result.subtests[0].name) 298 def test_summarize_failures(self): << 299 output = """ << 300 KTAP version 1 << 301 1..2 << 302 # Subtest: all_failed_ << 303 1..2 << 304 not ok 1 - test1 << 305 not ok 2 - test2 << 306 not ok 1 - all_failed_suite << 307 # Subtest: some_failed << 308 1..2 << 309 ok 1 - test1 << 310 not ok 2 - test2 << 311 not ok 1 - some_failed_suite << 312 """ << 313 result = kunit_parser.parse_ru << 314 self.assertEqual(kunit_parser. << 315 << 316 self.assertEqual(kunit_parser. << 317 'Failures: all_failed_ << 318 << 319 def test_ktap_format(self): << 320 ktap_log = test_data_path('tes << 321 with open(ktap_log) as file: << 322 result = kunit_parser. << 323 self.assertEqual(result.counts << 324 self.assertEqual('suite', resu << 325 self.assertEqual('case_1', res << 326 self.assertEqual('case_2', res << 327 << 328 def test_parse_subtest_header(self): << 329 ktap_log = test_data_path('tes << 330 with open(ktap_log) as file: << 331 kunit_parser.parse_run << 332 self.print_mock.assert_any_cal << 333 << 334 def test_parse_attributes(self): << 335 ktap_log = test_data_path('tes << 336 with open(ktap_log) as file: << 337 result = kunit_parser. << 338 << 339 # Test should pass with no err << 340 self.assertEqual(result.counts << 341 self.assertEqual(kunit_parser. << 342 << 343 # Ensure suite header is parse << 344 self.print_mock.assert_any_cal << 345 << 346 # Ensure attributes in correct << 347 self.assertContains('# module: << 348 self.assertContains('# test.sp << 349 << 350 def test_show_test_output_on_failure(s << 351 output = """ << 352 KTAP version 1 << 353 1..1 << 354 Test output. << 355 Indented more. << 356 not ok 1 test1 << 357 """ << 358 result = kunit_parser.parse_ru << 359 self.assertEqual(kunit_parser. << 360 << 361 self.print_mock.assert_any_cal << 362 self.print_mock.assert_any_cal << 363 self.noPrintCallContains('not << 364 332 365 def line_stream_from_strs(strs: Iterable[str]) 333 def line_stream_from_strs(strs: Iterable[str]) -> kunit_parser.LineStream: 366 return kunit_parser.LineStream(enumera 334 return kunit_parser.LineStream(enumerate(strs, start=1)) 367 335 368 class LineStreamTest(unittest.TestCase): 336 class LineStreamTest(unittest.TestCase): 369 337 370 def test_basic(self): 338 def test_basic(self): 371 stream = line_stream_from_strs 339 stream = line_stream_from_strs(['hello', 'world']) 372 340 373 self.assertTrue(stream, msg='S 341 self.assertTrue(stream, msg='Should be more input') 374 self.assertEqual(stream.line_n 342 self.assertEqual(stream.line_number(), 1) 375 self.assertEqual(stream.peek() 343 self.assertEqual(stream.peek(), 'hello') 376 self.assertEqual(stream.pop(), 344 self.assertEqual(stream.pop(), 'hello') 377 345 378 self.assertTrue(stream, msg='S 346 self.assertTrue(stream, msg='Should be more input') 379 self.assertEqual(stream.line_n 347 self.assertEqual(stream.line_number(), 2) 380 self.assertEqual(stream.peek() 348 self.assertEqual(stream.peek(), 'world') 381 self.assertEqual(stream.pop(), 349 self.assertEqual(stream.pop(), 'world') 382 350 383 self.assertFalse(stream, msg=' 351 self.assertFalse(stream, msg='Should be no more input') 384 with self.assertRaisesRegex(Va 352 with self.assertRaisesRegex(ValueError, 'LineStream: going past EOF'): 385 stream.pop() 353 stream.pop() 386 354 387 def test_is_lazy(self): 355 def test_is_lazy(self): 388 called_times = 0 356 called_times = 0 389 def generator(): 357 def generator(): 390 nonlocal called_times 358 nonlocal called_times 391 for _ in range(1,5): !! 359 for i in range(1,5): 392 called_times + 360 called_times += 1 393 yield called_t 361 yield called_times, str(called_times) 394 362 395 stream = kunit_parser.LineStre 363 stream = kunit_parser.LineStream(generator()) 396 self.assertEqual(called_times, 364 self.assertEqual(called_times, 0) 397 365 398 self.assertEqual(stream.pop(), 366 self.assertEqual(stream.pop(), '1') 399 self.assertEqual(called_times, 367 self.assertEqual(called_times, 1) 400 368 401 self.assertEqual(stream.pop(), 369 self.assertEqual(stream.pop(), '2') 402 self.assertEqual(called_times, 370 self.assertEqual(called_times, 2) 403 371 404 class LinuxSourceTreeTest(unittest.TestCase): 372 class LinuxSourceTreeTest(unittest.TestCase): 405 373 406 def setUp(self): 374 def setUp(self): 407 mock.patch.object(signal, 'sig 375 mock.patch.object(signal, 'signal').start() 408 self.addCleanup(mock.patch.sto 376 self.addCleanup(mock.patch.stopall) 409 377 410 def test_invalid_kunitconfig(self): 378 def test_invalid_kunitconfig(self): 411 with self.assertRaisesRegex(ku 379 with self.assertRaisesRegex(kunit_kernel.ConfigError, 'nonexistent.* does not exist'): 412 kunit_kernel.LinuxSour !! 380 kunit_kernel.LinuxSourceTree('', kunitconfig_path='/nonexistent_file') 413 381 414 def test_valid_kunitconfig(self): 382 def test_valid_kunitconfig(self): 415 with tempfile.NamedTemporaryFi 383 with tempfile.NamedTemporaryFile('wt') as kunitconfig: 416 kunit_kernel.LinuxSour !! 384 kunit_kernel.LinuxSourceTree('', kunitconfig_path=kunitconfig.name) 417 385 418 def test_dir_kunitconfig(self): 386 def test_dir_kunitconfig(self): 419 with tempfile.TemporaryDirecto 387 with tempfile.TemporaryDirectory('') as dir: 420 with open(os.path.join 388 with open(os.path.join(dir, '.kunitconfig'), 'w'): 421 pass 389 pass 422 kunit_kernel.LinuxSour !! 390 kunit_kernel.LinuxSourceTree('', kunitconfig_path=dir) 423 << 424 def test_multiple_kunitconfig(self): << 425 want_kconfig = kunit_config.Kc << 426 want_kconfig.add_entry('KUNIT' << 427 want_kconfig.add_entry('KUNIT_ << 428 << 429 with tempfile.TemporaryDirecto << 430 other = os.path.join(d << 431 with open(os.path.join << 432 f.write('CONFI << 433 with open(other, 'w') << 434 f.write('CONFI << 435 pass << 436 << 437 tree = kunit_kernel.Li << 438 self.assertTrue(want_k << 439 << 440 << 441 def test_multiple_kunitconfig_invalid( << 442 with tempfile.TemporaryDirecto << 443 other = os.path.join(d << 444 with open(os.path.join << 445 f.write('CONFI << 446 with open(other, 'w') << 447 f.write('CONFI << 448 << 449 with self.assertRaises << 450 kunit_kernel.L << 451 << 452 391 453 def test_kconfig_add(self): 392 def test_kconfig_add(self): 454 want_kconfig = kunit_config.Kc << 455 want_kconfig.add_entry('NOT_RE << 456 << 457 tree = kunit_kernel.LinuxSourc 393 tree = kunit_kernel.LinuxSourceTree('', kconfig_add=['CONFIG_NOT_REAL=y']) 458 self.assertTrue(want_kconfig.i !! 394 self.assertIn(kunit_config.KconfigEntry('NOT_REAL', 'y'), tree._kconfig.entries()) 459 395 460 def test_invalid_arch(self): 396 def test_invalid_arch(self): 461 with self.assertRaisesRegex(ku 397 with self.assertRaisesRegex(kunit_kernel.ConfigError, 'not a valid arch, options are.*x86_64'): 462 kunit_kernel.LinuxSour 398 kunit_kernel.LinuxSourceTree('', arch='invalid') 463 399 464 def test_run_kernel_hits_exception(sel 400 def test_run_kernel_hits_exception(self): 465 def fake_start(unused_args, un 401 def fake_start(unused_args, unused_build_dir): 466 return subprocess.Pope 402 return subprocess.Popen(['echo "hi\nbye"'], shell=True, text=True, stdout=subprocess.PIPE) 467 403 468 with tempfile.TemporaryDirecto 404 with tempfile.TemporaryDirectory('') as build_dir: 469 tree = kunit_kernel.Li !! 405 tree = kunit_kernel.LinuxSourceTree(build_dir, load_config=False) 470 mock.patch.object(tree 406 mock.patch.object(tree._ops, 'start', side_effect=fake_start).start() 471 407 472 with self.assertRaises 408 with self.assertRaises(ValueError): 473 for line in tr 409 for line in tree.run_kernel(build_dir=build_dir): 474 self.a 410 self.assertEqual(line, 'hi\n') 475 raise 411 raise ValueError('uh oh, did not read all output') 476 412 477 with open(kunit_kernel 413 with open(kunit_kernel.get_outfile_path(build_dir), 'rt') as outfile: 478 self.assertEqu 414 self.assertEqual(outfile.read(), 'hi\nbye\n', msg='Missing some output') 479 415 480 def test_build_reconfig_no_config(self 416 def test_build_reconfig_no_config(self): 481 with tempfile.TemporaryDirecto 417 with tempfile.TemporaryDirectory('') as build_dir: 482 with open(kunit_kernel 418 with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f: 483 f.write('CONFI 419 f.write('CONFIG_KUNIT=y') 484 420 485 tree = kunit_kernel.Li 421 tree = kunit_kernel.LinuxSourceTree(build_dir) 486 # Stub out the source << 487 # the defaults for any << 488 # way. << 489 tree._ops = kunit_kern << 490 mock_build_config = mo 422 mock_build_config = mock.patch.object(tree, 'build_config').start() 491 423 492 # Should generate the 424 # Should generate the .config 493 self.assertTrue(tree.b 425 self.assertTrue(tree.build_reconfig(build_dir, make_options=[])) 494 mock_build_config.asse 426 mock_build_config.assert_called_once_with(build_dir, []) 495 427 496 def test_build_reconfig_existing_confi 428 def test_build_reconfig_existing_config(self): 497 with tempfile.TemporaryDirecto 429 with tempfile.TemporaryDirectory('') as build_dir: 498 # Existing .config is 430 # Existing .config is a superset, should not touch it 499 with open(kunit_kernel 431 with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f: 500 f.write('CONFI 432 f.write('CONFIG_KUNIT=y') 501 with open(kunit_kernel 433 with open(kunit_kernel.get_old_kunitconfig_path(build_dir), 'w') as f: 502 f.write('CONFI 434 f.write('CONFIG_KUNIT=y') 503 with open(kunit_kernel 435 with open(kunit_kernel.get_kconfig_path(build_dir), 'w') as f: 504 f.write('CONFI 436 f.write('CONFIG_KUNIT=y\nCONFIG_KUNIT_TEST=y') 505 437 506 tree = kunit_kernel.Li 438 tree = kunit_kernel.LinuxSourceTree(build_dir) 507 # Stub out the source << 508 # the defaults for any << 509 # way. << 510 tree._ops = kunit_kern << 511 mock_build_config = mo 439 mock_build_config = mock.patch.object(tree, 'build_config').start() 512 440 513 self.assertTrue(tree.b 441 self.assertTrue(tree.build_reconfig(build_dir, make_options=[])) 514 self.assertEqual(mock_ 442 self.assertEqual(mock_build_config.call_count, 0) 515 443 516 def test_build_reconfig_remove_option( 444 def test_build_reconfig_remove_option(self): 517 with tempfile.TemporaryDirecto 445 with tempfile.TemporaryDirectory('') as build_dir: 518 # We removed CONFIG_KU 446 # We removed CONFIG_KUNIT_TEST=y from our .kunitconfig... 519 with open(kunit_kernel 447 with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f: 520 f.write('CONFI 448 f.write('CONFIG_KUNIT=y') 521 with open(kunit_kernel 449 with open(kunit_kernel.get_old_kunitconfig_path(build_dir), 'w') as f: 522 f.write('CONFI 450 f.write('CONFIG_KUNIT=y\nCONFIG_KUNIT_TEST=y') 523 with open(kunit_kernel 451 with open(kunit_kernel.get_kconfig_path(build_dir), 'w') as f: 524 f.write('CONFI 452 f.write('CONFIG_KUNIT=y\nCONFIG_KUNIT_TEST=y') 525 453 526 tree = kunit_kernel.Li 454 tree = kunit_kernel.LinuxSourceTree(build_dir) 527 # Stub out the source << 528 # the defaults for any << 529 # way. << 530 tree._ops = kunit_kern << 531 mock_build_config = mo 455 mock_build_config = mock.patch.object(tree, 'build_config').start() 532 456 533 # ... so we should tri 457 # ... so we should trigger a call to build_config() 534 self.assertTrue(tree.b 458 self.assertTrue(tree.build_reconfig(build_dir, make_options=[])) 535 mock_build_config.asse 459 mock_build_config.assert_called_once_with(build_dir, []) 536 460 537 # TODO: add more test cases. 461 # TODO: add more test cases. 538 462 539 463 540 class KUnitJsonTest(unittest.TestCase): 464 class KUnitJsonTest(unittest.TestCase): 541 def setUp(self): << 542 self.print_mock = mock.patch(' << 543 self.addCleanup(mock.patch.sto << 544 465 545 def _json_for(self, log_file): 466 def _json_for(self, log_file): 546 with open(test_data_path(log_f 467 with open(test_data_path(log_file)) as file: 547 test_result = kunit_pa 468 test_result = kunit_parser.parse_run_tests(file) 548 json_obj = kunit_json. 469 json_obj = kunit_json.get_json_result( 549 test=test_resu 470 test=test_result, 550 metadata=kunit !! 471 def_config='kunit_defconfig', >> 472 build_dir=None, >> 473 json_path='stdout') 551 return json.loads(json_obj) 474 return json.loads(json_obj) 552 475 553 def test_failed_test_json(self): 476 def test_failed_test_json(self): 554 result = self._json_for('test_ 477 result = self._json_for('test_is_test_passed-failure.log') 555 self.assertEqual( 478 self.assertEqual( 556 {'name': 'example_simp 479 {'name': 'example_simple_test', 'status': 'FAIL'}, 557 result["sub_groups"][1 480 result["sub_groups"][1]["test_cases"][0]) 558 481 559 def test_crashed_test_json(self): 482 def test_crashed_test_json(self): 560 result = self._json_for('test_ !! 483 result = self._json_for('test_is_test_passed-crash.log') 561 self.assertEqual( 484 self.assertEqual( 562 {'name': '', 'status': !! 485 {'name': 'example_simple_test', 'status': 'ERROR'}, 563 result["sub_groups"][2 !! 486 result["sub_groups"][1]["test_cases"][0]) 564 487 565 def test_skipped_test_json(self): 488 def test_skipped_test_json(self): 566 result = self._json_for('test_ 489 result = self._json_for('test_skip_tests.log') 567 self.assertEqual( 490 self.assertEqual( 568 {'name': 'example_skip 491 {'name': 'example_skip_test', 'status': 'SKIP'}, 569 result["sub_groups"][1 492 result["sub_groups"][1]["test_cases"][1]) 570 493 571 def test_no_tests_json(self): 494 def test_no_tests_json(self): 572 result = self._json_for('test_ 495 result = self._json_for('test_is_test_passed-no_tests_run_with_header.log') 573 self.assertEqual(0, len(result 496 self.assertEqual(0, len(result['sub_groups'])) 574 497 575 def test_nested_json(self): 498 def test_nested_json(self): 576 result = self._json_for('test_ 499 result = self._json_for('test_is_test_passed-all_passed_nested.log') 577 self.assertEqual( 500 self.assertEqual( 578 {'name': 'example_simp 501 {'name': 'example_simple_test', 'status': 'PASS'}, 579 result["sub_groups"][0 502 result["sub_groups"][0]["sub_groups"][0]["test_cases"][0]) 580 503 581 class StrContains(str): 504 class StrContains(str): 582 def __eq__(self, other): 505 def __eq__(self, other): 583 return self in other 506 return self in other 584 507 585 class KUnitMainTest(unittest.TestCase): 508 class KUnitMainTest(unittest.TestCase): 586 def setUp(self): 509 def setUp(self): 587 path = test_data_path('test_is 510 path = test_data_path('test_is_test_passed-all_passed.log') 588 with open(path) as file: 511 with open(path) as file: 589 all_passed_log = file. 512 all_passed_log = file.readlines() 590 513 591 self.print_mock = mock.patch(' !! 514 self.print_mock = mock.patch('builtins.print').start() 592 self.addCleanup(mock.patch.sto 515 self.addCleanup(mock.patch.stopall) 593 516 594 self.mock_linux_init = mock.pa !! 517 self.linux_source_mock = mock.Mock() 595 self.linux_source_mock = self. !! 518 self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) 596 self.linux_source_mock.build_r !! 519 self.linux_source_mock.build_kernel = mock.Mock(return_value=True) 597 self.linux_source_mock.build_k !! 520 self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) 598 self.linux_source_mock.run_ker << 599 521 600 def test_config_passes_args_pass(self) 522 def test_config_passes_args_pass(self): 601 kunit.main(['config', '--build !! 523 kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) 602 self.assertEqual(self.linux_so 524 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 603 self.assertEqual(self.linux_so 525 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) 604 526 605 def test_build_passes_args_pass(self): 527 def test_build_passes_args_pass(self): 606 kunit.main(['build']) !! 528 kunit.main(['build'], self.linux_source_mock) 607 self.assertEqual(self.linux_so 529 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 608 self.linux_source_mock.build_k !! 530 self.linux_source_mock.build_kernel.assert_called_once_with(False, kunit.get_default_jobs(), '.kunit', None) 609 self.assertEqual(self.linux_so 531 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) 610 532 611 def test_exec_passes_args_pass(self): 533 def test_exec_passes_args_pass(self): 612 kunit.main(['exec']) !! 534 kunit.main(['exec'], self.linux_source_mock) 613 self.assertEqual(self.linux_so 535 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) 614 self.assertEqual(self.linux_so 536 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 615 self.linux_source_mock.run_ker 537 self.linux_source_mock.run_kernel.assert_called_once_with( 616 args=None, build_dir=' !! 538 args=None, build_dir='.kunit', filter_glob='', timeout=300) 617 self.print_mock.assert_any_cal 539 self.print_mock.assert_any_call(StrContains('Testing complete.')) 618 540 619 def test_run_passes_args_pass(self): 541 def test_run_passes_args_pass(self): 620 kunit.main(['run']) !! 542 kunit.main(['run'], self.linux_source_mock) 621 self.assertEqual(self.linux_so 543 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 622 self.assertEqual(self.linux_so 544 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 623 self.linux_source_mock.run_ker 545 self.linux_source_mock.run_kernel.assert_called_once_with( 624 args=None, build_dir=' !! 546 args=None, build_dir='.kunit', filter_glob='', timeout=300) 625 self.print_mock.assert_any_cal 547 self.print_mock.assert_any_call(StrContains('Testing complete.')) 626 548 627 def test_exec_passes_args_fail(self): 549 def test_exec_passes_args_fail(self): 628 self.linux_source_mock.run_ker 550 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 629 with self.assertRaises(SystemE 551 with self.assertRaises(SystemExit) as e: 630 kunit.main(['exec']) !! 552 kunit.main(['exec'], self.linux_source_mock) 631 self.assertEqual(e.exception.c 553 self.assertEqual(e.exception.code, 1) 632 554 633 def test_run_passes_args_fail(self): 555 def test_run_passes_args_fail(self): 634 self.linux_source_mock.run_ker 556 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 635 with self.assertRaises(SystemE 557 with self.assertRaises(SystemExit) as e: 636 kunit.main(['run']) !! 558 kunit.main(['run'], self.linux_source_mock) 637 self.assertEqual(e.exception.c 559 self.assertEqual(e.exception.code, 1) 638 self.assertEqual(self.linux_so 560 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 639 self.assertEqual(self.linux_so 561 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 640 self.print_mock.assert_any_cal !! 562 self.print_mock.assert_any_call(StrContains('invalid KTAP input!')) 641 563 642 def test_exec_no_tests(self): 564 def test_exec_no_tests(self): 643 self.linux_source_mock.run_ker 565 self.linux_source_mock.run_kernel = mock.Mock(return_value=['TAP version 14', '1..0']) 644 with self.assertRaises(SystemE 566 with self.assertRaises(SystemExit) as e: 645 kunit.main(['run']) !! 567 kunit.main(['run'], self.linux_source_mock) 646 self.assertEqual(e.exception.c << 647 self.linux_source_mock.run_ker 568 self.linux_source_mock.run_kernel.assert_called_once_with( 648 args=None, build_dir=' !! 569 args=None, build_dir='.kunit', filter_glob='', timeout=300) 649 self.print_mock.assert_any_cal 570 self.print_mock.assert_any_call(StrContains(' 0 tests run!')) 650 571 651 def test_exec_raw_output(self): 572 def test_exec_raw_output(self): 652 self.linux_source_mock.run_ker 573 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 653 kunit.main(['exec', '--raw_out !! 574 kunit.main(['exec', '--raw_output'], self.linux_source_mock) 654 self.assertEqual(self.linux_so 575 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 655 for call in self.print_mock.ca 576 for call in self.print_mock.call_args_list: 656 self.assertNotEqual(ca 577 self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) 657 self.assertNotEqual(ca 578 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run!'))) 658 579 659 def test_run_raw_output(self): 580 def test_run_raw_output(self): 660 self.linux_source_mock.run_ker 581 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 661 kunit.main(['run', '--raw_outp !! 582 kunit.main(['run', '--raw_output'], self.linux_source_mock) 662 self.assertEqual(self.linux_so 583 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 663 self.assertEqual(self.linux_so 584 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 664 for call in self.print_mock.ca 585 for call in self.print_mock.call_args_list: 665 self.assertNotEqual(ca 586 self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) 666 self.assertNotEqual(ca 587 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run!'))) 667 588 668 def test_run_raw_output_kunit(self): 589 def test_run_raw_output_kunit(self): 669 self.linux_source_mock.run_ker 590 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 670 kunit.main(['run', '--raw_outp !! 591 kunit.main(['run', '--raw_output=kunit'], self.linux_source_mock) 671 self.assertEqual(self.linux_so 592 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 672 self.assertEqual(self.linux_so 593 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 673 for call in self.print_mock.ca 594 for call in self.print_mock.call_args_list: 674 self.assertNotEqual(ca 595 self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) 675 self.assertNotEqual(ca 596 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) 676 597 677 def test_run_raw_output_invalid(self): << 678 self.linux_source_mock.run_ker << 679 with self.assertRaises(SystemE << 680 kunit.main(['run', '-- << 681 self.assertNotEqual(e.exceptio << 682 << 683 def test_run_raw_output_does_not_take_ 598 def test_run_raw_output_does_not_take_positional_args(self): 684 # --raw_output is a string fla 599 # --raw_output is a string flag, but we don't want it to consume 685 # any positional arguments, on 600 # any positional arguments, only ones after an '=' 686 self.linux_source_mock.run_ker 601 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 687 kunit.main(['run', '--raw_outp !! 602 kunit.main(['run', '--raw_output', 'filter_glob'], self.linux_source_mock) 688 self.linux_source_mock.run_ker 603 self.linux_source_mock.run_kernel.assert_called_once_with( 689 args=None, build_dir=' !! 604 args=None, build_dir='.kunit', filter_glob='filter_glob', timeout=300) 690 605 691 def test_exec_timeout(self): 606 def test_exec_timeout(self): 692 timeout = 3453 607 timeout = 3453 693 kunit.main(['exec', '--timeout !! 608 kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) 694 self.linux_source_mock.run_ker 609 self.linux_source_mock.run_kernel.assert_called_once_with( 695 args=None, build_dir=' !! 610 args=None, build_dir='.kunit', filter_glob='', timeout=timeout) 696 self.print_mock.assert_any_cal 611 self.print_mock.assert_any_call(StrContains('Testing complete.')) 697 612 698 def test_run_timeout(self): 613 def test_run_timeout(self): 699 timeout = 3453 614 timeout = 3453 700 kunit.main(['run', '--timeout' !! 615 kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock) 701 self.assertEqual(self.linux_so 616 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 702 self.linux_source_mock.run_ker 617 self.linux_source_mock.run_kernel.assert_called_once_with( 703 args=None, build_dir=' !! 618 args=None, build_dir='.kunit', filter_glob='', timeout=timeout) 704 self.print_mock.assert_any_cal 619 self.print_mock.assert_any_call(StrContains('Testing complete.')) 705 620 706 def test_run_builddir(self): 621 def test_run_builddir(self): 707 build_dir = '.kunit' 622 build_dir = '.kunit' 708 kunit.main(['run', '--build_di !! 623 kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock) 709 self.assertEqual(self.linux_so 624 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 710 self.linux_source_mock.run_ker 625 self.linux_source_mock.run_kernel.assert_called_once_with( 711 args=None, build_dir=b !! 626 args=None, build_dir=build_dir, filter_glob='', timeout=300) 712 self.print_mock.assert_any_cal 627 self.print_mock.assert_any_call(StrContains('Testing complete.')) 713 628 714 def test_config_builddir(self): 629 def test_config_builddir(self): 715 build_dir = '.kunit' 630 build_dir = '.kunit' 716 kunit.main(['config', '--build !! 631 kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock) 717 self.assertEqual(self.linux_so 632 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 718 633 719 def test_build_builddir(self): 634 def test_build_builddir(self): 720 build_dir = '.kunit' 635 build_dir = '.kunit' 721 jobs = kunit.get_default_jobs( 636 jobs = kunit.get_default_jobs() 722 kunit.main(['build', '--build_ !! 637 kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock) 723 self.linux_source_mock.build_k !! 638 self.linux_source_mock.build_kernel.assert_called_once_with(False, jobs, build_dir, None) 724 639 725 def test_exec_builddir(self): 640 def test_exec_builddir(self): 726 build_dir = '.kunit' 641 build_dir = '.kunit' 727 kunit.main(['exec', '--build_d !! 642 kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock) 728 self.linux_source_mock.run_ker 643 self.linux_source_mock.run_kernel.assert_called_once_with( 729 args=None, build_dir=b !! 644 args=None, build_dir=build_dir, filter_glob='', timeout=300) 730 self.print_mock.assert_any_cal 645 self.print_mock.assert_any_call(StrContains('Testing complete.')) 731 646 732 def test_run_kunitconfig(self): !! 647 @mock.patch.object(kunit_kernel, 'LinuxSourceTree') >> 648 def test_run_kunitconfig(self, mock_linux_init): >> 649 mock_linux_init.return_value = self.linux_source_mock 733 kunit.main(['run', '--kunitcon 650 kunit.main(['run', '--kunitconfig=mykunitconfig']) 734 # Just verify that we parsed a 651 # Just verify that we parsed and initialized it correctly here. 735 self.mock_linux_init.assert_ca !! 652 mock_linux_init.assert_called_once_with('.kunit', 736 !! 653 kunitconfig_path='mykunitconfig', 737 !! 654 kconfig_add=None, 738 !! 655 arch='um', 739 !! 656 cross_compile=None, 740 !! 657 qemu_config_path=None) 741 << 742 << 743 def test_config_kunitconfig(self): << 744 kunit.main(['config', '--kunit << 745 # Just verify that we parsed a << 746 self.mock_linux_init.assert_ca << 747 << 748 << 749 << 750 << 751 << 752 << 753 << 754 def test_config_alltests(self): << 755 kunit.main(['config', '--kunit << 756 # Just verify that we parsed a << 757 self.mock_linux_init.assert_ca << 758 << 759 << 760 << 761 << 762 << 763 << 764 << 765 658 766 @mock.patch.object(kunit_kernel, 'Linu 659 @mock.patch.object(kunit_kernel, 'LinuxSourceTree') 767 def test_run_multiple_kunitconfig(self !! 660 def test_config_kunitconfig(self, mock_linux_init): 768 mock_linux_init.return_value = 661 mock_linux_init.return_value = self.linux_source_mock 769 kunit.main(['run', '--kunitcon !! 662 kunit.main(['config', '--kunitconfig=mykunitconfig']) 770 # Just verify that we parsed a 663 # Just verify that we parsed and initialized it correctly here. 771 mock_linux_init.assert_called_ 664 mock_linux_init.assert_called_once_with('.kunit', 772 !! 665 kunitconfig_path='mykunitconfig', 773 666 kconfig_add=None, 774 667 arch='um', 775 668 cross_compile=None, 776 !! 669 qemu_config_path=None) 777 << 778 670 779 def test_run_kconfig_add(self): !! 671 @mock.patch.object(kunit_kernel, 'LinuxSourceTree') >> 672 def test_run_kconfig_add(self, mock_linux_init): >> 673 mock_linux_init.return_value = self.linux_source_mock 780 kunit.main(['run', '--kconfig_ 674 kunit.main(['run', '--kconfig_add=CONFIG_KASAN=y', '--kconfig_add=CONFIG_KCSAN=y']) 781 # Just verify that we parsed a 675 # Just verify that we parsed and initialized it correctly here. 782 self.mock_linux_init.assert_ca !! 676 mock_linux_init.assert_called_once_with('.kunit', 783 !! 677 kunitconfig_path=None, 784 !! 678 kconfig_add=['CONFIG_KASAN=y', 'CONFIG_KCSAN=y'], 785 !! 679 arch='um', 786 !! 680 cross_compile=None, 787 !! 681 qemu_config_path=None) 788 << 789 << 790 def test_run_qemu_args(self): << 791 kunit.main(['run', '--arch=x86 << 792 # Just verify that we parsed a << 793 self.mock_linux_init.assert_ca << 794 << 795 << 796 << 797 << 798 << 799 << 800 682 801 def test_run_kernel_args(self): 683 def test_run_kernel_args(self): 802 kunit.main(['run', '--kernel_a !! 684 kunit.main(['run', '--kernel_args=a=1', '--kernel_args=b=2'], self.linux_source_mock) 803 self.assertEqual(self.linux_so 685 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 804 self.linux_source_mock.run_ker 686 self.linux_source_mock.run_kernel.assert_called_once_with( 805 args=['a=1','b=2'], buil !! 687 args=['a=1','b=2'], build_dir='.kunit', filter_glob='', timeout=300) 806 self.print_mock.assert_any_cal 688 self.print_mock.assert_any_call(StrContains('Testing complete.')) 807 689 808 def test_list_tests(self): 690 def test_list_tests(self): 809 want = ['suite.test1', 'suite. 691 want = ['suite.test1', 'suite.test2', 'suite2.test1'] 810 self.linux_source_mock.run_ker 692 self.linux_source_mock.run_kernel.return_value = ['TAP version 14', 'init: random output'] + want 811 693 812 got = kunit._list_tests(self.l 694 got = kunit._list_tests(self.linux_source_mock, 813 kunit.Kun !! 695 kunit.KunitExecRequest(None, '.kunit', None, 300, False, 'suite*', None, 'suite')) >> 696 814 self.assertEqual(got, want) 697 self.assertEqual(got, want) 815 # Should respect the user's fi 698 # Should respect the user's filter glob when listing tests. 816 self.linux_source_mock.run_ker 699 self.linux_source_mock.run_kernel.assert_called_once_with( 817 args=['kunit.action=li !! 700 args=['kunit.action=list'], build_dir='.kunit', filter_glob='suite*', timeout=300) >> 701 818 702 819 @mock.patch.object(kunit, '_list_tests 703 @mock.patch.object(kunit, '_list_tests') 820 def test_run_isolated_by_suite(self, m 704 def test_run_isolated_by_suite(self, mock_tests): 821 mock_tests.return_value = ['su 705 mock_tests.return_value = ['suite.test1', 'suite.test2', 'suite2.test1'] 822 kunit.main(['exec', '--run_iso !! 706 kunit.main(['exec', '--run_isolated=suite', 'suite*.test*'], self.linux_source_mock) 823 707 824 # Should respect the user's fi 708 # Should respect the user's filter glob when listing tests. 825 mock_tests.assert_called_once_ 709 mock_tests.assert_called_once_with(mock.ANY, 826 kunit.Kun !! 710 kunit.KunitExecRequest(None, '.kunit', None, 300, False, 'suite*.test*', None, 'suite')) 827 self.linux_source_mock.run_ker 711 self.linux_source_mock.run_kernel.assert_has_calls([ 828 mock.call(args=None, b !! 712 mock.call(args=None, build_dir='.kunit', filter_glob='suite.test*', timeout=300), 829 mock.call(args=None, b !! 713 mock.call(args=None, build_dir='.kunit', filter_glob='suite2.test*', timeout=300), 830 ]) 714 ]) 831 715 832 @mock.patch.object(kunit, '_list_tests 716 @mock.patch.object(kunit, '_list_tests') 833 def test_run_isolated_by_test(self, mo 717 def test_run_isolated_by_test(self, mock_tests): 834 mock_tests.return_value = ['su 718 mock_tests.return_value = ['suite.test1', 'suite.test2', 'suite2.test1'] 835 kunit.main(['exec', '--run_iso !! 719 kunit.main(['exec', '--run_isolated=test', 'suite*'], self.linux_source_mock) 836 720 837 # Should respect the user's fi 721 # Should respect the user's filter glob when listing tests. 838 mock_tests.assert_called_once_ 722 mock_tests.assert_called_once_with(mock.ANY, 839 kunit.Kun !! 723 kunit.KunitExecRequest(None, '.kunit', None, 300, False, 'suite*', None, 'test')) 840 self.linux_source_mock.run_ker 724 self.linux_source_mock.run_kernel.assert_has_calls([ 841 mock.call(args=None, b !! 725 mock.call(args=None, build_dir='.kunit', filter_glob='suite.test1', timeout=300), 842 mock.call(args=None, b !! 726 mock.call(args=None, build_dir='.kunit', filter_glob='suite.test2', timeout=300), 843 mock.call(args=None, b !! 727 mock.call(args=None, build_dir='.kunit', filter_glob='suite2.test1', timeout=300), 844 ]) 728 ]) >> 729 845 730 846 if __name__ == '__main__': 731 if __name__ == '__main__': 847 unittest.main() 732 unittest.main()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.