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	# TODO: add more test cases.
255
256
257class KUnitJsonTest(unittest.TestCase):
258
259	def _json_for(self, log_file):
260		with open(test_data_path(log_file)) as file:
261			test_result = kunit_parser.parse_run_tests(file)
262			json_obj = kunit_json.get_json_result(
263				test_result=test_result,
264				def_config='kunit_defconfig',
265				build_dir=None,
266				json_path='stdout')
267		return json.loads(json_obj)
268
269	def test_failed_test_json(self):
270		result = self._json_for('test_is_test_passed-failure.log')
271		self.assertEqual(
272			{'name': 'example_simple_test', 'status': 'FAIL'},
273			result["sub_groups"][1]["test_cases"][0])
274
275	def test_crashed_test_json(self):
276		result = self._json_for('test_is_test_passed-crash.log')
277		self.assertEqual(
278			{'name': 'example_simple_test', 'status': 'ERROR'},
279			result["sub_groups"][1]["test_cases"][0])
280
281	def test_no_tests_json(self):
282		result = self._json_for('test_is_test_passed-no_tests_run.log')
283		self.assertEqual(0, len(result['sub_groups']))
284
285class StrContains(str):
286	def __eq__(self, other):
287		return self in other
288
289class KUnitMainTest(unittest.TestCase):
290	def setUp(self):
291		path = test_data_path('test_is_test_passed-all_passed.log')
292		with open(path) as file:
293			all_passed_log = file.readlines()
294
295		self.print_mock = mock.patch('builtins.print').start()
296		self.addCleanup(mock.patch.stopall)
297
298		self.linux_source_mock = mock.Mock()
299		self.linux_source_mock.build_reconfig = mock.Mock(return_value=True)
300		self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True)
301		self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log)
302
303	def test_config_passes_args_pass(self):
304		kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock)
305		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
306		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0)
307
308	def test_build_passes_args_pass(self):
309		kunit.main(['build'], self.linux_source_mock)
310		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0)
311		self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, '.kunit', None)
312		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0)
313
314	def test_exec_passes_args_pass(self):
315		kunit.main(['exec'], self.linux_source_mock)
316		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0)
317		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
318		self.linux_source_mock.run_kernel.assert_called_once_with(
319			build_dir='.kunit', filter_glob='', timeout=300)
320		self.print_mock.assert_any_call(StrContains('Testing complete.'))
321
322	def test_run_passes_args_pass(self):
323		kunit.main(['run'], self.linux_source_mock)
324		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
325		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
326		self.linux_source_mock.run_kernel.assert_called_once_with(
327			build_dir='.kunit', filter_glob='', timeout=300)
328		self.print_mock.assert_any_call(StrContains('Testing complete.'))
329
330	def test_exec_passes_args_fail(self):
331		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
332		with self.assertRaises(SystemExit) as e:
333			kunit.main(['exec'], self.linux_source_mock)
334		self.assertEqual(e.exception.code, 1)
335
336	def test_run_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(['run'], self.linux_source_mock)
340		self.assertEqual(e.exception.code, 1)
341		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
342		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
343		self.print_mock.assert_any_call(StrContains(' 0 tests run'))
344
345	def test_exec_raw_output(self):
346		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
347		kunit.main(['exec', '--raw_output'], self.linux_source_mock)
348		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
349		for call in self.print_mock.call_args_list:
350			self.assertNotEqual(call, mock.call(StrContains('Testing complete.')))
351			self.assertNotEqual(call, mock.call(StrContains(' 0 tests run')))
352
353	def test_run_raw_output(self):
354		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
355		kunit.main(['run', '--raw_output'], self.linux_source_mock)
356		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
357		self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
358		for call in self.print_mock.call_args_list:
359			self.assertNotEqual(call, mock.call(StrContains('Testing complete.')))
360			self.assertNotEqual(call, mock.call(StrContains(' 0 tests run')))
361
362	def test_exec_timeout(self):
363		timeout = 3453
364		kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock)
365		self.linux_source_mock.run_kernel.assert_called_once_with(
366			build_dir='.kunit', filter_glob='', timeout=timeout)
367		self.print_mock.assert_any_call(StrContains('Testing complete.'))
368
369	def test_run_timeout(self):
370		timeout = 3453
371		kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock)
372		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
373		self.linux_source_mock.run_kernel.assert_called_once_with(
374			build_dir='.kunit', filter_glob='', timeout=timeout)
375		self.print_mock.assert_any_call(StrContains('Testing complete.'))
376
377	def test_run_builddir(self):
378		build_dir = '.kunit'
379		kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock)
380		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
381		self.linux_source_mock.run_kernel.assert_called_once_with(
382			build_dir=build_dir, filter_glob='', timeout=300)
383		self.print_mock.assert_any_call(StrContains('Testing complete.'))
384
385	def test_config_builddir(self):
386		build_dir = '.kunit'
387		kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock)
388		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
389
390	def test_build_builddir(self):
391		build_dir = '.kunit'
392		kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock)
393		self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, build_dir, None)
394
395	def test_exec_builddir(self):
396		build_dir = '.kunit'
397		kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock)
398		self.linux_source_mock.run_kernel.assert_called_once_with(
399			build_dir=build_dir, filter_glob='', timeout=300)
400		self.print_mock.assert_any_call(StrContains('Testing complete.'))
401
402	@mock.patch.object(kunit_kernel, 'LinuxSourceTree')
403	def test_run_kunitconfig(self, mock_linux_init):
404		mock_linux_init.return_value = self.linux_source_mock
405		kunit.main(['run', '--kunitconfig=mykunitconfig'])
406		# Just verify that we parsed and initialized it correctly here.
407		mock_linux_init.assert_called_once_with('.kunit', kunitconfig_path='mykunitconfig')
408
409	@mock.patch.object(kunit_kernel, 'LinuxSourceTree')
410	def test_config_kunitconfig(self, mock_linux_init):
411		mock_linux_init.return_value = self.linux_source_mock
412		kunit.main(['config', '--kunitconfig=mykunitconfig'])
413		# Just verify that we parsed and initialized it correctly here.
414		mock_linux_init.assert_called_once_with('.kunit', kunitconfig_path='mykunitconfig')
415
416if __name__ == '__main__':
417	unittest.main()
418