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