xref: /dpdk/app/test/test.c (revision 8809f78c7dd9f33a44a4f89c58fc91ded34296ed)
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 const char *prgname; /* to be set to argv[0] */
40 
41 static const char *recursive_call; /* used in linux for MP and other tests */
42 
43 static int
44 no_action(void){ return 0; }
45 
46 static int
47 do_recursive_call(void)
48 {
49 	unsigned i;
50 	struct {
51 		const char *env_var;
52 		int (*action_fn)(void);
53 	} actions[] =  {
54 			{ "run_secondary_instances", test_mp_secondary },
55 #ifdef RTE_LIB_PDUMP
56 #ifdef RTE_NET_RING
57 			{ "run_pdump_server_tests", test_pdump },
58 #endif
59 #endif
60 			{ "test_missing_c_flag", no_action },
61 			{ "test_main_lcore_flag", no_action },
62 			{ "test_invalid_n_flag", no_action },
63 			{ "test_no_hpet_flag", no_action },
64 			{ "test_whitelist_flag", no_action },
65 			{ "test_invalid_b_flag", no_action },
66 			{ "test_invalid_vdev_flag", no_action },
67 			{ "test_invalid_r_flag", no_action },
68 			{ "test_misc_flags", no_action },
69 			{ "test_memory_flags", no_action },
70 			{ "test_file_prefix", no_action },
71 			{ "test_no_huge_flag", no_action },
72 #ifdef RTE_LIB_TIMER
73 			{ "timer_secondary_spawn_wait", test_timer_secondary },
74 #endif
75 	};
76 
77 	if (recursive_call == NULL)
78 		return -1;
79 	for (i = 0; i < RTE_DIM(actions); i++) {
80 		if (strcmp(actions[i].env_var, recursive_call) == 0)
81 			return (actions[i].action_fn)();
82 	}
83 	printf("ERROR - missing action to take for %s\n", recursive_call);
84 	return -1;
85 }
86 
87 int last_test_result;
88 
89 #define MAX_EXTRA_ARGS 32
90 
91 int
92 main(int argc, char **argv)
93 {
94 #ifdef RTE_LIB_CMDLINE
95 	struct cmdline *cl;
96 #endif
97 	char *extra_args;
98 	int ret;
99 
100 	extra_args = getenv("DPDK_TEST_PARAMS");
101 	if (extra_args != NULL && strlen(extra_args) > 0) {
102 		char **all_argv;
103 		char *eargv[MAX_EXTRA_ARGS];
104 		int all_argc;
105 		int eargc;
106 		int i;
107 
108 		RTE_LOG(INFO, APP, "Using additional DPDK_TEST_PARAMS: '%s'\n",
109 				extra_args);
110 		eargc = rte_strsplit(extra_args, strlen(extra_args),
111 				eargv, MAX_EXTRA_ARGS, ' ');
112 
113 		/* merge argc/argv and the environment args */
114 		all_argc = argc + eargc;
115 		all_argv = malloc(sizeof(*all_argv) * (all_argc + 1));
116 		if (all_argv == NULL) {
117 			ret = -1;
118 			goto out;
119 		}
120 
121 		for (i = 0; i < argc; i++)
122 			all_argv[i] = argv[i];
123 		for (i = 0; i < eargc; i++)
124 			all_argv[argc + i] = eargv[i];
125 		all_argv[all_argc] = NULL;
126 
127 		/* call eal_init with combined args */
128 		ret = rte_eal_init(all_argc, all_argv);
129 		free(all_argv);
130 	} else
131 		ret = rte_eal_init(argc, argv);
132 	if (ret < 0) {
133 		ret = -1;
134 		goto out;
135 	}
136 
137 #ifdef RTE_LIB_TIMER
138 	if (rte_timer_subsystem_init() < 0) {
139 		ret = -1;
140 		goto out;
141 	}
142 #endif
143 
144 	if (commands_init() < 0) {
145 		ret = -1;
146 		goto out;
147 	}
148 
149 	argv += ret;
150 
151 	prgname = argv[0];
152 
153 	recursive_call = getenv(RECURSIVE_ENV_VAR);
154 	if (recursive_call != NULL) {
155 		ret = do_recursive_call();
156 		goto out;
157 	}
158 
159 #ifdef RTE_LIBEAL_USE_HPET
160 	if (rte_eal_hpet_init(1) < 0)
161 #endif
162 		RTE_LOG(INFO, APP,
163 				"HPET is not enabled, using TSC as default timer\n");
164 
165 
166 #ifdef RTE_LIB_CMDLINE
167 	cl = cmdline_stdin_new(main_ctx, "RTE>>");
168 	if (cl == NULL) {
169 		ret = -1;
170 		goto out;
171 	}
172 
173 	char *dpdk_test = getenv("DPDK_TEST");
174 	if (dpdk_test && strlen(dpdk_test)) {
175 		char buf[1024];
176 		snprintf(buf, sizeof(buf), "%s\n", dpdk_test);
177 		if (cmdline_in(cl, buf, strlen(buf)) < 0) {
178 			printf("error on cmdline input\n");
179 			ret = -1;
180 			goto out;
181 		}
182 
183 		cmdline_stdin_exit(cl);
184 		ret = last_test_result;
185 		goto out;
186 	}
187 	/* if no DPDK_TEST env variable, go interactive */
188 	cmdline_interact(cl);
189 	cmdline_stdin_exit(cl);
190 #endif
191 	ret = 0;
192 
193 out:
194 #ifdef RTE_LIB_TIMER
195 	rte_timer_subsystem_finalize();
196 #endif
197 	rte_eal_cleanup();
198 	return ret;
199 }
200 
201 
202 int
203 unit_test_suite_runner(struct unit_test_suite *suite)
204 {
205 	int test_success;
206 	unsigned int total = 0, executed = 0, skipped = 0;
207 	unsigned int succeeded = 0, failed = 0, unsupported = 0;
208 	const char *status;
209 
210 	if (suite->suite_name) {
211 		printf(" + ------------------------------------------------------- +\n");
212 		printf(" + Test Suite : %s\n", suite->suite_name);
213 	}
214 
215 	if (suite->setup) {
216 		test_success = suite->setup();
217 		if (test_success != 0) {
218 			/*
219 			 * setup did not pass, so count all enabled tests and
220 			 * mark them as failed/skipped
221 			 */
222 			while (suite->unit_test_cases[total].testcase) {
223 				if (!suite->unit_test_cases[total].enabled ||
224 				    test_success == TEST_SKIPPED)
225 					skipped++;
226 				else
227 					failed++;
228 				total++;
229 			}
230 			goto suite_summary;
231 		}
232 	}
233 
234 	printf(" + ------------------------------------------------------- +\n");
235 
236 	while (suite->unit_test_cases[total].testcase) {
237 		if (!suite->unit_test_cases[total].enabled) {
238 			skipped++;
239 			total++;
240 			continue;
241 		} else {
242 			executed++;
243 		}
244 
245 		/* run test case setup */
246 		if (suite->unit_test_cases[total].setup)
247 			test_success = suite->unit_test_cases[total].setup();
248 		else
249 			test_success = TEST_SUCCESS;
250 
251 		if (test_success == TEST_SUCCESS) {
252 			/* run the test case */
253 			test_success = suite->unit_test_cases[total].testcase();
254 			if (test_success == TEST_SUCCESS)
255 				succeeded++;
256 			else if (test_success == TEST_SKIPPED)
257 				skipped++;
258 			else if (test_success == -ENOTSUP)
259 				unsupported++;
260 			else
261 				failed++;
262 		} else if (test_success == -ENOTSUP) {
263 			unsupported++;
264 		} else {
265 			failed++;
266 		}
267 
268 		/* run the test case teardown */
269 		if (suite->unit_test_cases[total].teardown)
270 			suite->unit_test_cases[total].teardown();
271 
272 		if (test_success == TEST_SUCCESS)
273 			status = "succeeded";
274 		else if (test_success == TEST_SKIPPED)
275 			status = "skipped";
276 		else if (test_success == -ENOTSUP)
277 			status = "unsupported";
278 		else
279 			status = "failed";
280 
281 		printf(" + TestCase [%2d] : %s %s\n", total,
282 				suite->unit_test_cases[total].name, status);
283 
284 		total++;
285 	}
286 
287 	/* Run test suite teardown */
288 	if (suite->teardown)
289 		suite->teardown();
290 
291 	goto suite_summary;
292 
293 suite_summary:
294 	printf(" + ------------------------------------------------------- +\n");
295 	printf(" + Test Suite Summary \n");
296 	printf(" + Tests Total :       %2d\n", total);
297 	printf(" + Tests Skipped :     %2d\n", skipped);
298 	printf(" + Tests Executed :    %2d\n", executed);
299 	printf(" + Tests Unsupported:  %2d\n", unsupported);
300 	printf(" + Tests Passed :      %2d\n", succeeded);
301 	printf(" + Tests Failed :      %2d\n", failed);
302 	printf(" + ------------------------------------------------------- +\n");
303 
304 	last_test_result = failed;
305 
306 	if (failed)
307 		return TEST_FAILED;
308 	if (total == skipped)
309 		return TEST_SKIPPED;
310 	return TEST_SUCCESS;
311 }
312