executor.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/reboot.h>
  3. #include <kunit/test.h>
  4. #include <linux/glob.h>
  5. #include <linux/moduleparam.h>
  6. /*
  7. * These symbols point to the .kunit_test_suites section and are defined in
  8. * include/asm-generic/vmlinux.lds.h, and consequently must be extern.
  9. */
  10. extern struct kunit_suite * const __kunit_suites_start[];
  11. extern struct kunit_suite * const __kunit_suites_end[];
  12. #if IS_BUILTIN(CONFIG_KUNIT)
  13. static char *filter_glob_param;
  14. static char *action_param;
  15. module_param_named(filter_glob, filter_glob_param, charp, 0);
  16. MODULE_PARM_DESC(filter_glob,
  17. "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
  18. module_param_named(action, action_param, charp, 0);
  19. MODULE_PARM_DESC(action,
  20. "Changes KUnit executor behavior, valid values are:\n"
  21. "<none>: run the tests like normal\n"
  22. "'list' to list test names instead of running them.\n");
  23. /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
  24. struct kunit_test_filter {
  25. char *suite_glob;
  26. char *test_glob;
  27. };
  28. /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
  29. static void kunit_parse_filter_glob(struct kunit_test_filter *parsed,
  30. const char *filter_glob)
  31. {
  32. const int len = strlen(filter_glob);
  33. const char *period = strchr(filter_glob, '.');
  34. if (!period) {
  35. parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
  36. parsed->test_glob = NULL;
  37. strcpy(parsed->suite_glob, filter_glob);
  38. return;
  39. }
  40. parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
  41. parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
  42. strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
  43. strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
  44. }
  45. /* Create a copy of suite with only tests that match test_glob. */
  46. static struct kunit_suite *
  47. kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
  48. {
  49. int n = 0;
  50. struct kunit_case *filtered, *test_case;
  51. struct kunit_suite *copy;
  52. kunit_suite_for_each_test_case(suite, test_case) {
  53. if (!test_glob || glob_match(test_glob, test_case->name))
  54. ++n;
  55. }
  56. if (n == 0)
  57. return NULL;
  58. copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
  59. if (!copy)
  60. return ERR_PTR(-ENOMEM);
  61. filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
  62. if (!filtered) {
  63. kfree(copy);
  64. return ERR_PTR(-ENOMEM);
  65. }
  66. n = 0;
  67. kunit_suite_for_each_test_case(suite, test_case) {
  68. if (!test_glob || glob_match(test_glob, test_case->name))
  69. filtered[n++] = *test_case;
  70. }
  71. copy->test_cases = filtered;
  72. return copy;
  73. }
  74. static char *kunit_shutdown;
  75. core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
  76. /* Stores an array of suites, end points one past the end */
  77. struct suite_set {
  78. struct kunit_suite * const *start;
  79. struct kunit_suite * const *end;
  80. };
  81. static void kunit_free_suite_set(struct suite_set suite_set)
  82. {
  83. struct kunit_suite * const *suites;
  84. for (suites = suite_set.start; suites < suite_set.end; suites++) {
  85. kfree((*suites)->test_cases);
  86. kfree(*suites);
  87. }
  88. kfree(suite_set.start);
  89. }
  90. static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
  91. const char *filter_glob,
  92. int *err)
  93. {
  94. int i;
  95. struct kunit_suite **copy, *filtered_suite;
  96. struct suite_set filtered;
  97. struct kunit_test_filter filter;
  98. const size_t max = suite_set->end - suite_set->start;
  99. copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
  100. filtered.start = copy;
  101. if (!copy) { /* won't be able to run anything, return an empty set */
  102. filtered.end = copy;
  103. return filtered;
  104. }
  105. kunit_parse_filter_glob(&filter, filter_glob);
  106. for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
  107. if (!glob_match(filter.suite_glob, suite_set->start[i]->name))
  108. continue;
  109. filtered_suite = kunit_filter_tests(suite_set->start[i], filter.test_glob);
  110. if (IS_ERR(filtered_suite)) {
  111. *err = PTR_ERR(filtered_suite);
  112. return filtered;
  113. }
  114. if (!filtered_suite)
  115. continue;
  116. *copy++ = filtered_suite;
  117. }
  118. filtered.end = copy;
  119. kfree(filter.suite_glob);
  120. kfree(filter.test_glob);
  121. return filtered;
  122. }
  123. static void kunit_handle_shutdown(void)
  124. {
  125. if (!kunit_shutdown)
  126. return;
  127. if (!strcmp(kunit_shutdown, "poweroff"))
  128. kernel_power_off();
  129. else if (!strcmp(kunit_shutdown, "halt"))
  130. kernel_halt();
  131. else if (!strcmp(kunit_shutdown, "reboot"))
  132. kernel_restart(NULL);
  133. }
  134. static void kunit_exec_run_tests(struct suite_set *suite_set)
  135. {
  136. size_t num_suites = suite_set->end - suite_set->start;
  137. pr_info("KTAP version 1\n");
  138. pr_info("1..%zu\n", num_suites);
  139. __kunit_test_suites_init(suite_set->start, num_suites);
  140. }
  141. static void kunit_exec_list_tests(struct suite_set *suite_set)
  142. {
  143. struct kunit_suite * const *suites;
  144. struct kunit_case *test_case;
  145. /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
  146. pr_info("KTAP version 1\n");
  147. for (suites = suite_set->start; suites < suite_set->end; suites++)
  148. kunit_suite_for_each_test_case((*suites), test_case) {
  149. pr_info("%s.%s\n", (*suites)->name, test_case->name);
  150. }
  151. }
  152. int kunit_run_all_tests(void)
  153. {
  154. struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
  155. int err = 0;
  156. if (!kunit_enabled()) {
  157. pr_info("kunit: disabled\n");
  158. goto out;
  159. }
  160. if (filter_glob_param) {
  161. suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err);
  162. if (err) {
  163. pr_err("kunit executor: error filtering suites: %d\n", err);
  164. goto out;
  165. }
  166. }
  167. if (!action_param)
  168. kunit_exec_run_tests(&suite_set);
  169. else if (strcmp(action_param, "list") == 0)
  170. kunit_exec_list_tests(&suite_set);
  171. else
  172. pr_err("kunit executor: unknown action '%s'\n", action_param);
  173. if (filter_glob_param) { /* a copy was made of each suite */
  174. kunit_free_suite_set(suite_set);
  175. }
  176. out:
  177. kunit_handle_shutdown();
  178. return err;
  179. }
  180. EXPORT_SYMBOL_KUNIT(kunit_run_all_tests);
  181. #if IS_BUILTIN(CONFIG_KUNIT_TEST)
  182. #include "executor_test.c"
  183. #endif
  184. #endif /* IS_BUILTIN(CONFIG_KUNIT) */