github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/tests/acceptance/test_files/cache.bats (about) 1 load "$LIB_BATS_ASSERT/load.bash" 2 load "$LIB_BATS_SUPPORT/load.bash" 3 4 @test "steampipe cache functionality check ON" { 5 run steampipe plugin install chaos 6 cd $FUNCTIONALITY_TEST_MOD 7 8 run steampipe check benchmark.check_cache_benchmark --export test.json --max-parallel 1 9 10 # store the unique number from 1st control in `content` 11 content=$(cat test.json | jq '.groups[].controls[0].results[0].resource') 12 # store the unique number from 2nd control in `new_content` 13 new_content=$(cat test.json | jq '.groups[].controls[1].results[0].resource') 14 echo $content 15 echo $new_content 16 17 # verify that `content` and `new_content` are the same 18 assert_equal "$new_content" "$content" 19 rm -f test.json 20 } 21 22 @test "steampipe cache functionality check OFF" { 23 cd $FUNCTIONALITY_TEST_MOD 24 25 # set the env variable to false 26 export STEAMPIPE_CACHE=false 27 run steampipe check benchmark.check_cache_benchmark --export test.json --max-parallel 1 28 29 # store the unique number from 1st control in `content` 30 content=$(cat test.json | jq '.groups[].controls[0].results[0].resource') 31 # store the unique number from 2nd control in `new_content` 32 new_content=$(cat test.json | jq '.groups[].controls[1].results[0].resource') 33 echo $content 34 echo $new_content 35 36 # verify that `content` and `new_content` are not the same 37 if [[ "$content" == "$new_content" ]]; then 38 flag=1 39 else 40 flag=0 41 fi 42 assert_equal "$flag" "0" 43 rm -f test.json 44 } 45 46 @test "steampipe cache functionality check ON(check content of results, not just the unique column)" { 47 # start service to turn on caching 48 steampipe service start 49 50 steampipe query "select unique_col, a, b from chaos_cache_check" --output json &> output1.json 51 52 steampipe query "select unique_col, a, b from chaos_cache_check" --output json &> output2.json 53 54 # stop service 55 steampipe service stop 56 57 # verify that the json contents of output1 and output2 files are the same 58 run jd -f patch output1.json output2.json 59 echo $output 60 61 diff=$($FILE_PATH/json_patch.sh $output) 62 echo $diff 63 # check if there is no diff returned by the script 64 assert_equal "$diff" "" 65 66 rm -f output1.json 67 rm -f output2.json 68 } 69 70 @test "verify cache ttl works when set in Environment" { 71 cp $SRC_DATA_DIR/chaos_no_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 72 73 # start the service 74 steampipe service start 75 76 export STEAMPIPE_CACHE_TTL=10 77 78 # cache functionality check since cache=true in options 79 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out1.json 80 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out2.json 81 82 # wait for 15 seconds - the value of the TTL in environment 83 sleep 15 84 85 # run the query again 86 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out3.json 87 88 # stop the service 89 steampipe service stop 90 91 unique1=$(cat out1.json | jq '.rows[0].unique_col') 92 unique2=$(cat out2.json | jq '.rows[0].unique_col') 93 unique3=$(cat out3.json | jq '.rows[0].unique_col') 94 # remove the output and the config files 95 rm -f out*.json 96 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 97 98 # the first and the seconds query should have the same value 99 assert_equal "$unique1" "$unique2" 100 # the third query should have a different value 101 assert_not_equal "$unique1" "$unique3" 102 } 103 104 @test "verify cache ttl works when set in connections options" { 105 skip "skipping - this is a deprecated functionality" 106 107 cp $SRC_DATA_DIR/chaos_ttl_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_ttl_options.spc 108 109 # start the service 110 steampipe service start 111 112 # cache functionality check since cache=true in options 113 steampipe query "select unique_col from chaos_ttl_options.chaos_cache_check where id=2" --output json > out1.json 114 steampipe query "select unique_col from chaos_ttl_options.chaos_cache_check where id=2" --output json > out2.json 115 116 # wait for 15 seconds - the value of the TTL in connection options 117 sleep 15 118 119 # run the query again 120 steampipe query "select unique_col from chaos_ttl_options.chaos_cache_check where id=2" --output json > out3.json 121 122 # stop the service 123 steampipe service stop 124 125 unique1=$(cat out1.json | jq '.[].unique_col') 126 unique2=$(cat out2.json | jq '.[].unique_col') 127 unique3=$(cat out3.json | jq '.[].unique_col') 128 129 # remove the output and the config files 130 rm -f out*.json 131 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_ttl_options.spc 132 133 # the first and the seconds query should have the same value 134 assert_equal "$unique1" "$unique2" 135 # the third query should have a different value 136 assert_not_equal "$unique1" "$unique3" 137 } 138 139 @test "verify cache ttl works when set in database options" { 140 export STEAMPIPE_LOG=info 141 142 cp $SRC_DATA_DIR/chaos_no_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 143 cp $SRC_DATA_DIR/default_cache_ttl_10.spc $STEAMPIPE_INSTALL_DIR/config/default.spc 144 145 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 146 147 # start the service 148 steampipe service start 149 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 150 151 # cache functionality check since cache=true in options 152 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out1.json 153 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 154 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out2.json 155 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 156 157 # wait for 15 seconds - the value of the TTL in connection options 158 sleep 15 159 160 # run the query again 161 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out3.json 162 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 163 164 # stop the service 165 steampipe service stop 166 167 unique1=$(cat out1.json | jq '.rows[0].unique_col') 168 unique2=$(cat out2.json | jq '.rows[0].unique_col') 169 unique3=$(cat out3.json | jq '.rows[0].unique_col') 170 171 cat $STEAMPIPE_INSTALL_DIR/config/default.spc 172 cat $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 173 174 # remove the output and the config files 175 rm -f out*.json 176 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 177 rm -f $STEAMPIPE_INSTALL_DIR/config/default.spc 178 179 # the first and the seconds query should have the same value 180 assert_equal "$unique1" "$unique2" 181 # the third query should have a different value 182 assert_not_equal "$unique1" "$unique3" 183 } 184 185 @test "test caching with cache=true in workspace profile" { 186 cp $SRC_DATA_DIR/chaos_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_options.spc 187 cp $SRC_DATA_DIR/workspace_cache_enabled.spc $STEAMPIPE_INSTALL_DIR/config/workspace_cache_enabled.spc 188 189 # cache functionality check since cache=true in workspace profile 190 cd $CONFIG_PARSING_TEST_MOD 191 run steampipe check benchmark.config_parsing_benchmark --export test.json --max-parallel 1 192 193 # store the unique number from 1st control in `content` 194 content=$(cat test.json | jq '.groups[].controls[0].results[0].resource') 195 # store the unique number from 2nd control in `new_content` 196 new_content=$(cat test.json | jq '.groups[].controls[1].results[0].resource') 197 echo $content 198 echo $new_content 199 # remove the output and the config files 200 rm -f test.json 201 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_options.spc 202 rm -f $STEAMPIPE_INSTALL_DIR/config/workspace_cache_enabled.spc 203 204 # verify that `content` and `new_content` are the same 205 assert_equal "$new_content" "$content" 206 } 207 208 @test "test caching with cache=false in workspace profile" { 209 cp $SRC_DATA_DIR/chaos_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_options.spc 210 cp $SRC_DATA_DIR/workspace_cache_disabled.spc $STEAMPIPE_INSTALL_DIR/config/workspace_cache_disabled.spc 211 212 # cache functionality check since cache=false in workspace profile 213 cd $CONFIG_PARSING_TEST_MOD 214 run steampipe check benchmark.config_parsing_benchmark --export test.json --max-parallel 1 215 216 # store the unique number from 1st control in `content` 217 content=$(cat test.json | jq '.groups[].controls[0].results[0].resource') 218 # store the unique number from 2nd control in `new_content` 219 new_content=$(cat test.json | jq '.groups[].controls[1].results[0].resource') 220 echo $content 221 echo $new_content 222 223 # verify that `content` and `new_content` are not the same 224 if [[ "$content" == "$new_content" ]]; then 225 flag=1 226 else 227 flag=0 228 fi 229 # remove the output and the config files 230 rm -f test.json 231 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_options.spc 232 rm -f $STEAMPIPE_INSTALL_DIR/config/workspace_cache_disabled.spc 233 234 assert_equal "$flag" "0" 235 } 236 237 @test "verify cache ttl works when set in workspace profile" { 238 cp $FILE_PATH/test_data/source_files/workspace_cache_ttl.spc $STEAMPIPE_INSTALL_DIR/config/workspace.spc 239 cp $SRC_DATA_DIR/chaos_no_options.spc $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 240 241 # start the service 242 steampipe service start 243 244 # cache functionality check since cache=true in options 245 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out1.json 246 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out2.json 247 248 # wait for 15 seconds - the value of the TTL in connection options 249 sleep 15 250 251 # run the query again 252 steampipe query "select unique_col from chaos_no_options.chaos_cache_check where id=2" --output json > out3.json 253 254 # stop the service 255 steampipe service stop 256 257 unique1=$(cat out1.json | jq '.rows[0].unique_col') 258 unique2=$(cat out2.json | jq '.rows[0].unique_col') 259 unique3=$(cat out3.json | jq '.rows[0].unique_col') 260 261 # remove the output and the config files 262 rm -f out*.json 263 rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_no_options.spc 264 rm -f $STEAMPIPE_INSTALL_DIR/config/workspace.spc 265 266 # the first and the seconds query should have the same value 267 assert_equal "$unique1" "$unique2" 268 # the third query should have a different value 269 assert_not_equal "$unique1" "$unique3" 270 } 271 272 function teardown_file() { 273 # list running processes 274 ps -ef | grep steampipe 275 276 # check if any processes are running 277 num=$(ps aux | grep steampipe | grep -v bats | grep -v grep | grep -v tests/acceptance | wc -l | tr -d ' ') 278 assert_equal $num 0 279 }