| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test trace2 facility' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | # Turn off any inherited trace2 settings for this test. |
| 8 | sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT |
| 9 | sane_unset GIT_TRACE2_BARE |
| 10 | sane_unset GIT_TRACE2_CONFIG_PARAMS |
| 11 | |
| 12 | perl -MJSON::PP -e 0 >/dev/null 2>&1 && test_set_prereq JSON_PP |
| 13 | |
| 14 | # Add t/helper directory to PATH so that we can use a relative |
| 15 | # path to run nested instances of test-tool.exe (see 004child). |
| 16 | # This helps with HEREDOC comparisons later. |
| 17 | TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR |
| 18 | PATH="$TTDIR:$PATH" && export PATH |
| 19 | |
| 20 | # Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe |
| 21 | # Warning: to do the actual diff/comparison, so the HEREDOCs here |
| 22 | # Warning: only cover our actual calls to test-tool and/or git. |
| 23 | # Warning: So you may see extra lines in artifact files when |
| 24 | # Warning: interactively debugging. |
| 25 | |
| 26 | V=$(git version | sed -e 's/^git version //') && export V |
| 27 | |
| 28 | # There are multiple trace2 targets: normal, perf, and event. |
| 29 | # Trace2 events will/can be written to each active target (subject |
| 30 | # to whatever filtering that target decides to do). |
| 31 | # Test each target independently. |
| 32 | # |
| 33 | # Defer setting GIT_TRACE2_PERF until the actual command we want to |
| 34 | # test because hidden git and test-tool commands in the test |
| 35 | # harness can contaminate our output. |
| 36 | |
| 37 | # We don't bother repeating the 001return and 002exit tests, since they |
| 38 | # have coverage in the normal and perf targets. |
| 39 | |
| 40 | # Verb 003error |
| 41 | # |
| 42 | # To the above, add multiple 'error <msg>' events |
| 43 | |
| 44 | test_expect_success JSON_PP 'event stream, error event' ' |
| 45 | test_when_finished "rm trace.event actual expect" && |
| 46 | GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 003error "hello world" "this is a test" && |
| 47 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 48 | sed -e "s/^|//" >expect <<-EOF && |
| 49 | |VAR1 = { |
| 50 | | "_SID0_":{ |
| 51 | | "argv":[ |
| 52 | | "_EXE_", |
| 53 | | "trace2", |
| 54 | | "003error", |
| 55 | | "hello world", |
| 56 | | "this is a test" |
| 57 | | ], |
| 58 | | "errors":[ |
| 59 | | "%s", |
| 60 | | "%s" |
| 61 | | ], |
| 62 | | "exit_code":0, |
| 63 | | "hierarchy":"trace2", |
| 64 | | "name":"trace2", |
| 65 | | "version":"$V" |
| 66 | | } |
| 67 | |}; |
| 68 | EOF |
| 69 | test_cmp expect actual |
| 70 | ' |
| 71 | |
| 72 | # Verb 004child |
| 73 | # |
| 74 | # Test nested spawning of child processes. |
| 75 | # |
| 76 | # Conceptually, this looks like: |
| 77 | # P1: TT trace2 004child |
| 78 | # P2: |--- TT trace2 004child |
| 79 | # P3: |--- TT trace2 001return 0 |
| 80 | |
| 81 | test_expect_success JSON_PP 'event stream, return code 0' ' |
| 82 | test_when_finished "rm trace.event actual expect" && |
| 83 | GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 004child test-tool trace2 004child test-tool trace2 001return 0 && |
| 84 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 85 | sed -e "s/^|//" >expect <<-EOF && |
| 86 | |VAR1 = { |
| 87 | | "_SID0_":{ |
| 88 | | "argv":[ |
| 89 | | "_EXE_", |
| 90 | | "trace2", |
| 91 | | "004child", |
| 92 | | "test-tool", |
| 93 | | "trace2", |
| 94 | | "004child", |
| 95 | | "test-tool", |
| 96 | | "trace2", |
| 97 | | "001return", |
| 98 | | "0" |
| 99 | | ], |
| 100 | | "child":{ |
| 101 | | "0":{ |
| 102 | | "child_argv":[ |
| 103 | | "_EXE_", |
| 104 | | "trace2", |
| 105 | | "004child", |
| 106 | | "test-tool", |
| 107 | | "trace2", |
| 108 | | "001return", |
| 109 | | "0" |
| 110 | | ], |
| 111 | | "child_class":"?", |
| 112 | | "child_code":0, |
| 113 | | "use_shell":0 |
| 114 | | } |
| 115 | | }, |
| 116 | | "exit_code":0, |
| 117 | | "hierarchy":"trace2", |
| 118 | | "name":"trace2", |
| 119 | | "version":"$V" |
| 120 | | }, |
| 121 | | "_SID0_/_SID1_":{ |
| 122 | | "argv":[ |
| 123 | | "_EXE_", |
| 124 | | "trace2", |
| 125 | | "004child", |
| 126 | | "test-tool", |
| 127 | | "trace2", |
| 128 | | "001return", |
| 129 | | "0" |
| 130 | | ], |
| 131 | | "child":{ |
| 132 | | "0":{ |
| 133 | | "child_argv":[ |
| 134 | | "_EXE_", |
| 135 | | "trace2", |
| 136 | | "001return", |
| 137 | | "0" |
| 138 | | ], |
| 139 | | "child_class":"?", |
| 140 | | "child_code":0, |
| 141 | | "use_shell":0 |
| 142 | | } |
| 143 | | }, |
| 144 | | "exit_code":0, |
| 145 | | "hierarchy":"trace2/trace2", |
| 146 | | "name":"trace2", |
| 147 | | "version":"$V" |
| 148 | | }, |
| 149 | | "_SID0_/_SID1_/_SID2_":{ |
| 150 | | "argv":[ |
| 151 | | "_EXE_", |
| 152 | | "trace2", |
| 153 | | "001return", |
| 154 | | "0" |
| 155 | | ], |
| 156 | | "exit_code":0, |
| 157 | | "hierarchy":"trace2/trace2/trace2", |
| 158 | | "name":"trace2", |
| 159 | | "version":"$V" |
| 160 | | } |
| 161 | |}; |
| 162 | EOF |
| 163 | test_cmp expect actual |
| 164 | ' |
| 165 | |
| 166 | # Test listing of all "interesting" config settings. |
| 167 | |
| 168 | test_expect_success JSON_PP 'event stream, list config' ' |
| 169 | test_when_finished "rm trace.event actual expect" && |
| 170 | git config --local t0212.abc 1 && |
| 171 | git config --local t0212.def "hello world" && |
| 172 | GIT_TRACE2_EVENT="$(pwd)/trace.event" GIT_TRACE2_CONFIG_PARAMS="t0212.*" test-tool trace2 001return 0 && |
| 173 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 174 | sed -e "s/^|//" >expect <<-EOF && |
| 175 | |VAR1 = { |
| 176 | | "_SID0_":{ |
| 177 | | "argv":[ |
| 178 | | "_EXE_", |
| 179 | | "trace2", |
| 180 | | "001return", |
| 181 | | "0" |
| 182 | | ], |
| 183 | | "exit_code":0, |
| 184 | | "hierarchy":"trace2", |
| 185 | | "name":"trace2", |
| 186 | | "params":[ |
| 187 | | { |
| 188 | | "param":"t0212.abc", |
| 189 | | "value":"1" |
| 190 | | }, |
| 191 | | { |
| 192 | | "param":"t0212.def", |
| 193 | | "value":"hello world" |
| 194 | | } |
| 195 | | ], |
| 196 | | "version":"$V" |
| 197 | | } |
| 198 | |}; |
| 199 | EOF |
| 200 | test_cmp expect actual |
| 201 | ' |
| 202 | |
| 203 | # Test listing of all "interesting" environment variables. |
| 204 | |
| 205 | test_expect_success JSON_PP 'event stream, list env vars' ' |
| 206 | test_when_finished "rm trace.event actual expect" && |
| 207 | GIT_TRACE2_EVENT="$(pwd)/trace.event" \ |
| 208 | GIT_TRACE2_ENV_VARS="A_VAR,OTHER_VAR,MISSING" \ |
| 209 | A_VAR=1 OTHER_VAR="hello world" test-tool trace2 001return 0 && |
| 210 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 211 | sed -e "s/^|//" >expect <<-EOF && |
| 212 | |VAR1 = { |
| 213 | | "_SID0_":{ |
| 214 | | "argv":[ |
| 215 | | "_EXE_", |
| 216 | | "trace2", |
| 217 | | "001return", |
| 218 | | "0" |
| 219 | | ], |
| 220 | | "exit_code":0, |
| 221 | | "hierarchy":"trace2", |
| 222 | | "name":"trace2", |
| 223 | | "params":[ |
| 224 | | { |
| 225 | | "param":"A_VAR", |
| 226 | | "value":"1" |
| 227 | | }, |
| 228 | | { |
| 229 | | "param":"OTHER_VAR", |
| 230 | | "value":"hello world" |
| 231 | | } |
| 232 | | ], |
| 233 | | "version":"$V" |
| 234 | | } |
| 235 | |}; |
| 236 | EOF |
| 237 | test_cmp expect actual |
| 238 | ' |
| 239 | |
| 240 | test_expect_success JSON_PP 'basic trace2_data' ' |
| 241 | test_when_finished "rm trace.event actual expect" && |
| 242 | GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 006data test_category k1 v1 test_category k2 v2 && |
| 243 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 244 | sed -e "s/^|//" >expect <<-EOF && |
| 245 | |VAR1 = { |
| 246 | | "_SID0_":{ |
| 247 | | "argv":[ |
| 248 | | "_EXE_", |
| 249 | | "trace2", |
| 250 | | "006data", |
| 251 | | "test_category", |
| 252 | | "k1", |
| 253 | | "v1", |
| 254 | | "test_category", |
| 255 | | "k2", |
| 256 | | "v2" |
| 257 | | ], |
| 258 | | "data":{ |
| 259 | | "test_category":{ |
| 260 | | "k1":"v1", |
| 261 | | "k2":"v2" |
| 262 | | } |
| 263 | | }, |
| 264 | | "exit_code":0, |
| 265 | | "hierarchy":"trace2", |
| 266 | | "name":"trace2", |
| 267 | | "version":"$V" |
| 268 | | } |
| 269 | |}; |
| 270 | EOF |
| 271 | test_cmp expect actual |
| 272 | ' |
| 273 | |
| 274 | # Now test without environment variables and get all Trace2 settings |
| 275 | # from the global config. |
| 276 | |
| 277 | test_expect_success JSON_PP 'using global config, event stream, error event' ' |
| 278 | test_when_finished "rm trace.event actual expect" && |
| 279 | test_config_global trace2.eventTarget "$(pwd)/trace.event" && |
| 280 | test-tool trace2 003error "hello world" "this is a test" && |
| 281 | perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual && |
| 282 | sed -e "s/^|//" >expect <<-EOF && |
| 283 | |VAR1 = { |
| 284 | | "_SID0_":{ |
| 285 | | "argv":[ |
| 286 | | "_EXE_", |
| 287 | | "trace2", |
| 288 | | "003error", |
| 289 | | "hello world", |
| 290 | | "this is a test" |
| 291 | | ], |
| 292 | | "errors":[ |
| 293 | | "%s", |
| 294 | | "%s" |
| 295 | | ], |
| 296 | | "exit_code":0, |
| 297 | | "hierarchy":"trace2", |
| 298 | | "name":"trace2", |
| 299 | | "version":"$V" |
| 300 | | } |
| 301 | |}; |
| 302 | EOF |
| 303 | test_cmp expect actual |
| 304 | ' |
| 305 | |
| 306 | test_expect_success 'discard traces when there are too many files' ' |
| 307 | mkdir trace_target_dir && |
| 308 | test_when_finished "rm -r trace_target_dir" && |
| 309 | ( |
| 310 | GIT_TRACE2_MAX_FILES=5 && |
| 311 | export GIT_TRACE2_MAX_FILES && |
| 312 | cd trace_target_dir && |
| 313 | test_seq $GIT_TRACE2_MAX_FILES >../expected_filenames.txt && |
| 314 | xargs touch <../expected_filenames.txt && |
| 315 | cd .. && |
| 316 | GIT_TRACE2_EVENT="$(pwd)/trace_target_dir" test-tool trace2 001return 0 |
| 317 | ) && |
| 318 | echo git-trace2-discard >>expected_filenames.txt && |
| 319 | ls trace_target_dir >ls_output.txt && |
| 320 | test_cmp expected_filenames.txt ls_output.txt && |
| 321 | head -n1 trace_target_dir/git-trace2-discard | grep \"event\":\"version\" && |
| 322 | head -n2 trace_target_dir/git-trace2-discard | tail -n1 | grep \"event\":\"too_many_files\" |
| 323 | ' |
| 324 | |
| 325 | # In the following "...redact..." tests, skip testing the GIT_TRACE2_REDACT=0 |
| 326 | # case because we would need to exactly model the full JSON event stream like |
| 327 | # we did in the basic tests above and I do not think it is worth it. |
| 328 | |
| 329 | test_expect_success 'unsafe URLs are redacted by default in cmd_start events' ' |
| 330 | test_when_finished \ |
| 331 | "rm -r trace.event" && |
| 332 | |
| 333 | GIT_TRACE2_EVENT="$(pwd)/trace.event" \ |
| 334 | test-tool trace2 300redact_start git clone https://user:pwd@example.com/ clone2 && |
| 335 | test_grep ! user:pwd trace.event |
| 336 | ' |
| 337 | |
| 338 | test_expect_success 'unsafe URLs are redacted by default in child_start events' ' |
| 339 | test_when_finished \ |
| 340 | "rm -r trace.event" && |
| 341 | |
| 342 | GIT_TRACE2_EVENT="$(pwd)/trace.event" \ |
| 343 | test-tool trace2 301redact_child_start git clone https://user:pwd@example.com/ clone2 && |
| 344 | test_grep ! user:pwd trace.event |
| 345 | ' |
| 346 | |
| 347 | test_expect_success 'unsafe URLs are redacted by default in exec events' ' |
| 348 | test_when_finished \ |
| 349 | "rm -r trace.event" && |
| 350 | |
| 351 | GIT_TRACE2_EVENT="$(pwd)/trace.event" \ |
| 352 | test-tool trace2 302redact_exec git clone https://user:pwd@example.com/ clone2 && |
| 353 | test_grep ! user:pwd trace.event |
| 354 | ' |
| 355 | |
| 356 | test_expect_success 'unsafe URLs are redacted by default in def_param events' ' |
| 357 | test_when_finished \ |
| 358 | "rm -r trace.event" && |
| 359 | |
| 360 | GIT_TRACE2_EVENT="$(pwd)/trace.event" \ |
| 361 | test-tool trace2 303redact_def_param url https://user:pwd@example.com/ && |
| 362 | test_grep ! user:pwd trace.event |
| 363 | ' |
| 364 | |
| 365 | test_done |