test: refactor unit test suite runner
[dpdk.git] / app / test / test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <termios.h>
12 #include <ctype.h>
13 #include <sys/queue.h>
14
15 #ifdef RTE_LIB_CMDLINE
16 #include <cmdline_rdline.h>
17 #include <cmdline_parse.h>
18 #include <cmdline_socket.h>
19 #include <cmdline.h>
20 extern cmdline_parse_ctx_t main_ctx[];
21 #endif
22
23 #include <rte_memory.h>
24 #include <rte_eal.h>
25 #include <rte_cycles.h>
26 #include <rte_log.h>
27 #include <rte_string_fns.h>
28 #ifdef RTE_LIB_TIMER
29 #include <rte_timer.h>
30 #endif
31
32 #include "test.h"
33 #ifdef RTE_LIB_PDUMP
34 #include "test_pdump.h"
35 #endif
36
37 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
38
39 #define FOR_EACH_SUITE_TESTCASE(iter, suite, case)                      \
40         for (iter = 0, case = suite->unit_test_cases[0];                \
41                 suite->unit_test_cases[iter].testcase;                  \
42                 iter++, case = suite->unit_test_cases[iter])
43
44 const char *prgname; /* to be set to argv[0] */
45
46 static const char *recursive_call; /* used in linux for MP and other tests */
47
48 static int
49 no_action(void){ return 0; }
50
51 static int
52 do_recursive_call(void)
53 {
54         unsigned i;
55         struct {
56                 const char *env_var;
57                 int (*action_fn)(void);
58         } actions[] =  {
59                         { "run_secondary_instances", test_mp_secondary },
60 #ifdef RTE_LIB_PDUMP
61 #ifdef RTE_NET_RING
62                         { "run_pdump_server_tests", test_pdump },
63 #endif
64 #endif
65                         { "test_missing_c_flag", no_action },
66                         { "test_main_lcore_flag", no_action },
67                         { "test_invalid_n_flag", no_action },
68                         { "test_no_hpet_flag", no_action },
69                         { "test_allow_flag", no_action },
70                         { "test_invalid_b_flag", no_action },
71                         { "test_invalid_vdev_flag", no_action },
72                         { "test_invalid_r_flag", no_action },
73                         { "test_misc_flags", no_action },
74                         { "test_memory_flags", no_action },
75                         { "test_file_prefix", no_action },
76                         { "test_no_huge_flag", no_action },
77 #ifdef RTE_LIB_TIMER
78                         { "timer_secondary_spawn_wait", test_timer_secondary },
79 #endif
80         };
81
82         if (recursive_call == NULL)
83                 return -1;
84         for (i = 0; i < RTE_DIM(actions); i++) {
85                 if (strcmp(actions[i].env_var, recursive_call) == 0)
86                         return (actions[i].action_fn)();
87         }
88         printf("ERROR - missing action to take for %s\n", recursive_call);
89         return -1;
90 }
91
92 int last_test_result;
93
94 #define MAX_EXTRA_ARGS 32
95
96 int
97 main(int argc, char **argv)
98 {
99 #ifdef RTE_LIB_CMDLINE
100         struct cmdline *cl;
101         char *tests[argc]; /* store an array of tests to run */
102         int test_count = 0;
103         int i;
104 #endif
105         char *extra_args;
106         int ret;
107
108         extra_args = getenv("DPDK_TEST_PARAMS");
109         if (extra_args != NULL && strlen(extra_args) > 0) {
110                 char **all_argv;
111                 char *eargv[MAX_EXTRA_ARGS];
112                 int all_argc;
113                 int eargc;
114                 int i;
115
116                 RTE_LOG(INFO, APP, "Using additional DPDK_TEST_PARAMS: '%s'\n",
117                                 extra_args);
118                 eargc = rte_strsplit(extra_args, strlen(extra_args),
119                                 eargv, MAX_EXTRA_ARGS, ' ');
120
121                 /* merge argc/argv and the environment args */
122                 all_argc = argc + eargc;
123                 all_argv = malloc(sizeof(*all_argv) * (all_argc + 1));
124                 if (all_argv == NULL) {
125                         ret = -1;
126                         goto out;
127                 }
128
129                 for (i = 0; i < argc; i++)
130                         all_argv[i] = argv[i];
131                 for (i = 0; i < eargc; i++)
132                         all_argv[argc + i] = eargv[i];
133                 all_argv[all_argc] = NULL;
134
135                 /* call eal_init with combined args */
136                 ret = rte_eal_init(all_argc, all_argv);
137                 free(all_argv);
138         } else
139                 ret = rte_eal_init(argc, argv);
140         if (ret < 0) {
141                 ret = -1;
142                 goto out;
143         }
144
145         argv += ret;
146         argc -= ret;
147
148         prgname = argv[0];
149
150 #ifdef RTE_LIB_TIMER
151         ret = rte_timer_subsystem_init();
152         if (ret < 0 && ret != -EALREADY) {
153                 ret = -1;
154                 goto out;
155         }
156 #endif
157
158         if (commands_init() < 0) {
159                 ret = -1;
160                 goto out;
161         }
162
163         recursive_call = getenv(RECURSIVE_ENV_VAR);
164         if (recursive_call != NULL) {
165                 ret = do_recursive_call();
166                 goto out;
167         }
168
169 #ifdef RTE_LIBEAL_USE_HPET
170         if (rte_eal_hpet_init(1) < 0)
171 #endif
172                 RTE_LOG(INFO, APP,
173                                 "HPET is not enabled, using TSC as default timer\n");
174
175
176 #ifdef RTE_LIB_CMDLINE
177         char *dpdk_test = getenv("DPDK_TEST");
178
179         if (dpdk_test && strlen(dpdk_test) == 0)
180                 dpdk_test = NULL;
181
182         if (dpdk_test && !command_valid(dpdk_test)) {
183                 RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test);
184                 dpdk_test = NULL;
185         }
186
187         if (dpdk_test)
188                 tests[test_count++] = dpdk_test;
189         for (i = 1; i < argc; i++) {
190                 if (!command_valid(argv[i]))
191                         RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]);
192                 else
193                         tests[test_count++] = argv[i];
194         }
195
196         if (test_count > 0) {
197                 char buf[1024];
198
199                 cl = cmdline_new(main_ctx, "RTE>>", 0, 1);
200                 if (cl == NULL) {
201                         ret = -1;
202                         goto out;
203                 }
204
205                 for (i = 0; i < test_count; i++) {
206                         snprintf(buf, sizeof(buf), "%s\n", tests[i]);
207                         if (cmdline_in(cl, buf, strlen(buf)) < 0) {
208                                 printf("error on cmdline input\n");
209
210                                 ret = -1;
211                         } else
212                                 ret = last_test_result;
213
214                         if (ret != 0)
215                                 break;
216                 }
217                 cmdline_free(cl);
218                 goto out;
219         } else {
220                 /* if no DPDK_TEST env variable, go interactive */
221                 cl = cmdline_stdin_new(main_ctx, "RTE>>");
222                 if (cl == NULL) {
223                         ret = -1;
224                         goto out;
225                 }
226
227                 cmdline_interact(cl);
228                 cmdline_stdin_exit(cl);
229                 cmdline_free(cl);
230         }
231 #endif
232         ret = 0;
233
234 out:
235 #ifdef RTE_LIB_TIMER
236         rte_timer_subsystem_finalize();
237 #endif
238         rte_eal_cleanup();
239         return ret;
240 }
241
242 static void
243 unit_test_suite_count_tcs_on_setup_fail(struct unit_test_suite *suite,
244                 int test_success)
245 {
246         struct unit_test_case tc;
247         int i;
248
249         FOR_EACH_SUITE_TESTCASE(i, suite, tc) {
250                 suite->total++;
251                 if (!tc.enabled || test_success == TEST_SKIPPED)
252                         suite->skipped++;
253                 else
254                         suite->failed++;
255         }
256 }
257
258 static void
259 unit_test_suite_reset_counts(struct unit_test_suite *suite)
260 {
261         suite->total = 0;
262         suite->executed = 0;
263         suite->succeeded = 0;
264         suite->skipped = 0;
265         suite->failed = 0;
266         suite->unsupported = 0;
267 }
268
269 int
270 unit_test_suite_runner(struct unit_test_suite *suite)
271 {
272         int test_success;
273         const char *status;
274         struct unit_test_case tc;
275
276         unit_test_suite_reset_counts(suite);
277
278         if (suite->suite_name) {
279                 printf(" + ------------------------------------------------------- +\n");
280                 printf(" + Test Suite : %s\n", suite->suite_name);
281         }
282
283         if (suite->setup) {
284                 test_success = suite->setup();
285                 if (test_success != 0) {
286                         /*
287                          * setup did not pass, so count all enabled tests and
288                          * mark them as failed/skipped
289                          */
290                         unit_test_suite_count_tcs_on_setup_fail(suite,
291                                         test_success);
292                         goto suite_summary;
293                 }
294         }
295
296         printf(" + ------------------------------------------------------- +\n");
297
298         FOR_EACH_SUITE_TESTCASE(suite->total, suite, tc) {
299                 if (!tc.enabled) {
300                         suite->skipped++;
301                         continue;
302                 } else {
303                         suite->executed++;
304                 }
305
306                 /* run test case setup */
307                 if (tc.setup)
308                         test_success = tc.setup();
309                 else
310                         test_success = TEST_SUCCESS;
311
312                 if (test_success == TEST_SUCCESS) {
313                         /* run the test case */
314                         test_success = tc.testcase();
315                         if (test_success == TEST_SUCCESS)
316                                 suite->succeeded++;
317                         else if (test_success == TEST_SKIPPED)
318                                 suite->skipped++;
319                         else if (test_success == -ENOTSUP)
320                                 suite->unsupported++;
321                         else
322                                 suite->failed++;
323                 } else if (test_success == -ENOTSUP) {
324                         suite->unsupported++;
325                 } else {
326                         suite->failed++;
327                 }
328
329                 /* run the test case teardown */
330                 if (tc.teardown)
331                         tc.teardown();
332
333                 if (test_success == TEST_SUCCESS)
334                         status = "succeeded";
335                 else if (test_success == TEST_SKIPPED)
336                         status = "skipped";
337                 else if (test_success == -ENOTSUP)
338                         status = "unsupported";
339                 else
340                         status = "failed";
341
342                 printf(" + TestCase [%2d] : %s %s\n", suite->total,
343                                 tc.name, status);
344         }
345
346         /* Run test suite teardown */
347         if (suite->teardown)
348                 suite->teardown();
349
350         goto suite_summary;
351
352 suite_summary:
353         printf(" + ------------------------------------------------------- +\n");
354         printf(" + Test Suite Summary : %s\n", suite->suite_name);
355         printf(" + Tests Total :       %2d\n", suite->total);
356         printf(" + Tests Skipped :     %2d\n", suite->skipped);
357         printf(" + Tests Executed :    %2d\n", suite->executed);
358         printf(" + Tests Unsupported:  %2d\n", suite->unsupported);
359         printf(" + Tests Passed :      %2d\n", suite->succeeded);
360         printf(" + Tests Failed :      %2d\n", suite->failed);
361         printf(" + ------------------------------------------------------- +\n");
362
363         last_test_result = suite->failed;
364
365         if (suite->failed)
366                 return TEST_FAILED;
367         if (suite->total == suite->skipped)
368                 return TEST_SKIPPED;
369         return TEST_SUCCESS;
370 }