18c2ecf20Sopenharmony_ci#!/usr/bin/env python3
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# A collection of tests for tools/testing/kunit/kunit.py
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Copyright (C) 2019, Google LLC.
78c2ecf20Sopenharmony_ci# Author: Brendan Higgins <brendanhiggins@google.com>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciimport unittest
108c2ecf20Sopenharmony_cifrom unittest import mock
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciimport tempfile, shutil # Handling test_tmpdir
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciimport json
158c2ecf20Sopenharmony_ciimport os
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciimport kunit_config
188c2ecf20Sopenharmony_ciimport kunit_parser
198c2ecf20Sopenharmony_ciimport kunit_kernel
208c2ecf20Sopenharmony_ciimport kunit_json
218c2ecf20Sopenharmony_ciimport kunit
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_citest_tmpdir = ''
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cidef setUpModule():
268c2ecf20Sopenharmony_ci	global test_tmpdir
278c2ecf20Sopenharmony_ci	test_tmpdir = tempfile.mkdtemp()
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cidef tearDownModule():
308c2ecf20Sopenharmony_ci	shutil.rmtree(test_tmpdir)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cidef get_absolute_path(path):
338c2ecf20Sopenharmony_ci	return os.path.join(os.path.dirname(__file__), path)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciclass KconfigTest(unittest.TestCase):
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	def test_is_subset_of(self):
388c2ecf20Sopenharmony_ci		kconfig0 = kunit_config.Kconfig()
398c2ecf20Sopenharmony_ci		self.assertTrue(kconfig0.is_subset_of(kconfig0))
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci		kconfig1 = kunit_config.Kconfig()
428c2ecf20Sopenharmony_ci		kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y'))
438c2ecf20Sopenharmony_ci		self.assertTrue(kconfig1.is_subset_of(kconfig1))
448c2ecf20Sopenharmony_ci		self.assertTrue(kconfig0.is_subset_of(kconfig1))
458c2ecf20Sopenharmony_ci		self.assertFalse(kconfig1.is_subset_of(kconfig0))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	def test_read_from_file(self):
488c2ecf20Sopenharmony_ci		kconfig = kunit_config.Kconfig()
498c2ecf20Sopenharmony_ci		kconfig_path = get_absolute_path(
508c2ecf20Sopenharmony_ci			'test_data/test_read_from_file.kconfig')
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci		kconfig.read_from_file(kconfig_path)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		expected_kconfig = kunit_config.Kconfig()
558c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
568c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('UML', 'y'))
578c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
588c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('MMU', 'y'))
598c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
608c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('TEST', 'y'))
618c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
628c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
638c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
648c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('MK8', 'n'))
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci		self.assertEqual(kconfig.entries(), expected_kconfig.entries())
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	def test_write_to_file(self):
698c2ecf20Sopenharmony_ci		kconfig_path = os.path.join(test_tmpdir, '.config')
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		expected_kconfig = kunit_config.Kconfig()
728c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
738c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('UML', 'y'))
748c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
758c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('MMU', 'y'))
768c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
778c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('TEST', 'y'))
788c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
798c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
808c2ecf20Sopenharmony_ci		expected_kconfig.add_entry(
818c2ecf20Sopenharmony_ci			kunit_config.KconfigEntry('MK8', 'n'))
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		expected_kconfig.write_to_file(kconfig_path)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci		actual_kconfig = kunit_config.Kconfig()
868c2ecf20Sopenharmony_ci		actual_kconfig.read_from_file(kconfig_path)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		self.assertEqual(actual_kconfig.entries(),
898c2ecf20Sopenharmony_ci				 expected_kconfig.entries())
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciclass KUnitParserTest(unittest.TestCase):
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	def assertContains(self, needle, haystack):
948c2ecf20Sopenharmony_ci		for line in haystack:
958c2ecf20Sopenharmony_ci			if needle in line:
968c2ecf20Sopenharmony_ci				return
978c2ecf20Sopenharmony_ci		raise AssertionError('"' +
988c2ecf20Sopenharmony_ci			str(needle) + '" not found in "' + str(haystack) + '"!')
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	def test_output_isolated_correctly(self):
1018c2ecf20Sopenharmony_ci		log_path = get_absolute_path(
1028c2ecf20Sopenharmony_ci			'test_data/test_output_isolated_correctly.log')
1038c2ecf20Sopenharmony_ci		file = open(log_path)
1048c2ecf20Sopenharmony_ci		result = kunit_parser.isolate_kunit_output(file.readlines())
1058c2ecf20Sopenharmony_ci		self.assertContains('TAP version 14', result)
1068c2ecf20Sopenharmony_ci		self.assertContains('	# Subtest: example', result)
1078c2ecf20Sopenharmony_ci		self.assertContains('	1..2', result)
1088c2ecf20Sopenharmony_ci		self.assertContains('	ok 1 - example_simple_test', result)
1098c2ecf20Sopenharmony_ci		self.assertContains('	ok 2 - example_mock_test', result)
1108c2ecf20Sopenharmony_ci		self.assertContains('ok 1 - example', result)
1118c2ecf20Sopenharmony_ci		file.close()
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	def test_output_with_prefix_isolated_correctly(self):
1148c2ecf20Sopenharmony_ci		log_path = get_absolute_path(
1158c2ecf20Sopenharmony_ci			'test_data/test_pound_sign.log')
1168c2ecf20Sopenharmony_ci		with open(log_path) as file:
1178c2ecf20Sopenharmony_ci			result = kunit_parser.isolate_kunit_output(file.readlines())
1188c2ecf20Sopenharmony_ci		self.assertContains('TAP version 14', result)
1198c2ecf20Sopenharmony_ci		self.assertContains('	# Subtest: kunit-resource-test', result)
1208c2ecf20Sopenharmony_ci		self.assertContains('	1..5', result)
1218c2ecf20Sopenharmony_ci		self.assertContains('	ok 1 - kunit_resource_test_init_resources', result)
1228c2ecf20Sopenharmony_ci		self.assertContains('	ok 2 - kunit_resource_test_alloc_resource', result)
1238c2ecf20Sopenharmony_ci		self.assertContains('	ok 3 - kunit_resource_test_destroy_resource', result)
1248c2ecf20Sopenharmony_ci		self.assertContains(' foo bar 	#', result)
1258c2ecf20Sopenharmony_ci		self.assertContains('	ok 4 - kunit_resource_test_cleanup_resources', result)
1268c2ecf20Sopenharmony_ci		self.assertContains('	ok 5 - kunit_resource_test_proper_free_ordering', result)
1278c2ecf20Sopenharmony_ci		self.assertContains('ok 1 - kunit-resource-test', result)
1288c2ecf20Sopenharmony_ci		self.assertContains(' foo bar 	# non-kunit output', result)
1298c2ecf20Sopenharmony_ci		self.assertContains('	# Subtest: kunit-try-catch-test', result)
1308c2ecf20Sopenharmony_ci		self.assertContains('	1..2', result)
1318c2ecf20Sopenharmony_ci		self.assertContains('	ok 1 - kunit_test_try_catch_successful_try_no_catch',
1328c2ecf20Sopenharmony_ci				    result)
1338c2ecf20Sopenharmony_ci		self.assertContains('	ok 2 - kunit_test_try_catch_unsuccessful_try_does_catch',
1348c2ecf20Sopenharmony_ci				    result)
1358c2ecf20Sopenharmony_ci		self.assertContains('ok 2 - kunit-try-catch-test', result)
1368c2ecf20Sopenharmony_ci		self.assertContains('	# Subtest: string-stream-test', result)
1378c2ecf20Sopenharmony_ci		self.assertContains('	1..3', result)
1388c2ecf20Sopenharmony_ci		self.assertContains('	ok 1 - string_stream_test_empty_on_creation', result)
1398c2ecf20Sopenharmony_ci		self.assertContains('	ok 2 - string_stream_test_not_empty_after_add', result)
1408c2ecf20Sopenharmony_ci		self.assertContains('	ok 3 - string_stream_test_get_string', result)
1418c2ecf20Sopenharmony_ci		self.assertContains('ok 3 - string-stream-test', result)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	def test_parse_successful_test_log(self):
1448c2ecf20Sopenharmony_ci		all_passed_log = get_absolute_path(
1458c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-all_passed.log')
1468c2ecf20Sopenharmony_ci		file = open(all_passed_log)
1478c2ecf20Sopenharmony_ci		result = kunit_parser.parse_run_tests(file.readlines())
1488c2ecf20Sopenharmony_ci		self.assertEqual(
1498c2ecf20Sopenharmony_ci			kunit_parser.TestStatus.SUCCESS,
1508c2ecf20Sopenharmony_ci			result.status)
1518c2ecf20Sopenharmony_ci		file.close()
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	def test_parse_failed_test_log(self):
1548c2ecf20Sopenharmony_ci		failed_log = get_absolute_path(
1558c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-failure.log')
1568c2ecf20Sopenharmony_ci		file = open(failed_log)
1578c2ecf20Sopenharmony_ci		result = kunit_parser.parse_run_tests(file.readlines())
1588c2ecf20Sopenharmony_ci		self.assertEqual(
1598c2ecf20Sopenharmony_ci			kunit_parser.TestStatus.FAILURE,
1608c2ecf20Sopenharmony_ci			result.status)
1618c2ecf20Sopenharmony_ci		file.close()
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	def test_no_tests(self):
1648c2ecf20Sopenharmony_ci		empty_log = get_absolute_path(
1658c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-no_tests_run.log')
1668c2ecf20Sopenharmony_ci		file = open(empty_log)
1678c2ecf20Sopenharmony_ci		result = kunit_parser.parse_run_tests(
1688c2ecf20Sopenharmony_ci			kunit_parser.isolate_kunit_output(file.readlines()))
1698c2ecf20Sopenharmony_ci		self.assertEqual(0, len(result.suites))
1708c2ecf20Sopenharmony_ci		self.assertEqual(
1718c2ecf20Sopenharmony_ci			kunit_parser.TestStatus.NO_TESTS,
1728c2ecf20Sopenharmony_ci			result.status)
1738c2ecf20Sopenharmony_ci		file.close()
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	def test_no_kunit_output(self):
1768c2ecf20Sopenharmony_ci		crash_log = get_absolute_path(
1778c2ecf20Sopenharmony_ci			'test_data/test_insufficient_memory.log')
1788c2ecf20Sopenharmony_ci		file = open(crash_log)
1798c2ecf20Sopenharmony_ci		print_mock = mock.patch('builtins.print').start()
1808c2ecf20Sopenharmony_ci		result = kunit_parser.parse_run_tests(
1818c2ecf20Sopenharmony_ci			kunit_parser.isolate_kunit_output(file.readlines()))
1828c2ecf20Sopenharmony_ci		print_mock.assert_any_call(StrContains('no tests run!'))
1838c2ecf20Sopenharmony_ci		print_mock.stop()
1848c2ecf20Sopenharmony_ci		file.close()
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	def test_crashed_test(self):
1878c2ecf20Sopenharmony_ci		crashed_log = get_absolute_path(
1888c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-crash.log')
1898c2ecf20Sopenharmony_ci		file = open(crashed_log)
1908c2ecf20Sopenharmony_ci		result = kunit_parser.parse_run_tests(file.readlines())
1918c2ecf20Sopenharmony_ci		self.assertEqual(
1928c2ecf20Sopenharmony_ci			kunit_parser.TestStatus.TEST_CRASHED,
1938c2ecf20Sopenharmony_ci			result.status)
1948c2ecf20Sopenharmony_ci		file.close()
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	def test_ignores_prefix_printk_time(self):
1978c2ecf20Sopenharmony_ci		prefix_log = get_absolute_path(
1988c2ecf20Sopenharmony_ci			'test_data/test_config_printk_time.log')
1998c2ecf20Sopenharmony_ci		with open(prefix_log) as file:
2008c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2018c2ecf20Sopenharmony_ci			self.assertEqual(
2028c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.SUCCESS,
2038c2ecf20Sopenharmony_ci				result.status)
2048c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	def test_ignores_multiple_prefixes(self):
2078c2ecf20Sopenharmony_ci		prefix_log = get_absolute_path(
2088c2ecf20Sopenharmony_ci			'test_data/test_multiple_prefixes.log')
2098c2ecf20Sopenharmony_ci		with open(prefix_log) as file:
2108c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2118c2ecf20Sopenharmony_ci			self.assertEqual(
2128c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.SUCCESS,
2138c2ecf20Sopenharmony_ci				result.status)
2148c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	def test_prefix_mixed_kernel_output(self):
2178c2ecf20Sopenharmony_ci		mixed_prefix_log = get_absolute_path(
2188c2ecf20Sopenharmony_ci			'test_data/test_interrupted_tap_output.log')
2198c2ecf20Sopenharmony_ci		with open(mixed_prefix_log) as file:
2208c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2218c2ecf20Sopenharmony_ci			self.assertEqual(
2228c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.SUCCESS,
2238c2ecf20Sopenharmony_ci				result.status)
2248c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	def test_prefix_poundsign(self):
2278c2ecf20Sopenharmony_ci		pound_log = get_absolute_path('test_data/test_pound_sign.log')
2288c2ecf20Sopenharmony_ci		with open(pound_log) as file:
2298c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2308c2ecf20Sopenharmony_ci			self.assertEqual(
2318c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.SUCCESS,
2328c2ecf20Sopenharmony_ci				result.status)
2338c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	def test_kernel_panic_end(self):
2368c2ecf20Sopenharmony_ci		panic_log = get_absolute_path('test_data/test_kernel_panic_interrupt.log')
2378c2ecf20Sopenharmony_ci		with open(panic_log) as file:
2388c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2398c2ecf20Sopenharmony_ci			self.assertEqual(
2408c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.TEST_CRASHED,
2418c2ecf20Sopenharmony_ci				result.status)
2428c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	def test_pound_no_prefix(self):
2458c2ecf20Sopenharmony_ci		pound_log = get_absolute_path('test_data/test_pound_no_prefix.log')
2468c2ecf20Sopenharmony_ci		with open(pound_log) as file:
2478c2ecf20Sopenharmony_ci			result = kunit_parser.parse_run_tests(file.readlines())
2488c2ecf20Sopenharmony_ci			self.assertEqual(
2498c2ecf20Sopenharmony_ci				kunit_parser.TestStatus.SUCCESS,
2508c2ecf20Sopenharmony_ci				result.status)
2518c2ecf20Sopenharmony_ci			self.assertEqual('kunit-resource-test', result.suites[0].name)
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ciclass KUnitJsonTest(unittest.TestCase):
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	def _json_for(self, log_file):
2568c2ecf20Sopenharmony_ci		with(open(get_absolute_path(log_file))) as file:
2578c2ecf20Sopenharmony_ci			test_result = kunit_parser.parse_run_tests(file)
2588c2ecf20Sopenharmony_ci			json_obj = kunit_json.get_json_result(
2598c2ecf20Sopenharmony_ci				test_result=test_result,
2608c2ecf20Sopenharmony_ci				def_config='kunit_defconfig',
2618c2ecf20Sopenharmony_ci				build_dir=None,
2628c2ecf20Sopenharmony_ci				json_path='stdout')
2638c2ecf20Sopenharmony_ci		return json.loads(json_obj)
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	def test_failed_test_json(self):
2668c2ecf20Sopenharmony_ci		result = self._json_for(
2678c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-failure.log')
2688c2ecf20Sopenharmony_ci		self.assertEqual(
2698c2ecf20Sopenharmony_ci			{'name': 'example_simple_test', 'status': 'FAIL'},
2708c2ecf20Sopenharmony_ci			result["sub_groups"][1]["test_cases"][0])
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	def test_crashed_test_json(self):
2738c2ecf20Sopenharmony_ci		result = self._json_for(
2748c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-crash.log')
2758c2ecf20Sopenharmony_ci		self.assertEqual(
2768c2ecf20Sopenharmony_ci			{'name': 'example_simple_test', 'status': 'ERROR'},
2778c2ecf20Sopenharmony_ci			result["sub_groups"][1]["test_cases"][0])
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	def test_no_tests_json(self):
2808c2ecf20Sopenharmony_ci		result = self._json_for(
2818c2ecf20Sopenharmony_ci			'test_data/test_is_test_passed-no_tests_run.log')
2828c2ecf20Sopenharmony_ci		self.assertEqual(0, len(result['sub_groups']))
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ciclass StrContains(str):
2858c2ecf20Sopenharmony_ci	def __eq__(self, other):
2868c2ecf20Sopenharmony_ci		return self in other
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ciclass KUnitMainTest(unittest.TestCase):
2898c2ecf20Sopenharmony_ci	def setUp(self):
2908c2ecf20Sopenharmony_ci		path = get_absolute_path('test_data/test_is_test_passed-all_passed.log')
2918c2ecf20Sopenharmony_ci		with open(path) as file:
2928c2ecf20Sopenharmony_ci			all_passed_log = file.readlines()
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		self.print_mock = mock.patch('builtins.print').start()
2958c2ecf20Sopenharmony_ci		self.addCleanup(mock.patch.stopall)
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		self.linux_source_mock = mock.Mock()
2988c2ecf20Sopenharmony_ci		self.linux_source_mock.build_reconfig = mock.Mock(return_value=True)
2998c2ecf20Sopenharmony_ci		self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True)
3008c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log)
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	def test_config_passes_args_pass(self):
3038c2ecf20Sopenharmony_ci		kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock)
3048c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3058c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 0
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	def test_build_passes_args_pass(self):
3088c2ecf20Sopenharmony_ci		kunit.main(['build'], self.linux_source_mock)
3098c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 0
3108c2ecf20Sopenharmony_ci		self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, '.kunit', None)
3118c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 0
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	def test_exec_passes_args_pass(self):
3148c2ecf20Sopenharmony_ci		kunit.main(['exec'], self.linux_source_mock)
3158c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 0
3168c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 1
3178c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='.kunit', timeout=300)
3188c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	def test_run_passes_args_pass(self):
3218c2ecf20Sopenharmony_ci		kunit.main(['run'], self.linux_source_mock)
3228c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3238c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 1
3248c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(
3258c2ecf20Sopenharmony_ci			build_dir='.kunit', timeout=300)
3268c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	def test_exec_passes_args_fail(self):
3298c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
3308c2ecf20Sopenharmony_ci		with self.assertRaises(SystemExit) as e:
3318c2ecf20Sopenharmony_ci			kunit.main(['exec'], self.linux_source_mock)
3328c2ecf20Sopenharmony_ci		assert type(e.exception) == SystemExit
3338c2ecf20Sopenharmony_ci		assert e.exception.code == 1
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	def test_run_passes_args_fail(self):
3368c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
3378c2ecf20Sopenharmony_ci		with self.assertRaises(SystemExit) as e:
3388c2ecf20Sopenharmony_ci			kunit.main(['run'], self.linux_source_mock)
3398c2ecf20Sopenharmony_ci		assert type(e.exception) == SystemExit
3408c2ecf20Sopenharmony_ci		assert e.exception.code == 1
3418c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3428c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 1
3438c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains(' 0 tests run'))
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	def test_exec_raw_output(self):
3468c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
3478c2ecf20Sopenharmony_ci		kunit.main(['exec', '--raw_output'], self.linux_source_mock)
3488c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 1
3498c2ecf20Sopenharmony_ci		for kall in self.print_mock.call_args_list:
3508c2ecf20Sopenharmony_ci			assert kall != mock.call(StrContains('Testing complete.'))
3518c2ecf20Sopenharmony_ci			assert kall != mock.call(StrContains(' 0 tests run'))
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	def test_run_raw_output(self):
3548c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
3558c2ecf20Sopenharmony_ci		kunit.main(['run', '--raw_output'], self.linux_source_mock)
3568c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3578c2ecf20Sopenharmony_ci		assert self.linux_source_mock.run_kernel.call_count == 1
3588c2ecf20Sopenharmony_ci		for kall in self.print_mock.call_args_list:
3598c2ecf20Sopenharmony_ci			assert kall != mock.call(StrContains('Testing complete.'))
3608c2ecf20Sopenharmony_ci			assert kall != mock.call(StrContains(' 0 tests run'))
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	def test_exec_timeout(self):
3638c2ecf20Sopenharmony_ci		timeout = 3453
3648c2ecf20Sopenharmony_ci		kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock)
3658c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='.kunit', timeout=timeout)
3668c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	def test_run_timeout(self):
3698c2ecf20Sopenharmony_ci		timeout = 3453
3708c2ecf20Sopenharmony_ci		kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock)
3718c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3728c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(
3738c2ecf20Sopenharmony_ci			build_dir='.kunit', timeout=timeout)
3748c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	def test_run_builddir(self):
3778c2ecf20Sopenharmony_ci		build_dir = '.kunit'
3788c2ecf20Sopenharmony_ci		kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock)
3798c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3808c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(
3818c2ecf20Sopenharmony_ci			build_dir=build_dir, timeout=300)
3828c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	def test_config_builddir(self):
3858c2ecf20Sopenharmony_ci		build_dir = '.kunit'
3868c2ecf20Sopenharmony_ci		kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock)
3878c2ecf20Sopenharmony_ci		assert self.linux_source_mock.build_reconfig.call_count == 1
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	def test_build_builddir(self):
3908c2ecf20Sopenharmony_ci		build_dir = '.kunit'
3918c2ecf20Sopenharmony_ci		kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock)
3928c2ecf20Sopenharmony_ci		self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, build_dir, None)
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	def test_exec_builddir(self):
3958c2ecf20Sopenharmony_ci		build_dir = '.kunit'
3968c2ecf20Sopenharmony_ci		kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock)
3978c2ecf20Sopenharmony_ci		self.linux_source_mock.run_kernel.assert_called_once_with(build_dir=build_dir, timeout=300)
3988c2ecf20Sopenharmony_ci		self.print_mock.assert_any_call(StrContains('Testing complete.'))
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ciif __name__ == '__main__':
4018c2ecf20Sopenharmony_ci	unittest.main()
402