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

     1  load "$LIB_BATS_ASSERT/load.bash"
     2  load "$LIB_BATS_SUPPORT/load.bash"
     3  
     4  @test "plugin install" {
     5    run steampipe plugin install chaos
     6    assert_success
     7    steampipe plugin uninstall chaos
     8  }
     9  
    10  @test "plugin install from stream" {
    11    run steampipe plugin install chaos@0.4
    12    assert_success
    13    steampipe plugin uninstall chaos@0.4
    14  }
    15  
    16  @test "plugin install from stream (prefixed with v)" {
    17    run steampipe plugin install chaos@v0.4
    18    assert_success
    19    steampipe plugin uninstall chaos@0.4
    20  }
    21  
    22  @test "plugin install from caret constraint" {
    23    run steampipe plugin install chaos@^0.4
    24    assert_success
    25    steampipe plugin uninstall chaos@^0.4
    26  }
    27  
    28  @test "plugin install from tilde constraint" {
    29    run steampipe plugin install chaos@~0.4.0
    30    assert_success
    31    steampipe plugin uninstall chaos@~0.4.0
    32  }
    33  
    34  @test "plugin install from wildcard constraint" {
    35    run steampipe plugin install chaos@0.4.*
    36    assert_success
    37    steampipe plugin uninstall chaos@0.4.*
    38  }
    39  
    40  @test "plugin install gte constraint" {
    41    run steampipe plugin install "chaos@>=0.4"
    42    assert_success
    43    steampipe plugin uninstall "chaos@>=0.4"
    44  }
    45  
    46  @test "create a local plugin, add connection and query" {
    47    run steampipe plugin install chaos
    48  
    49    # create a local plugin directory
    50    mkdir $STEAMPIPE_INSTALL_DIR/plugins/local
    51    mkdir $STEAMPIPE_INSTALL_DIR/plugins/local/myplugin
    52    # use the chaos plugin binary to get a plugin binary for the local plugin
    53    cp $STEAMPIPE_INSTALL_DIR/plugins/hub.steampipe.io/plugins/turbot/chaos@latest/steampipe-plugin-chaos.plugin $STEAMPIPE_INSTALL_DIR/plugins/local/myplugin/myplugin.plugin
    54    # create a connection config file for the new local plugin
    55    echo "connection \"myplugin\" {
    56      plugin = \"local/myplugin\"
    57    }" > $STEAMPIPE_INSTALL_DIR/config/myplugin.spc
    58  
    59    run steampipe query "select * from myplugin.chaos_all_column_types"
    60    assert_success
    61    run steampipe plugin list
    62    assert_output --partial "local/myplugin"
    63  }
    64  
    65  @test "start service, install plugin and query" {
    66    skip
    67    # start service
    68    steampipe service start
    69  
    70    # install plugin
    71    steampipe plugin install chaos
    72  
    73    steampipe query "select 1"
    74  
    75    # query the plugin
    76    run steampipe query "select time_col from chaos_cache_check limit 1"
    77    # check if the query passes
    78    assert_success
    79  
    80    # stop service
    81    steampipe service stop
    82  
    83    # check service status
    84    run steampipe service status
    85  
    86    assert_output "$output" "Service is not running"
    87  }
    88  
    89  @test "steampipe plugin list" {
    90      run steampipe plugin list
    91      assert_success
    92  }
    93  
    94  @test "steampipe plugin list works with disabled connections" {
    95    rm -f $STEAMPIPE_INSTALL_DIR/config/*
    96    cp $SRC_DATA_DIR/chaos_conn_import_disabled.spc $STEAMPIPE_INSTALL_DIR/config/chaos_conn_import_disabled.spc
    97    run steampipe plugin list 2>&3 1>&3
    98    rm -f $STEAMPIPE_INSTALL_DIR/config/chaos_conn_import_disabled.spc
    99    assert_success
   100  }
   101  
   102  @test "plugin list - output table and json" {
   103    export STEAMPIPE_DISPLAY_WIDTH=100
   104  
   105    # Create a copy of the install directory
   106    copy_install_directory
   107  
   108    steampipe plugin install hackernews@0.8.0 bitbucket@0.7.1 --progress=false --install-dir $MY_TEST_COPY
   109  
   110    # check table output
   111    run steampipe plugin list --install-dir $MY_TEST_COPY
   112  
   113    assert_equal "$output" "$(cat $TEST_DATA_DIR/expected_plugin_list_table.txt)"
   114  
   115    # check json output
   116    steampipe plugin list --install-dir $MY_TEST_COPY --output json > output.json
   117    run jd $TEST_DATA_DIR/expected_plugin_list_json.json output.json
   118    echo $output
   119    assert_success
   120    rm -rf $MY_TEST_COPY
   121  }
   122  
   123  @test "plugin list - output table and json (with a missing plugin)" {
   124    export STEAMPIPE_DISPLAY_WIDTH=100
   125  
   126    # Create a copy of the install directory
   127    copy_install_directory
   128  
   129    steampipe plugin install hackernews@0.8.0 bitbucket@0.7.1 --progress=false --install-dir $MY_TEST_COPY
   130    # uninstall a plugin but dont remove the config - to simulate the missing plugin scenario
   131    steampipe plugin uninstall hackernews@0.8.0 --install-dir $MY_TEST_COPY
   132  
   133    # check table output
   134    run steampipe plugin list --install-dir $MY_TEST_COPY
   135    assert_equal "$output" "$(cat $TEST_DATA_DIR/expected_plugin_list_table_with_missing_plugins.txt)"
   136  
   137    # check json output
   138    steampipe plugin list --install-dir $MY_TEST_COPY --output json > output.json
   139  
   140    run jd $TEST_DATA_DIR/expected_plugin_list_json_with_missing_plugins.json output.json
   141    echo $output
   142    assert_success
   143    rm -rf $MY_TEST_COPY
   144  }
   145  
   146  # # TODO: finds other ways to simulate failed plugins
   147  
   148  @test "plugin list - output table and json (with a failed plugin)" {
   149    skip "finds other ways to simulate failed plugins"
   150    export STEAMPIPE_DISPLAY_WIDTH=100
   151    
   152    # Create a copy of the install directory
   153    copy_install_directory
   154  
   155    steampipe plugin install hackernews@0.8.0 bitbucket@0.7.1 --progress=false --install-dir $MY_TEST_COPY
   156    # remove the contents of a plugin execuatable to simulate the failed plugin scenario
   157    cat /dev/null > $MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/hackernews@0.8.0/steampipe-plugin-hackernews.plugin
   158  
   159    # check table output
   160    run steampipe plugin list --install-dir $MY_TEST_COPY
   161    echo $output
   162    assert_equal "$output" "$(cat $TEST_DATA_DIR/expected_plugin_list_table_with_failed_plugins.txt)"
   163  
   164    # check json output
   165    steampipe plugin list --install-dir $MY_TEST_COPY --output json > output.json
   166    run jd $TEST_DATA_DIR/expected_plugin_list_json_with_failed_plugins.json output.json
   167    echo $output
   168    assert_success
   169    rm -rf $MY_TEST_COPY
   170  }
   171  
   172  @test "verify that installing plugins creates individual version.json files" {
   173    # Create a copy of the install directory
   174    copy_install_directory
   175  
   176    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   177    assert_success
   178    
   179    vFile1="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/version.json"
   180    vFile2="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/chaos@latest/version.json"
   181    
   182    [ ! -f $vFile1 ] && fail "could not find $vFile1"
   183    [ ! -f $vFile2 ] && fail "could not find $vFile2"
   184    
   185    rm -rf $MY_TEST_COPY
   186  }
   187  
   188  @test "verify that backfilling of individual plugin version.json works" {
   189    # Create a copy of the install directory
   190    copy_install_directory
   191  
   192    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   193    assert_success
   194    
   195    vFile1="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/version.json"
   196    vFile2="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/chaos@latest/version.json"
   197    
   198    file1Content=$(cat $vFile1)
   199    file2Content=$(cat $vFile2)
   200    
   201    # remove the individual version files
   202    rm -f $vFile1
   203    rm -f $vFile2
   204    
   205    # run steampipe again so that the plugin version files get backfilled
   206    run steampipe plugin list --install-dir $MY_TEST_COPY
   207    
   208    [ ! -f $vFile1 ] && fail "could not find $vFile1"
   209    [ ! -f $vFile2 ] && fail "could not find $vFile2"
   210    echo "$file1Content" > $MY_TEST_COPY/f1.json
   211    echo "$file2Content" > $MY_TEST_COPY/f2.json
   212    cat "$vFile1" > $MY_TEST_COPY/v1.json
   213    cat "$vFile2" > $MY_TEST_COPY/v2.json
   214  
   215    # Compare the json file contents
   216    run jd "$MY_TEST_COPY/f1.json" "$MY_TEST_COPY/v1.json"
   217    echo $output
   218    assert_success
   219  
   220    run jd "$MY_TEST_COPY/f2.json" "$MY_TEST_COPY/v2.json"
   221    echo $output
   222    assert_success
   223    rm -rf $MY_TEST_COPY
   224  }
   225  
   226  @test "verify that backfilling of individual plugin version.json works where it is only partially backfilled" {
   227    # Create a copy of the install directory
   228    copy_install_directory
   229  
   230    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   231    assert_success
   232    
   233    vFile1="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/version.json"
   234    vFile2="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/chaos@latest/version.json"
   235    
   236    file1Content=$(cat $vFile1)
   237    file2Content=$(cat $vFile2)
   238    
   239    # remove one individual version file
   240    rm -f $vFile1
   241    
   242    # run steampipe again so that the plugin version files get backfilled
   243    run steampipe plugin list --install-dir $MY_TEST_COPY
   244    
   245    [ ! -f $vFile1 ] && fail "could not find $vFile1"
   246    [ ! -f $vFile2 ] && fail "could not find $vFile2"
   247    
   248    echo "$file1Content" > $MY_TEST_COPY/f1.json
   249    echo "$file2Content" > $MY_TEST_COPY/f2.json
   250    cat "$vFile1" > $MY_TEST_COPY/v1.json
   251    cat "$vFile2" > $MY_TEST_COPY/v2.json
   252  
   253    # Compare the json file contents
   254    run jd "$MY_TEST_COPY/f1.json" "$MY_TEST_COPY/v1.json"
   255    echo $output
   256    assert_success
   257  
   258    run jd "$MY_TEST_COPY/f2.json" "$MY_TEST_COPY/v2.json"
   259    echo $output
   260    assert_success
   261    
   262    rm -rf $MY_TEST_COPY
   263  }
   264  
   265  @test "verify that global plugin/versions.json is composed from individual version.json files when it is absent" {
   266    # Create a copy of the install directory
   267    copy_install_directory
   268  
   269    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   270    assert_success
   271    
   272    vFile="$MY_TEST_COPY/plugins/versions.json"
   273    
   274    fileContent=$(cat $vFile)
   275    
   276    # remove global version file
   277    rm -f $vFile
   278    
   279    # run steampipe again so that the plugin version files get backfilled
   280    run steampipe plugin list --install-dir $MY_TEST_COPY
   281    
   282    ls -la $vFile
   283    
   284    [ ! -f $vFile ] && fail "could not find $vFile"
   285    
   286    echo "$fileContent" > $MY_TEST_COPY/f.json
   287    cat "$vFile" > $MY_TEST_COPY/v.json
   288  
   289    # Compare the json file contents
   290    run jd "$MY_TEST_COPY/f.json" "$MY_TEST_COPY/v.json"
   291    echo $output
   292    assert_success
   293  
   294    rm -rf $MY_TEST_COPY
   295  }
   296  
   297  @test "verify that global plugin/versions.json is composed from individual version.json files when it is corrupt" {
   298    # Create a copy of the install directory
   299    copy_install_directory
   300  
   301    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   302    assert_success
   303    
   304    vFile="$MY_TEST_COPY/plugins/versions.json"
   305    fileContent=$(cat $vFile)
   306    
   307    # remove global version file
   308    echo "badline to corrupt versions.json" >> $vFile
   309    
   310    # run steampipe again so that the plugin version files get backfilled
   311    run steampipe plugin list --install-dir $MY_TEST_COPY
   312    
   313    [ ! -f $vFile ] && fail "could not find $vFile"
   314    
   315    echo "$fileContent" > $MY_TEST_COPY/f.json
   316    cat "$vFile" > $MY_TEST_COPY/v.json
   317  
   318    # Compare the json file contents
   319    run jd "$MY_TEST_COPY/f.json" "$MY_TEST_COPY/v.json"
   320    echo $output
   321    assert_success
   322    
   323    rm -rf $MY_TEST_COPY
   324  }
   325  
   326  @test "verify that composition of global plugin/versions.json works when an individual version.json file is corrupt" {
   327    # Create a copy of the install directory
   328    copy_install_directory
   329  
   330    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   331    assert_success
   332    
   333    vFile="$MY_TEST_COPY/plugins/versions.json"  
   334    vFile1="$MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/version.json"
   335    
   336    # corrupt a version file
   337    echo "bad line to corrupt" >> $vFile1
   338    
   339    # remove global file
   340    rm -f $vFile
   341    
   342    # run steampipe again so that the plugin version files get backfilled
   343    run steampipe plugin list --install-dir $MY_TEST_COPY
   344  
   345    # verify that global file got created
   346    [ ! -f $vFile ] && fail "could not find $vFile"
   347    
   348    rm -rf $MY_TEST_COPY
   349  }
   350  
   351  @test "verify that plugin installed from registry are marked as 'local' when the modtime of the binary is after the install time" {
   352    # Create a copy of the install directory
   353    copy_install_directory
   354  
   355    run steampipe plugin install net chaos --install-dir $MY_TEST_COPY
   356    assert_success
   357  
   358    # wait for a couple of seconds
   359    sleep 2
   360  
   361    # touch one of the plugin binaries
   362    touch $MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/steampipe-plugin-net.plugin
   363  
   364    # run steampipe again so that the plugin version files get backfilled
   365    version=$(steampipe plugin list --install-dir $MY_TEST_COPY --output json | jq '.installed' | jq '. | map(select(.name | contains("net@latest")))' | jq '.[0].version')
   366  
   367    # assert
   368    assert_equal "$version" '"local"'
   369  
   370    rm -rf $MY_TEST_COPY
   371  }
   372  
   373  @test "verify that steampipe check should bypass plugin requirement detection if installed plugin is local" {
   374    # Create a copy of the install directory
   375    copy_install_directory
   376  
   377    run steampipe plugin install net --install-dir $MY_TEST_COPY
   378    assert_success
   379  
   380    # wait for a couple of seconds
   381    sleep 2
   382  
   383    # touch one of the plugin binaries
   384    touch $MY_TEST_COPY/plugins/hub.steampipe.io/plugins/turbot/net@latest/steampipe-plugin-net.plugin
   385  
   386    run steampipe plugin list --install-dir $MY_TEST_COPY
   387    echo $output
   388  
   389    # clone a mod which has a net plugin requirement
   390    cd $MY_TEST_COPY
   391    git clone https://github.com/turbot/steampipe-mod-net-insights.git
   392    cd steampipe-mod-net-insights
   393  
   394    # run steampipe check
   395    run steampipe check all --install-dir $MY_TEST_COPY
   396  
   397    # check - the plugin requirement warning should not be present in the output
   398    substring="Warning: could not find plugin which satisfies requirement"
   399    if [[ ! $output == *"$substring"* ]]; then
   400      run echo "Warning is not present in the output"
   401    else
   402      run echo "Warning is present in the output"
   403    fi
   404  
   405    assert_equal "$output" "Warning is not present in the output"
   406    rm -rf $MY_TEST_COPY
   407  }
   408  
   409  @test "verify that plugin installed with --skip-config as true, should not have create a default config .spc file in config folder" {
   410    # Create a copy of the install directory
   411    copy_install_directory
   412  
   413    run steampipe plugin install aws --skip-config --install-dir $MY_TEST_COPY
   414    assert_success
   415  
   416    run test -f $MY_TEST_COPY/config/aws.spc
   417    assert_failure
   418  
   419    rm -rf $MY_TEST_COPY
   420  }
   421  
   422  @test "verify that plugin installed with --skip-config as false(default), should have default config .spc file in config folder" {
   423    # Create a copy of the install directory
   424    copy_install_directory
   425  
   426    run steampipe plugin install aws --install-dir $MY_TEST_COPY
   427    assert_success
   428  
   429    run test -f $MY_TEST_COPY/config/aws.spc
   430    assert_success
   431  
   432    rm -rf $MY_TEST_COPY
   433  }
   434  
   435  @test "verify reinstalling a plugin does not overwrite existing plugin config" {
   436    # check if the default/tweaked config file for a plugin is not deleted after
   437    # re-installation of a plugin
   438  
   439    # Create a copy of the install directory
   440    copy_install_directory
   441  
   442    run steampipe plugin install aws --install-dir $MY_TEST_COPY
   443  
   444    run test -f $MY_TEST_COPY/config/aws.spc
   445    assert_success
   446  
   447    echo '
   448    connection "aws" {
   449      plugin = "aws"
   450      endpoint_url = "http://localhost:4566"
   451    }
   452    ' >> $MY_TEST_COPY/config/aws.spc
   453    cp $MY_TEST_COPY/config/aws.spc config.spc
   454  
   455    run steampipe plugin uninstall aws --install-dir $MY_TEST_COPY
   456  
   457    run steampipe plugin install aws --skip-config --install-dir $MY_TEST_COPY
   458  
   459    run test -f $MY_TEST_COPY/config/aws.spc
   460    assert_success
   461  
   462    run diff $MY_TEST_COPY/config/aws.spc config.spc
   463    assert_success
   464  
   465    rm config.spc
   466    rm -rf $MY_TEST_COPY
   467  }
   468  
   469  # Custom function to create a copy of the install directory
   470  copy_install_directory() {
   471    cp -r "$MY_TEST_DIRECTORY" "/tmp/test_copy"
   472    export MY_TEST_COPY="/tmp/test_copy"
   473  }
   474  
   475  function setup_file() {
   476    export BATS_TEST_TIMEOUT=180
   477    echo "# setup_file()">&3
   478  
   479    tmpdir="$(mktemp -d)"
   480    steampipe query "select 1" --install-dir $tmpdir
   481    # Export the directory path as an environment variable
   482    export MY_TEST_DIRECTORY=$tmpdir
   483  }
   484  
   485  function teardown_file() {
   486    # list running processes
   487    ps -ef | grep steampipe
   488  
   489    # check if any processes are running
   490    num=$(ps aux | grep steampipe | grep -v bats | grep -v grep | grep -v tests/acceptance | wc -l | tr -d ' ')
   491    assert_equal $num 0
   492  }