3 test_description='test trace2 facility'
6 # Turn off any inherited trace2 settings for this test.
7 sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
8 sane_unset GIT_TRACE2_BARE
9 sane_unset GIT_TRACE2_CONFIG_PARAMS
11 perl -MJSON::PP -e 0 >/dev/null 2>&1 && test_set_prereq JSON_PP
13 # Add t/helper directory to PATH so that we can use a relative
14 # path to run nested instances of test-tool.exe (see 004child).
15 # This helps with HEREDOC comparisons later.
16 TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
17 PATH="$TTDIR:$PATH" && export PATH
19 # Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe
20 # Warning: to do the actual diff/comparison, so the HEREDOCs here
21 # Warning: only cover our actual calls to test-tool and/or git.
22 # Warning: So you may see extra lines in artifact files when
23 # Warning: interactively debugging.
25 V=$(git version | sed -e 's/^git version //') && export V
27 # There are multiple trace2 targets: normal, perf, and event.
28 # Trace2 events will/can be written to each active target (subject
29 # to whatever filtering that target decides to do).
30 # Test each target independently.
32 # Defer setting GIT_TRACE2_PERF until the actual command we want to
33 # test because hidden git and test-tool commands in the test
34 # harness can contaminate our output.
36 # We don't bother repeating the 001return and 002exit tests, since they
37 # have coverage in the normal and perf targets.
41 # To the above, add multiple 'error <msg>' events
43 test_expect_success JSON_PP 'event stream, error event' '
44 test_when_finished "rm trace.event actual expect" &&
45 GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 003error "hello world" "this is a test" &&
46 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
47 sed -e "s/^|//" >expect <<-EOF &&
62 | "hierarchy":"trace2",
68 test_cmp expect actual
73 # Test nested spawning of child processes.
75 # Conceptually, this looks like:
76 # P1: TT trace2 004child
77 # P2: |--- TT trace2 004child
78 # P3: |--- TT trace2 001return 0
80 test_expect_success JSON_PP 'event stream, return code 0' '
81 test_when_finished "rm trace.event actual expect" &&
82 GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 004child test-tool trace2 004child test-tool trace2 001return 0 &&
83 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
84 sed -e "s/^|//" >expect <<-EOF &&
116 | "hierarchy":"trace2",
144 | "hierarchy":"trace2/trace2",
148 | "_SID0_/_SID1_/_SID2_":{
156 | "hierarchy":"trace2/trace2/trace2",
162 test_cmp expect actual
165 # Test listing of all "interesting" config settings.
167 test_expect_success JSON_PP 'event stream, list config' '
168 test_when_finished "rm trace.event actual expect" &&
169 git config --local t0212.abc 1 &&
170 git config --local t0212.def "hello world" &&
171 GIT_TRACE2_EVENT="$(pwd)/trace.event" GIT_TRACE2_CONFIG_PARAMS="t0212.*" test-tool trace2 001return 0 &&
172 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
173 sed -e "s/^|//" >expect <<-EOF &&
183 | "hierarchy":"trace2",
187 | "param":"t0212.abc",
191 | "param":"t0212.def",
192 | "value":"hello world"
199 test_cmp expect actual
202 # Test listing of all "interesting" environment variables.
204 test_expect_success JSON_PP 'event stream, list env vars' '
205 test_when_finished "rm trace.event actual expect" &&
206 GIT_TRACE2_EVENT="$(pwd)/trace.event" \
207 GIT_TRACE2_ENV_VARS="A_VAR,OTHER_VAR,MISSING" \
208 A_VAR=1 OTHER_VAR="hello world" test-tool trace2 001return 0 &&
209 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
210 sed -e "s/^|//" >expect <<-EOF &&
220 | "hierarchy":"trace2",
228 | "param":"OTHER_VAR",
229 | "value":"hello world"
236 test_cmp expect actual
239 test_expect_success JSON_PP 'basic trace2_data' '
240 test_when_finished "rm trace.event actual expect" &&
241 GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 006data test_category k1 v1 test_category k2 v2 &&
242 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
243 sed -e "s/^|//" >expect <<-EOF &&
264 | "hierarchy":"trace2",
270 test_cmp expect actual
273 # Now test without environment variables and get all Trace2 settings
274 # from the global config.
276 test_expect_success JSON_PP 'using global config, event stream, error event' '
277 test_when_finished "rm trace.event actual expect" &&
278 test_config_global trace2.eventTarget "$(pwd)/trace.event" &&
279 test-tool trace2 003error "hello world" "this is a test" &&
280 perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
281 sed -e "s/^|//" >expect <<-EOF &&
296 | "hierarchy":"trace2",
302 test_cmp expect actual
305 test_expect_success 'discard traces when there are too many files' '
306 mkdir trace_target_dir &&
307 test_when_finished "rm -r trace_target_dir" &&
309 GIT_TRACE2_MAX_FILES=5 &&
310 export GIT_TRACE2_MAX_FILES &&
311 cd trace_target_dir &&
312 test_seq $GIT_TRACE2_MAX_FILES >../expected_filenames.txt &&
313 xargs touch <../expected_filenames.txt &&
315 GIT_TRACE2_EVENT="$(pwd)/trace_target_dir" test-tool trace2 001return 0
317 echo git-trace2-discard >>expected_filenames.txt &&
318 ls trace_target_dir >ls_output.txt &&
319 test_cmp expected_filenames.txt ls_output.txt &&
320 head -n1 trace_target_dir/git-trace2-discard | grep \"event\":\"version\" &&
321 head -n2 trace_target_dir/git-trace2-discard | tail -n1 | grep \"event\":\"too_many_files\"