]>
git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/testsuite.c
10 #include <sys/prctl.h>
13 #include "testsuite.h"
15 static const char *progname
;
16 static int oneshot
= 0;
17 static const char options_short
[] = "lhn";
18 static const struct option options
[] = {
19 { "list", no_argument
, 0, 'l' },
20 { "help", no_argument
, 0, 'h' },
24 static void help(void)
26 const struct option
*itr
;
27 const char *itr_short
;
30 "\t%s [options] <test>\n"
31 "Options:\n", basename(progname
));
33 for (itr
= options
, itr_short
= options_short
;
34 itr
->name
!= NULL
; itr
++, itr_short
++)
35 printf("\t-%c, --%s\n", *itr_short
, itr
->name
);
38 static void test_list(const struct test
*tests
[])
42 printf("Available tests:\n");
43 for (i
= 0; tests
[i
] != NULL
; i
++)
44 printf("\t%s, %s\n", tests
[i
]->name
, tests
[i
]->description
);
47 int test_init(int argc
, char *const argv
[], const struct test
*tests
[])
53 c
= getopt_long(argc
, argv
, options_short
, options
, &idx
);
69 ERR("unexpected getopt_long() value %c\n", c
);
77 const struct test
*test_find(const struct test
*tests
[], const char *name
)
81 for (i
= 0; tests
[i
] != NULL
; i
++) {
82 if (strcmp(tests
[i
]->name
, name
) == 0)
89 static int test_spawn_test(const struct test
*t
)
91 const char *const args
[] = { progname
, "-n", t
->name
, NULL
};
93 execv(progname
, (char *const *) args
);
95 ERR("failed to spawn %s for %s: %m\n", progname
, t
->name
);
99 static int test_run_spawned(const struct test
*t
)
101 int err
= t
->func(t
);
107 int test_spawn_prog(const char *prog
, const char *args
[])
109 execv(prog
, (char *const *) args
);
111 ERR("failed to spawn %s ", prog
);
118 int test_run(const struct test
*t
)
123 if (t
->need_spawn
&& oneshot
)
126 LOG("running %s, in forked context\n", t
->name
);
130 ERR("could not fork(): %m\n");
131 LOG("FAILED: %s\n", t
->name
);
139 ERR("error waitpid(): %m\n");
142 } while (!WIFEXITED(err
) && !WIFSIGNALED(err
));
145 ERR("error while running %s\n", t
->name
);
147 LOG("%s: %s\n", err
== 0 ? "PASSED" : "FAILED", t
->name
);
151 /* kill child if parent dies */
152 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
155 return test_spawn_test(t
);
157 return test_run_spawned(t
);