1#!/usr/bin/env python3 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 9import unittest 10from unittest import mock 11 12import tempfile, shutil # Handling test_tmpdir 13 14import itertools 15import json 16import signal 17import os 18 19import kunit_config 20import kunit_parser 21import kunit_kernel 22import kunit_json 23import kunit 24 25test_tmpdir = '' 26abs_test_data_dir = '' 27 28def setUpModule(): 29 global test_tmpdir, abs_test_data_dir 30 test_tmpdir = tempfile.mkdtemp() 31 abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data')) 32 33def tearDownModule(): 34 shutil.rmtree(test_tmpdir) 35 36def test_data_path(path): 37 return os.path.join(abs_test_data_dir, path) 38 39class 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() 46 kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y')) 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() 53 kconfig_path = test_data_path('test_read_from_file.kconfig') 54 55 kconfig.read_from_file(kconfig_path) 56 57 expected_kconfig = kunit_config.Kconfig() 58 expected_kconfig.add_entry( 59 kunit_config.KconfigEntry('UML', 'y')) 60 expected_kconfig.add_entry( 61 kunit_config.KconfigEntry('MMU', 'y')) 62 expected_kconfig.add_entry( 63 kunit_config.KconfigEntry('TEST', 'y')) 64 expected_kconfig.add_entry( 65 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) 66 expected_kconfig.add_entry( 67 kunit_config.KconfigEntry('MK8', 'n')) 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( 76 kunit_config.KconfigEntry('UML', 'y')) 77 expected_kconfig.add_entry( 78 kunit_config.KconfigEntry('MMU', 'y')) 79 expected_kconfig.add_entry( 80 kunit_config.KconfigEntry('TEST', 'y')) 81 expected_kconfig.add_entry( 82 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y')) 83 expected_kconfig.add_entry( 84 kunit_config.KconfigEntry('MK8', 'n')) 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 94class KUnitParserTest(unittest.TestCase): 95 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: 100 if needle in line: 101 return 102 raise AssertionError(f'"{needle}" not found in {list(backup)}!') 103 104 def test_output_isolated_correctly(self): 105 log_path = test_data_path('test_output_isolated_correctly.log') 106 with open(log_path) as file: 107 result = kunit_parser.extract_tap_lines(file.readlines()) 108 self.assertContains('TAP version 14', result) 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) 114 115 def test_output_with_prefix_isolated_correctly(self): 116 log_path = test_data_path('test_pound_sign.log') 117 with open(log_path) as file: 118 result = kunit_parser.extract_tap_lines(file.readlines()) 119 self.assertContains('TAP version 14', result) 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 144 def test_parse_successful_test_log(self): 145 all_passed_log = test_data_path('test_is_test_passed-all_passed.log') 146 with open(all_passed_log) as file: 147 result = kunit_parser.parse_run_tests(file.readlines()) 148 self.assertEqual( 149 kunit_parser.TestStatus.SUCCESS, 150 result.status) 151 152 def test_parse_failed_test_log(self): 153 failed_log = test_data_path('test_is_test_passed-failure.log') 154 with open(failed_log) as file: 155 result = kunit_parser.parse_run_tests(file.readlines()) 156 self.assertEqual( 157 kunit_parser.TestStatus.FAILURE, 158 result.status) 159 160 def test_no_tests(self): 161 empty_log = test_data_path('test_is_test_passed-no_tests_run.log') 162 with open(empty_log) as file: 163 result = kunit_parser.parse_run_tests( 164 kunit_parser.extract_tap_lines(file.readlines())) 165 self.assertEqual(0, len(result.suites)) 166 self.assertEqual( 167 kunit_parser.TestStatus.NO_TESTS, 168 result.status) 169 170 def test_no_kunit_output(self): 171 crash_log = test_data_path('test_insufficient_memory.log') 172 print_mock = mock.patch('builtins.print').start() 173 with open(crash_log) as file: 174 result = kunit_parser.parse_run_tests( 175 kunit_parser.extract_tap_lines(file.readlines())) 176 print_mock.assert_any_call(StrContains('no tests run!')) 177 print_mock.stop() 178 file.close() 179 180 def test_crashed_test(self): 181 crashed_log = test_data_path('test_is_test_passed-crash.log') 182 with open(crashed_log) as file: 183 result = kunit_parser.parse_run_tests(file.readlines()) 184 self.assertEqual( 185 kunit_parser.TestStatus.TEST_CRASHED, 186 result.status) 187 188 def test_skipped_test(self): 189 skipped_log = test_data_path('test_skip_tests.log') 190 file = open(skipped_log) 191 result = kunit_parser.parse_run_tests(file.readlines()) 192 193 # A skipped test does not fail the whole suite. 194 self.assertEqual( 195 kunit_parser.TestStatus.SUCCESS, 196 result.status) 197 file.close() 198 199 def test_skipped_all_tests(self): 200 skipped_log = test_data_path('test_skip_all_tests.log') 201 file = open(skipped_log) 202 result = kunit_parser.parse_run_tests(file.readlines()) 203 204 self.assertEqual( 205 kunit_parser.TestStatus.SKIPPED, 206 result.status) 207 file.close() 208 209 210 def test_ignores_prefix_printk_time(self): 211 prefix_log = test_data_path('test_config_printk_time.log') 212 with open(prefix_log) as file: 213 result = kunit_parser.parse_run_tests(file.readlines()) 214 self.assertEqual( 215 kunit_parser.TestStatus.SUCCESS, 216 result.status) 217 self.assertEqual('kunit-resource-test', result.suites[0].name) 218 219 def test_ignores_multiple_prefixes(self): 220 prefix_log = test_data_path('test_multiple_prefixes.log') 221 with open(prefix_log) as file: 222 result = kunit_parser.parse_run_tests(file.readlines()) 223 self.assertEqual( 224 kunit_parser.TestStatus.SUCCESS, 225 result.status) 226 self.assertEqual('kunit-resource-test', result.suites[0].name) 227 228 def test_prefix_mixed_kernel_output(self): 229 mixed_prefix_log = test_data_path('test_interrupted_tap_output.log') 230 with open(mixed_prefix_log) as file: 231 result = kunit_parser.parse_run_tests(file.readlines()) 232 self.assertEqual( 233 kunit_parser.TestStatus.SUCCESS, 234 result.status) 235 self.assertEqual('kunit-resource-test', result.suites[0].name) 236 237 def test_prefix_poundsign(self): 238 pound_log = test_data_path('test_pound_sign.log') 239 with open(pound_log) as file: 240 result = kunit_parser.parse_run_tests(file.readlines()) 241 self.assertEqual( 242 kunit_parser.TestStatus.SUCCESS, 243 result.status) 244 self.assertEqual('kunit-resource-test', result.suites[0].name) 245 246 def test_kernel_panic_end(self): 247 panic_log = test_data_path('test_kernel_panic_interrupt.log') 248 with open(panic_log) as file: 249 result = kunit_parser.parse_run_tests(file.readlines()) 250 self.assertEqual( 251 kunit_parser.TestStatus.TEST_CRASHED, 252 result.status) 253 self.assertEqual('kunit-resource-test', result.suites[0].name) 254 255 def test_pound_no_prefix(self): 256 pound_log = test_data_path('test_pound_no_prefix.log') 257 with open(pound_log) as file: 258 result = kunit_parser.parse_run_tests(file.readlines()) 259 self.assertEqual( 260 kunit_parser.TestStatus.SUCCESS, 261 result.status) 262 self.assertEqual('kunit-resource-test', result.suites[0].name) 263 264class LinuxSourceTreeTest(unittest.TestCase): 265 266 def setUp(self): 267 mock.patch.object(signal, 'signal').start() 268 self.addCleanup(mock.patch.stopall) 269 270 def test_invalid_kunitconfig(self): 271 with self.assertRaisesRegex(kunit_kernel.ConfigError, 'nonexistent.* does not exist'): 272 kunit_kernel.LinuxSourceTree('', kunitconfig_path='/nonexistent_file') 273 274 def test_valid_kunitconfig(self): 275 with tempfile.NamedTemporaryFile('wt') as kunitconfig: 276 tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=kunitconfig.name) 277 278 def test_dir_kunitconfig(self): 279 with tempfile.TemporaryDirectory('') as dir: 280 with open(os.path.join(dir, '.kunitconfig'), 'w') as f: 281 pass 282 tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=dir) 283 284 # TODO: add more test cases. 285 286 287class KUnitJsonTest(unittest.TestCase): 288 289 def _json_for(self, log_file): 290 with open(test_data_path(log_file)) as file: 291 test_result = kunit_parser.parse_run_tests(file) 292 json_obj = kunit_json.get_json_result( 293 test_result=test_result, 294 def_config='kunit_defconfig', 295 build_dir=None, 296 json_path='stdout') 297 return json.loads(json_obj) 298 299 def test_failed_test_json(self): 300 result = self._json_for('test_is_test_passed-failure.log') 301 self.assertEqual( 302 {'name': 'example_simple_test', 'status': 'FAIL'}, 303 result["sub_groups"][1]["test_cases"][0]) 304 305 def test_crashed_test_json(self): 306 result = self._json_for('test_is_test_passed-crash.log') 307 self.assertEqual( 308 {'name': 'example_simple_test', 'status': 'ERROR'}, 309 result["sub_groups"][1]["test_cases"][0]) 310 311 def test_no_tests_json(self): 312 result = self._json_for('test_is_test_passed-no_tests_run.log') 313 self.assertEqual(0, len(result['sub_groups'])) 314 315class StrContains(str): 316 def __eq__(self, other): 317 return self in other 318 319class KUnitMainTest(unittest.TestCase): 320 def setUp(self): 321 path = test_data_path('test_is_test_passed-all_passed.log') 322 with open(path) as file: 323 all_passed_log = file.readlines() 324 325 self.print_mock = mock.patch('builtins.print').start() 326 self.addCleanup(mock.patch.stopall) 327 328 self.linux_source_mock = mock.Mock() 329 self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) 330 self.linux_source_mock.build_kernel = mock.Mock(return_value=True) 331 self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) 332 333 def test_config_passes_args_pass(self): 334 kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) 335 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 336 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) 337 338 def test_build_passes_args_pass(self): 339 kunit.main(['build'], self.linux_source_mock) 340 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) 341 self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, '.kunit', None) 342 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0) 343 344 def test_exec_passes_args_pass(self): 345 kunit.main(['exec'], self.linux_source_mock) 346 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) 347 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 348 self.linux_source_mock.run_kernel.assert_called_once_with( 349 build_dir='.kunit', filter_glob='', timeout=300) 350 self.print_mock.assert_any_call(StrContains('Testing complete.')) 351 352 def test_run_passes_args_pass(self): 353 kunit.main(['run'], self.linux_source_mock) 354 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 355 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 356 self.linux_source_mock.run_kernel.assert_called_once_with( 357 build_dir='.kunit', filter_glob='', timeout=300) 358 self.print_mock.assert_any_call(StrContains('Testing complete.')) 359 360 def test_exec_passes_args_fail(self): 361 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 362 with self.assertRaises(SystemExit) as e: 363 kunit.main(['exec'], self.linux_source_mock) 364 self.assertEqual(e.exception.code, 1) 365 366 def test_run_passes_args_fail(self): 367 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 368 with self.assertRaises(SystemExit) as e: 369 kunit.main(['run'], self.linux_source_mock) 370 self.assertEqual(e.exception.code, 1) 371 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 372 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 373 self.print_mock.assert_any_call(StrContains(' 0 tests run')) 374 375 def test_exec_raw_output(self): 376 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 377 kunit.main(['exec', '--raw_output'], self.linux_source_mock) 378 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 379 for call in self.print_mock.call_args_list: 380 self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) 381 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) 382 383 def test_run_raw_output(self): 384 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 385 kunit.main(['run', '--raw_output'], self.linux_source_mock) 386 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 387 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) 388 for call in self.print_mock.call_args_list: 389 self.assertNotEqual(call, mock.call(StrContains('Testing complete.'))) 390 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run'))) 391 392 def test_exec_timeout(self): 393 timeout = 3453 394 kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) 395 self.linux_source_mock.run_kernel.assert_called_once_with( 396 build_dir='.kunit', filter_glob='', timeout=timeout) 397 self.print_mock.assert_any_call(StrContains('Testing complete.')) 398 399 def test_run_timeout(self): 400 timeout = 3453 401 kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock) 402 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 403 self.linux_source_mock.run_kernel.assert_called_once_with( 404 build_dir='.kunit', filter_glob='', timeout=timeout) 405 self.print_mock.assert_any_call(StrContains('Testing complete.')) 406 407 def test_run_builddir(self): 408 build_dir = '.kunit' 409 kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock) 410 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 411 self.linux_source_mock.run_kernel.assert_called_once_with( 412 build_dir=build_dir, filter_glob='', timeout=300) 413 self.print_mock.assert_any_call(StrContains('Testing complete.')) 414 415 def test_config_builddir(self): 416 build_dir = '.kunit' 417 kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock) 418 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) 419 420 def test_build_builddir(self): 421 build_dir = '.kunit' 422 kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock) 423 self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, build_dir, None) 424 425 def test_exec_builddir(self): 426 build_dir = '.kunit' 427 kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock) 428 self.linux_source_mock.run_kernel.assert_called_once_with( 429 build_dir=build_dir, filter_glob='', timeout=300) 430 self.print_mock.assert_any_call(StrContains('Testing complete.')) 431 432 @mock.patch.object(kunit_kernel, 'LinuxSourceTree') 433 def test_run_kunitconfig(self, mock_linux_init): 434 mock_linux_init.return_value = self.linux_source_mock 435 kunit.main(['run', '--kunitconfig=mykunitconfig']) 436 # Just verify that we parsed and initialized it correctly here. 437 mock_linux_init.assert_called_once_with('.kunit', 438 kunitconfig_path='mykunitconfig', 439 arch='um', 440 cross_compile=None, 441 qemu_config_path=None) 442 443 @mock.patch.object(kunit_kernel, 'LinuxSourceTree') 444 def test_config_kunitconfig(self, mock_linux_init): 445 mock_linux_init.return_value = self.linux_source_mock 446 kunit.main(['config', '--kunitconfig=mykunitconfig']) 447 # Just verify that we parsed and initialized it correctly here. 448 mock_linux_init.assert_called_once_with('.kunit', 449 kunitconfig_path='mykunitconfig', 450 arch='um', 451 cross_compile=None, 452 qemu_config_path=None) 453 454if __name__ == '__main__': 455 unittest.main() 456