kunit: drop assumption in kunit-log-test about current suite
Subsystems (4)
PATCH v4 1/2 kunit: drop assumption in kunit-log-test about current suite
This test assumes that the declared kunit_suite object is the exact one which is being executed, which KUnit will not guarantee [1].
Specifically, suite->log is not initialized until a suite object is executed. So if KUnit makes a copy of the suite and runs that instead, this test dereferences an invalid pointer and (hopefully) segfaults.
N.B. since we no longer assume this, we can no longer verify that suite->log is *not* allocated during normal execution.
An alternative to this patch that would allow us to test that would require exposing an API for the current test to get its current suite. Exposing that for one internal kunit test seems like overkill, and grants users more footguns (e.g. reusing a test case in multiple suites and changing behavior based on the suite name, dynamically modifying the setup/cleanup funcs, storing/reading stuff out of the suite->log, etc.).
[1] In a subsequent patch, KUnit will allow running subsets of test cases within a suite by making a copy of the suite w/ the filtered test list. But there are other reasons KUnit might execute a copy, e.g. if it ever wants to support parallel execution of different suites, recovering from errors and restarting suites
--- lib/kunit/kunit-test.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
lib/kunit/kunit-test.c +8 -6
@@ -415,12 +415,15 @@ static struct kunit_suite kunit_log_test_suite = {static void kunit_log_test (struct kunit *test) { -struct kunit_suite *suite = &kunit_log_test_suite; +struct kunit_suite suite; + + suite.log =kunit_kzalloc (test,KUNIT_LOG_SIZE ,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, suite.log);kunit_log (KERN_INFO , test,"put this in log." );kunit_log (KERN_INFO , test,"this too." ); -kunit_log (KERN_INFO , suite,"add to suite log." ); -kunit_log (KERN_INFO , suite,"along with this." ); +kunit_log (KERN_INFO , &suite,"add to suite log." ); +kunit_log (KERN_INFO , &suite,"along with this." );#ifdef CONFIG_KUNIT_DEBUGFS KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
@@ -428,12 +431,11 @@ static void kunit_log_test(struct kunit *test)KUNIT_EXPECT_NOT_ERR_OR_NULL (test,strstr (test->log,"this too." ));KUNIT_EXPECT_NOT_ERR_OR_NULL (test, -strstr (suite->log,"add to suite log." )); +strstr (suite.log,"add to suite log." ));KUNIT_EXPECT_NOT_ERR_OR_NULL (test, -strstr (suite->log,"along with this." )); +strstr (suite.log,"along with this." ));#else KUNIT_EXPECT_PTR_EQ (test, test->log, (char *)NULL ); -KUNIT_EXPECT_PTR_EQ (test, suite->log, (char *)NULL );#endif }
base-commit: a3fa7a101dcff93791d1b1bdb3affcad1410c8c1
Signature · 1 line
2.33.0.309.g3052b89438-goog
PATCH v4 2/2 kunit: tool: allow filtering test cases via glob
Commit 1d71307a6f94 ("kunit: add unit test for filtering suites by names") introduced the ability to filter which suites we run via glob.
This change extends it so we can also filter individual test cases inside of suites as well.
This is quite useful when, e.g.
* trying to run just the tests cases you've just added or are working on
* trying to debug issues with test hermeticity
Examples:
$ ./tools/testing/kunit/kunit.py run --kunitconfig=lib/kunit '*exec*.parse*' ... ============================================================ ======== [PASSED] kunit_executor_test ======== [PASSED] parse_filter_test ============================================================ Testing complete. 1 tests run. 0 failed. 0 crashed.
$ ./tools/testing/kunit/kunit.py run --kunitconfig=lib/kunit '*.no_matching_tests' ... [ERROR] no tests run!
---
v1 -> v2: update Documentation/.../running_tips.rst
v2 -> v3: add missing static (thanks lkp bot), add prework patch to fix kunit-log-test's assumption about the current suite. v3 -> v4: s/alloc_and_get_resource/alloc_resource, fixes a leak. Thanks to Xiyu Yang and Xin Tan for finding this.
--- .../dev-tools/kunit/running_tips.rst | 11 +- lib/kunit/executor.c | 107 +++++++++++++++-- lib/kunit/executor_test.c | 109 +++++++++++++++--- tools/testing/kunit/kunit.py | 5 +- 4 files changed, 203 insertions(+), 29 deletions(-)
Documentation/dev-tools/kunit/running_tips.rst +9 -2
@@ -25,8 +25,8 @@ It can be handy to create a bash function like: Running a subset of tests ------------------------- -``kunit.py run`` accepts an optional glob argument to filter tests. Currently -this only matches against suite names, but this may change in the future. +``kunit.py run`` accepts an optional glob argument to filter tests. The format +is ``"<suite_glob>[.test_glob]"``. Say that we wanted to run the sysctl tests, we could do so via:
@@ -35,6 +35,13 @@ Say that we wanted to run the sysctl tests, we could do so via: $ echo -e 'CONFIG_KUNIT=y\nCONFIG_KUNIT_ALL_TESTS=y' > .kunit/.kunitconfig $ ./tools/testing/kunit/kunit.py run 'sysctl*' +We can filter down to just the "write" tests via: + +.. code-block:: bash + + $ echo -e 'CONFIG_KUNIT=y\nCONFIG_KUNIT_ALL_TESTS=y' > .kunit/.kunitconfig + $ ./tools/testing/kunit/kunit.py run 'sysctl*.*write*' + We're paying the cost of building more tests than we need this way, but it's easier than fiddling with ``.kunitconfig`` files or commenting out ``kunit_suite``'s.
lib/kunit/executor.c +96 -11
158 lines not on this page · raw message
lib/kunit/executor_test.c +96 -13
170 lines not on this page · raw message
tools/testing/kunit/kunit.py +2 -3
@@ -243,9 +243,8 @@ def add_exec_opts(parser) -> None: default=300, metavar='timeout') parser.add_argument('filter_glob', - help='maximum number of seconds to allow for all tests ' - 'to run. This does not include time taken to build the ' - 'tests.', + help='Filter which KUnit test suites/tests run at ' + 'boot-time, e.g. list* or list*.*del_test', type=str, nargs='?', default='',
Signature · 1 line
2.33.0.309.g3052b89438-goog
kernel test robot
build botRe: [PATCH v4 2/2] kunit: tool: allow filtering test cases via glob
Thank you for the patch! Perhaps something to improve:
Hi Daniel,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on a3fa7a101dcff93791d1b1bdb3affcad1410c8c1]
url: https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-drop-assumption-in-kunit-log-test-about-current-suite/20210910-011213 base: a3fa7a101dcff93791d1b1bdb3affcad1410c8c1 config: riscv-randconfig-s032-20210910 (attached as .config) compiler: riscv64-linux-gcc (GCC) 11.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/0day-ci/linux/commit/3d5c1af613421e9022fdf3523933c7c5fa3f7357
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Daniel-Latypov/kunit-drop-assumption-in-kunit-log-test-about-current-suite/20210910-011213
git checkout 3d5c1af613421e9022fdf3523933c7c5fa3f7357
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=riscv SHELL=/bin/bash lib/
If you fix the issue, kindly add following tag as appropriate
sparse warnings: (new ones prefixed by >>)
lib/kunit/executor.c: note: in included file:
>> lib/kunit/executor_test.c:20:19: sparse: sparse: symbol 'dummy_test_cases' was not declared. Should it be static?
vim +/dummy_test_cases +20 lib/kunit/executor_test.c
19
> 20 struct kunit_case dummy_test_cases[] = {
21 /* .run_case is not important, just needs to be non-NULL */
22 { .name = "test1", .run_case = dummy_test },
23 { .name = "test2", .run_case = dummy_test },
24 {},
25 };
26 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org