52b84b
From 9beae637a919326ddc072c4dd53cb66e80273c8f Mon Sep 17 00:00:00 2001
52b84b
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
52b84b
Date: Fri, 15 Mar 2019 13:42:55 +0100
52b84b
Subject: [PATCH] test-execute: allow filtering test cases by pattern
52b84b
52b84b
When debugging failure in one of the cases, it's annoying to have to wade
52b84b
through the output from all the other cases. Let's allow picking select
52b84b
cases.
52b84b
52b84b
(cherry picked from commit 9efb96315ae502dabeb94ab35816ea8955563b7a)
52b84b
52b84b
Related: #1737283
52b84b
---
52b84b
 src/test/test-execute.c | 104 ++++++++++++++++++++++------------------
52b84b
 1 file changed, 58 insertions(+), 46 deletions(-)
52b84b
52b84b
diff --git a/src/test/test-execute.c b/src/test/test-execute.c
52b84b
index d88de52d2a..d077674efc 100644
52b84b
--- a/src/test/test-execute.c
52b84b
+++ b/src/test/test-execute.c
52b84b
@@ -659,8 +659,15 @@ static void test_exec_standardoutput_append(Manager *m) {
52b84b
         test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
52b84b
 }
52b84b
 
52b84b
-static int run_tests(UnitFileScope scope, const test_function_t *tests) {
52b84b
-        const test_function_t *test = NULL;
52b84b
+typedef struct test_entry {
52b84b
+        test_function_t f;
52b84b
+        const char *name;
52b84b
+} test_entry;
52b84b
+
52b84b
+#define entry(x) {x, #x}
52b84b
+
52b84b
+static int run_tests(UnitFileScope scope, const test_entry tests[], char **patterns) {
52b84b
+        const test_entry *test = NULL;
52b84b
         _cleanup_(manager_freep) Manager *m = NULL;
52b84b
         int r;
52b84b
 
52b84b
@@ -674,55 +681,60 @@ static int run_tests(UnitFileScope scope, const test_function_t *tests) {
52b84b
         assert_se(r >= 0);
52b84b
         assert_se(manager_startup(m, NULL, NULL) >= 0);
52b84b
 
52b84b
-        for (test = tests; test && *test; test++)
52b84b
-                (*test)(m);
52b84b
+        for (test = tests; test && test->f; test++)
52b84b
+                if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE))
52b84b
+                        test->f(m);
52b84b
+                else
52b84b
+                        log_info("Skipping %s because it does not match any pattern.", test->name);
52b84b
 
52b84b
         return 0;
52b84b
 }
52b84b
 
