Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/harness: Switch to TAP output

Using the kselftest_harness.h would result in non-TAP test reporting,
which didn't make much sense given that all the requirements for using
the low-level API were met. Switch to using ksft_*() helpers while
retaining as much of a human-readability as possible.

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Kees Cook and committed by
Shuah Khan
e80068be 245dd604

+33 -24
+3 -2
tools/testing/selftests/kselftest.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 /* 3 - * kselftest.h: kselftest framework return codes to include from 4 - * selftests. 3 + * kselftest.h: low-level kselftest framework to include from 4 + * selftest programs. When possible, please use 5 + * kselftest_harness.h instead. 5 6 * 6 7 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com> 7 8 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+30 -22
tools/testing/selftests/kselftest_harness.h
··· 50 50 #ifndef __KSELFTEST_HARNESS_H 51 51 #define __KSELFTEST_HARNESS_H 52 52 53 + #ifndef _GNU_SOURCE 53 54 #define _GNU_SOURCE 55 + #endif 54 56 #include <asm/types.h> 55 57 #include <errno.h> 56 58 #include <stdbool.h> ··· 63 61 #include <sys/types.h> 64 62 #include <sys/wait.h> 65 63 #include <unistd.h> 64 + 65 + #include "kselftest.h" 66 66 67 67 #define TEST_TIMEOUT_DEFAULT 30 68 68 ··· 108 104 109 105 /* Unconditional logger for internal use. */ 110 106 #define __TH_LOG(fmt, ...) \ 111 - fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \ 107 + fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \ 112 108 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__) 113 109 114 110 /** ··· 123 119 */ 124 120 #define XFAIL(statement, fmt, ...) do { \ 125 121 if (TH_LOG_ENABLED) { \ 126 - fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \ 122 + fprintf(TH_LOG_STREAM, "# XFAIL " fmt "\n", \ 127 123 ##__VA_ARGS__); \ 128 124 } \ 129 125 /* TODO: find a way to pass xfail to test runner process. */ \ ··· 817 813 /* Sanity check handler execution environment. */ 818 814 if (!t) { 819 815 fprintf(TH_LOG_STREAM, 820 - "no active test in SIGALRM handler!?\n"); 816 + "# no active test in SIGALRM handler!?\n"); 821 817 abort(); 822 818 } 823 819 if (sig != SIGALRM || sig != info->si_signo) { 824 820 fprintf(TH_LOG_STREAM, 825 - "%s: SIGALRM handler caught signal %d!?\n", 821 + "# %s: SIGALRM handler caught signal %d!?\n", 826 822 t->name, sig != SIGALRM ? sig : info->si_signo); 827 823 abort(); 828 824 } ··· 843 839 if (sigaction(SIGALRM, &action, &saved_action)) { 844 840 t->passed = 0; 845 841 fprintf(TH_LOG_STREAM, 846 - "%s: unable to install SIGALRM handler\n", 842 + "# %s: unable to install SIGALRM handler\n", 847 843 t->name); 848 844 return; 849 845 } ··· 855 851 if (sigaction(SIGALRM, &saved_action, NULL)) { 856 852 t->passed = 0; 857 853 fprintf(TH_LOG_STREAM, 858 - "%s: unable to uninstall SIGALRM handler\n", 854 + "# %s: unable to uninstall SIGALRM handler\n", 859 855 t->name); 860 856 return; 861 857 } ··· 864 860 if (t->timed_out) { 865 861 t->passed = 0; 866 862 fprintf(TH_LOG_STREAM, 867 - "%s: Test terminated by timeout\n", t->name); 863 + "# %s: Test terminated by timeout\n", t->name); 868 864 } else if (WIFEXITED(status)) { 869 865 t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0; 870 866 if (t->termsig != -1) { 871 867 fprintf(TH_LOG_STREAM, 872 - "%s: Test exited normally " 873 - "instead of by signal (code: %d)\n", 868 + "# %s: Test exited normally instead of by signal (code: %d)\n", 874 869 t->name, 875 870 WEXITSTATUS(status)); 876 871 } else if (!t->passed) { 877 872 fprintf(TH_LOG_STREAM, 878 - "%s: Test failed at step #%d\n", 873 + "# %s: Test failed at step #%d\n", 879 874 t->name, 880 875 WEXITSTATUS(status)); 881 876 } ··· 882 879 t->passed = 0; 883 880 if (WTERMSIG(status) == SIGABRT) { 884 881 fprintf(TH_LOG_STREAM, 885 - "%s: Test terminated by assertion\n", 882 + "# %s: Test terminated by assertion\n", 886 883 t->name); 887 884 } else if (WTERMSIG(status) == t->termsig) { 888 885 t->passed = 1; 889 886 } else { 890 887 fprintf(TH_LOG_STREAM, 891 - "%s: Test terminated unexpectedly " 892 - "by signal %d\n", 888 + "# %s: Test terminated unexpectedly by signal %d\n", 893 889 t->name, 894 890 WTERMSIG(status)); 895 891 } 896 892 } else { 897 893 fprintf(TH_LOG_STREAM, 898 - "%s: Test ended in some other way [%u]\n", 894 + "# %s: Test ended in some other way [%u]\n", 899 895 t->name, 900 896 status); 901 897 } ··· 910 908 t->step = 0; 911 909 t->no_print = 0; 912 910 913 - printf("[ RUN ] %s%s%s.%s\n", 911 + ksft_print_msg(" RUN %s%s%s.%s ...\n", 914 912 f->name, variant->name[0] ? "." : "", variant->name, t->name); 915 913 t->pid = fork(); 916 914 if (t->pid < 0) { 917 - printf("ERROR SPAWNING TEST CHILD\n"); 915 + ksft_print_msg("ERROR SPAWNING TEST CHILD\n"); 918 916 t->passed = 0; 919 917 } else if (t->pid == 0) { 920 918 t->fn(t, variant); ··· 923 921 } else { 924 922 __wait_for_test(t); 925 923 } 926 - printf("[ %4s ] %s%s%s.%s\n", (t->passed ? "OK" : "FAIL"), 924 + ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL", 925 + f->name, variant->name[0] ? "." : "", variant->name, t->name); 926 + ksft_test_result(t->passed, "%s%s%s.%s\n", 927 927 f->name, variant->name[0] ? "." : "", variant->name, t->name); 928 928 } 929 929 ··· 949 945 } 950 946 } 951 947 952 - /* TODO(wad) add optional arguments similar to gtest. */ 953 - printf("[==========] Running %u tests from %u test cases.\n", 948 + ksft_print_header(); 949 + ksft_set_plan(test_count); 950 + ksft_print_msg("Starting %u tests from %u test cases.\n", 954 951 test_count, case_count); 955 952 for (f = __fixture_list; f; f = f->next) { 956 953 for (v = f->variant ?: &no_variant; v; v = v->next) { ··· 965 960 } 966 961 } 967 962 } 968 - printf("[==========] %u / %u tests passed.\n", pass_count, count); 969 - printf("[ %s ]\n", (ret ? "FAILED" : "PASSED")); 970 - return ret; 963 + ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED", 964 + pass_count, count); 965 + ksft_exit(ret == 0); 966 + 967 + /* unreachable */ 968 + return KSFT_FAIL; 971 969 } 972 970 973 971 static void __attribute__((constructor)) __constructor_order_first(void)