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