t / t0212-trace2-event.shon commit trace2: use system/global config for default trace2 settings (bce9db6)
   1#!/bin/sh
   2
   3test_description='test trace2 facility'
   4. ./test-lib.sh
   5
   6# Turn off any inherited trace2 settings for this test.
   7sane_unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
   8sane_unset GIT_TR2_BARE
   9sane_unset GIT_TR2_CONFIG_PARAMS
  10
  11perl -MJSON::PP -e 0 >/dev/null 2>&1 && test_set_prereq JSON_PP
  12
  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.
  16TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
  17PATH="$TTDIR:$PATH" && export PATH
  18
  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.
  24
  25V=$(git version | sed -e 's/^git version //') && export V
  26
  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.
  31#
  32# Defer setting GIT_TR2_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.
  35
  36# We don't bother repeating the 001return and 002exit tests, since they
  37# have coverage in the normal and perf targets.
  38
  39# Verb 003error
  40#
  41# To the above, add multiple 'error <msg>' events
  42
  43test_expect_success JSON_PP 'event stream, error event' '
  44        test_when_finished "rm trace.event actual expect" &&
  45        GIT_TR2_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 &&
  48        |VAR1 = {
  49        |  "_SID0_":{
  50        |    "argv":[
  51        |      "_EXE_",
  52        |      "trace2",
  53        |      "003error",
  54        |      "hello world",
  55        |      "this is a test"
  56        |    ],
  57        |    "errors":[
  58        |      "%s",
  59        |      "%s"
  60        |    ],
  61        |    "exit_code":0,
  62        |    "hierarchy":"trace2",
  63        |    "name":"trace2",
  64        |    "version":"$V"
  65        |  }
  66        |};
  67        EOF
  68        test_cmp expect actual
  69'
  70
  71# Verb 004child
  72#
  73# Test nested spawning of child processes.
  74#
  75# Conceptually, this looks like:
  76#    P1: TT trace2 004child
  77#    P2: |--- TT trace2 004child
  78#    P3:      |--- TT trace2 001return 0
  79
  80test_expect_success JSON_PP 'event stream, return code 0' '
  81        test_when_finished "rm trace.event actual expect" &&
  82        GIT_TR2_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 &&
  85        |VAR1 = {
  86        |  "_SID0_":{
  87        |    "argv":[
  88        |      "_EXE_",
  89        |      "trace2",
  90        |      "004child",
  91        |      "test-tool",
  92        |      "trace2",
  93        |      "004child",
  94        |      "test-tool",
  95        |      "trace2",
  96        |      "001return",
  97        |      "0"
  98        |    ],
  99        |    "child":{
 100        |      "0":{
 101        |        "child_argv":[
 102        |          "_EXE_",
 103        |          "trace2",
 104        |          "004child",
 105        |          "test-tool",
 106        |          "trace2",
 107        |          "001return",
 108        |          "0"
 109        |        ],
 110        |        "child_class":"?",
 111        |        "child_code":0,
 112        |        "use_shell":0
 113        |      }
 114        |    },
 115        |    "exit_code":0,
 116        |    "hierarchy":"trace2",
 117        |    "name":"trace2",
 118        |    "version":"$V"
 119        |  },
 120        |  "_SID0_/_SID1_":{
 121        |    "argv":[
 122        |      "_EXE_",
 123        |      "trace2",
 124        |      "004child",
 125        |      "test-tool",
 126        |      "trace2",
 127        |      "001return",
 128        |      "0"
 129        |    ],
 130        |    "child":{
 131        |      "0":{
 132        |        "child_argv":[
 133        |          "_EXE_",
 134        |          "trace2",
 135        |          "001return",
 136        |          "0"
 137        |        ],
 138        |        "child_class":"?",
 139        |        "child_code":0,
 140        |        "use_shell":0
 141        |      }
 142        |    },
 143        |    "exit_code":0,
 144        |    "hierarchy":"trace2/trace2",
 145        |    "name":"trace2",
 146        |    "version":"$V"
 147        |  },
 148        |  "_SID0_/_SID1_/_SID2_":{
 149        |    "argv":[
 150        |      "_EXE_",
 151        |      "trace2",
 152        |      "001return",
 153        |      "0"
 154        |    ],
 155        |    "exit_code":0,
 156        |    "hierarchy":"trace2/trace2/trace2",
 157        |    "name":"trace2",
 158        |    "version":"$V"
 159        |  }
 160        |};
 161        EOF
 162        test_cmp expect actual
 163'
 164
 165# Test listing of all "interesting" config settings.
 166
 167test_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_TR2_EVENT="$(pwd)/trace.event" GIT_TR2_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 &&
 174        |VAR1 = {
 175        |  "_SID0_":{
 176        |    "argv":[
 177        |      "_EXE_",
 178        |      "trace2",
 179        |      "001return",
 180        |      "0"
 181        |    ],
 182        |    "exit_code":0,
 183        |    "hierarchy":"trace2",
 184        |    "name":"trace2",
 185        |    "params":[
 186        |      {
 187        |        "param":"t0212.abc",
 188        |        "value":"1"
 189        |      },
 190        |      {
 191        |        "param":"t0212.def",
 192        |        "value":"hello world"
 193        |      }
 194        |    ],
 195        |    "version":"$V"
 196        |  }
 197        |};
 198        EOF
 199        test_cmp expect actual
 200'
 201
 202test_expect_success JSON_PP 'basic trace2_data' '
 203        test_when_finished "rm trace.event actual expect" &&
 204        GIT_TR2_EVENT="$(pwd)/trace.event" test-tool trace2 006data test_category k1 v1 test_category k2 v2 &&
 205        perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
 206        sed -e "s/^|//" >expect <<-EOF &&
 207        |VAR1 = {
 208        |  "_SID0_":{
 209        |    "argv":[
 210        |      "_EXE_",
 211        |      "trace2",
 212        |      "006data",
 213        |      "test_category",
 214        |      "k1",
 215        |      "v1",
 216        |      "test_category",
 217        |      "k2",
 218        |      "v2"
 219        |    ],
 220        |    "data":{
 221        |      "test_category":{
 222        |        "k1":"v1",
 223        |        "k2":"v2"
 224        |      }
 225        |    },
 226        |    "exit_code":0,
 227        |    "hierarchy":"trace2",
 228        |    "name":"trace2",
 229        |    "version":"$V"
 230        |  }
 231        |};
 232        EOF
 233        test_cmp expect actual
 234'
 235
 236# Now test without environment variables and get all Trace2 settings
 237# from the global config.
 238
 239test_expect_success JSON_PP 'using global config, event stream, error event' '
 240        test_when_finished "rm trace.event actual expect" &&
 241        test_config_global trace2.eventTarget "$(pwd)/trace.event" &&
 242        test-tool trace2 003error "hello world" "this is a test" &&
 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        |      "003error",
 251        |      "hello world",
 252        |      "this is a test"
 253        |    ],
 254        |    "errors":[
 255        |      "%s",
 256        |      "%s"
 257        |    ],
 258        |    "exit_code":0,
 259        |    "hierarchy":"trace2",
 260        |    "name":"trace2",
 261        |    "version":"$V"
 262        |  }
 263        |};
 264        EOF
 265        test_cmp expect actual
 266'
 267
 268test_done