github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/tests/acceptance/test_files/service.bats (about) 1 load "$LIB_BATS_ASSERT/load.bash" 2 load "$LIB_BATS_SUPPORT/load.bash" 3 4 @test "steampipe service start" { 5 run steampipe service start 6 assert_success 7 } 8 9 @test "steampipe service restart" { 10 run steampipe service restart 11 assert_success 12 } 13 14 @test "steampipe service stop" { 15 run steampipe service stop 16 assert_success 17 } 18 19 @test "custom database name" { 20 # Set the STEAMPIPE_INITDB_DATABASE_NAME env variable 21 export STEAMPIPE_INITDB_DATABASE_NAME="custom_db_name" 22 23 target_install_directory=$(mktemp -d) 24 25 # Start the service 26 run steampipe service start --install-dir $target_install_directory 27 echo $output 28 # Check if database name in the output is the same 29 assert_output --partial 'custom_db_name' 30 31 # Extract password from the state file 32 db_name=$(cat $target_install_directory/internal/steampipe.json | jq .database) 33 echo $db_name 34 35 # Both should be equal 36 assert_equal "$db_name" "\"custom_db_name\"" 37 38 run steampipe service stop --install-dir $target_install_directory 39 40 rm -rf $target_install_directory 41 } 42 43 @test "custom database name - should not start with uppercase characters" { 44 # Set the STEAMPIPE_INITDB_DATABASE_NAME env variable 45 export STEAMPIPE_INITDB_DATABASE_NAME="Custom_db_name" 46 47 target_install_directory=$(mktemp -d) 48 49 # Start the service 50 run steampipe service start --install-dir $target_install_directory 51 52 assert_failure 53 run steampipe service stop --force 54 rm -rf $target_install_directory 55 } 56 57 @test "start service and verify that passwords stored in .passwd and steampipe.json are same" { 58 # Start the service 59 run steampipe service start 60 61 # Extract password from the state file 62 state_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq .password) 63 echo $state_file_pass 64 65 # Extract password stored in .passwd file 66 pass_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/.passwd) 67 pass_file_pass=\"${pass_file_pass}\" 68 echo "$pass_file_pass" 69 70 # Both should be equal 71 assert_equal "$state_file_pass" "$pass_file_pass" 72 73 run steampipe service stop 74 } 75 76 @test "start service with --database-password flag and verify that the password used in flag and stored in steampipe.json are same" { 77 # Start the service with --database-password flag 78 run steampipe service start --database-password "abcd-efgh-ijkl" 79 80 # Extract password from the state file 81 state_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq .password) 82 echo $state_file_pass 83 84 # Both should be equal 85 assert_equal "$state_file_pass" "\"abcd-efgh-ijkl\"" 86 87 run steampipe service stop 88 } 89 90 @test "start service with password in env variable and verify that the password used in env and stored in steampipe.json are same" { 91 # Set the STEAMPIPE_DATABASE_PASSWORD env variable 92 export STEAMPIPE_DATABASE_PASSWORD="dcba-hgfe-lkji" 93 94 # Start the service 95 run steampipe service start 96 97 # Extract password from the state file 98 state_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq .password) 99 echo $state_file_pass 100 101 # Both should be equal 102 assert_equal "$state_file_pass" "\"dcba-hgfe-lkji\"" 103 104 run steampipe service stop 105 } 106 107 @test "start service with --database-password flag and env variable set, verify that the password used in flag gets higher precedence and is stored in steampipe.json" { 108 # Set the STEAMPIPE_DATABASE_PASSWORD env variable 109 export STEAMPIPE_DATABASE_PASSWORD="dcba-hgfe-lkji" 110 111 # Start the service with --database-password flag 112 run steampipe service start --database-password "abcd-efgh-ijkl" 113 114 # Extract password from the state file 115 state_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq .password) 116 echo $state_file_pass 117 118 # Both should be equal 119 assert_equal "$state_file_pass" "\"abcd-efgh-ijkl\"" 120 121 run steampipe service stop 122 } 123 124 @test "start service after removing .passwd file, verify new .passwd file gets created and also passwords stored in .passwd and steampipe.json are same" { 125 # Remove the .passwd file 126 rm -f $STEAMPIPE_INSTALL_DIR/internal/.passwd 127 128 # Start the service 129 run steampipe service start 130 131 # Extract password from the state file 132 state_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq .password) 133 echo $state_file_pass 134 135 # Extract password stored in new .passwd file 136 pass_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/.passwd) 137 pass_file_pass=\"${pass_file_pass}\" 138 echo "$pass_file_pass" 139 140 # Both should be equal 141 assert_equal "$state_file_pass" "$pass_file_pass" 142 143 run steampipe service stop 144 } 145 146 @test "start service with --database-password flag and verify that the password used in flag is not stored in .passwd file" { 147 # Start the service with --database-password flag 148 run steampipe service start --database-password "abcd-efgh-ijkl" 149 150 # Extract password stored in .passwd file 151 pass_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/.passwd) 152 echo "$pass_file_pass" 153 154 # Both should not be equal 155 if [[ "$pass_file_pass" != "abcd-efgh-ijkl" ]] 156 then 157 temp=1 158 fi 159 160 assert_equal "$temp" "1" 161 162 run steampipe service stop 163 } 164 165 @test "start service with password in env variable and verify that the password used in env is not stored in .passwd file" { 166 # Set the STEAMPIPE_DATABASE_PASSWORD env variable 167 export STEAMPIPE_DATABASE_PASSWORD="dcba-hgfe-lkji" 168 169 # Start the service 170 run steampipe service start 171 172 # Extract password stored in .passwd file 173 pass_file_pass=$(cat $STEAMPIPE_INSTALL_DIR/internal/.passwd) 174 echo "$pass_file_pass" 175 176 # Both should not be equal 177 if [[ "$pass_file_pass" != "dcba-hgfe-lkji" ]] 178 then 179 temp=1 180 fi 181 182 assert_equal "$temp" "1" 183 184 run steampipe service stop 185 } 186 187 ## service extensions 188 189 # tests for tablefunc module 190 191 @test "test crosstab function" { 192 # create table and insert values 193 steampipe query "CREATE TABLE ct(id SERIAL, rowid TEXT, attribute TEXT, value TEXT);" 194 steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1');" 195 steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2');" 196 steampipe query "INSERT INTO ct(rowid, attribute, value) VALUES('test1','att3','val3');" 197 198 # crosstab function 199 run steampipe query "SELECT * FROM crosstab('select rowid, attribute, value from ct where attribute = ''att2'' or attribute = ''att3'' order by 1,2') AS ct(row_name text, category_1 text, category_2 text);" 200 echo $output 201 202 # drop table 203 steampipe query "DROP TABLE ct" 204 205 # match output with expected 206 assert_equal "$output" "$(cat $TEST_DATA_DIR/expected_crosstab_results.txt)" 207 } 208 209 @test "test normal_rand function" { 210 # normal_rand function 211 run steampipe query "SELECT * FROM normal_rand(10, 5, 3);" 212 213 # previous query should pass 214 assert_success 215 } 216 217 @test "verify installed fdw version" { 218 run steampipe query "select * from steampipe_internal.steampipe_server_settings" --output=json 219 220 # extract the first mod_name from the list 221 fdw_version=$(echo $output | jq '.rows[0].fdw_version') 222 desired_fdw_version=$(cat $STEAMPIPE_INSTALL_DIR/db/versions.json | jq '.fdw_extension.version') 223 224 assert_equal "$fdw_version" "$desired_fdw_version" 225 } 226 227 @test "service stability" { 228 echo "# Setting up" 229 steampipe query "select 1" 230 echo "# Setup Done" 231 echo "# Executing tests" 232 233 # pick up the test definitions 234 tests=$(cat $FILE_PATH/test_data/source_files/service.json) 235 236 test_indices=$(echo $tests | jq '. | keys[]') 237 238 cd $FILE_PATH/test_data/mods/service_mod 239 240 # prepare a sample sql file 241 echo 'select 1' > sample.sql 242 243 # loop through the tests 244 for i in $test_indices; do 245 test_name=$(echo $tests | jq -c ".[${i}]" | jq ".name") 246 echo ">>> TEST NAME: '$test_name'" 247 # pick up the commands that need to run for this test 248 runs=$(echo $tests | jq -c ".[${i}]" | jq ".run") 249 250 # get the indices of the commands to run 251 run_indices=$(echo $runs | jq '. | keys[]') 252 253 for k in 1..10; do 254 # loop through the run indices 255 for j in $run_indices; do 256 cmd=$(echo $runs | jq ".[${j}]" | tr -d '"') 257 echo ">>>>>>Command: $cmd" 258 # run the command 259 run $command 260 261 # make sure that the command executed successfully 262 assert_success 263 done 264 265 # make sure that there are no steampipe service processes running 266 assert_equal $(ps aux | grep steampipe | grep -v bats |grep -v grep | wc -l | tr -d ' ') 0 267 done 268 done 269 270 # remove the sample sql file 271 rm -f sample.sql 272 } 273 274 @test "steampipe test database config with default listen option(hcl)" { 275 run steampipe service start 276 277 assert_success 278 279 # Extract listen from the state file 280 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c '.listen | index("'$IPV4_ADDR'")') 281 echo $listen 282 283 assert_not_equal "$listen" "null" 284 285 run steampipe service stop 286 287 assert_success 288 } 289 290 @test "steampipe test database config with local listen option(hcl)" { 291 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 292 sed -i.bak 's/LISTEN_PLACEHOLDER/local/' $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 293 294 run steampipe service start 295 296 assert_success 297 298 # Extract listen from the state file 299 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c .listen) 300 echo $listen 301 302 assert_equal "$listen" '["127.0.0.1","::1","localhost"]' 303 304 run steampipe service stop 305 306 # remove the config file 307 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 308 309 assert_success 310 } 311 312 @test "steampipe test database config with network listen option(hcl)" { 313 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 314 sed -i.bak 's/LISTEN_PLACEHOLDER/network/' $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 315 316 run steampipe service start 317 318 assert_success 319 320 # Extract listen from the state file 321 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c '.listen | index("'$IPV4_ADDR'")') 322 echo $listen 323 324 assert_not_equal "$listen" "null" 325 326 run steampipe service stop 327 328 # remove the config file 329 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 330 331 assert_success 332 } 333 334 @test "steampipe test database config with listen IPv4 loopback option(hcl)" { 335 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 336 sed -i.bak 's/LISTEN_PLACEHOLDER/127.0.0.1/' $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 337 338 run steampipe service start 339 340 assert_success 341 342 # Extract listen from the state file 343 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c .listen) 344 echo $listen 345 346 assert_equal "$listen" '["127.0.0.1"]' 347 348 run steampipe service stop 349 350 # remove the config file 351 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 352 353 assert_success 354 } 355 356 @test "steampipe test database config with listen IPv6 loopback option(hcl)" { 357 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 358 sed -i.bak 's/LISTEN_PLACEHOLDER/::1/' $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 359 360 run steampipe service start 361 362 assert_success 363 364 # Extract listen from the state file 365 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c .listen) 366 echo $listen 367 368 assert_equal "$listen" '["127.0.0.1","::1"]' 369 370 run steampipe service stop 371 372 # remove the config file 373 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 374 375 assert_success 376 } 377 378 @test "steampipe test database config with listen IPv4 address option(hcl)" { 379 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 380 381 sed -i.bak "s/LISTEN_PLACEHOLDER/$IPV4_ADDR/" $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 382 383 run steampipe service start 384 385 assert_success 386 387 # Extract listen from the state file 388 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c '.listen | index("'$IPV4_ADDR'")') 389 echo $listen 390 391 assert_not_equal "$listen" "null" 392 393 run steampipe service stop 394 395 # remove the config file 396 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 397 398 assert_success 399 } 400 401 @test "steampipe test database config with listen IPv6 address option(hcl)" { 402 if [ -z "$IPV6_ADDR" ]; then 403 skip "No IPv6 address is available, skipping test." 404 fi 405 406 cp $SRC_DATA_DIR/database_options_listen_placeholder.spc $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 407 sed -i.bak "s/LISTEN_PLACEHOLDER/$IPV6_ADDR/" $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc 408 409 run steampipe service start 410 411 assert_success 412 413 # Extract listen from the state file 414 listen=$(cat $STEAMPIPE_INSTALL_DIR/internal/steampipe.json | jq -c .listen) 415 echo $listen 416 417 assert_equal "$listen" '["127.0.0.1","'$IPV6_ADDR'"]' 418 419 run steampipe service stop 420 421 # remove the config file 422 rm -f $STEAMPIPE_INSTALL_DIR/config/database_options_listen.spc{,.bak} 423 424 assert_success 425 } 426 427 @test "verify steampipe_connection_state table is getting properly migrated" { 428 skip "needs updating when new migration is complete" 429 430 # create a temp directory to install steampipe(0.13.6) 431 tmpdir="$(mktemp -d)" 432 mkdir -p "${tmpdir}" 433 tmpdir="${tmpdir%/}" 434 435 # find the name of the zip file as per OS and arch 436 case $(uname -sm) in 437 "Darwin x86_64") target="darwin_amd64.zip" ;; 438 "Darwin arm64") target="darwin_arm64.zip" ;; 439 "Linux x86_64") target="linux_amd64.tar.gz" ;; 440 "Linux aarch64") target="linux_arm64.tar.gz" ;; 441 *) echo "Error: '$(uname -sm)' is not supported yet." 1>&2;exit 1 ;; 442 esac 443 444 # download the zip and extract 445 steampipe_uri="https://github.com/turbot/steampipe/releases/download/v0.20.6/steampipe_${target}" 446 case $(uname -s) in 447 "Darwin") zip_location="${tmpdir}/steampipe.zip" ;; 448 "Linux") zip_location="${tmpdir}/steampipe.tar.gz" ;; 449 *) echo "Error: steampipe is not supported on '$(uname -s)' yet." 1>&2;exit 1 ;; 450 esac 451 curl --fail --location --progress-bar --output "$zip_location" "$steampipe_uri" 452 tar -xf "$zip_location" -C "$tmpdir" 453 454 # install a couple of plugins which can work with default config 455 $tmpdir/steampipe --install-dir $tmpdir plugin install chaos net --progress=false 456 $tmpdir/steampipe --install-dir $tmpdir query "select * from steampipe_internal.steampipe_connection_state" --output json 457 458 run steampipe --install-dir $tmpdir query "select * from steampipe_internal.steampipe_connection_state" --output json 459 460 rm -rf $tmpdir 461 462 assert_success 463 } 464 465 function setup_file() { 466 export BATS_TEST_TIMEOUT=180 467 echo "# setup_file()">&3 468 export IPV4_ADDR=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1) 469 export IPV6_ADDR=$(ifconfig | grep -Eo 'inet6 (addr:)?([0-9a-f]*:){7}[0-9a-f]*' | grep -Eo '([0-9a-f]*:){7}[0-9a-f]*' | head -n 1) 470 } 471 472 function teardown_file() { 473 # list running processes 474 ps -ef | grep steampipe 475 476 # check if any processes are running 477 num=$(ps aux | grep steampipe | grep -v bats | grep -v grep | grep -v tests/acceptance | wc -l | tr -d ' ') 478 assert_equal $num 0 479 }