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

kselftest: create fixture objects

Grouping tests by fixture will allow us to parametrize
test runs. Create full objects for fixtures.

Add a "global" fixture for tests without a fixture.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jakub Kicinski and committed by
David S. Miller
142aca6b 1a89595c

+38 -13
+38 -13
tools/testing/selftests/kselftest_harness.h
··· 169 169 #define __TEST_IMPL(test_name, _signal) \ 170 170 static void test_name(struct __test_metadata *_metadata); \ 171 171 static struct __test_metadata _##test_name##_object = \ 172 - { .name = "global." #test_name, \ 173 - .fn = &test_name, .termsig = _signal, \ 172 + { .name = #test_name, \ 173 + .fn = &test_name, \ 174 + .fixture = &_fixture_global, \ 175 + .termsig = _signal, \ 174 176 .timeout = TEST_TIMEOUT_DEFAULT, }; \ 175 177 static void __attribute__((constructor)) _register_##test_name(void) \ 176 178 { \ ··· 214 212 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN(). 215 213 */ 216 214 #define FIXTURE(fixture_name) \ 215 + static struct __fixture_metadata _##fixture_name##_fixture_object = \ 216 + { .name = #fixture_name, }; \ 217 217 static void __attribute__((constructor)) \ 218 218 _register_##fixture_name##_data(void) \ 219 219 { \ 220 - __fixture_count++; \ 220 + __register_fixture(&_##fixture_name##_fixture_object); \ 221 221 } \ 222 222 FIXTURE_DATA(fixture_name) 223 223 ··· 313 309 } \ 314 310 static struct __test_metadata \ 315 311 _##fixture_name##_##test_name##_object = { \ 316 - .name = #fixture_name "." #test_name, \ 312 + .name = #test_name, \ 317 313 .fn = &wrapper_##fixture_name##_##test_name, \ 314 + .fixture = &_##fixture_name##_fixture_object, \ 318 315 .termsig = signal, \ 319 316 .timeout = tmout, \ 320 317 }; \ ··· 659 654 } \ 660 655 } 661 656 657 + /* Contains all the information about a fixture. */ 658 + struct __fixture_metadata { 659 + const char *name; 660 + struct __fixture_metadata *prev, *next; 661 + } _fixture_global __attribute__((unused)) = { 662 + .name = "global", 663 + .prev = &_fixture_global, 664 + }; 665 + 666 + static struct __fixture_metadata *__fixture_list = &_fixture_global; 667 + static unsigned int __fixture_count; 668 + static int __constructor_order; 669 + 670 + #define _CONSTRUCTOR_ORDER_FORWARD 1 671 + #define _CONSTRUCTOR_ORDER_BACKWARD -1 672 + 673 + static inline void __register_fixture(struct __fixture_metadata *f) 674 + { 675 + __fixture_count++; 676 + __LIST_APPEND(__fixture_list, f); 677 + } 678 + 662 679 /* Contains all the information for test execution and status checking. */ 663 680 struct __test_metadata { 664 681 const char *name; 665 682 void (*fn)(struct __test_metadata *); 666 683 pid_t pid; /* pid of test when being run */ 684 + struct __fixture_metadata *fixture; 667 685 int termsig; 668 686 int passed; 669 687 int trigger; /* extra handler after the evaluation */ ··· 700 672 /* Storage for the (global) tests to be run. */ 701 673 static struct __test_metadata *__test_list; 702 674 static unsigned int __test_count; 703 - static unsigned int __fixture_count; 704 - static int __constructor_order; 705 - 706 - #define _CONSTRUCTOR_ORDER_FORWARD 1 707 - #define _CONSTRUCTOR_ORDER_BACKWARD -1 708 675 709 676 /* 710 677 * Since constructors are called in reverse order, reverse the test ··· 819 796 } 820 797 } 821 798 822 - void __run_test(struct __test_metadata *t) 799 + void __run_test(struct __fixture_metadata *f, 800 + struct __test_metadata *t) 823 801 { 824 802 t->passed = 1; 825 803 t->trigger = 0; 826 - printf("[ RUN ] %s\n", t->name); 804 + printf("[ RUN ] %s.%s\n", f->name, t->name); 827 805 t->pid = fork(); 828 806 if (t->pid < 0) { 829 807 printf("ERROR SPAWNING TEST CHILD\n"); ··· 836 812 } else { 837 813 __wait_for_test(t); 838 814 } 839 - printf("[ %4s ] %s\n", (t->passed ? "OK" : "FAIL"), t->name); 815 + printf("[ %4s ] %s.%s\n", (t->passed ? "OK" : "FAIL"), 816 + f->name, t->name); 840 817 } 841 818 842 819 static int test_harness_run(int __attribute__((unused)) argc, ··· 853 828 __test_count, __fixture_count + 1); 854 829 for (t = __test_list; t; t = t->next) { 855 830 count++; 856 - __run_test(t); 831 + __run_test(t->fixture, t); 857 832 if (t->passed) 858 833 pass_count++; 859 834 else