blob: 75045aa0f8a16b43e8ff7b93bfce36edbb133e18 [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
Daniel Latypovb29b14f2021-05-26 01:22:17 -070014import itertools
Heidi Fahim21a6d172020-08-11 14:27:56 -070015import json
Daniel Latypov243180f2021-02-01 12:55:14 -080016import signal
Felix Guo6ebf5862019-09-23 02:02:43 -070017import os
18
19import kunit_config
20import kunit_parser
21import kunit_kernel
Heidi Fahim21a6d172020-08-11 14:27:56 -070022import kunit_json
Felix Guo6ebf5862019-09-23 02:02:43 -070023import kunit
24
25test_tmpdir = ''
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -080026abs_test_data_dir = ''
Felix Guo6ebf5862019-09-23 02:02:43 -070027
28def setUpModule():
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -080029 global test_tmpdir, abs_test_data_dir
Felix Guo6ebf5862019-09-23 02:02:43 -070030 test_tmpdir = tempfile.mkdtemp()
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -080031 abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data'))
Felix Guo6ebf5862019-09-23 02:02:43 -070032
33def tearDownModule():
34 shutil.rmtree(test_tmpdir)
35
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -080036def test_data_path(path):
37 return os.path.join(abs_test_data_dir, path)
Felix Guo6ebf5862019-09-23 02:02:43 -070038
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()
David Gow97752c32020-03-23 19:43:33 -070046 kconfig1.add_entry(kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070047 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()
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -080053 kconfig_path = test_data_path('test_read_from_file.kconfig')
Felix Guo6ebf5862019-09-23 02:02:43 -070054
55 kconfig.read_from_file(kconfig_path)
56
57 expected_kconfig = kunit_config.Kconfig()
58 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070059 kunit_config.KconfigEntry('UML', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070060 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070061 kunit_config.KconfigEntry('MMU', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070062 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070063 kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070064 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070065 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070066 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070067 kunit_config.KconfigEntry('MK8', 'n'))
Felix Guo6ebf5862019-09-23 02:02:43 -070068
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(
David Gow97752c32020-03-23 19:43:33 -070076 kunit_config.KconfigEntry('UML', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070077 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070078 kunit_config.KconfigEntry('MMU', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070079 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070080 kunit_config.KconfigEntry('TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070081 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070082 kunit_config.KconfigEntry('EXAMPLE_TEST', 'y'))
Felix Guo6ebf5862019-09-23 02:02:43 -070083 expected_kconfig.add_entry(
David Gow97752c32020-03-23 19:43:33 -070084 kunit_config.KconfigEntry('MK8', 'n'))
Felix Guo6ebf5862019-09-23 02:02:43 -070085
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
Daniel Latypovb29b14f2021-05-26 01:22:17 -070096 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:
Felix Guo6ebf5862019-09-23 02:02:43 -0700100 if needle in line:
101 return
Daniel Latypovb29b14f2021-05-26 01:22:17 -0700102 raise AssertionError(f'"{needle}" not found in {list(backup)}!')
Felix Guo6ebf5862019-09-23 02:02:43 -0700103
104 def test_output_isolated_correctly(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800105 log_path = test_data_path('test_output_isolated_correctly.log')
Daniel Latypova3ece072020-12-02 11:08:23 -0800106 with open(log_path) as file:
Daniel Latypovb29b14f2021-05-26 01:22:17 -0700107 result = kunit_parser.extract_tap_lines(file.readlines())
Daniel Latypov060352e2020-10-30 15:38:53 -0700108 self.assertContains('TAP version 14', result)
Felix Guo6ebf5862019-09-23 02:02:43 -0700109 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)
Felix Guo6ebf5862019-09-23 02:02:43 -0700114
Heidi Fahimafc63da2020-03-16 13:21:24 -0700115 def test_output_with_prefix_isolated_correctly(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800116 log_path = test_data_path('test_pound_sign.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700117 with open(log_path) as file:
Daniel Latypovb29b14f2021-05-26 01:22:17 -0700118 result = kunit_parser.extract_tap_lines(file.readlines())
Daniel Latypov060352e2020-10-30 15:38:53 -0700119 self.assertContains('TAP version 14', result)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700120 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
Felix Guo6ebf5862019-09-23 02:02:43 -0700144 def test_parse_successful_test_log(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800145 all_passed_log = test_data_path('test_is_test_passed-all_passed.log')
Daniel Latypova3ece072020-12-02 11:08:23 -0800146 with open(all_passed_log) as file:
147 result = kunit_parser.parse_run_tests(file.readlines())
Felix Guo6ebf5862019-09-23 02:02:43 -0700148 self.assertEqual(
149 kunit_parser.TestStatus.SUCCESS,
150 result.status)
Felix Guo6ebf5862019-09-23 02:02:43 -0700151
152 def test_parse_failed_test_log(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800153 failed_log = test_data_path('test_is_test_passed-failure.log')
Daniel Latypova3ece072020-12-02 11:08:23 -0800154 with open(failed_log) as file:
155 result = kunit_parser.parse_run_tests(file.readlines())
Felix Guo6ebf5862019-09-23 02:02:43 -0700156 self.assertEqual(
157 kunit_parser.TestStatus.FAILURE,
158 result.status)
Felix Guo6ebf5862019-09-23 02:02:43 -0700159
Rae Moared01ad32021-07-01 17:52:31 +0000160 def test_no_header(self):
161 empty_log = test_data_path('test_is_test_passed-no_tests_run_no_header.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.FAILURE_TO_PARSE_TESTS,
168 result.status)
169
Felix Guo6ebf5862019-09-23 02:02:43 -0700170 def test_no_tests(self):
Rae Moared01ad32021-07-01 17:52:31 +0000171 empty_log = test_data_path('test_is_test_passed-no_tests_run_with_header.log')
Daniel Latypova3ece072020-12-02 11:08:23 -0800172 with open(empty_log) as file:
173 result = kunit_parser.parse_run_tests(
Daniel Latypovb29b14f2021-05-26 01:22:17 -0700174 kunit_parser.extract_tap_lines(file.readlines()))
Felix Guo6ebf5862019-09-23 02:02:43 -0700175 self.assertEqual(0, len(result.suites))
176 self.assertEqual(
177 kunit_parser.TestStatus.NO_TESTS,
178 result.status)
Felix Guo6ebf5862019-09-23 02:02:43 -0700179
Uriel Guajardoe173b8b2020-06-11 21:05:45 +0000180 def test_no_kunit_output(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800181 crash_log = test_data_path('test_insufficient_memory.log')
Uriel Guajardoe173b8b2020-06-11 21:05:45 +0000182 print_mock = mock.patch('builtins.print').start()
Daniel Latypova3ece072020-12-02 11:08:23 -0800183 with open(crash_log) as file:
184 result = kunit_parser.parse_run_tests(
Daniel Latypovb29b14f2021-05-26 01:22:17 -0700185 kunit_parser.extract_tap_lines(file.readlines()))
Rae Moared01ad32021-07-01 17:52:31 +0000186 print_mock.assert_any_call(StrContains('could not parse test results!'))
Uriel Guajardoe173b8b2020-06-11 21:05:45 +0000187 print_mock.stop()
188 file.close()
189
Felix Guo6ebf5862019-09-23 02:02:43 -0700190 def test_crashed_test(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800191 crashed_log = test_data_path('test_is_test_passed-crash.log')
Daniel Latypova3ece072020-12-02 11:08:23 -0800192 with open(crashed_log) as file:
193 result = kunit_parser.parse_run_tests(file.readlines())
Felix Guo6ebf5862019-09-23 02:02:43 -0700194 self.assertEqual(
195 kunit_parser.TestStatus.TEST_CRASHED,
196 result.status)
Felix Guo6ebf5862019-09-23 02:02:43 -0700197
David Gow5acaf602021-06-24 23:58:13 -0700198 def test_skipped_test(self):
199 skipped_log = test_data_path('test_skip_tests.log')
200 file = open(skipped_log)
201 result = kunit_parser.parse_run_tests(file.readlines())
202
203 # A skipped test does not fail the whole suite.
204 self.assertEqual(
205 kunit_parser.TestStatus.SUCCESS,
206 result.status)
207 file.close()
208
209 def test_skipped_all_tests(self):
210 skipped_log = test_data_path('test_skip_all_tests.log')
211 file = open(skipped_log)
212 result = kunit_parser.parse_run_tests(file.readlines())
213
214 self.assertEqual(
215 kunit_parser.TestStatus.SKIPPED,
216 result.status)
217 file.close()
218
219
Heidi Fahimafc63da2020-03-16 13:21:24 -0700220 def test_ignores_prefix_printk_time(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800221 prefix_log = test_data_path('test_config_printk_time.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700222 with open(prefix_log) as file:
223 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700224 self.assertEqual(
225 kunit_parser.TestStatus.SUCCESS,
226 result.status)
227 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700228
229 def test_ignores_multiple_prefixes(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800230 prefix_log = test_data_path('test_multiple_prefixes.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700231 with open(prefix_log) as file:
232 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700233 self.assertEqual(
234 kunit_parser.TestStatus.SUCCESS,
235 result.status)
236 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700237
238 def test_prefix_mixed_kernel_output(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800239 mixed_prefix_log = test_data_path('test_interrupted_tap_output.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700240 with open(mixed_prefix_log) as file:
241 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700242 self.assertEqual(
243 kunit_parser.TestStatus.SUCCESS,
244 result.status)
245 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700246
247 def test_prefix_poundsign(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800248 pound_log = test_data_path('test_pound_sign.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700249 with open(pound_log) as file:
250 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700251 self.assertEqual(
252 kunit_parser.TestStatus.SUCCESS,
253 result.status)
254 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700255
256 def test_kernel_panic_end(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800257 panic_log = test_data_path('test_kernel_panic_interrupt.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700258 with open(panic_log) as file:
259 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700260 self.assertEqual(
261 kunit_parser.TestStatus.TEST_CRASHED,
262 result.status)
263 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700264
265 def test_pound_no_prefix(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800266 pound_log = test_data_path('test_pound_no_prefix.log')
Heidi Fahimafc63da2020-03-16 13:21:24 -0700267 with open(pound_log) as file:
268 result = kunit_parser.parse_run_tests(file.readlines())
Brendan Higgins0d0d2452020-10-21 13:39:14 -0700269 self.assertEqual(
270 kunit_parser.TestStatus.SUCCESS,
271 result.status)
272 self.assertEqual('kunit-resource-test', result.suites[0].name)
Heidi Fahimafc63da2020-03-16 13:21:24 -0700273
Daniel Latypov243180f2021-02-01 12:55:14 -0800274class LinuxSourceTreeTest(unittest.TestCase):
275
276 def setUp(self):
277 mock.patch.object(signal, 'signal').start()
278 self.addCleanup(mock.patch.stopall)
279
280 def test_invalid_kunitconfig(self):
281 with self.assertRaisesRegex(kunit_kernel.ConfigError, 'nonexistent.* does not exist'):
282 kunit_kernel.LinuxSourceTree('', kunitconfig_path='/nonexistent_file')
283
284 def test_valid_kunitconfig(self):
285 with tempfile.NamedTemporaryFile('wt') as kunitconfig:
286 tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=kunitconfig.name)
287
Daniel Latypov98547812021-02-22 14:52:41 -0800288 def test_dir_kunitconfig(self):
289 with tempfile.TemporaryDirectory('') as dir:
290 with open(os.path.join(dir, '.kunitconfig'), 'w') as f:
291 pass
292 tree = kunit_kernel.LinuxSourceTree('', kunitconfig_path=dir)
293
Daniel Latypov243180f2021-02-01 12:55:14 -0800294 # TODO: add more test cases.
295
296
Heidi Fahim21a6d172020-08-11 14:27:56 -0700297class KUnitJsonTest(unittest.TestCase):
298
299 def _json_for(self, log_file):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800300 with open(test_data_path(log_file)) as file:
Heidi Fahim21a6d172020-08-11 14:27:56 -0700301 test_result = kunit_parser.parse_run_tests(file)
302 json_obj = kunit_json.get_json_result(
303 test_result=test_result,
304 def_config='kunit_defconfig',
305 build_dir=None,
306 json_path='stdout')
307 return json.loads(json_obj)
308
309 def test_failed_test_json(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800310 result = self._json_for('test_is_test_passed-failure.log')
Heidi Fahim21a6d172020-08-11 14:27:56 -0700311 self.assertEqual(
312 {'name': 'example_simple_test', 'status': 'FAIL'},
313 result["sub_groups"][1]["test_cases"][0])
314
315 def test_crashed_test_json(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800316 result = self._json_for('test_is_test_passed-crash.log')
Heidi Fahim21a6d172020-08-11 14:27:56 -0700317 self.assertEqual(
318 {'name': 'example_simple_test', 'status': 'ERROR'},
319 result["sub_groups"][1]["test_cases"][0])
320
321 def test_no_tests_json(self):
Rae Moared01ad32021-07-01 17:52:31 +0000322 result = self._json_for('test_is_test_passed-no_tests_run_with_header.log')
Heidi Fahim21a6d172020-08-11 14:27:56 -0700323 self.assertEqual(0, len(result['sub_groups']))
324
Felix Guo6ebf5862019-09-23 02:02:43 -0700325class StrContains(str):
326 def __eq__(self, other):
327 return self in other
328
329class KUnitMainTest(unittest.TestCase):
330 def setUp(self):
Daniel Latypovcd4a9bc2020-12-02 11:08:24 -0800331 path = test_data_path('test_is_test_passed-all_passed.log')
Daniel Latypovcfd607e2020-12-02 11:08:21 -0800332 with open(path) as file:
333 all_passed_log = file.readlines()
334
335 self.print_mock = mock.patch('builtins.print').start()
336 self.addCleanup(mock.patch.stopall)
337
Felix Guo6ebf5862019-09-23 02:02:43 -0700338 self.linux_source_mock = mock.Mock()
339 self.linux_source_mock.build_reconfig = mock.Mock(return_value=True)
Brendan Higgins87c9c162021-05-26 14:24:06 -0700340 self.linux_source_mock.build_kernel = mock.Mock(return_value=True)
Felix Guo6ebf5862019-09-23 02:02:43 -0700341 self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log)
342
David Gow45ba7a82020-04-30 21:27:01 -0700343 def test_config_passes_args_pass(self):
Brendan Higginsd43c7fb2020-07-14 13:41:30 -0700344 kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800345 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
346 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0)
David Gow45ba7a82020-04-30 21:27:01 -0700347
348 def test_build_passes_args_pass(self):
349 kunit.main(['build'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800350 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0)
Brendan Higgins87c9c162021-05-26 14:24:06 -0700351 self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, '.kunit', None)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800352 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 0)
David Gow45ba7a82020-04-30 21:27:01 -0700353
354 def test_exec_passes_args_pass(self):
355 kunit.main(['exec'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800356 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0)
357 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
Daniel Latypovd992880b2021-02-05 16:08:53 -0800358 self.linux_source_mock.run_kernel.assert_called_once_with(
359 build_dir='.kunit', filter_glob='', timeout=300)
David Gow45ba7a82020-04-30 21:27:01 -0700360 self.print_mock.assert_any_call(StrContains('Testing complete.'))
361
Felix Guo6ebf5862019-09-23 02:02:43 -0700362 def test_run_passes_args_pass(self):
363 kunit.main(['run'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800364 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
365 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700366 self.linux_source_mock.run_kernel.assert_called_once_with(
Daniel Latypovd992880b2021-02-05 16:08:53 -0800367 build_dir='.kunit', filter_glob='', timeout=300)
Felix Guo6ebf5862019-09-23 02:02:43 -0700368 self.print_mock.assert_any_call(StrContains('Testing complete.'))
369
David Gow45ba7a82020-04-30 21:27:01 -0700370 def test_exec_passes_args_fail(self):
371 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
372 with self.assertRaises(SystemExit) as e:
373 kunit.main(['exec'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800374 self.assertEqual(e.exception.code, 1)
David Gow45ba7a82020-04-30 21:27:01 -0700375
Felix Guo6ebf5862019-09-23 02:02:43 -0700376 def test_run_passes_args_fail(self):
377 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
378 with self.assertRaises(SystemExit) as e:
379 kunit.main(['run'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800380 self.assertEqual(e.exception.code, 1)
381 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
382 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
Felix Guo6ebf5862019-09-23 02:02:43 -0700383 self.print_mock.assert_any_call(StrContains(' 0 tests run'))
384
David Gow45ba7a82020-04-30 21:27:01 -0700385 def test_exec_raw_output(self):
386 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
387 kunit.main(['exec', '--raw_output'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800388 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
389 for call in self.print_mock.call_args_list:
390 self.assertNotEqual(call, mock.call(StrContains('Testing complete.')))
391 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run')))
David Gow45ba7a82020-04-30 21:27:01 -0700392
Felix Guo6ebf5862019-09-23 02:02:43 -0700393 def test_run_raw_output(self):
394 self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
David Gow45ba7a82020-04-30 21:27:01 -0700395 kunit.main(['run', '--raw_output'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800396 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
397 self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
398 for call in self.print_mock.call_args_list:
399 self.assertNotEqual(call, mock.call(StrContains('Testing complete.')))
400 self.assertNotEqual(call, mock.call(StrContains(' 0 tests run')))
David Gow45ba7a82020-04-30 21:27:01 -0700401
402 def test_exec_timeout(self):
403 timeout = 3453
404 kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock)
Daniel Latypovd992880b2021-02-05 16:08:53 -0800405 self.linux_source_mock.run_kernel.assert_called_once_with(
406 build_dir='.kunit', filter_glob='', timeout=timeout)
David Gow45ba7a82020-04-30 21:27:01 -0700407 self.print_mock.assert_any_call(StrContains('Testing complete.'))
Felix Guo6ebf5862019-09-23 02:02:43 -0700408
409 def test_run_timeout(self):
410 timeout = 3453
411 kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800412 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700413 self.linux_source_mock.run_kernel.assert_called_once_with(
Daniel Latypovd992880b2021-02-05 16:08:53 -0800414 build_dir='.kunit', filter_glob='', timeout=timeout)
Felix Guo6ebf5862019-09-23 02:02:43 -0700415 self.print_mock.assert_any_call(StrContains('Testing complete.'))
416
SeongJae Parkb1b35202019-12-20 05:14:08 +0000417 def test_run_builddir(self):
418 build_dir = '.kunit'
Brendan Higginsd43c7fb2020-07-14 13:41:30 -0700419 kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800420 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
Heidi Fahim021ed9f2020-03-16 13:21:25 -0700421 self.linux_source_mock.run_kernel.assert_called_once_with(
Daniel Latypovd992880b2021-02-05 16:08:53 -0800422 build_dir=build_dir, filter_glob='', timeout=300)
SeongJae Parkb1b35202019-12-20 05:14:08 +0000423 self.print_mock.assert_any_call(StrContains('Testing complete.'))
424
David Gow45ba7a82020-04-30 21:27:01 -0700425 def test_config_builddir(self):
426 build_dir = '.kunit'
427 kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock)
Daniel Latypov0b3e6802020-12-02 11:08:22 -0800428 self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
David Gow45ba7a82020-04-30 21:27:01 -0700429
430 def test_build_builddir(self):
431 build_dir = '.kunit'
432 kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock)
Brendan Higgins87c9c162021-05-26 14:24:06 -0700433 self.linux_source_mock.build_kernel.assert_called_once_with(False, 8, build_dir, None)
David Gow45ba7a82020-04-30 21:27:01 -0700434
435 def test_exec_builddir(self):
436 build_dir = '.kunit'
437 kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock)
Daniel Latypovd992880b2021-02-05 16:08:53 -0800438 self.linux_source_mock.run_kernel.assert_called_once_with(
439 build_dir=build_dir, filter_glob='', timeout=300)
David Gow45ba7a82020-04-30 21:27:01 -0700440 self.print_mock.assert_any_call(StrContains('Testing complete.'))
441
Daniel Latypov243180f2021-02-01 12:55:14 -0800442 @mock.patch.object(kunit_kernel, 'LinuxSourceTree')
443 def test_run_kunitconfig(self, mock_linux_init):
444 mock_linux_init.return_value = self.linux_source_mock
445 kunit.main(['run', '--kunitconfig=mykunitconfig'])
446 # Just verify that we parsed and initialized it correctly here.
Brendan Higgins87c9c162021-05-26 14:24:06 -0700447 mock_linux_init.assert_called_once_with('.kunit',
448 kunitconfig_path='mykunitconfig',
449 arch='um',
450 cross_compile=None,
451 qemu_config_path=None)
Daniel Latypov243180f2021-02-01 12:55:14 -0800452
453 @mock.patch.object(kunit_kernel, 'LinuxSourceTree')
454 def test_config_kunitconfig(self, mock_linux_init):
455 mock_linux_init.return_value = self.linux_source_mock
456 kunit.main(['config', '--kunitconfig=mykunitconfig'])
457 # Just verify that we parsed and initialized it correctly here.
Brendan Higgins87c9c162021-05-26 14:24:06 -0700458 mock_linux_init.assert_called_once_with('.kunit',
459 kunitconfig_path='mykunitconfig',
460 arch='um',
461 cross_compile=None,
462 qemu_config_path=None)
Daniel Latypov243180f2021-02-01 12:55:14 -0800463
Felix Guo6ebf5862019-09-23 02:02:43 -0700464if __name__ == '__main__':
465 unittest.main()