52b84b
+
52b84b
 int main(int argc, char *argv[]) {
52b84b
         _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
52b84b
-        static const test_function_t user_tests[] = {
52b84b
-                test_exec_basic,
52b84b
-                test_exec_ambientcapabilities,
52b84b
-                test_exec_bindpaths,
52b84b
-                test_exec_capabilityboundingset,
52b84b
-                test_exec_cpuaffinity,
52b84b
-                test_exec_environment,
52b84b
-                test_exec_environmentfile,
52b84b
-                test_exec_group,
52b84b
-                test_exec_ignoresigpipe,
52b84b
-                test_exec_inaccessiblepaths,
52b84b
-                test_exec_ioschedulingclass,
52b84b
-                test_exec_oomscoreadjust,
52b84b
-                test_exec_passenvironment,
52b84b
-                test_exec_personality,
52b84b
-                test_exec_privatedevices,
52b84b
-                test_exec_privatenetwork,
52b84b
-                test_exec_privatetmp,
52b84b
-                test_exec_protectkernelmodules,
52b84b
-                test_exec_readonlypaths,
52b84b
-                test_exec_readwritepaths,
52b84b
-                test_exec_restrictnamespaces,
52b84b
-                test_exec_runtimedirectory,
52b84b
-                test_exec_standardinput,
52b84b
-                test_exec_standardoutput,
52b84b
-                test_exec_standardoutput_append,
52b84b
-                test_exec_supplementarygroups,
52b84b
-                test_exec_systemcallerrornumber,
52b84b
-                test_exec_systemcallfilter,
52b84b
-                test_exec_temporaryfilesystem,
52b84b
-                test_exec_umask,
52b84b
-                test_exec_unsetenvironment,
52b84b
-                test_exec_user,
52b84b
-                test_exec_workingdirectory,
52b84b
-                NULL,
52b84b
+        _cleanup_free_ char *test_execute_path = NULL;
52b84b
+        static const test_entry user_tests[] = {
52b84b
+                entry(test_exec_basic),
52b84b
+                entry(test_exec_ambientcapabilities),
52b84b
+                entry(test_exec_bindpaths),
52b84b
+                entry(test_exec_capabilityboundingset),
52b84b
+                entry(test_exec_cpuaffinity),
52b84b
+                entry(test_exec_environment),
52b84b
+                entry(test_exec_environmentfile),
52b84b
+                entry(test_exec_group),
52b84b
+                entry(test_exec_ignoresigpipe),
52b84b
+                entry(test_exec_inaccessiblepaths),
52b84b
+                entry(test_exec_ioschedulingclass),
52b84b
+                entry(test_exec_oomscoreadjust),
52b84b
+                entry(test_exec_passenvironment),
52b84b
+                entry(test_exec_personality),
52b84b
+                entry(test_exec_privatedevices),
52b84b
+                entry(test_exec_privatenetwork),
52b84b
+                entry(test_exec_privatetmp),
52b84b
+                entry(test_exec_protectkernelmodules),
52b84b
+                entry(test_exec_readonlypaths),
52b84b
+                entry(test_exec_readwritepaths),
52b84b
+                entry(test_exec_restrictnamespaces),
52b84b
+                entry(test_exec_runtimedirectory),
52b84b
+                entry(test_exec_standardinput),
52b84b
+                entry(test_exec_standardoutput),
52b84b
+                entry(test_exec_standardoutput_append),
52b84b
+                entry(test_exec_supplementarygroups),
52b84b
+                entry(test_exec_systemcallerrornumber),
52b84b
+                entry(test_exec_systemcallfilter),
52b84b
+                entry(test_exec_temporaryfilesystem),
52b84b
+                entry(test_exec_umask),
52b84b
+                entry(test_exec_unsetenvironment),
52b84b
+                entry(test_exec_user),
52b84b
+                entry(test_exec_workingdirectory),
52b84b
+                {},
52b84b
         };
52b84b
-        static const test_function_t system_tests[] = {
52b84b
-                test_exec_dynamicuser,
52b84b
-                test_exec_specifier,
52b84b
-                test_exec_systemcallfilter_system,
52b84b
-                NULL,
52b84b
+        static const test_entry system_tests[] = {
52b84b
+                entry(test_exec_dynamicuser),
52b84b
+                entry(test_exec_specifier),
52b84b
+                entry(test_exec_systemcallfilter_system),
52b84b
+                {},
52b84b
         };
52b84b
         int r;
52b84b
 
52b84b
@@ -759,9 +771,9 @@ int main(int argc, char *argv[]) {
52b84b
         assert_se(unsetenv("VAR2") == 0);
52b84b
         assert_se(unsetenv("VAR3") == 0);
52b84b
 
52b84b
-        r = run_tests(UNIT_FILE_USER, user_tests);
52b84b
+        r = run_tests(UNIT_FILE_USER, user_tests, argv + 1);
52b84b
         if (r != 0)
52b84b
                 return r;
52b84b
 
52b84b
-        return run_tests(UNIT_FILE_SYSTEM, system_tests);
52b84b
+        return run_tests(UNIT_FILE_SYSTEM, system_tests, argv + 1);
52b84b
 }