|
3 | 3 | */
|
4 | 4 | #include "test_progs.h"
|
5 | 5 | #include "bpf_rlimit.h"
|
| 6 | +#include <argp.h> |
6 | 7 |
|
7 | 8 | int error_cnt, pass_cnt;
|
8 | 9 | bool jit_enabled;
|
@@ -156,22 +157,89 @@ void *spin_lock_thread(void *arg)
|
156 | 157 | pthread_exit(arg);
|
157 | 158 | }
|
158 | 159 |
|
159 |
| -#define DECLARE |
| 160 | +/* extern declarations for test funcs */ |
| 161 | +#define DEFINE_TEST(name) extern void test_##name(); |
160 | 162 | #include <prog_tests/tests.h>
|
161 |
| -#undef DECLARE |
| 163 | +#undef DEFINE_TEST |
162 | 164 |
|
163 |
| -int main(int ac, char **av) |
| 165 | +struct prog_test_def { |
| 166 | + const char *test_name; |
| 167 | + void (*run_test)(void); |
| 168 | +}; |
| 169 | + |
| 170 | +static struct prog_test_def prog_test_defs[] = { |
| 171 | +#define DEFINE_TEST(name) { \ |
| 172 | + .test_name = #name, \ |
| 173 | + .run_test = &test_##name, \ |
| 174 | +}, |
| 175 | +#include <prog_tests/tests.h> |
| 176 | +#undef DEFINE_TEST |
| 177 | +}; |
| 178 | + |
| 179 | +const char *argp_program_version = "test_progs 0.1"; |
| 180 | +const char *argp_program_bug_address = "<[email protected]>"; |
| 181 | +const char argp_program_doc[] = "BPF selftests test runner"; |
| 182 | + |
| 183 | +enum ARG_KEYS { |
| 184 | + ARG_VERIFIER_STATS = 's', |
| 185 | +}; |
| 186 | + |
| 187 | +static const struct argp_option opts[] = { |
| 188 | + { "verifier-stats", ARG_VERIFIER_STATS, NULL, 0, |
| 189 | + "Output verifier statistics", }, |
| 190 | + {}, |
| 191 | +}; |
| 192 | + |
| 193 | +struct test_env { |
| 194 | + bool verifier_stats; |
| 195 | +}; |
| 196 | + |
| 197 | +static struct test_env env = {}; |
| 198 | + |
| 199 | +static error_t parse_arg(int key, char *arg, struct argp_state *state) |
164 | 200 | {
|
| 201 | + struct test_env *env = state->input; |
| 202 | + |
| 203 | + switch (key) { |
| 204 | + case ARG_VERIFIER_STATS: |
| 205 | + env->verifier_stats = true; |
| 206 | + break; |
| 207 | + case ARGP_KEY_ARG: |
| 208 | + argp_usage(state); |
| 209 | + break; |
| 210 | + case ARGP_KEY_END: |
| 211 | + break; |
| 212 | + default: |
| 213 | + return ARGP_ERR_UNKNOWN; |
| 214 | + } |
| 215 | + return 0; |
| 216 | +} |
| 217 | + |
| 218 | + |
| 219 | +int main(int argc, char **argv) |
| 220 | +{ |
| 221 | + static const struct argp argp = { |
| 222 | + .options = opts, |
| 223 | + .parser = parse_arg, |
| 224 | + .doc = argp_program_doc, |
| 225 | + }; |
| 226 | + const struct prog_test_def *def; |
| 227 | + int err, i; |
| 228 | + |
| 229 | + err = argp_parse(&argp, argc, argv, 0, NULL, &env); |
| 230 | + if (err) |
| 231 | + return err; |
| 232 | + |
165 | 233 | srand(time(NULL));
|
166 | 234 |
|
167 | 235 | jit_enabled = is_jit_enabled();
|
168 | 236 |
|
169 |
| - if (ac == 2 && strcmp(av[1], "-s") == 0) |
170 |
| - verifier_stats = true; |
| 237 | + verifier_stats = env.verifier_stats; |
171 | 238 |
|
172 |
| -#define CALL |
173 |
| -#include <prog_tests/tests.h> |
174 |
| -#undef CALL |
| 239 | + for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) { |
| 240 | + def = &prog_test_defs[i]; |
| 241 | + def->run_test(); |
| 242 | + } |
175 | 243 |
|
176 | 244 | printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
|
177 | 245 | return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
0 commit comments