Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kunit: tool: copy caller args in run_kernel to prevent mutation

run_kernel() appended KUnit flags directly to the caller-provided args
list. When exec_tests() calls run_kernel() repeatedly (e.g. with
--run_isolated), each call mutated the same list, causing later runs
to inherit stale filter_glob values and duplicate kunit.enable flags.

Fix this by copying args at the start of run_kernel(). Add a regression
test that calls run_kernel() twice with the same list and verifies the
original remains unchanged.

Fixes: ff9e09a3762f ("kunit: tool: support running each suite/test separately")
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
Reviewed-by: David Gow <david@davidgow.net>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Shuvam Pandey and committed by
Shuah Khan
40804c49 7dd34dfc

+30 -2
+4 -2
tools/testing/kunit/kunit_kernel.py
··· 346 346 return self.validate_config(build_dir) 347 347 348 348 def run_kernel(self, args: Optional[List[str]]=None, build_dir: str='', filter_glob: str='', filter: str='', filter_action: Optional[str]=None, timeout: Optional[int]=None) -> Iterator[str]: 349 - if not args: 350 - args = [] 349 + # Copy to avoid mutating the caller-supplied list. exec_tests() reuses 350 + # the same args across repeated run_kernel() calls (e.g. --run_isolated), 351 + # so appending to the original would accumulate stale flags on each call. 352 + args = list(args) if args else [] 351 353 if filter_glob: 352 354 args.append('kunit.filter_glob=' + filter_glob) 353 355 if filter:
+26
tools/testing/kunit/kunit_tool_test.py
··· 503 503 with open(kunit_kernel.get_outfile_path(build_dir), 'rt') as outfile: 504 504 self.assertEqual(outfile.read(), 'hi\nbye\n', msg='Missing some output') 505 505 506 + def test_run_kernel_args_not_mutated(self): 507 + """Verify run_kernel() copies args so callers can reuse them.""" 508 + start_calls = [] 509 + 510 + def fake_start(start_args, unused_build_dir): 511 + start_calls.append(list(start_args)) 512 + return subprocess.Popen(['printf', 'KTAP version 1\n'], 513 + text=True, stdout=subprocess.PIPE) 514 + 515 + with tempfile.TemporaryDirectory('') as build_dir: 516 + tree = kunit_kernel.LinuxSourceTree(build_dir, 517 + kunitconfig_paths=[os.devnull]) 518 + with mock.patch.object(tree._ops, 'start', side_effect=fake_start), \ 519 + mock.patch.object(kunit_kernel.subprocess, 'call'): 520 + kernel_args = ['mem=1G'] 521 + for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir, 522 + filter_glob='suite.test1'): 523 + pass 524 + for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir, 525 + filter_glob='suite.test2'): 526 + pass 527 + self.assertEqual(kernel_args, ['mem=1G'], 528 + 'run_kernel() should not modify caller args') 529 + self.assertIn('kunit.filter_glob=suite.test1', start_calls[0]) 530 + self.assertIn('kunit.filter_glob=suite.test2', start_calls[1]) 531 + 506 532 def test_build_reconfig_no_config(self): 507 533 with tempfile.TemporaryDirectory('') as build_dir: 508 534 with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f: