1 /*
2 * test-run-command.c: test run command API.
3 *
4 * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
5 *
6 * This code is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include "test-tool.h"
12 #include "git-compat-util.h"
13 #include "cache.h"
14 #include "run-command.h"
15 #include "strvec.h"
16 #include "strbuf.h"
17 #include "parse-options.h"
18 #include "string-list.h"
19 #include "thread-utils.h"
20 #include "wildmatch.h"
21 #include "gettext.h"
22 #include "parse-options.h"
23
24 static int number_callbacks;
parallel_next(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)25 static int parallel_next(struct child_process *cp,
26 struct strbuf *err,
27 void *cb,
28 void **task_cb)
29 {
30 struct child_process *d = cb;
31 if (number_callbacks >= 4)
32 return 0;
33
34 strvec_pushv(&cp->args, d->argv);
35 strbuf_addstr(err, "preloaded output of a child\n");
36 number_callbacks++;
37 return 1;
38 }
39
no_job(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)40 static int no_job(struct child_process *cp,
41 struct strbuf *err,
42 void *cb,
43 void **task_cb)
44 {
45 strbuf_addstr(err, "no further jobs available\n");
46 return 0;
47 }
48
task_finished(int result,struct strbuf * err,void * pp_cb,void * pp_task_cb)49 static int task_finished(int result,
50 struct strbuf *err,
51 void *pp_cb,
52 void *pp_task_cb)
53 {
54 strbuf_addstr(err, "asking for a quick stop\n");
55 return 1;
56 }
57
58 struct testsuite {
59 struct string_list tests, failed;
60 int next;
61 int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
62 };
63 #define TESTSUITE_INIT { \
64 .tests = STRING_LIST_INIT_DUP, \
65 .failed = STRING_LIST_INIT_DUP, \
66 }
67
next_test(struct child_process * cp,struct strbuf * err,void * cb,void ** task_cb)68 static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
69 void **task_cb)
70 {
71 struct testsuite *suite = cb;
72 const char *test;
73 if (suite->next >= suite->tests.nr)
74 return 0;
75
76 test = suite->tests.items[suite->next++].string;
77 strvec_pushl(&cp->args, "sh", test, NULL);
78 if (suite->quiet)
79 strvec_push(&cp->args, "--quiet");
80 if (suite->immediate)
81 strvec_push(&cp->args, "-i");
82 if (suite->verbose)
83 strvec_push(&cp->args, "-v");
84 if (suite->verbose_log)
85 strvec_push(&cp->args, "-V");
86 if (suite->trace)
87 strvec_push(&cp->args, "-x");
88 if (suite->write_junit_xml)
89 strvec_push(&cp->args, "--write-junit-xml");
90
91 strbuf_addf(err, "Output of '%s':\n", test);
92 *task_cb = (void *)test;
93
94 return 1;
95 }
96
test_finished(int result,struct strbuf * err,void * cb,void * task_cb)97 static int test_finished(int result, struct strbuf *err, void *cb,
98 void *task_cb)
99 {
100 struct testsuite *suite = cb;
101 const char *name = (const char *)task_cb;
102
103 if (result)
104 string_list_append(&suite->failed, name);
105
106 strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
107
108 return 0;
109 }
110
test_failed(struct strbuf * out,void * cb,void * task_cb)111 static int test_failed(struct strbuf *out, void *cb, void *task_cb)
112 {
113 struct testsuite *suite = cb;
114 const char *name = (const char *)task_cb;
115
116 string_list_append(&suite->failed, name);
117 strbuf_addf(out, "FAILED TO START: '%s'\n", name);
118
119 return 0;
120 }
121
122 static const char * const testsuite_usage[] = {
123 "test-run-command testsuite [<options>] [<pattern>...]",
124 NULL
125 };
126
testsuite(int argc,const char ** argv)127 static int testsuite(int argc, const char **argv)
128 {
129 struct testsuite suite = TESTSUITE_INIT;
130 int max_jobs = 1, i, ret;
131 DIR *dir;
132 struct dirent *d;
133 struct option options[] = {
134 OPT_BOOL('i', "immediate", &suite.immediate,
135 "stop at first failed test case(s)"),
136 OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
137 OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
138 OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
139 OPT_BOOL('V', "verbose-log", &suite.verbose_log,
140 "be verbose, redirected to a file"),
141 OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
142 OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
143 "write JUnit-style XML files"),
144 OPT_END()
145 };
146
147 argc = parse_options(argc, argv, NULL, options,
148 testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
149
150 if (max_jobs <= 0)
151 max_jobs = online_cpus();
152
153 dir = opendir(".");
154 if (!dir)
155 die("Could not open the current directory");
156 while ((d = readdir(dir))) {
157 const char *p = d->d_name;
158
159 if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
160 !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
161 !ends_with(p, ".sh"))
162 continue;
163
164 /* No pattern: match all */
165 if (!argc) {
166 string_list_append(&suite.tests, p);
167 continue;
168 }
169
170 for (i = 0; i < argc; i++)
171 if (!wildmatch(argv[i], p, 0)) {
172 string_list_append(&suite.tests, p);
173 break;
174 }
175 }
176 closedir(dir);
177
178 if (!suite.tests.nr)
179 die("No tests match!");
180 if (max_jobs > suite.tests.nr)
181 max_jobs = suite.tests.nr;
182
183 fprintf(stderr, "Running %d tests (%d at a time)\n",
184 suite.tests.nr, max_jobs);
185
186 ret = run_processes_parallel(max_jobs, next_test, test_failed,
187 test_finished, &suite);
188
189 if (suite.failed.nr > 0) {
190 ret = 1;
191 fprintf(stderr, "%d tests failed:\n\n", suite.failed.nr);
192 for (i = 0; i < suite.failed.nr; i++)
193 fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
194 }
195
196 string_list_clear(&suite.tests, 0);
197 string_list_clear(&suite.failed, 0);
198
199 return !!ret;
200 }
201
202 static uint64_t my_random_next = 1234;
203
my_random(void)204 static uint64_t my_random(void)
205 {
206 uint64_t res = my_random_next;
207 my_random_next = my_random_next * 1103515245 + 12345;
208 return res;
209 }
210
quote_stress_test(int argc,const char ** argv)211 static int quote_stress_test(int argc, const char **argv)
212 {
213 /*
214 * We are running a quote-stress test.
215 * spawn a subprocess that runs quote-stress with a
216 * special option that echoes back the arguments that
217 * were passed in.
218 */
219 char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
220 int i, j, k, trials = 100, skip = 0, msys2 = 0;
221 struct strbuf out = STRBUF_INIT;
222 struct strvec args = STRVEC_INIT;
223 struct option options[] = {
224 OPT_INTEGER('n', "trials", &trials, "Number of trials"),
225 OPT_INTEGER('s', "skip", &skip, "Skip <n> trials"),
226 OPT_BOOL('m', "msys2", &msys2, "Test quoting for MSYS2's sh"),
227 OPT_END()
228 };
229 const char * const usage[] = {
230 "test-tool run-command quote-stress-test <options>",
231 NULL
232 };
233
234 argc = parse_options(argc, argv, NULL, options, usage, 0);
235
236 setenv("MSYS_NO_PATHCONV", "1", 0);
237
238 for (i = 0; i < trials; i++) {
239 struct child_process cp = CHILD_PROCESS_INIT;
240 size_t arg_count, arg_offset;
241 int ret = 0;
242
243 strvec_clear(&args);
244 if (msys2)
245 strvec_pushl(&args, "sh", "-c",
246 "printf %s\\\\0 \"$@\"", "skip", NULL);
247 else
248 strvec_pushl(&args, "test-tool", "run-command",
249 "quote-echo", NULL);
250 arg_offset = args.nr;
251
252 if (argc > 0) {
253 trials = 1;
254 arg_count = argc;
255 for (j = 0; j < arg_count; j++)
256 strvec_push(&args, argv[j]);
257 } else {
258 arg_count = 1 + (my_random() % 5);
259 for (j = 0; j < arg_count; j++) {
260 char buf[20];
261 size_t min_len = 1;
262 size_t arg_len = min_len +
263 (my_random() % (ARRAY_SIZE(buf) - min_len));
264
265 for (k = 0; k < arg_len; k++)
266 buf[k] = special[my_random() %
267 ARRAY_SIZE(special)];
268 buf[arg_len] = '\0';
269
270 strvec_push(&args, buf);
271 }
272 }
273
274 if (i < skip)
275 continue;
276
277 cp.argv = args.v;
278 strbuf_reset(&out);
279 if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
280 return error("Failed to spawn child process");
281
282 for (j = 0, k = 0; j < arg_count; j++) {
283 const char *arg = args.v[j + arg_offset];
284
285 if (strcmp(arg, out.buf + k))
286 ret = error("incorrectly quoted arg: '%s', "
287 "echoed back as '%s'",
288 arg, out.buf + k);
289 k += strlen(out.buf + k) + 1;
290 }
291
292 if (k != out.len)
293 ret = error("got %d bytes, but consumed only %d",
294 (int)out.len, (int)k);
295
296 if (ret) {
297 fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
298 for (j = 0; j < arg_count; j++)
299 fprintf(stderr, "arg #%d: '%s'\n",
300 (int)j, args.v[j + arg_offset]);
301
302 strbuf_release(&out);
303 strvec_clear(&args);
304
305 return ret;
306 }
307
308 if (i && (i % 100) == 0)
309 fprintf(stderr, "Trials completed: %d\n", (int)i);
310 }
311
312 strbuf_release(&out);
313 strvec_clear(&args);
314
315 return 0;
316 }
317
quote_echo(int argc,const char ** argv)318 static int quote_echo(int argc, const char **argv)
319 {
320 while (argc > 1) {
321 fwrite(argv[1], strlen(argv[1]), 1, stdout);
322 fputc('\0', stdout);
323 argv++;
324 argc--;
325 }
326
327 return 0;
328 }
329
inherit_handle(const char * argv0)330 static int inherit_handle(const char *argv0)
331 {
332 struct child_process cp = CHILD_PROCESS_INIT;
333 char path[PATH_MAX];
334 int tmp;
335
336 /* First, open an inheritable handle */
337 xsnprintf(path, sizeof(path), "out-XXXXXX");
338 tmp = xmkstemp(path);
339
340 strvec_pushl(&cp.args,
341 "test-tool", argv0, "inherited-handle-child", NULL);
342 cp.in = -1;
343 cp.no_stdout = cp.no_stderr = 1;
344 if (start_command(&cp) < 0)
345 die("Could not start child process");
346
347 /* Then close it, and try to delete it. */
348 close(tmp);
349 if (unlink(path))
350 die("Could not delete '%s'", path);
351
352 if (close(cp.in) < 0 || finish_command(&cp) < 0)
353 die("Child did not finish");
354
355 return 0;
356 }
357
inherit_handle_child(void)358 static int inherit_handle_child(void)
359 {
360 struct strbuf buf = STRBUF_INIT;
361
362 if (strbuf_read(&buf, 0, 0) < 0)
363 die("Could not read stdin");
364 printf("Received %s\n", buf.buf);
365 strbuf_release(&buf);
366
367 return 0;
368 }
369
cmd__run_command(int argc,const char ** argv)370 int cmd__run_command(int argc, const char **argv)
371 {
372 struct child_process proc = CHILD_PROCESS_INIT;
373 int jobs;
374
375 if (argc > 1 && !strcmp(argv[1], "testsuite"))
376 exit(testsuite(argc - 1, argv + 1));
377 if (!strcmp(argv[1], "inherited-handle"))
378 exit(inherit_handle(argv[0]));
379 if (!strcmp(argv[1], "inherited-handle-child"))
380 exit(inherit_handle_child());
381
382 if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
383 return !!quote_stress_test(argc - 1, argv + 1);
384
385 if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
386 return !!quote_echo(argc - 1, argv + 1);
387
388 if (argc < 3)
389 return 1;
390 while (!strcmp(argv[1], "env")) {
391 if (!argv[2])
392 die("env specifier without a value");
393 strvec_push(&proc.env_array, argv[2]);
394 argv += 2;
395 argc -= 2;
396 }
397 if (argc < 3)
398 return 1;
399 proc.argv = (const char **)argv + 2;
400
401 if (!strcmp(argv[1], "start-command-ENOENT")) {
402 if (start_command(&proc) < 0 && errno == ENOENT)
403 return 0;
404 fprintf(stderr, "FAIL %s\n", argv[1]);
405 return 1;
406 }
407 if (!strcmp(argv[1], "run-command"))
408 exit(run_command(&proc));
409
410 jobs = atoi(argv[2]);
411 proc.argv = (const char **)argv + 3;
412
413 if (!strcmp(argv[1], "run-command-parallel"))
414 exit(run_processes_parallel(jobs, parallel_next,
415 NULL, NULL, &proc));
416
417 if (!strcmp(argv[1], "run-command-abort"))
418 exit(run_processes_parallel(jobs, parallel_next,
419 NULL, task_finished, &proc));
420
421 if (!strcmp(argv[1], "run-command-no-jobs"))
422 exit(run_processes_parallel(jobs, no_job,
423 NULL, task_finished, &proc));
424
425 fprintf(stderr, "check usage\n");
426 return 1;
427 }
428