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