]>
Commit | Line | Data |
---|---|---|
a15860dc JH |
1 | #include "test-tool.h" |
2 | #include "cache.h" | |
dbbcd44f | 3 | #include "strvec.h" |
a15860dc JH |
4 | #include "run-command.h" |
5 | #include "exec-cmd.h" | |
6 | #include "config.h" | |
7 | ||
8 | typedef int(fn_unit_test)(int argc, const char **argv); | |
9 | ||
10 | struct unit_test { | |
11 | fn_unit_test *ut_fn; | |
12 | const char *ut_name; | |
13 | const char *ut_usage; | |
14 | }; | |
15 | ||
16 | #define MyOk 0 | |
17 | #define MyError 1 | |
18 | ||
19 | static int get_i(int *p_value, const char *data) | |
20 | { | |
21 | char *endptr; | |
22 | ||
23 | if (!data || !*data) | |
24 | return MyError; | |
25 | ||
26 | *p_value = strtol(data, &endptr, 10); | |
27 | if (*endptr || errno == ERANGE) | |
28 | return MyError; | |
29 | ||
30 | return MyOk; | |
31 | } | |
32 | ||
33 | /* | |
34 | * Cause process to exit with the requested value via "return". | |
35 | * | |
36 | * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit() | |
37 | * with our result. | |
38 | * | |
39 | * Test harness can confirm: | |
40 | * [] the process-exit value. | |
41 | * [] the "code" field in the "exit" trace2 event. | |
42 | * [] the "code" field in the "atexit" trace2 event. | |
43 | * [] the "name" field in the "cmd_name" trace2 event. | |
44 | * [] "def_param" events for all of the "interesting" pre-defined | |
45 | * config settings. | |
46 | */ | |
47 | static int ut_001return(int argc, const char **argv) | |
48 | { | |
49 | int rc; | |
50 | ||
51 | if (get_i(&rc, argv[0])) | |
52 | die("expect <exit_code>"); | |
53 | ||
54 | return rc; | |
55 | } | |
56 | ||
57 | /* | |
58 | * Cause the process to exit with the requested value via "exit()". | |
59 | * | |
60 | * Test harness can confirm: | |
61 | * [] the "code" field in the "exit" trace2 event. | |
62 | * [] the "code" field in the "atexit" trace2 event. | |
63 | * [] the "name" field in the "cmd_name" trace2 event. | |
64 | * [] "def_param" events for all of the "interesting" pre-defined | |
65 | * config settings. | |
66 | */ | |
67 | static int ut_002exit(int argc, const char **argv) | |
68 | { | |
69 | int rc; | |
70 | ||
71 | if (get_i(&rc, argv[0])) | |
72 | die("expect <exit_code>"); | |
73 | ||
74 | exit(rc); | |
75 | } | |
76 | ||
77 | /* | |
78 | * Send an "error" event with each value in argv. Normally, git only issues | |
79 | * a single "error" event immediately before issuing an "exit" event (such | |
80 | * as in die() or BUG()), but multiple "error" events are allowed. | |
81 | * | |
82 | * Test harness can confirm: | |
83 | * [] a trace2 "error" event for each value in argv. | |
84 | * [] the "name" field in the "cmd_name" trace2 event. | |
85 | * [] (optional) the file:line in the "exit" event refers to this function. | |
86 | */ | |
87 | static int ut_003error(int argc, const char **argv) | |
88 | { | |
89 | int k; | |
90 | ||
91 | if (!argv[0] || !*argv[0]) | |
92 | die("expect <error_message>"); | |
93 | ||
94 | for (k = 0; k < argc; k++) | |
95 | error("%s", argv[k]); | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Run a child process and wait for it to finish and exit with its return code. | |
102 | * test-tool trace2 004child [<child-command-line>] | |
103 | * | |
104 | * For example: | |
105 | * test-tool trace2 004child git version | |
106 | * test-tool trace2 004child test-tool trace2 001return 0 | |
107 | * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child | |
108 | * test-tool trace2 004child git -c alias.xyz=version xyz | |
109 | * | |
110 | * Test harness can confirm: | |
111 | * [] the "name" field in the "cmd_name" trace2 event. | |
112 | * [] that the outer process has a single component SID (or depth "d0" in | |
113 | * the PERF stream). | |
114 | * [] that "child_start" and "child_exit" events are generated for the child. | |
115 | * [] if the child process is an instrumented executable: | |
116 | * [] that "version", "start", ..., "exit", and "atexit" events are | |
117 | * generated by the child process. | |
118 | * [] that the child process events have a multiple component SID (or | |
119 | * depth "dN+1" in the PERF stream). | |
120 | * [] that the child exit code is propagated to the parent process "exit" | |
121 | * and "atexit" events.. | |
122 | * [] (optional) that the "t_abs" field in the child process "atexit" event | |
123 | * is less than the "t_rel" field in the "child_exit" event of the parent | |
124 | * process. | |
125 | * [] if the child process is like the alias example above, | |
126 | * [] (optional) the child process attempts to run "git-xyx" as a dashed | |
127 | * command. | |
128 | * [] the child process emits an "alias" event with "xyz" => "version" | |
129 | * [] the child process runs "git version" as a child process. | |
130 | * [] the child process has a 3 component SID (or depth "d2" in the PERF | |
131 | * stream). | |
132 | */ | |
133 | static int ut_004child(int argc, const char **argv) | |
134 | { | |
ddbb47fd | 135 | struct child_process cmd = CHILD_PROCESS_INIT; |
a15860dc JH |
136 | int result; |
137 | ||
138 | /* | |
139 | * Allow empty <child_command_line> so we can do arbitrarily deep | |
140 | * command nesting and let the last one be null. | |
141 | */ | |
142 | if (!argc) | |
143 | return 0; | |
144 | ||
ddbb47fd RS |
145 | strvec_pushv(&cmd.args, argv); |
146 | result = run_command(&cmd); | |
a15860dc JH |
147 | exit(result); |
148 | } | |
149 | ||
150 | /* | |
151 | * Exec a git command. This may either create a child process (Windows) | |
152 | * or replace the existing process. | |
153 | * test-tool trace2 005exec <git_command_args> | |
154 | * | |
155 | * For example: | |
156 | * test-tool trace2 005exec version | |
157 | * | |
158 | * Test harness can confirm (on Windows): | |
159 | * [] the "name" field in the "cmd_name" trace2 event. | |
160 | * [] that the outer process has a single component SID (or depth "d0" in | |
161 | * the PERF stream). | |
162 | * [] that "exec" and "exec_result" events are generated for the child | |
163 | * process (since the Windows compatibility layer fakes an exec() with | |
164 | * a CreateProcess(), WaitForSingleObject(), and exit()). | |
165 | * [] that the child process has multiple component SID (or depth "dN+1" | |
166 | * in the PERF stream). | |
167 | * | |
168 | * Test harness can confirm (on platforms with a real exec() function): | |
169 | * [] TODO talk about process replacement and how it affects SID. | |
170 | */ | |
171 | static int ut_005exec(int argc, const char **argv) | |
172 | { | |
173 | int result; | |
174 | ||
175 | if (!argc) | |
176 | return 0; | |
177 | ||
178 | result = execv_git_cmd(argv); | |
179 | return result; | |
180 | } | |
181 | ||
182 | static int ut_006data(int argc, const char **argv) | |
183 | { | |
184 | const char *usage_error = | |
185 | "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]"; | |
186 | ||
187 | if (argc % 3 != 0) | |
188 | die("%s", usage_error); | |
189 | ||
190 | while (argc) { | |
191 | if (!argv[0] || !*argv[0] || !argv[1] || !*argv[1] || | |
192 | !argv[2] || !*argv[2]) | |
193 | die("%s", usage_error); | |
194 | ||
195 | trace2_data_string(argv[0], the_repository, argv[1], argv[2]); | |
196 | argv += 3; | |
197 | argc -= 3; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
0cc05b04 | 203 | static int ut_007BUG(int argc, const char **argv) |
0a9dde4a JT |
204 | { |
205 | /* | |
206 | * Exercise BUG() to ensure that the message is printed to trace2. | |
207 | */ | |
208 | BUG("the bug message"); | |
209 | } | |
210 | ||
0cc05b04 ÆAB |
211 | static int ut_008bug(int argc, const char **argv) |
212 | { | |
213 | bug("a bug message"); | |
214 | bug("another bug message"); | |
215 | BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required"); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static int ut_009bug_BUG(int argc, const char **argv) | |
220 | { | |
221 | bug("a bug message"); | |
222 | bug("another bug message"); | |
223 | /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */ | |
224 | return 0; | |
225 | } | |
226 | ||
227 | static int ut_010bug_BUG(int argc, const char **argv) | |
228 | { | |
f8535596 JK |
229 | bug("a %s message", "bug"); |
230 | BUG("a %s message", "BUG"); | |
0cc05b04 ÆAB |
231 | } |
232 | ||
a15860dc JH |
233 | /* |
234 | * Usage: | |
235 | * test-tool trace2 <ut_name_1> <ut_usage_1> | |
236 | * test-tool trace2 <ut_name_2> <ut_usage_2> | |
237 | * ... | |
238 | */ | |
239 | #define USAGE_PREFIX "test-tool trace2" | |
240 | ||
241 | /* clang-format off */ | |
242 | static struct unit_test ut_table[] = { | |
243 | { ut_001return, "001return", "<exit_code>" }, | |
244 | { ut_002exit, "002exit", "<exit_code>" }, | |
245 | { ut_003error, "003error", "<error_message>+" }, | |
246 | { ut_004child, "004child", "[<child_command_line>]" }, | |
247 | { ut_005exec, "005exec", "<git_command_args>" }, | |
248 | { ut_006data, "006data", "[<category> <key> <value>]+" }, | |
0cc05b04 ÆAB |
249 | { ut_007BUG, "007bug", "" }, |
250 | { ut_008bug, "008bug", "" }, | |
251 | { ut_009bug_BUG, "009bug_BUG","" }, | |
252 | { ut_010bug_BUG, "010bug_BUG","" }, | |
a15860dc JH |
253 | }; |
254 | /* clang-format on */ | |
255 | ||
256 | /* clang-format off */ | |
257 | #define for_each_ut(k, ut_k) \ | |
258 | for (k = 0, ut_k = &ut_table[k]; \ | |
259 | k < ARRAY_SIZE(ut_table); \ | |
260 | k++, ut_k = &ut_table[k]) | |
261 | /* clang-format on */ | |
262 | ||
263 | static int print_usage(void) | |
264 | { | |
265 | int k; | |
266 | struct unit_test *ut_k; | |
267 | ||
268 | fprintf(stderr, "usage:\n"); | |
269 | for_each_ut (k, ut_k) | |
270 | fprintf(stderr, "\t%s %s %s\n", USAGE_PREFIX, ut_k->ut_name, | |
271 | ut_k->ut_usage); | |
272 | ||
273 | return 129; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Issue various trace2 events for testing. | |
278 | * | |
279 | * We assume that these trace2 routines has already been called: | |
280 | * [] trace2_initialize() [common-main.c:main()] | |
281 | * [] trace2_cmd_start() [common-main.c:main()] | |
282 | * [] trace2_cmd_name() [test-tool.c:cmd_main()] | |
283 | * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()] | |
284 | * So that: | |
285 | * [] the various trace2 streams are open. | |
286 | * [] the process SID has been created. | |
287 | * [] the "version" event has been generated. | |
288 | * [] the "start" event has been generated. | |
289 | * [] the "cmd_name" event has been generated. | |
290 | * [] this writes various "def_param" events for interesting config values. | |
291 | * | |
368b5843 ÆAB |
292 | * We return from here and let test-tool.c::cmd_main() pass the exit |
293 | * code to common-main.c::main(), which will use it to call | |
294 | * trace2_cmd_exit(). | |
a15860dc JH |
295 | */ |
296 | int cmd__trace2(int argc, const char **argv) | |
297 | { | |
298 | int k; | |
299 | struct unit_test *ut_k; | |
300 | ||
301 | argc--; /* skip over "trace2" arg */ | |
302 | argv++; | |
303 | ||
304 | if (argc) | |
305 | for_each_ut (k, ut_k) | |
306 | if (!strcmp(argv[0], ut_k->ut_name)) | |
307 | return ut_k->ut_fn(argc - 1, argv + 1); | |
308 | ||
309 | return print_usage(); | |
310 | } |