Commit 6cb51a18 authored by Daniel Latypov's avatar Daniel Latypov Committed by Shuah Khan

kunit: tool: add --kernel_args to allow setting module params

kunit.py currently does not make it possible for users to specify module
parameters (/kernel arguments more generally) unless one directly tweaks
the kunit.py code itself.

This hasn't mattered much so far, but this would make it easier to port
existing tests that expose module parameters over to KUnit and/or let
current KUnit tests take advantage of them.

Tested using an kunit internal parameter:
$ ./tools/testing/kunit/kunit.py run --kunitconfig=lib/kunit \
    --kernel_args=kunit.filter_glob=kunit_status
...
Testing complete. 2 tests run. 0 failed. 0 crashed. 0 skipped.
Signed-off-by: default avatarDaniel Latypov <dlatypov@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Reviewed-by: default avatarBrendan Higgins <brendanhiggins@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 1195505f
...@@ -80,6 +80,16 @@ file ``.kunitconfig``, you can just pass in the dir, e.g. ...@@ -80,6 +80,16 @@ file ``.kunitconfig``, you can just pass in the dir, e.g.
automagically, but tests could theoretically depend on incompatible automagically, but tests could theoretically depend on incompatible
options, so handling that would be tricky. options, so handling that would be tricky.
Setting kernel commandline parameters
-------------------------------------
You can use ``--kernel_args`` to pass arbitrary kernel arguments, e.g.
.. code-block:: bash
$ ./tools/testing/kunit/kunit.py run --kernel_args=param=42 --kernel_args=param2=false
Generating code coverage reports under UML Generating code coverage reports under UML
------------------------------------------ ------------------------------------------
......
...@@ -30,12 +30,13 @@ KunitBuildRequest = namedtuple('KunitBuildRequest', ...@@ -30,12 +30,13 @@ KunitBuildRequest = namedtuple('KunitBuildRequest',
['jobs', 'build_dir', 'alltests', ['jobs', 'build_dir', 'alltests',
'make_options']) 'make_options'])
KunitExecRequest = namedtuple('KunitExecRequest', KunitExecRequest = namedtuple('KunitExecRequest',
['timeout', 'build_dir', 'alltests', 'filter_glob']) ['timeout', 'build_dir', 'alltests',
'filter_glob', 'kernel_args'])
KunitParseRequest = namedtuple('KunitParseRequest', KunitParseRequest = namedtuple('KunitParseRequest',
['raw_output', 'input_data', 'build_dir', 'json']) ['raw_output', 'input_data', 'build_dir', 'json'])
KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
'build_dir', 'alltests', 'filter_glob', 'build_dir', 'alltests', 'filter_glob',
'json', 'make_options']) 'kernel_args', 'json', 'make_options'])
KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0] KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
...@@ -94,6 +95,7 @@ def exec_tests(linux: kunit_kernel.LinuxSourceTree, ...@@ -94,6 +95,7 @@ def exec_tests(linux: kunit_kernel.LinuxSourceTree,
kunit_parser.print_with_timestamp('Starting KUnit Kernel ...') kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
test_start = time.time() test_start = time.time()
result = linux.run_kernel( result = linux.run_kernel(
args=request.kernel_args,
timeout=None if request.alltests else request.timeout, timeout=None if request.alltests else request.timeout,
filter_glob=request.filter_glob, filter_glob=request.filter_glob,
build_dir=request.build_dir) build_dir=request.build_dir)
...@@ -152,7 +154,8 @@ def run_tests(linux: kunit_kernel.LinuxSourceTree, ...@@ -152,7 +154,8 @@ def run_tests(linux: kunit_kernel.LinuxSourceTree,
return build_result return build_result
exec_request = KunitExecRequest(request.timeout, request.build_dir, exec_request = KunitExecRequest(request.timeout, request.build_dir,
request.alltests, request.filter_glob) request.alltests, request.filter_glob,
request.kernel_args)
exec_result = exec_tests(linux, exec_request) exec_result = exec_tests(linux, exec_request)
if exec_result.status != KunitStatus.SUCCESS: if exec_result.status != KunitStatus.SUCCESS:
return exec_result return exec_result
...@@ -238,6 +241,9 @@ def add_exec_opts(parser) -> None: ...@@ -238,6 +241,9 @@ def add_exec_opts(parser) -> None:
nargs='?', nargs='?',
default='', default='',
metavar='filter_glob') metavar='filter_glob')
parser.add_argument('--kernel_args',
help='Kernel command-line parameters. Maybe be repeated',
action='append')
def add_parse_opts(parser) -> None: def add_parse_opts(parser) -> None:
parser.add_argument('--raw_output', help='don\'t format output from kernel', parser.add_argument('--raw_output', help='don\'t format output from kernel',
...@@ -309,6 +315,7 @@ def main(argv, linux=None): ...@@ -309,6 +315,7 @@ def main(argv, linux=None):
cli_args.build_dir, cli_args.build_dir,
cli_args.alltests, cli_args.alltests,
cli_args.filter_glob, cli_args.filter_glob,
cli_args.kernel_args,
cli_args.json, cli_args.json,
cli_args.make_options) cli_args.make_options)
result = run_tests(linux, request) result = run_tests(linux, request)
...@@ -363,7 +370,8 @@ def main(argv, linux=None): ...@@ -363,7 +370,8 @@ def main(argv, linux=None):
exec_request = KunitExecRequest(cli_args.timeout, exec_request = KunitExecRequest(cli_args.timeout,
cli_args.build_dir, cli_args.build_dir,
cli_args.alltests, cli_args.alltests,
cli_args.filter_glob) cli_args.filter_glob,
cli_args.kernel_args)
exec_result = exec_tests(linux, exec_request) exec_result = exec_tests(linux, exec_request)
parse_request = KunitParseRequest(cli_args.raw_output, parse_request = KunitParseRequest(cli_args.raw_output,
exec_result.result, exec_result.result,
......
...@@ -356,7 +356,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -356,7 +356,7 @@ class KUnitMainTest(unittest.TestCase):
self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0) self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 0)
self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir='.kunit', filter_glob='', timeout=300) args=None, build_dir='.kunit', filter_glob='', timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
def test_run_passes_args_pass(self): def test_run_passes_args_pass(self):
...@@ -364,7 +364,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -364,7 +364,7 @@ class KUnitMainTest(unittest.TestCase):
self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1) self.assertEqual(self.linux_source_mock.run_kernel.call_count, 1)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir='.kunit', filter_glob='', timeout=300) args=None, build_dir='.kunit', filter_glob='', timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
def test_exec_passes_args_fail(self): def test_exec_passes_args_fail(self):
...@@ -403,7 +403,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -403,7 +403,7 @@ class KUnitMainTest(unittest.TestCase):
timeout = 3453 timeout = 3453
kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir='.kunit', filter_glob='', timeout=timeout) args=None, build_dir='.kunit', filter_glob='', timeout=timeout)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
def test_run_timeout(self): def test_run_timeout(self):
...@@ -411,7 +411,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -411,7 +411,7 @@ class KUnitMainTest(unittest.TestCase):
kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock) kunit.main(['run', '--timeout', str(timeout)], self.linux_source_mock)
self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir='.kunit', filter_glob='', timeout=timeout) args=None, build_dir='.kunit', filter_glob='', timeout=timeout)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
def test_run_builddir(self): def test_run_builddir(self):
...@@ -419,7 +419,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -419,7 +419,7 @@ class KUnitMainTest(unittest.TestCase):
kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock) kunit.main(['run', '--build_dir=.kunit'], self.linux_source_mock)
self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1) self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir=build_dir, filter_glob='', timeout=300) args=None, build_dir=build_dir, filter_glob='', timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
def test_config_builddir(self): def test_config_builddir(self):
...@@ -436,7 +436,7 @@ class KUnitMainTest(unittest.TestCase): ...@@ -436,7 +436,7 @@ class KUnitMainTest(unittest.TestCase):
build_dir = '.kunit' build_dir = '.kunit'
kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock) kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock)
self.linux_source_mock.run_kernel.assert_called_once_with( self.linux_source_mock.run_kernel.assert_called_once_with(
build_dir=build_dir, filter_glob='', timeout=300) args=None, build_dir=build_dir, filter_glob='', timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.')) self.print_mock.assert_any_call(StrContains('Testing complete.'))
@mock.patch.object(kunit_kernel, 'LinuxSourceTree') @mock.patch.object(kunit_kernel, 'LinuxSourceTree')
...@@ -461,5 +461,13 @@ class KUnitMainTest(unittest.TestCase): ...@@ -461,5 +461,13 @@ class KUnitMainTest(unittest.TestCase):
cross_compile=None, cross_compile=None,
qemu_config_path=None) qemu_config_path=None)
def test_run_kernel_args(self):
kunit.main(['run', '--kernel_args=a=1', '--kernel_args=b=2'], self.linux_source_mock)
self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
self.linux_source_mock.run_kernel.assert_called_once_with(
args=['a=1','b=2'], build_dir='.kunit', filter_glob='', timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.'))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment