github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/tests/acceptance/test_files/config_precedence.bats (about)

     1  load "$LIB_BATS_ASSERT/load.bash"
     2  load "$LIB_BATS_SUPPORT/load.bash"
     3  
     4  ## workspace tests
     5  
     6  @test "generic config precedence test" {
     7      skip 'disabled for now'
     8    cp $FILE_PATH/test_data/source_files/config_tests/default.spc $STEAMPIPE_INSTALL_DIR/config/default.spc
     9    
    10    # setup test folder and read the test-cases file
    11    cd $FILE_PATH/test_data/source_files/config_tests
    12    tests=$(cat workspace_tests.json)
    13    # echo $tests
    14  
    15    # to create the failure message
    16    err=""
    17    flag=0
    18  
    19    # fetch the keys(test names)
    20    test_keys=$(echo $tests | jq '. | keys[]')
    21    # echo $test_keys
    22  
    23    for i in $test_keys; do
    24      # each test case do the following
    25      unset STEAMPIPE_INSTALL_DIR
    26      cwd=$(pwd)
    27      export STEAMPIPE_CONFIG_DUMP=config_json
    28  
    29      # check the command(query/check/dashboard) and prepare the steampipe
    30      # command accordingly
    31      cmd=$(echo $tests | jq -c ".[${i}]" | jq ".cmd")
    32      if [[ $cmd == '"query"' ]]; then
    33        sp_cmd='steampipe query "select 1"'
    34      elif [[ $cmd == '"check"' ]]; then
    35        sp_cmd='steampipe check all'
    36      elif [[ $cmd == '"dashboard"' ]]; then
    37        sp_cmd='steampipe dashboard'
    38      fi
    39      # echo $sp_cmd
    40  
    41      # key=$(echo $i)
    42      echo -e "\n"
    43      test_name=$(echo $tests | jq -c ".[${i}]" | jq ".test")
    44      echo ">>> TEST NAME: $test_name"
    45  
    46      # env variables needed for setup
    47      env=$(echo $tests | jq -c ".[${i}]" | jq ".setup.env")
    48      # echo $env
    49  
    50      # set env variables
    51      for e in $(echo "${env}" | jq -r '.[]'); do
    52        export $e
    53      done
    54  
    55      # args to run with steampipe query command
    56      args=$(echo $tests | jq -c ".[${i}]" | jq ".setup.args")
    57      echo $args
    58  
    59      # construct the steampipe command to be run with the args
    60      for arg in $(echo "${args}" | jq -r '.[]'); do
    61        sp_cmd="${sp_cmd} ${arg}"
    62      done
    63      echo "steampipe command: $sp_cmd" # help debugging in case of failures
    64  
    65      # get the actual config by running the constructed steampipe command
    66      run $sp_cmd
    67      echo "output from steampipe command: $output" # help debugging in case of failures
    68      actual_config=$(echo $output | jq -c '.')
    69      echo "actual config: \n$actual_config" # help debugging in case of failures
    70  
    71      # get expected config from test case
    72      expected_config=$(echo $tests | jq -c ".[${i}]" | jq ".expected")
    73      # echo $expected_config
    74  
    75      # fetch only keys from expected config
    76      exp_keys=$(echo $expected_config | jq '. | keys[]' | jq -s 'flatten | @sh' | tr -d '\'\' | tr -d '"')
    77  
    78      for key in $exp_keys; do
    79        # get the expected and the actual value for the keys
    80        exp_val=$(echo $(echo $expected_config | jq --arg KEY $key '.[$KEY]' | tr -d '"'))
    81        act_val=$(echo $(echo $actual_config | jq --arg KEY $key '.[$KEY]' | tr -d '"'))
    82  
    83        # get the absolute paths for install-dir and mod-location
    84        if [[ $key == "install-dir" ]] || [[ $key == "mod-location" ]]; then
    85          exp_val="${cwd}/${exp_val}"
    86        fi
    87        echo "expected $key: $exp_val"
    88        echo "actual $key: $act_val"
    89  
    90        # check the values
    91        if [[ "$exp_val" != "$act_val" ]]; then
    92          flag=1
    93          err="FAILED: $test_name >> key: $key ; expected: $exp_val ; actual: $act_val \n${err}"
    94        fi
    95      done
    96  
    97      # check if all passed
    98      if [[ $flag -eq 0 ]]; then
    99        echo "PASSED ✅"
   100      else
   101        echo "FAILED ❌"
   102      fi
   103      # reset flag back to 0 for the next test case 
   104      flag=0
   105    done
   106    echo -e "\n"
   107    echo -e "$err"
   108    assert_equal "$err" ""
   109    rm -f err
   110  }
   111  
   112  function teardown_file() {
   113    # list running processes
   114    ps -ef | grep steampipe
   115  
   116    # check if any processes are running
   117    num=$(ps aux | grep steampipe | grep -v bats | grep -v grep | grep -v tests/acceptance | wc -l | tr -d ' ')
   118    assert_equal $num 0
   119  }