github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/tests/acceptance/test_files/mod.bats (about) 1 load "$LIB_BATS_ASSERT/load.bash" 2 load "$LIB_BATS_SUPPORT/load.bash" 3 4 ############### QUERIES ############### 5 6 @test "query with default params and no params passed through CLI" { 7 cd $FUNCTIONALITY_TEST_MOD 8 run steampipe query query.query_params_with_all_defaults --output json 9 10 # store the reason field in `content` 11 content=$(echo $output | jq '.rows[0].reason') 12 13 assert_equal "$content" '"default_parameter_1 default_parameter_2 default_parameter_3"' 14 } 15 16 @test "query with default params and some positional params passed through CLI" { 17 cd $FUNCTIONALITY_TEST_MOD 18 run steampipe query "query.query_params_with_all_defaults(\"command_param_1\")" --output json 19 20 # store the reason field in `content` 21 content=$(echo $output | jq '.rows[0].reason') 22 23 assert_equal "$content" '"command_param_1 default_parameter_2 default_parameter_3"' 24 } 25 26 @test "query with default params and some named params passed through CLI" { 27 cd $FUNCTIONALITY_TEST_MOD 28 run steampipe query "query.query_params_with_all_defaults(p1 => \"command_param_1\")" --output json 29 30 # store the reason field in `content` 31 content=$(echo $output | jq '.rows[0].reason') 32 33 assert_equal "$content" '"command_param_1 default_parameter_2 default_parameter_3"' 34 } 35 36 @test "query with no default params and no params passed through CLI" { 37 cd $FUNCTIONALITY_TEST_MOD 38 run steampipe query query.query_params_with_no_defaults --output json 39 40 assert_output --partial 'failed to resolve args for functionality_test_mod.query.query_params_with_no_defaults: p1,p2,p3' 41 } 42 43 @test "query with no default params and all params passed through CLI" { 44 cd $FUNCTIONALITY_TEST_MOD 45 run steampipe query "query.query_params_with_all_defaults(\"command_param_1\",\"command_param_2\",\"command_param_3\")" --output json 46 47 # store the reason field in `content` 48 content=$(echo $output | jq '.rows[0].reason') 49 50 assert_equal "$content" '"command_param_1 command_param_2 command_param_3"' 51 } 52 53 @test "query specific array index from param - DISABLED" { 54 # cd $FUNCTIONALITY_TEST_MOD 55 # run steampipe query query.query_array_params_with_default --output json 56 57 # # store the reason field in `content` 58 # content=$(echo $output | jq '.rows[0].reason') 59 60 # assert_equal "$content" '"default_p1_element_02"' 61 } 62 63 @test "query with invalid param syntax" { 64 cd $FUNCTIONALITY_TEST_MOD 65 run steampipe query "query.query_map_params_with_default(\"foo \")" --output json 66 67 # should return an error `invalid input syntax for type json` 68 assert_output --partial 'invalid input syntax for type json' 69 cd - 70 } 71 72 @test "query specific property from map param" { 73 cd $FUNCTIONALITY_TEST_MOD 74 run steampipe query query.query_map_params_with_default --output json 75 76 # store the reason field in `content` 77 content=$(echo $output | jq '.rows[0].reason') 78 79 assert_equal "$content" '"default_property_value_01"' 80 } 81 82 ############### CONTROLS ############### 83 84 @test "control with default params and no args passed in control" { 85 cd $FUNCTIONALITY_TEST_MOD 86 run steampipe check control.query_params_with_defaults_and_no_args --export test.json 87 echo $output 88 ls 89 90 # store the reason field in `content` 91 content=$(cat test.json | jq '.controls[0].results[0].reason') 92 93 assert_equal "$content" '"default_parameter_1 default_parameter_2 default_parameter_3"' 94 rm -f test.json 95 } 96 97 @test "control with default params and partial named args passed in control" { 98 cd $FUNCTIONALITY_TEST_MOD 99 run steampipe check control.query_params_with_defaults_and_partial_named_args --export test.json 100 101 # store the reason field in `content` 102 content=$(cat test.json | jq '.controls[0].results[0].reason') 103 104 assert_equal "$content" '"default_parameter_1 command_parameter_2 default_parameter_3"' 105 rm -f test.json 106 } 107 108 @test "control with default params and partial positional args passed in control" { 109 cd $FUNCTIONALITY_TEST_MOD 110 run steampipe check control.query_params_with_defaults_and_partial_positional_args --export test.json 111 112 # store the reason field in `content` 113 content=$(cat test.json | jq '.controls[0].results[0].reason') 114 115 assert_equal "$content" '"command_parameter_1 default_parameter_2 default_parameter_3"' 116 rm -f test.json 117 } 118 119 @test "control with default params and all named args passed in control" { 120 cd $FUNCTIONALITY_TEST_MOD 121 run steampipe check control.query_params_with_defaults_and_all_named_args --export test.json 122 123 # store the reason field in `content` 124 content=$(cat test.json | jq '.controls[0].results[0].reason') 125 126 assert_equal "$content" '"command_parameter_1 command_parameter_2 command_parameter_3"' 127 rm -f test.json 128 } 129 130 @test "control with default params and all positional args passed in control" { 131 cd $FUNCTIONALITY_TEST_MOD 132 run steampipe check control.query_params_with_defaults_and_all_positional_args --export test.json 133 134 # store the reason field in `content` 135 content=$(cat test.json | jq '.controls[0].results[0].reason') 136 137 assert_equal "$content" '"command_parameter_1 command_parameter_2 command_parameter_3"' 138 rm -f test.json 139 } 140 141 @test "control with no default params and no args passed in control" { 142 cd $FUNCTIONALITY_TEST_MOD 143 run steampipe check control.query_params_with_no_defaults_and_no_args --output json 144 145 # should return an error `failed to resolve value for 3 parameters` 146 echo $output 147 [ $(echo $output | grep "failed to resolve value for 3 parameters" | wc -l | tr -d ' ') -eq 0 ] 148 } 149 150 @test "control with no default params and all args passed in control" { 151 cd $FUNCTIONALITY_TEST_MOD 152 run steampipe check control.query_params_with_no_defaults_with_named_args --export test.json 153 154 # store the reason field in `content` 155 content=$(cat test.json | jq '.controls[0].results[0].reason') 156 157 assert_equal "$content" '"command_parameter_1 command_parameter_2 command_parameter_3"' 158 rm -f test.json 159 } 160 161 @test "control to access specific array index from param - DISABLED" { 162 # cd $FUNCTIONALITY_TEST_MOD 163 # run steampipe check control.query_params_array_with_default --export test.json 164 165 # # store the reason field in `content` 166 # content=$(cat test.json | jq '.controls[0].results[0].reason') 167 168 # assert_equal "$content" '"default_p1_element_02"' 169 # rm -f test.json 170 } 171 172 @test "control to access specific property from map" { 173 cd $FUNCTIONALITY_TEST_MOD 174 run steampipe check control.query_params_map_with_default --export test.json 175 176 # store the reason field in `content` 177 content=$(cat test.json | jq '.controls[0].results[0].reason') 178 179 assert_equal "$content" '"default_property_value_01"' 180 rm -f test.json 181 } 182 183 @test "control with invaild args syntax passed in control" { 184 cd $FUNCTIONALITY_TEST_MOD 185 run steampipe check control.query_params_invalid_arg_syntax --output json 186 187 # store the results field in `content` 188 content=$(cat output.json | jq '.controls[0].results') 189 190 # should return an error `invalid input syntax for type json`, so the results should be empty 191 assert_equal "$content" "" 192 } 193 194 @test "control with inline sql with partial named args passed in control" { 195 cd $FUNCTIONALITY_TEST_MOD 196 run steampipe check control.query_inline_sql_from_control_with_partial_named_args --export test.json 197 198 # store the reason field in `content` 199 content=$(cat test.json | jq '.controls[0].results[0].reason') 200 201 assert_equal "$content" '"command_parameter_1 default_parameter_2 command_parameter_3"' 202 rm -f test.json 203 } 204 205 @test "control with inline sql with partial positional args passed in control" { 206 cd $FUNCTIONALITY_TEST_MOD 207 run steampipe check control.query_inline_sql_from_control_with_partial_positional_args --export test.json 208 209 # store the reason field in `content` 210 content=$(cat test.json | jq '.controls[0].results[0].reason') 211 212 assert_equal "$content" '"command_parameter_1 command_parameter_2 default_parameter_3"' 213 rm -f test.json 214 } 215 216 @test "control with inline sql with no args passed in control" { 217 cd $FUNCTIONALITY_TEST_MOD 218 run steampipe check control.query_inline_sql_from_control_with_no_args --export test.json 219 220 # store the reason field in `content` 221 content=$(cat test.json | jq '.controls[0].results[0].reason') 222 223 assert_equal "$content" '"default_parameter_1 default_parameter_2 default_parameter_3"' 224 rm -f test.json 225 } 226 227 @test "control with inline sql with all named args passed in control" { 228 cd $FUNCTIONALITY_TEST_MOD 229 run steampipe check control.query_inline_sql_from_control_with_all_named_args --export test.json 230 231 # store the reason field in `content` 232 content=$(cat test.json | jq '.controls[0].results[0].reason') 233 234 assert_equal "$content" '"command_parameter_1 command_parameter_2 command_parameter_3"' 235 rm -f test.json 236 } 237 238 @test "control with inline sql with all positional args passed in control" { 239 cd $FUNCTIONALITY_TEST_MOD 240 run steampipe check control.query_inline_sql_from_control_with_all_positional_args --export test.json 241 242 # store the reason field in `content` 243 content=$(cat test.json | jq '.controls[0].results[0].reason') 244 245 assert_equal "$content" '"command_parameter_1 command_parameter_2 command_parameter_3"' 246 rm -f test.json 247 } 248 249 ## 250 251 @test "control with neither query property nor sql property" { 252 cd $BAD_TEST_MOD_DIR 253 run steampipe check control.control_fail_with_no_query_no_sql --output json 254 255 # store the results field in `content` 256 content=$(cat output.json | jq '.controls[0].results') 257 258 # should return an error `must define either a 'sql' property or a 'query' property`, 259 # so the results should be empty 260 assert_equal "$content" "" 261 } 262 263 @test "control with both query property and sql property" { 264 cd $BAD_TEST_MOD_DIR 265 run steampipe check control.control_fail_with_both_query_and_sql --output json 266 267 # store the results field in `content` 268 content=$(cat output.json | jq '.controls[0].results') 269 270 # should return an error `must define either a 'sql' property or a 'query' property`, 271 # so the results should be empty 272 assert_equal "$content" "" 273 } 274 275 @test "control with both params property and query property" { 276 cd $BAD_TEST_MOD_DIR 277 run steampipe check control.control_fail_with_params_and_query --output json 278 279 # store the results field in `content` 280 content=$(cat output.json | jq '.controls[0].results') 281 282 # should return an error `has 'query' property set so cannot define param blocks`, 283 # so the results should be empty 284 assert_equal "$content" "" 285 } 286 287 @test "control referring to query with no params definitions and named args passed" { 288 cd $BAD_TEST_MOD_DIR 289 run steampipe check control.control_fail_with_query_with_no_def_and_named_args_passed --output json 290 291 # store the results field in `content` 292 content=$(cat output.json | jq '.controls[0].results') 293 294 # should return an error since query has o parameter definitions, 295 # so the results should be empty 296 assert_equal "$content" "" 297 } 298 299 @test "control referring to query with no params defaults and partial positional args passed" { 300 cd $BAD_TEST_MOD_DIR 301 run steampipe check control.control_fail_with_insufficient_positional_args_passed --output json 302 303 # store the results field in `content` 304 content=$(cat output.json | jq '.controls[0].results') 305 306 # should return an error `failed to resolve value for 3 parameters` 307 # so the results should be empty 308 assert_equal "$content" "" 309 } 310 311 @test "control referring to query with no params defaults and partial named args passed" { 312 cd $BAD_TEST_MOD_DIR 313 run steampipe check control.control_fail_with_insufficient_named_args_passed --output json 314 315 # store the results field in `content` 316 content=$(cat output.json | jq '.controls[0].results') 317 318 # should return an error `failed to resolve value for 3 parameters`, 319 # so the results should be empty 320 assert_equal "$content" "" 321 } 322 323 @test "control referring to query with empty list as param" { 324 cd $STRING_LIST_TEST_MOD 325 run steampipe check all --var 'string_list=[]' 326 assert_success 327 } 328 329 ## traversal 330 331 # This test consists of a mod with nested folders, with mod.sp file within one of them(folder11). 332 # Running steampipe check from folder111 should give us the result since the mod.sp file is present somewhere 333 # up the directory tree 334 @test "load a mod from an arbitrarily nested sub folder - PASS" { 335 # go to the nested sub directory within the mod 336 cd $FILE_PATH/test_data/mods/nested_mod/folder1/folder11/folder111 337 338 run steampipe check all 339 assert_success 340 cd - 341 } 342 343 # This test consists of a mod with nested folders, with mod.sp file within one of them(folder11). 344 # Running steampipe check from folder1(i.e. _above_ the mod folder) should return an error, since the mod.sp file is present nowhere 345 # up the directory tree 346 @test "load a mod from an arbitrarily nested sub folder - FAIL" { 347 # go to the nested sub directory within the mod 348 cd $FILE_PATH/test_data/mods/nested_mod/folder1 349 350 run steampipe check all 351 assert_output --partial "This command requires a mod definition file (mod.sp) - could not find in the current directory tree." 352 cd - 353 } 354 355 # This test consists of a mod with nested folders, with no mod.sp file in any of them. 356 # Running steampipe check from folder11 should return an error, since the mod.sp file is present nowhere 357 # up the directory tree 358 # Running steampipe query from folder11 should give us the result since query is independent of mod.sp file. 359 @test "check and query from an arbitrarily nested sub folder - PASS & FAIL" { 360 # go to the nested sub directory within the mod 361 cd $FILE_PATH/test_data/mods/nested_mod_no_mod_file/folder1/folder11 362 363 run steampipe check all 364 assert_output --partial "This command requires a mod definition file (mod.sp) - could not find in the current directory tree." 365 366 run steampipe query control.check_1 367 assert_success 368 cd - 369 } 370 371 ## parsing 372 373 @test "mod parsing" { 374 skip "Skipping for now, need to re-design" 375 # install necessary plugins 376 steampipe plugin install aws oci azure azuread 377 378 # create a directory to install the mods 379 target_directory=$(mktemp -d) 380 cd $target_directory 381 382 # install steampipe-mod-aws-compliance 383 steampipe mod install github.com/turbot/steampipe-mod-aws-compliance 384 # go to the mod directory and run steampipe query to verify parsing 385 cd .steampipe/mods/github.com/turbot/steampipe-mod-aws-compliance@* 386 run steampipe query "select 1" 387 assert_success 388 cd - 389 390 # install steampipe-mod-aws-thrifty 391 steampipe mod install github.com/turbot/steampipe-mod-aws-thrifty 392 # go to the mod directory and run steampipe query to verify parsing 393 cd .steampipe/mods/github.com/turbot/steampipe-mod-aws-thrifty@* 394 run steampipe query "select 1" 395 assert_success 396 cd - 397 398 # install steampipe-mod-oci-compliance 399 steampipe mod install github.com/turbot/steampipe-mod-oci-compliance 400 # go to the mod directory and run steampipe query to verify parsing 401 cd .steampipe/mods/github.com/turbot/steampipe-mod-oci-compliance@* 402 run steampipe query "select 1" 403 assert_success 404 cd - 405 406 # install steampipe-mod-azure-compliance 407 steampipe mod install github.com/turbot/steampipe-mod-azure-compliance 408 # go to the mod directory and run steampipe query to verify parsing 409 cd .steampipe/mods/github.com/turbot/steampipe-mod-azure-compliance@* 410 run steampipe query "select 1" 411 assert_success 412 cd - 413 414 # remove the directory 415 cd .. 416 rm -rf $target_directory 417 418 419 # remove the connection config files 420 rm -f $STEAMPIPE_INSTALL_DIR/config/aws.spc 421 rm -f $STEAMPIPE_INSTALL_DIR/config/ibm.spc 422 rm -f $STEAMPIPE_INSTALL_DIR/config/oci.spc 423 rm -f $STEAMPIPE_INSTALL_DIR/config/azure.spc 424 rm -f $STEAMPIPE_INSTALL_DIR/config/azuread.spc 425 426 # uninstall the plugins 427 steampipe plugin uninstall aws oci azure azuread 428 429 # rerun steampipe to make sure they are removed from steampipe 430 steampipe query "select 1" 431 } 432 433 ## dependency resolution tests 434 435 @test "complex mod dependency resolution - test vars resolution from require section of local mod" { 436 cd $FILE_PATH/test_data/mods/local_mod_with_args_in_require 437 steampipe mod install 438 439 run steampipe query dependency_vars_1.query.version --output csv 440 # check the output - query should use the value of variable from the local 441 # mod require section("v3.0.0") which will give the output: 442 # +--------+----------+--------+ 443 # | reason | resource | status | 444 # +--------+----------+--------+ 445 # | v3.0.0 | v3.0.0 | ok | 446 # +--------+----------+--------+ 447 assert_output 'reason,resource,status 448 v3.0.0,v3.0.0,ok' 449 450 rm -rf .steampipe/ 451 rm -rf .mod.cache.json 452 } 453 454 function teardown_file() { 455 # list running processes 456 ps -ef | grep steampipe 457 458 # check if any processes are running 459 num=$(ps aux | grep steampipe | grep -v bats | grep -v grep | grep -v tests/acceptance | wc -l | tr -d ' ') 460 assert_equal $num 0 461 }