Skip to content

Commit 40804c4

Browse files
shuv-ampshuahkh
authored andcommitted
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: ff9e09a ("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>
1 parent 7dd34df commit 40804c4

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

tools/testing/kunit/kunit_kernel.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ def build_kernel(self, jobs: int, build_dir: str, make_options: Optional[List[st
346346
return self.validate_config(build_dir)
347347

348348
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 []
351353
if filter_glob:
352354
args.append('kunit.filter_glob=' + filter_glob)
353355
if filter:

tools/testing/kunit/kunit_tool_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,32 @@ def fake_start(unused_args, unused_build_dir):
503503
with open(kunit_kernel.get_outfile_path(build_dir), 'rt') as outfile:
504504
self.assertEqual(outfile.read(), 'hi\nbye\n', msg='Missing some output')
505505

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+
506532
def test_build_reconfig_no_config(self):
507533
with tempfile.TemporaryDirectory('') as build_dir:
508534
with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f:

0 commit comments

Comments
 (0)