1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/queue.h>
15 #ifdef RTE_LIB_CMDLINE
16 #include <cmdline_rdline.h>
17 #include <cmdline_parse.h>
18 #include <cmdline_socket.h>
20 extern cmdline_parse_ctx_t main_ctx[];
23 #include <rte_memory.h>
25 #include <rte_cycles.h>
27 #include <rte_string_fns.h>
29 #include <rte_timer.h>
34 #include "test_pdump.h"
37 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
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 suite->unit_test_cases[iter].testcase_with_data; \
43 iter++, case = suite->unit_test_cases[iter])
45 #define FOR_EACH_SUITE_TESTSUITE(iter, suite, sub_ts) \
46 for (iter = 0, sub_ts = suite->unit_test_suites ? \
47 suite->unit_test_suites[0]:NULL; sub_ts && \
48 suite->unit_test_suites[iter]->suite_name != NULL; \
49 iter++, sub_ts = suite->unit_test_suites[iter])
51 const char *prgname; /* to be set to argv[0] */
53 static const char *recursive_call; /* used in linux for MP and other tests */
56 no_action(void){ return 0; }
59 do_recursive_call(void)
64 int (*action_fn)(void);
66 { "run_secondary_instances", test_mp_secondary },
69 { "run_pdump_server_tests", test_pdump },
72 { "test_missing_c_flag", no_action },
73 { "test_main_lcore_flag", no_action },
74 { "test_invalid_n_flag", no_action },
75 { "test_no_hpet_flag", no_action },
76 { "test_allow_flag", no_action },
77 { "test_invalid_b_flag", no_action },
78 { "test_invalid_vdev_flag", no_action },
79 { "test_invalid_r_flag", no_action },
80 { "test_misc_flags", no_action },
81 { "test_memory_flags", no_action },
82 { "test_file_prefix", no_action },
83 { "test_no_huge_flag", no_action },
85 { "timer_secondary_spawn_wait", test_timer_secondary },
89 if (recursive_call == NULL)
91 for (i = 0; i < RTE_DIM(actions); i++) {
92 if (strcmp(actions[i].env_var, recursive_call) == 0)
93 return (actions[i].action_fn)();
95 printf("ERROR - missing action to take for %s\n", recursive_call);
101 #define MAX_EXTRA_ARGS 32
104 main(int argc, char **argv)
106 #ifdef RTE_LIB_CMDLINE
108 char *tests[argc]; /* store an array of tests to run */
115 extra_args = getenv("DPDK_TEST_PARAMS");
116 if (extra_args != NULL && strlen(extra_args) > 0) {
118 char *eargv[MAX_EXTRA_ARGS];
123 RTE_LOG(INFO, APP, "Using additional DPDK_TEST_PARAMS: '%s'\n",
125 eargc = rte_strsplit(extra_args, strlen(extra_args),
126 eargv, MAX_EXTRA_ARGS, ' ');
128 /* merge argc/argv and the environment args */
129 all_argc = argc + eargc;
130 all_argv = malloc(sizeof(*all_argv) * (all_argc + 1));
131 if (all_argv == NULL) {
136 for (i = 0; i < argc; i++)
137 all_argv[i] = argv[i];
138 for (i = 0; i < eargc; i++)
139 all_argv[argc + i] = eargv[i];
140 all_argv[all_argc] = NULL;
142 /* call eal_init with combined args */
143 ret = rte_eal_init(all_argc, all_argv);
146 ret = rte_eal_init(argc, argv);
158 ret = rte_timer_subsystem_init();
159 if (ret < 0 && ret != -EALREADY) {
165 if (commands_init() < 0) {
170 recursive_call = getenv(RECURSIVE_ENV_VAR);
171 if (recursive_call != NULL) {
172 ret = do_recursive_call();
176 #ifdef RTE_LIBEAL_USE_HPET
177 if (rte_eal_hpet_init(1) < 0)
180 "HPET is not enabled, using TSC as default timer\n");
183 #ifdef RTE_LIB_CMDLINE
184 char *dpdk_test = getenv("DPDK_TEST");
186 if (dpdk_test && strlen(dpdk_test) == 0)
189 if (dpdk_test && !command_valid(dpdk_test)) {
190 RTE_LOG(WARNING, APP, "Invalid DPDK_TEST value '%s'\n", dpdk_test);
195 tests[test_count++] = dpdk_test;
196 for (i = 1; i < argc; i++) {
197 if (!command_valid(argv[i]))
198 RTE_LOG(WARNING, APP, "Invalid test requested: '%s'\n", argv[i]);
200 tests[test_count++] = argv[i];
203 if (test_count > 0) {
206 cl = cmdline_new(main_ctx, "RTE>>", 0, 1);
212 for (i = 0; i < test_count; i++) {
213 snprintf(buf, sizeof(buf), "%s\n", tests[i]);
214 if (cmdline_in(cl, buf, strlen(buf)) < 0) {
215 printf("error on cmdline input\n");
219 ret = last_test_result;
227 /* if no DPDK_TEST env variable, go interactive */
228 cl = cmdline_stdin_new(main_ctx, "RTE>>");
234 cmdline_interact(cl);
235 cmdline_stdin_exit(cl);
242 rte_timer_subsystem_finalize();
249 unit_test_suite_count_tcs_on_setup_fail(struct unit_test_suite *suite,
250 int test_success, unsigned int *sub_ts_failed,
251 unsigned int *sub_ts_skipped, unsigned int *sub_ts_total)
253 struct unit_test_case tc;
254 struct unit_test_suite *ts;
257 FOR_EACH_SUITE_TESTSUITE(i, suite, ts) {
258 unit_test_suite_count_tcs_on_setup_fail(
259 ts, test_success, sub_ts_failed,
260 sub_ts_skipped, sub_ts_total);
261 suite->total += ts->total;
262 suite->failed += ts->failed;
263 suite->skipped += ts->skipped;
270 FOR_EACH_SUITE_TESTCASE(i, suite, tc) {
272 if (!tc.enabled || test_success == TEST_SKIPPED)
280 unit_test_suite_reset_counts(struct unit_test_suite *suite)
282 struct unit_test_suite *ts;
285 FOR_EACH_SUITE_TESTSUITE(i, suite, ts)
286 unit_test_suite_reset_counts(ts);
289 suite->succeeded = 0;
292 suite->unsupported = 0;
296 unit_test_suite_runner(struct unit_test_suite *suite)
298 int test_success, i, ret;
300 struct unit_test_case tc;
301 struct unit_test_suite *ts;
302 unsigned int sub_ts_succeeded = 0, sub_ts_failed = 0;
303 unsigned int sub_ts_skipped = 0, sub_ts_total = 0;
305 unit_test_suite_reset_counts(suite);
307 if (suite->suite_name) {
308 printf(" + ------------------------------------------------------- +\n");
309 printf(" + Test Suite : %s\n", suite->suite_name);
313 test_success = suite->setup();
314 if (test_success != 0) {
316 * setup did not pass, so count all enabled tests and
317 * mark them as failed/skipped
319 unit_test_suite_count_tcs_on_setup_fail(suite,
320 test_success, &sub_ts_failed,
321 &sub_ts_skipped, &sub_ts_total);
326 printf(" + ------------------------------------------------------- +\n");
328 FOR_EACH_SUITE_TESTCASE(suite->total, suite, tc) {
336 /* run test case setup */
338 test_success = tc.setup();
340 test_success = TEST_SUCCESS;
342 if (test_success == TEST_SUCCESS) {
343 /* run the test case */
345 test_success = tc.testcase();
346 else if (tc.testcase_with_data)
347 test_success = tc.testcase_with_data(tc.data);
349 test_success = -ENOTSUP;
351 if (test_success == TEST_SUCCESS)
353 else if (test_success == TEST_SKIPPED)
355 else if (test_success == -ENOTSUP)
356 suite->unsupported++;
359 } else if (test_success == -ENOTSUP) {
360 suite->unsupported++;
365 /* run the test case teardown */
369 if (test_success == TEST_SUCCESS)
370 status = "succeeded";
371 else if (test_success == TEST_SKIPPED)
373 else if (test_success == -ENOTSUP)
374 status = "unsupported";
378 printf(" + TestCase [%2d] : %s %s\n", suite->total,
381 FOR_EACH_SUITE_TESTSUITE(i, suite, ts) {
382 ret = unit_test_suite_runner(ts);
383 if (ret == TEST_SUCCESS)
385 else if (ret == TEST_SKIPPED)
391 suite->total += ts->total;
392 suite->succeeded += ts->succeeded;
393 suite->failed += ts->failed;
394 suite->skipped += ts->skipped;
395 suite->unsupported += ts->unsupported;
396 suite->executed += ts->executed;
399 /* Run test suite teardown */
406 printf(" + ------------------------------------------------------- +\n");
407 printf(" + Test Suite Summary : %s\n", suite->suite_name);
408 printf(" + ------------------------------------------------------- +\n");
410 FOR_EACH_SUITE_TESTSUITE(i, suite, ts)
411 printf(" + %s : %d/%d passed, %d/%d skipped, "
412 "%d/%d failed, %d/%d unsupported\n", ts->suite_name,
413 ts->succeeded, ts->total, ts->skipped, ts->total,
414 ts->failed, ts->total, ts->unsupported, ts->total);
416 if (suite->unit_test_suites) {
417 printf(" + ------------------------------------------------------- +\n");
418 printf(" + Sub Testsuites Total : %2d\n", sub_ts_total);
419 printf(" + Sub Testsuites Skipped : %2d\n", sub_ts_skipped);
420 printf(" + Sub Testsuites Passed : %2d\n", sub_ts_succeeded);
421 printf(" + Sub Testsuites Failed : %2d\n", sub_ts_failed);
422 printf(" + ------------------------------------------------------- +\n");
425 printf(" + Tests Total : %2d\n", suite->total);
426 printf(" + Tests Skipped : %2d\n", suite->skipped);
427 printf(" + Tests Executed : %2d\n", suite->executed);
428 printf(" + Tests Unsupported: %2d\n", suite->unsupported);
429 printf(" + Tests Passed : %2d\n", suite->succeeded);
430 printf(" + Tests Failed : %2d\n", suite->failed);
431 printf(" + ------------------------------------------------------- +\n");
433 last_test_result = suite->failed;
437 if (suite->total == suite->skipped)