Re: [PATCH v2 2/2] kunit: tool: Fail on out-of-sequence test numbers

From: Poulin, Louis

Date: Wed Nov 25 2026 - 18:05:26 EST


On 25/11/2026 13:22, David Gow wrote:
> Hmm... I distinctly recall there being a reason we didn't fail in this
> case: I think it was due to some tests being triggered early, so having
> two concatenated KTAP outputs in the kernel log at boot. That seems
> fixed, though, so I don't have a fundamental objection to this.

I had an opportunity to do some digging so I did. I do not have a real
boot log holding two outputs; what I have is below.

lore, messages where the error text appears in a log (links at the end):

[1] 2022-02-11, Ricardo Ribalda: kunit.py run --arch=x86_64 with PCI,
USB4 and USB4_KUNIT_TEST enabled gives "plenty of" "[ERROR] Test
property-entry: Expected test number N but found N+1" (numbers as
he wrote them). He writes that with the previous patch reverted
"all works fine".
[2] 2022-11-01, Mauro Chehab: i915 mock selftests loaded twice, the
dmesg re-assembled by hand (the "TAP version" lines first) so
that kunit.py parse would read it. It printed "Expected test
number 2 but found 1", both suites [PASSED], "Ran 36 tests:
passed: 36, errors: 1".
[3] 2022-11-07, Daniel Latypov: two plain files standing in for
module output in debugfs (he could not get module tests to work
locally), read in one kunit.py parse run with a patch of his:
both suites [PASSED], "Expected test number 2 but found 1",
"Ran 2 tests: passed: 2, errors: 1". He writes that the top
level test counter resets between modules, "iirc".
[4] 2023-10-04, Michal Wajdeczko: kunit-test.ko, then
kunit-example-test.ko: "Expected test number 1 but found 7",
"Ran 12 tests: passed: 6, skipped: 6, errors: 1". His patch
resets the suite counter right before running tests.
[5] 2024-05-14, Stephen Boyd: kunit.py run --arch=s390 printed
"Expected test number 1 but found N" (N = 2..9) before each of
eight [PASSED] suites; the run ends with "The kernel seems to
have crashed". His message does not discuss those lines. I have
not reproduced them.
[6] 2025-10-23, Sander Vanheule: regmap tests printed "Expected test
number 7 but found 8" (up to "23 but found 24") on passing
parameterized cases, in a run with tens of thousands of log
lines from a dev_warn() his patch added. He proposed
dev_warn_once() instead and Mark Brown agreed. I do not know
the cause.

My runs: QEMU x86_64, KUnit built as modules, dmesg saved after
kunit-example-test.ko loaded, parsed with v7.4-rc1 kunit.py without and
with this series. The counter fix is 2e3c94aed51e ("kunit: Reset suite
counter right before running tests"), in v6.7 and v6.6.13.

v6.5 No top-level plan. Example alone ("ok 1") and after
kunit-test.ko ("ok 6"): exit 0 with one error ("missing
subtest result line"); the same with the series.
v6.6 After kunit-test.ko: "ok 6 example" under "1..1". Exit 0
with one error ("Expected test number 1 but found 6");
with the series, exit 1, failed 1, errors 1. Example
alone ("ok 1"): exit 0, no errors; the same with the
series.
v7.4-rc1 "ok 3 example" under "1..3", alone and after
kunit-test.ko: exit 0, no errors; the same with the
series.

Two outputs concatenated, built from the saved dmesg (v6.5 with one
"KTAP version 1" line added in front, as in [2]):

v6.5 Exit 0 with "Expected test number 2 but found 1"; with
the series, exit 1, failed 1, errors 1.
v6.6 and The first output is parsed and the second is not; exit 0,
v7.4-rc1 the same with the series.

Synthetic: two top-level blocks ("KTAP version 1", "1..1", one suite)
in one file, the second numbered 1, numbered 2, or "not ok". Both
parsers print the first block and exit 0; the second block, including
its "not ok", does not appear in either output.

> That being said, if anyone's using the KUnit KTAP parser for
> non-KUnit-y things which would fall afoul of this, now's the time to
> speak up!

In the tree, kunit_parser is imported by kunit.py, kunit_json.py,
kunit_junit.py and kunit_tool_test.py; I checked with grep and a GitHub
code search of torvalds/linux (its one other hit, in
tools/verification/rvgen, is an unrelated variable name). I have no
information about out-of-tree users.

> For the comments: I think they need to be more specific about what
> we're doing (and targeted at people reading the code, not people
> looking at the changes in the patch).

In the v3 I have prepared (not sent), the comment reads "Mark the test
as failed if it is not correctly numbered." The original comment stays
in test_parse_late_test_plan(), whose input is now "ok 1 test1" before
"1..4" (SUCCESS, errors=1).

> Do you think it'd be sensible to consolidate a few of them into a
> smaller number of more complicated examples. [...] it'd be great to
> have the test inputs in separate files in tools/testing/kunit/
> test_data/ as with most of the other tests.

The seven inline tests are two tests that read
test_data/test_wrong_test_numbers.log and
test_data/test_wrong_suite_numbers.log. The first log has repeated,
skipped and failed results with the wrong number; the second has a
wrongly numbered suite, a wrongly numbered nested case and a wrongly
numbered suite with a skipped case.

> [...] I'm not sure we need to separately verify that this causes a
> specific exit code given we're already testing that it gives tests
> with TestStatus.FAILURE above [...]

The CLI exit-code test is removed.

Louis

[1] https://lore.kernel.org/r/CANiDSCs7M_hSb2njr50_d3z=cx=N9gWHzVe-HkpCV1Au8yVwOw@xxxxxxxxxxxxxx
[2] https://lore.kernel.org/r/20221101134334.59500e01@maurocar-mobl2
[3] https://lore.kernel.org/r/CAGS_qxpZokOs5vEhiiTtD4_c0h79MabGm4dmQ=C%2Bt0qoZEc6iQ@xxxxxxxxxxxxxx
[4] https://lore.kernel.org/r/20231004205700.1511-1-michal.wajdeczko@xxxxxxxxx
[5] https://lore.kernel.org/r/9be47cd74b62ba8e4a36f3139fc8d275.sboyd@xxxxxxxxxx
[6] https://lore.kernel.org/r/833a6495c2b16e1da90c19826839191eec21e6f2.camel@xxxxxxxxxxxxx