Branch data Line data Source code
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 <ctype.h>
12 : : #include <sys/queue.h>
13 : :
14 : : #include <cmdline_rdline.h>
15 : : #include <cmdline_parse.h>
16 : : #include <cmdline_socket.h>
17 : : #include <cmdline.h>
18 : : extern cmdline_parse_ctx_t main_ctx[];
19 : :
20 : : #include <rte_memory.h>
21 : : #include <rte_eal.h>
22 : : #include <rte_cycles.h>
23 : : #include <rte_log.h>
24 : : #include <rte_string_fns.h>
25 : : #ifdef RTE_LIB_TIMER
26 : : #include <rte_timer.h>
27 : : #endif
28 : :
29 : : #include "test.h"
30 : : #ifdef RTE_LIB_PDUMP
31 : : #include "test_pdump.h"
32 : : #endif
33 : :
34 : : #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
35 : :
36 : : #define FOR_EACH_SUITE_TESTCASE(iter, suite, case) \
37 : : for (iter = 0, case = suite->unit_test_cases[0]; \
38 : : suite->unit_test_cases[iter].testcase || \
39 : : suite->unit_test_cases[iter].testcase_with_data; \
40 : : iter++, case = suite->unit_test_cases[iter])
41 : :
42 : : #define FOR_EACH_SUITE_TESTSUITE(iter, suite, sub_ts) \
43 : : for (iter = 0, sub_ts = suite->unit_test_suites ? \
44 : : suite->unit_test_suites[0]:NULL; sub_ts && \
45 : : suite->unit_test_suites[iter]->suite_name != NULL; \
46 : : iter++, sub_ts = suite->unit_test_suites[iter])
47 : :
48 : : const char *prgname; /* to be set to argv[0] */
49 : :
50 : : static const char *recursive_call; /* used in linux for MP and other tests */
51 : :
52 : : static int
53 : 62 : no_action(void){ return 0; }
54 : :
55 : : static int
56 : 69 : do_recursive_call(void)
57 : : {
58 : : unsigned i;
59 : : struct {
60 : : const char *env_var;
61 : : int (*action_fn)(void);
62 : 69 : } actions[] = {
63 : : #ifndef RTE_EXEC_ENV_WINDOWS
64 : : { "run_secondary_instances", test_mp_secondary },
65 : : #endif
66 : : #ifdef RTE_LIB_PDUMP
67 : : #ifdef RTE_NET_RING
68 : : { "run_pdump_server_tests", test_pdump },
69 : : #endif
70 : : #endif
71 : : { "test_missing_c_flag", no_action },
72 : : { "test_main_lcore_flag", no_action },
73 : : { "test_invalid_n_flag", no_action },
74 : : { "test_no_hpet_flag", no_action },
75 : : { "test_allow_flag", no_action },
76 : : { "test_invalid_b_flag", no_action },
77 : : { "test_invalid_vdev_flag", no_action },
78 : : { "test_invalid_r_flag", no_action },
79 : : { "test_misc_flags", no_action },
80 : : { "test_memory_flags", no_action },
81 : : { "test_file_prefix", no_action },
82 : : { "test_no_huge_flag", no_action },
83 : : { "test_pagesz_mem_flags", no_action },
84 : : { "test_panic", test_panic },
85 : : { "test_exit", test_exit },
86 : : #ifdef RTE_LIB_TIMER
87 : : #ifndef RTE_EXEC_ENV_WINDOWS
88 : : { "timer_secondary_spawn_wait", test_timer_secondary },
89 : : #endif
90 : : #endif
91 : : };
92 : :
93 [ + - ]: 69 : if (recursive_call == NULL)
94 : : return -1;
95 [ + - ]: 712 : for (i = 0; i < RTE_DIM(actions); i++) {
96 [ + + ]: 712 : if (strcmp(actions[i].env_var, recursive_call) == 0) {
97 : : printf("Calling recursive action for %s\n", recursive_call);
98 : 69 : int ret = actions[i].action_fn();
99 : 64 : printf("Returned from recursive action for %s with %d\n",
100 : : recursive_call, ret);
101 : 64 : return ret;
102 : : }
103 : : }
104 : : printf("ERROR - missing action to take for %s\n", recursive_call);
105 : 0 : return -1;
106 : : }
107 : :
108 : : int last_test_result;
109 : :
110 : : #define MAX_EXTRA_ARGS 32
111 : :
112 : : int
113 : 305 : main(int argc, char **argv)
114 : : {
115 : : struct cmdline *cl;
116 : 305 : char **tests = alloca(sizeof(char *) * argc); /* store an array of tests to run */
117 : : int test_count = 0;
118 : : int i;
119 : : char *extra_args;
120 : : int ret;
121 : :
122 : 305 : extra_args = getenv("DPDK_TEST_PARAMS");
123 [ - + - - ]: 305 : if (extra_args != NULL && strlen(extra_args) > 0) {
124 : : char **all_argv;
125 : : char *eargv[MAX_EXTRA_ARGS];
126 : : int all_argc;
127 : : int eargc;
128 : : int i;
129 : :
130 : 0 : RTE_LOG(INFO, APP, "Using additional DPDK_TEST_PARAMS: '%s'\n",
131 : : extra_args);
132 : 0 : eargc = rte_strsplit(extra_args, strlen(extra_args),
133 : : eargv, MAX_EXTRA_ARGS, ' ');
134 : :
135 : : /* merge argc/argv and the environment args */
136 : 0 : all_argc = argc + eargc;
137 : 0 : all_argv = malloc(sizeof(*all_argv) * (all_argc + 1));
138 [ # # ]: 0 : if (all_argv == NULL) {
139 : : ret = -1;
140 : 0 : goto out;
141 : : }
142 : :
143 [ # # ]: 0 : for (i = 0; i < argc; i++)
144 : 0 : all_argv[i] = argv[i];
145 [ # # ]: 0 : for (i = 0; i < eargc; i++)
146 : 0 : all_argv[argc + i] = eargv[i];
147 : 0 : all_argv[all_argc] = NULL;
148 : :
149 : : /* call eal_init with combined args */
150 : 0 : ret = rte_eal_init(all_argc, all_argv);
151 : 0 : free(all_argv);
152 : : } else
153 : 305 : ret = rte_eal_init(argc, argv);
154 [ + + ]: 298 : if (ret < 0) {
155 : : printf("Error with EAL initialization, ret = %d\n", ret);
156 : : ret = -1;
157 : 74 : goto out;
158 : : }
159 : :
160 : 224 : argv += ret;
161 : 224 : argc -= ret;
162 : :
163 : 224 : prgname = argv[0];
164 : :
165 [ - + ]: 224 : if (commands_init() < 0) {
166 : : ret = -1;
167 : 0 : goto out;
168 : : }
169 : :
170 : 224 : recursive_call = getenv(RECURSIVE_ENV_VAR);
171 [ + + ]: 224 : if (recursive_call != NULL) {
172 : 69 : ret = do_recursive_call();
173 : 64 : goto out;
174 : : }
175 : :
176 : : #ifdef RTE_LIBEAL_USE_HPET
177 : : if (rte_eal_hpet_init(1) < 0)
178 : : #endif
179 : 155 : RTE_LOG(INFO, APP,
180 : : "HPET is not enabled, using TSC as default timer\n");
181 : :
182 : :
183 : 155 : char *dpdk_test = getenv("DPDK_TEST");
184 : :
185 [ + + + - ]: 155 : if (dpdk_test && strlen(dpdk_test) > 0)
186 : 154 : tests[test_count++] = dpdk_test;
187 [ - + ]: 155 : for (i = 1; i < argc; i++)
188 : 0 : tests[test_count++] = argv[i];
189 : :
190 [ + + ]: 155 : if (test_count > 0) {
191 : : char buf[1024];
192 : 154 : char *dpdk_test_skip = getenv("DPDK_TEST_SKIP");
193 : 154 : char *skip_tests[128] = {0};
194 : : size_t n_skip_tests = 0;
195 : :
196 [ - + - - ]: 154 : if (dpdk_test_skip != NULL && strlen(dpdk_test_skip) > 0) {
197 : : int split_ret;
198 : 0 : char *dpdk_test_skip_cp = strdup(dpdk_test_skip);
199 [ # # ]: 0 : if (dpdk_test_skip_cp == NULL) {
200 : : ret = -1;
201 : 0 : goto out;
202 : : }
203 : : dpdk_test_skip = dpdk_test_skip_cp;
204 : 0 : split_ret = rte_strsplit(dpdk_test_skip, strlen(dpdk_test_skip),
205 : : skip_tests, RTE_DIM(skip_tests), ',');
206 [ # # ]: 0 : if (split_ret > 0)
207 : 0 : n_skip_tests = split_ret;
208 : : else
209 : 0 : free(dpdk_test_skip);
210 : : }
211 : :
212 : 154 : cl = cmdline_new(main_ctx, "RTE>>", 0, 1);
213 [ - + ]: 154 : if (cl == NULL) {
214 : : ret = -1;
215 : 0 : goto out;
216 : : }
217 : :
218 [ + + ]: 308 : for (i = 0; i < test_count; i++) {
219 : : /* check if test is to be skipped */
220 [ - + ]: 154 : for (size_t j = 0; j < n_skip_tests; j++) {
221 [ # # ]: 0 : if (strcmp(tests[i], skip_tests[j]) == 0) {
222 : 0 : fprintf(stderr, "Skipping %s [DPDK_TEST_SKIP]\n", tests[i]);
223 : : ret = TEST_SKIPPED;
224 : 0 : goto end_of_cmd;
225 : : }
226 : : }
227 : :
228 : 154 : snprintf(buf, sizeof(buf), "%s\n", tests[i]);
229 [ - + ]: 154 : if (cmdline_parse_check(cl, buf) < 0) {
230 : : printf("Error: invalid test command: '%s'\n", tests[i]);
231 : : ret = -1;
232 [ - + ]: 154 : } else if (cmdline_in(cl, buf, strlen(buf)) < 0) {
233 : : printf("error on cmdline input\n");
234 : : ret = -1;
235 : : } else
236 : 154 : ret = last_test_result;
237 : :
238 : 154 : end_of_cmd:
239 [ + - ]: 154 : if (ret != 0 && ret != TEST_SKIPPED)
240 : : break;
241 : : }
242 [ - + ]: 154 : if (n_skip_tests > 0)
243 : 0 : free(dpdk_test_skip);
244 : :
245 : 154 : cmdline_free(cl);
246 : 154 : goto out;
247 : : } else {
248 : : /* if no DPDK_TEST env variable, go interactive */
249 : 1 : cl = cmdline_stdin_new(main_ctx, "RTE>>");
250 [ - + ]: 1 : if (cl == NULL) {
251 : : ret = -1;
252 : 0 : goto out;
253 : : }
254 : :
255 : 1 : cmdline_interact(cl);
256 : 1 : cmdline_stdin_exit(cl);
257 : : }
258 : : ret = 0;
259 : :
260 : 293 : out:
261 [ + + ]: 293 : if (recursive_call != NULL)
262 : 64 : printf("Cleaning up %s recursive instance\n", argv[0]);
263 : : #ifdef RTE_LIB_TIMER
264 : 293 : rte_timer_subsystem_finalize();
265 : : #endif
266 : 293 : rte_eal_cleanup();
267 [ + + ]: 293 : if (recursive_call != NULL)
268 : 64 : printf("%s recursive instance returning %d\n", argv[0], ret);
269 : : return ret;
270 : : }
271 : :
272 : : static void
273 : 38 : unit_test_suite_count_tcs_on_setup_fail(struct unit_test_suite *suite,
274 : : int test_success, unsigned int *sub_ts_failed,
275 : : unsigned int *sub_ts_skipped, unsigned int *sub_ts_total)
276 : : {
277 : : struct unit_test_case tc;
278 : : struct unit_test_suite *ts;
279 : : int i;
280 : :
281 [ + + + + : 82 : FOR_EACH_SUITE_TESTSUITE(i, suite, ts) {
+ - ]
282 : 6 : unit_test_suite_count_tcs_on_setup_fail(
283 : : ts, test_success, sub_ts_failed,
284 : : sub_ts_skipped, sub_ts_total);
285 : 6 : suite->total += ts->total;
286 : 6 : suite->failed += ts->failed;
287 : 6 : suite->skipped += ts->skipped;
288 [ - + ]: 6 : if (ts->failed)
289 : 0 : (*sub_ts_failed)++;
290 : : else
291 : 6 : (*sub_ts_skipped)++;
292 : 6 : (*sub_ts_total)++;
293 : : }
294 [ + + + + ]: 877 : FOR_EACH_SUITE_TESTCASE(i, suite, tc) {
295 : 839 : suite->total++;
296 [ + - + - ]: 839 : if (!tc.enabled || test_success == TEST_SKIPPED)
297 : 839 : suite->skipped++;
298 : : else
299 : 0 : suite->failed++;
300 : : }
301 : 38 : }
302 : :
303 : : static void
304 : 163 : unit_test_suite_reset_counts(struct unit_test_suite *suite)
305 : : {
306 : : struct unit_test_suite *ts;
307 : : int i;
308 : :
309 [ + + + + : 387 : FOR_EACH_SUITE_TESTSUITE(i, suite, ts)
+ + ]
310 : 61 : unit_test_suite_reset_counts(ts);
311 : 163 : suite->total = 0;
312 : 163 : suite->executed = 0;
313 : 163 : suite->succeeded = 0;
314 : 163 : suite->skipped = 0;
315 : 163 : suite->failed = 0;
316 : 163 : suite->unsupported = 0;
317 : 163 : }
318 : :
319 : : int
320 : 102 : unit_test_suite_runner(struct unit_test_suite *suite)
321 : : {
322 : : int test_success, i, ret;
323 : : const char *status;
324 : : struct unit_test_case tc;
325 : : struct unit_test_suite *ts;
326 : 102 : unsigned int sub_ts_succeeded = 0, sub_ts_failed = 0;
327 : 102 : unsigned int sub_ts_skipped = 0, sub_ts_total = 0;
328 : :
329 : 102 : unit_test_suite_reset_counts(suite);
330 : :
331 [ + - ]: 102 : if (suite->suite_name) {
332 : : printf(" + ------------------------------------------------------- +\n");
333 : 102 : printf(" + Test Suite : %s\n", suite->suite_name);
334 : : }
335 : :
336 [ + + ]: 102 : if (suite->setup) {
337 : 78 : test_success = suite->setup();
338 [ + + ]: 78 : if (test_success != 0) {
339 : : /*
340 : : * setup did not pass, so count all enabled tests and
341 : : * mark them as failed/skipped
342 : : */
343 : 32 : unit_test_suite_count_tcs_on_setup_fail(suite,
344 : : test_success, &sub_ts_failed,
345 : : &sub_ts_skipped, &sub_ts_total);
346 : 32 : goto suite_summary;
347 : : }
348 : : }
349 : :
350 : : printf(" + ------------------------------------------------------- +\n");
351 : :
352 [ + + + + ]: 1095 : FOR_EACH_SUITE_TESTCASE(suite->total, suite, tc) {
353 [ - + ]: 1025 : if (!tc.enabled) {
354 : 0 : suite->skipped++;
355 : 0 : continue;
356 : : } else {
357 : 1025 : suite->executed++;
358 : : }
359 : :
360 : : /* run test case setup */
361 [ + + ]: 1025 : if (tc.setup)
362 : 606 : test_success = tc.setup();
363 : : else
364 : : test_success = TEST_SUCCESS;
365 : :
366 [ + + ]: 606 : if (test_success == TEST_SUCCESS) {
367 : : /* run the test case */
368 [ + + ]: 1021 : if (tc.testcase)
369 : 743 : test_success = tc.testcase();
370 [ + - ]: 278 : else if (tc.testcase_with_data)
371 : 278 : test_success = tc.testcase_with_data(tc.data);
372 : : else
373 : : test_success = -ENOTSUP;
374 : :
375 [ + + ]: 1021 : if (test_success == TEST_SUCCESS)
376 : 839 : suite->succeeded++;
377 [ + - ]: 182 : else if (test_success == TEST_SKIPPED) {
378 : 182 : suite->skipped++;
379 : 182 : suite->executed--;
380 [ # # ]: 0 : } else if (test_success == -ENOTSUP) {
381 : 0 : suite->unsupported++;
382 : 0 : suite->executed--;
383 : : } else
384 : 0 : suite->failed++;
385 [ - + ]: 4 : } else if (test_success == -ENOTSUP) {
386 : 0 : suite->unsupported++;
387 [ + - ]: 4 : } else if (test_success == TEST_SKIPPED) {
388 : 4 : suite->skipped++;
389 : : } else {
390 : 0 : suite->failed++;
391 : : }
392 : :
393 : : /* run the test case teardown */
394 [ + + ]: 1025 : if (tc.teardown)
395 : 567 : tc.teardown();
396 : :
397 [ + + ]: 1025 : if (test_success == TEST_SUCCESS)
398 : : status = "succeeded";
399 [ - + ]: 186 : else if (test_success == TEST_SKIPPED)
400 : : status = "skipped";
401 [ # # ]: 0 : else if (test_success == -ENOTSUP)
402 : : status = "unsupported";
403 : : else
404 : : status = "failed";
405 : :
406 : 1025 : printf(" + TestCase [%2d] : %s %s\n", suite->total,
407 : : tc.name, status);
408 : : }
409 [ + + + + : 195 : FOR_EACH_SUITE_TESTSUITE(i, suite, ts) {
+ + ]
410 : 55 : ret = unit_test_suite_runner(ts);
411 [ + + ]: 55 : if (ret == TEST_SUCCESS)
412 : 25 : sub_ts_succeeded++;
413 [ + - ]: 30 : else if (ret == TEST_SKIPPED)
414 : 30 : sub_ts_skipped++;
415 : : else
416 : 0 : sub_ts_failed++;
417 : 55 : sub_ts_total++;
418 : :
419 : 55 : suite->total += ts->total;
420 : 55 : suite->succeeded += ts->succeeded;
421 : 55 : suite->failed += ts->failed;
422 : 55 : suite->skipped += ts->skipped;
423 : 55 : suite->unsupported += ts->unsupported;
424 : 55 : suite->executed += ts->executed;
425 : : }
426 : :
427 : : /* Run test suite teardown */
428 [ + + ]: 70 : if (suite->teardown)
429 : 21 : suite->teardown();
430 : :
431 : 70 : goto suite_summary;
432 : :
433 : 102 : suite_summary:
434 : : printf(" + ------------------------------------------------------- +\n");
435 : 102 : printf(" + Test Suite Summary : %s\n", suite->suite_name);
436 : : printf(" + ------------------------------------------------------- +\n");
437 : :
438 [ + + + + : 265 : FOR_EACH_SUITE_TESTSUITE(i, suite, ts)
+ + ]
439 : 61 : printf(" + %s : %d/%d passed, %d/%d skipped, "
440 : : "%d/%d failed, %d/%d unsupported\n", ts->suite_name,
441 : : ts->succeeded, ts->total, ts->skipped, ts->total,
442 : : ts->failed, ts->total, ts->unsupported, ts->total);
443 : :
444 [ + + ]: 102 : if (suite->unit_test_suites) {
445 : : printf(" + ------------------------------------------------------- +\n");
446 : 3 : printf(" + Sub Testsuites Total : %2d\n", sub_ts_total);
447 : 3 : printf(" + Sub Testsuites Skipped : %2d\n", sub_ts_skipped);
448 : : printf(" + Sub Testsuites Passed : %2d\n", sub_ts_succeeded);
449 : 3 : printf(" + Sub Testsuites Failed : %2d\n", sub_ts_failed);
450 : : printf(" + ------------------------------------------------------- +\n");
451 : : }
452 : :
453 : 102 : printf(" + Tests Total : %2d\n", suite->total);
454 : 102 : printf(" + Tests Skipped : %2d\n", suite->skipped);
455 : 102 : printf(" + Tests Executed : %2d\n", suite->executed);
456 : 102 : printf(" + Tests Unsupported: %2d\n", suite->unsupported);
457 : 102 : printf(" + Tests Passed : %2d\n", suite->succeeded);
458 : 102 : printf(" + Tests Failed : %2d\n", suite->failed);
459 : : printf(" + ------------------------------------------------------- +\n");
460 : :
461 : 102 : last_test_result = suite->failed;
462 : :
463 [ + - ]: 102 : if (suite->failed)
464 : : return TEST_FAILED;
465 [ + + ]: 102 : if (suite->total == suite->skipped)
466 : 34 : return TEST_SKIPPED;
467 : : return TEST_SUCCESS;
468 : : }
|