Skip to content

Commit 2709473

Browse files
kuba-moodavem330
authored andcommitted
selftests: kselftest_harness: support using xfail
Currently some tests report skip for things they expect to fail e.g. when given combination of parameters is known to be unsupported. This is confusing because in an ideal test environment and fully featured kernel no tests should be skipped. Selftest summary line already includes xfail and xpass counters, e.g.: Totals: pass:725 fail:0 xfail:0 xpass:0 skip:0 error:0 but there's no way to use it from within the harness. Add a new per-fixture+variant combination list of test cases we expect to fail. Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 378193e commit 2709473

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

tools/testing/selftests/kselftest_harness.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,37 @@ struct __fixture_metadata {
803803
.prev = &_fixture_global,
804804
};
805805

806+
struct __test_xfail {
807+
struct __fixture_metadata *fixture;
808+
struct __fixture_variant_metadata *variant;
809+
struct __test_metadata *test;
810+
struct __test_xfail *prev, *next;
811+
};
812+
813+
/**
814+
* XFAIL_ADD() - mark variant + test case combination as expected to fail
815+
* @fixture_name: name of the fixture
816+
* @variant_name: name of the variant
817+
* @test_name: name of the test case
818+
*
819+
* Mark a combination of variant + test case for a given fixture as expected
820+
* to fail. Tests marked this way will report XPASS / XFAIL return codes,
821+
* instead of PASS / FAIL,and use respective counters.
822+
*/
823+
#define XFAIL_ADD(fixture_name, variant_name, test_name) \
824+
static struct __test_xfail \
825+
_##fixture_name##_##variant_name##_##test_name##_xfail = \
826+
{ \
827+
.fixture = &_##fixture_name##_fixture_object, \
828+
.variant = &_##fixture_name##_##variant_name##_object, \
829+
.test = &_##fixture_name##_##test_name##_object, \
830+
}; \
831+
static void __attribute__((constructor)) \
832+
_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
833+
{ \
834+
__register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
835+
}
836+
806837
static struct __fixture_metadata *__fixture_list = &_fixture_global;
807838
static int __constructor_order;
808839

@@ -817,6 +848,7 @@ static inline void __register_fixture(struct __fixture_metadata *f)
817848
struct __fixture_variant_metadata {
818849
const char *name;
819850
const void *data;
851+
struct __test_xfail *xfails;
820852
struct __fixture_variant_metadata *prev, *next;
821853
};
822854

@@ -866,6 +898,11 @@ static inline void __register_test(struct __test_metadata *t)
866898
__LIST_APPEND(t->fixture->tests, t);
867899
}
868900

901+
static inline void __register_xfail(struct __test_xfail *xf)
902+
{
903+
__LIST_APPEND(xf->variant->xfails, xf);
904+
}
905+
869906
static inline int __bail(int for_realz, struct __test_metadata *t)
870907
{
871908
/* if this is ASSERT, return immediately. */
@@ -941,7 +978,9 @@ void __wait_for_test(struct __test_metadata *t)
941978
fprintf(TH_LOG_STREAM,
942979
"# %s: Test terminated by timeout\n", t->name);
943980
} else if (WIFEXITED(status)) {
944-
if (WEXITSTATUS(status) == KSFT_SKIP) {
981+
if (WEXITSTATUS(status) == KSFT_SKIP ||
982+
WEXITSTATUS(status) == KSFT_XPASS ||
983+
WEXITSTATUS(status) == KSFT_XFAIL) {
945984
t->exit_code = WEXITSTATUS(status);
946985
} else if (t->termsig != -1) {
947986
t->exit_code = KSFT_FAIL;
@@ -1110,6 +1149,7 @@ void __run_test(struct __fixture_metadata *f,
11101149
struct __fixture_variant_metadata *variant,
11111150
struct __test_metadata *t)
11121151
{
1152+
struct __test_xfail *xfail;
11131153
char test_name[LINE_MAX];
11141154
const char *diagnostic;
11151155

@@ -1141,6 +1181,13 @@ void __run_test(struct __fixture_metadata *f,
11411181
ksft_print_msg(" %4s %s\n",
11421182
__test_passed(t) ? "OK" : "FAIL", test_name);
11431183

1184+
/* Check if we're expecting this test to fail */
1185+
for (xfail = variant->xfails; xfail; xfail = xfail->next)
1186+
if (xfail->test == t)
1187+
break;
1188+
if (xfail)
1189+
t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;
1190+
11441191
if (t->results->reason[0])
11451192
diagnostic = t->results->reason;
11461193
else if (t->exit_code == KSFT_PASS || t->exit_code == KSFT_FAIL)

0 commit comments

Comments
 (0)