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