blob: b593f4448e83968cddb5b2ffd735cd85601036ac [file] [log] [blame]
Finn Behrensc25ce582020-11-23 15:15:33 +01001#!/usr/bin/env python3
Felix Guo6ebf5862019-09-23 02:02:43 -07002# 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
Heidi Fahim21a6d172020-08-11 14:27:56 -070014import json
Felix Guo6ebf5862019-09-23 02:02:43 -070015import os
16
17import kunit_config
18import kunit_parser
19import kunit_kernel
Heidi Fahim21a6d172020-08-11 14:27:56 -070020import kunit_json
Felix Guo6ebf5862019-09-23 02:02:43 -070021import kunit
22
23test_tmpdir = ''
24
25def setUpModule():
26 global test_tmpdir
27 test_tmpdir = tempfile.mkdtemp()
28
29def tearDownModule():
30 shutil.rmtree(test_tmpdir)
31
32def get_absolute_path(path):
33 return os.path.join(os.path.dirname(__file__), path)
34
35class KconfigTest(unittest.TestCase):
36
37 def test_is_subset_of(self):
38 kconfig0 = kunit_config.Kconfig()
39 self.assertTrue(kconfig0.is_subset_of(kconfig0))
40
41 kconfig1 = kunit_config.Kconfig()
David Gow97752c32020-03-23 19:43:33 -070042 kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070043 self.assertTrue(kconfig1.is_subset_of(kconfig1))
44 self.assertTrue(kconfig0.is_subset_of(kconfig1))
45 self.assertFalse(kconfig1.is_subset_of(kconfig0))
46
47 def test_read_from_file(self):
48 kconfig = kunit_config.Kconfig()
49 kconfig_path = get_absolute_path(
50 'test_data/test_read_from_file.kconfig')
51
52 kconfig.read_from_file(kconfig_path)
53
54 expected_kconfig = kunit_config.Kconfig()
55 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070056 kunit_config.KconfigEntry('UML', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070057 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070058 kunit_config.KconfigEntry('MMU', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070059 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070060 kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070061 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070062 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070063 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070064 kunit_config.KconfigEntry('MK8', 'n'))
Felix Guo6ebf5862019-09-23 02:02:43 -070065
66 self.assertEqual(kconfig.entries(), expected_kconfig.entries())
67
68 def test_write_to_file(self):
69 kconfig_path = os.path.join(test_tmpdir, '.config')
70
71 expected_kconfig = kunit_config.Kconfig()
72 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070073 kunit_config.KconfigEntry('UML', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070074 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070075 kunit_config.KconfigEntry('MMU', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070076 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070077 kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070078 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070079 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070080 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070081 kunit_config.KconfigEntry('MK8', 'n'))
Felix Guo6ebf5862019-09-23 02:02:43 -070082
83 expected_kconfig.write_to_file(kconfig_path)
84
85 actual_kconfig = kunit_config.Kconfig()
86 actual_kconfig.read_from_file(kconfig_path)
87
88 self.assertEqual(actual_kconfig.entries(),
89 expected_kconfig.entries())
90
91class KUnitParserTest(unittest.TestCase):
92
93 def assertContains(self, needle, haystack):
94 for line in haystack:
95 if needle in line:
96 return
97 raise AssertionError('"' +
98 str(needle) + '" not found in "' + str(haystack) + '"!')
99
100 def test_output_isolated_correctly(self):
101 log_path = get_absolute_path(
102 'test_data/test_output_isolated_correctly.log')
103 file = open(log_path)
104 result = kunit_parser.isolate_kunit_output(file.readlines())
Daniel Latypov060352e2020-10-30 15:38:53 -0700105 self.assertContains('TAP version 14', result)
Felix Guo6ebf5862019-09-23 02:02:43 -0700106 self.assertContains(' # Subtest: example', result)
107 self.assertContains(' 1..2', result)
108 self.assertContains(' ok 1 - example_simple_test', result)
109 self.assertContains(' ok 2 - example_mock_test', result)
110 self.assertContains('ok 1 - example', result)
111 file.close()
112
Heidi Fahimafc63da2020-03-16 13:21:24 -0700113 def test_output_with_prefix_isolated_correctly(self):
114 log_path = get_absolute_path(
115 'test_data/test_pound_sign.log')
116 with open(log_path) as file:
117 result = kunit_parser.isolate_kunit_output(file.readlines())
Daniel Latypov060352e2020-10-30 15:38:53 -0700118 self.assertContains('TAP version 14', result)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700119 self.assertContains(' # Subtest: kunit-resource-test', result)
120 self.assertContains(' 1..5', result)
121 self.assertContains(' ok 1 - kunit_resource_test_init_resources', result)
122 self.assertContains(' ok 2 - kunit_resource_test_alloc_resource', result)
123 self.assertContains(' ok 3 - kunit_resource_test_destroy_resource', result)
124 self.assertContains(' foo bar #', result)
125 self.assertContains(' ok 4 - kunit_resource_test_cleanup_resources', result)
126 self.assertContains(' ok 5 - kunit_resource_test_proper_free_ordering', result)
127 self.assertContains('ok 1 - kunit-resource-test', result)
128 self.assertContains(' foo bar # non-kunit output', result)
129 self.assertContains(' # Subtest: kunit-try-catch-test', result)
130 self.assertContains(' 1..2', result)
131 self.assertContains(' ok 1 - kunit_test_try_catch_successful_try_no_catch',
132 result)
133 self.assertContains(' ok 2 - kunit_test_try_catch_unsuccessful_try_does_catch',
134 result)
135 self.assertContains('ok 2 - kunit-try-catch-test', result)
136 self.assertContains(' # Subtest: string-stream-test', result)
137 self.assertContains(' 1..3', result)
138 self.assertContains(' ok 1 - string_stream_test_empty_on_creation', result)
139 self.assertContains(' ok 2 - string_stream_test_not_empty_after_add', result)
140 self.assertContains(' ok 3 - string_stream_test_get_string', result)
141 self.assertContains('ok 3 - string-stream-test', result)
142
Felix Guo6ebf5862019-09-23 02:02:43 -0700143 def test_parse_successful_test_log(self):
144 all_passed_log = get_absolute_path(
145 'test_data/test_is_test_passed-all_passed.log')
146 file = open(all_passed_log)
147 result = kunit_parser.parse_run_tests(file.readlines())
148 self.assertEqual(
149 kunit_parser.TestStatus.SUCCESS,
150 result.status)
151 file.close()
152
153 def test_parse_failed_test_log(self):
154 failed_log = get_absolute_path(
155 'test_data/test_is_test_passed-failure.log')
156 file = open(failed_log)
157 result = kunit_parser.parse_run_tests(file.readlines())
158 self.assertEqual(
159 kunit_parser.TestStatus.FAILURE,
160 result.status)
161 file.close()
162
163 def test_no_tests(self):
164 empty_log = get_absolute_path(
165 'test_data/test_is_test_passed-no_tests_run.log')
166 file = open(empty_log)
167 result = kunit_parser.parse_run_tests(
168 kunit_parser.isolate_kunit_output(file.readlines()))
169 self.assertEqual(0, len(result.suites))
170 self.assertEqual(
171 kunit_parser.TestStatus.NO_TESTS,
172 result.status)
173 file.close()
174
Uriel Guajardoe173b8b2020-06-11 21:05:45 +0000175 def test_no_kunit_output(self):
176 crash_log = get_absolute_path(
177 'test_data/test_insufficient_memory.log')
178 file = open(crash_log)
179 print_mock = mock.patch('builtins.print').start()
180 result = kunit_parser.parse_run_tests(
181 kunit_parser.isolate_kunit_output(file.readlines()))
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700182 print_mock.assert_any_call(StrContains('no tests run!'))
Uriel Guajardoe173b8b2020-06-11 21:05:45 +0000183 print_mock.stop()
184 file.close()
185
Felix Guo6ebf5862019-09-23 02:02:43 -0700186 def test_crashed_test(self):
187 crashed_log = get_absolute_path(
188 'test_data/test_is_test_passed-crash.log')
189 file = open(crashed_log)
190 result = kunit_parser.parse_run_tests(file.readlines())
191 self.assertEqual(
192 kunit_parser.TestStatus.TEST_CRASHED,
193 result.status)
194 file.close()
195
Heidi Fahimafc63da2020-03-16 13:21:24 -0700196 def test_ignores_prefix_printk_time(self):
197 prefix_log = get_absolute_path(
198 'test_data/test_config_printk_time.log')
199 with open(prefix_log) as file:
200 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700201 self.assertEqual(
202 kunit_parser.TestStatus.SUCCESS,
203 result.status)
204 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700205
206 def test_ignores_multiple_prefixes(self):
207 prefix_log = get_absolute_path(
208 'test_data/test_multiple_prefixes.log')
209 with open(prefix_log) as file:
210 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700211 self.assertEqual(
212 kunit_parser.TestStatus.SUCCESS,
213 result.status)
214 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700215
216 def test_prefix_mixed_kernel_output(self):
217 mixed_prefix_log = get_absolute_path(
218 'test_data/test_interrupted_tap_output.log')
219 with open(mixed_prefix_log) as file:
220 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700221 self.assertEqual(
222 kunit_parser.TestStatus.SUCCESS,
223 result.status)
224 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700225
226 def test_prefix_poundsign(self):
227 pound_log = get_absolute_path('test_data/test_pound_sign.log')
228 with open(pound_log) as file:
229 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700230 self.assertEqual(
231 kunit_parser.TestStatus.SUCCESS,
232 result.status)
233 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700234
235 def test_kernel_panic_end(self):
236 panic_log = get_absolute_path('test_data/test_kernel_panic_interrupt.log')
237 with open(panic_log) as file:
238 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700239 self.assertEqual(
240 kunit_parser.TestStatus.TEST_CRASHED,
241 result.status)
242 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700243
244 def test_pound_no_prefix(self):
245 pound_log = get_absolute_path('test_data/test_pound_no_prefix.log')
246 with open(pound_log) as file:
247 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700248 self.assertEqual(
249 kunit_parser.TestStatus.SUCCESS,
250 result.status)
251 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700252
Heidi Fahim21a6d172020-08-11 14:27:56 -0700253class KUnitJsonTest(unittest.TestCase):
254
255 def _json_for(self, log_file):
256 with(open(get_absolute_path(log_file))) as file:
257 test_result = kunit_parser.parse_run_tests(file)
258 json_obj = kunit_json.get_json_result(
259 test_result=test_result,
260 def_config='kunit_defconfig',
261 build_dir=None,
262 json_path='stdout')
263 return json.loads(json_obj)
264
265 def test_failed_test_json(self):
266 result = self._json_for(
267 'test_data/test_is_test_passed-failure.log')
268 self.assertEqual(
269 {'name': 'example_simple_test', 'status': 'FAIL'},
270 result["sub_groups"][1]["test_cases"][0])
271
272 def test_crashed_test_json(self):
273 result = self._json_for(
274 'test_data/test_is_test_passed-crash.log')
275 self.assertEqual(
276 {'name': 'example_simple_test', 'status': 'ERROR'},
277 result["sub_groups"][1]["test_cases"][0])
278
279 def test_no_tests_json(self):
280 result = self._json_for(
281 'test_data/test_is_test_passed-no_tests_run.log')
282 self.assertEqual(0, len(result['sub_groups']))
283
Felix Guo6ebf5862019-09-23 02:02:43 -0700284class StrContains(str):
285 def __eq__(self, other):
286 return self in other
287
288class KUnitMainTest(unittest.TestCase):
289 def setUp(self):
290 path = get_absolute_path('test_data/test_is_test_passed-all_passed.log')
291 file = open(path)
292 all_passed_log = file.readlines()
293 self.print_patch = mock.patch('builtins.print')
294 self.print_mock = self.print_patch.start()
295 self.linux_source_mock = mock.Mock()
296 self.linux_source_mock.build_reconfig = mock.Mock(return_value=True)
297 self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True)
298 self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log)
299
300 def tearDown(self):
301 self.print_patch.stop()
302 pass
303
David Gow45ba7a82020-04-30 21:27:01 -0700304 def test_config_passes_args_pass(self):
Brendan Higginsd43c7fb2020-07-14 13:41:30 -0700305 kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock)
David Gow45ba7a82020-04-30 21:27:01 -0700306 assert self.linux_source_mock.build_reconfig.call_count == 1
307 assert self.linux_source_mock.run_kernel.call_count == 0
308
309 def test_build_passes_args_pass(self):
310 kunit.main(['build'], self.linux_source_mock)
311 assert self.linux_source_mock.build_reconfig.call_count == 0
Brendan Higgins6816fe62020-07-14 13:41:29 -0700312 self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, '.kunit', None)
David Gow45ba7a82020-04-30 21:27:01 -0700313 assert self.linux_source_mock.run_kernel.call_count == 0
314
315 def test_exec_passes_args_pass(self):
316 kunit.main(['exec'], self.linux_source_mock)
317 assert self.linux_source_mock.build_reconfig.call_count == 0
318 assert self.linux_source_mock.run_kernel.call_count == 1
Brendan Higgins6816fe62020-07-14 13:41:29 -0700319 self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='.kunit', timeout=300)
David Gow45ba7a82020-04-30 21:27:01 -0700320 self.print_mock.assert_any_call(StrContains('Testing complete.'))
321
Felix Guo6ebf5862019-09-23 02:02:43 -0700322 def test_run_passes_args_pass(self):
323 kunit.main(['run'], self.linux_source_mock)
324 assert self.linux_source_mock.build_reconfig.call_count == 1
325 assert self.linux_source_mock.run_kernel.call_count == 1
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700326 self.linux_source_mock.run_kernel.assert_called_once_with(
Brendan Higgins6816fe62020-07-14 13:41:29 -0700327 build_dir='.kunit', timeout=300)
Felix Guo6ebf5862019-09-23 02:02:43 -0700328 self.print_mock.assert_any_call(StrContains('Testing complete.'))
329
David Gow45ba7a82020-04-30 21:27:01 -0700330 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 assert type(e.exception) == SystemExit
335 assert e.exception.code == 1
336
Felix Guo6ebf5862019-09-23 02:02:43 -0700337 def test_run_passes_args_fail(self):
338 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
339 with self.assertRaises(SystemExit) as e:
340 kunit.main(['run'], self.linux_source_mock)
341 assert type(e.exception) == SystemExit
342 assert e.exception.code == 1
343 assert self.linux_source_mock.build_reconfig.call_count == 1
344 assert self.linux_source_mock.run_kernel.call_count == 1
345 self.print_mock.assert_any_call(StrContains(' 0 tests run'))
346
David Gow45ba7a82020-04-30 21:27:01 -0700347 def test_exec_raw_output(self):
348 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
349 kunit.main(['exec', '--raw_output'], self.linux_source_mock)
350 assert self.linux_source_mock.run_kernel.call_count == 1
351 for kall in self.print_mock.call_args_list:
352 assert kall != mock.call(StrContains('Testing complete.'))
353 assert kall != mock.call(StrContains(' 0 tests run'))
354
Felix Guo6ebf5862019-09-23 02:02:43 -0700355 def test_run_raw_output(self):
356 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
David Gow45ba7a82020-04-30 21:27:01 -0700357 kunit.main(['run', '--raw_output'], self.linux_source_mock)
Felix Guo6ebf5862019-09-23 02:02:43 -0700358 assert self.linux_source_mock.build_reconfig.call_count == 1
359 assert self.linux_source_mock.run_kernel.call_count == 1
David Gow45ba7a82020-04-30 21:27:01 -0700360 for kall in self.print_mock.call_args_list:
361 assert kall != mock.call(StrContains('Testing complete.'))
362 assert kall != mock.call(StrContains(' 0 tests run'))
363
364 def test_exec_timeout(self):
365 timeout = 3453
366 kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock)
Brendan Higgins6816fe62020-07-14 13:41:29 -0700367 self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='.kunit', timeout=timeout)
David Gow45ba7a82020-04-30 21:27:01 -0700368 self.print_mock.assert_any_call(StrContains('Testing complete.'))
Felix Guo6ebf5862019-09-23 02:02:43 -0700369
370 def test_run_timeout(self):
371 timeout = 3453
372 kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock)
373 assert self.linux_source_mock.build_reconfig.call_count == 1
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700374 self.linux_source_mock.run_kernel.assert_called_once_with(
Brendan Higgins6816fe62020-07-14 13:41:29 -0700375 build_dir='.kunit', timeout=timeout)
Felix Guo6ebf5862019-09-23 02:02:43 -0700376 self.print_mock.assert_any_call(StrContains('Testing complete.'))
377
SeongJae Parkb1b35202019-12-20 05:14:08 +0000378 def test_run_builddir(self):
379 build_dir = '.kunit'
Brendan Higginsd43c7fb2020-07-14 13:41:30 -0700380 kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock)
SeongJae Parkb1b35202019-12-20 05:14:08 +0000381 assert self.linux_source_mock.build_reconfig.call_count == 1
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700382 self.linux_source_mock.run_kernel.assert_called_once_with(
383 build_dir=build_dir, timeout=300)
SeongJae Parkb1b35202019-12-20 05:14:08 +0000384 self.print_mock.assert_any_call(StrContains('Testing complete.'))
385
David Gow45ba7a82020-04-30 21:27:01 -0700386 def test_config_builddir(self):
387 build_dir = '.kunit'
388 kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock)
389 assert self.linux_source_mock.build_reconfig.call_count == 1
390
391 def test_build_builddir(self):
392 build_dir = '.kunit'
393 kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock)
394 self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, build_dir, None)
395
396 def test_exec_builddir(self):
397 build_dir = '.kunit'
398 kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock)
399 self.linux_source_mock.run_kernel.assert_called_once_with(build_dir=build_dir, timeout=300)
400 self.print_mock.assert_any_call(StrContains('Testing complete.'))
401
Felix Guo6ebf5862019-09-23 02:02:43 -0700402if __name__ == '__main__':
403 unittest.main()