From b2376356dda99cd22083e0fe0c0dd5df06021b05 Mon Sep 17 00:00:00 2001 From: Elim Tsiagbey Date: Fri, 24 Mar 2023 17:38:39 -0400 Subject: [PATCH] Bats Tests: Target Host Sources (#3107) * Bats Tests: Target Host Sources - Add bats test coverage for adding a Host Source to a Target - Create `target_host_sources.bash` file with helper functions for bats test file ## Considerations Created new resources like a target & host catalog instead of using already created resources because the changes could affect existing tests and it made it easy to change the newly created resources --- .../cli/boundary/_target_host_sources.bash | 64 +++++ internal/tests/cli/boundary/_targets.bash | 30 --- internal/tests/cli/boundary/target.bats | 12 +- .../cli/boundary/target_host_sources.bats | 239 ++++++++++++++++++ 4 files changed, 310 insertions(+), 35 deletions(-) create mode 100644 internal/tests/cli/boundary/_target_host_sources.bash create mode 100644 internal/tests/cli/boundary/target_host_sources.bats diff --git a/internal/tests/cli/boundary/_target_host_sources.bash b/internal/tests/cli/boundary/_target_host_sources.bash new file mode 100644 index 000000000..192331038 --- /dev/null +++ b/internal/tests/cli/boundary/_target_host_sources.bash @@ -0,0 +1,64 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +load _authorized_actions + +function set_target_host_sources() { + local tid=$1 + local hostSources=${@:2} + for i in "${hostSources}"; do + hostSource+="-host-source $i " + done + + boundary targets set-host-sources -id $tid $hostSource +} + +function add_target_host_sources() { + local tid=$1 + local hostSources=${@:2} + for i in "${hostSources}"; do + hostSource+="-host-source $i " + done + + boundary targets add-host-sources -id $tid $hostSource +} + +function remove_target_host_sources() { + local tid=$1 + local hostSources=${@:2} + for i in "${hostSources}"; do + hostSource+="-host-source $i " + done + + boundary targets remove-host-sources -id $tid $hostSource +} + +function target_has_host_source_id() { + local tid=$1 + local format=$2 + local hostSources=${@:3} + + if [[ "$format" == "json" ]]; then + ids=$(boundary targets read -id $tid -format json | jq '.item.host_sources[].id') + for i in "${hostSources}"; do + local hsid=$i + for id in $ids; do + if [ $(strip "$id") == "$hsid" ]; then + return 0 + fi + done + done + return 1 + elif [[ "$format" == "table" ]]; then + for i in "${hostSources}"; do + pattern="Host Sources:.*ID:.*$i*" + OUTPUT=$(boundary targets read -id $tid -format table) + if ! [[ $OUTPUT =~ $pattern ]]; then + echo "Host source id '$i' not found on target" + return 1 + fi + done + return 0 + fi + return 1 +} diff --git a/internal/tests/cli/boundary/_targets.bash b/internal/tests/cli/boundary/_targets.bash index 57f887f18..cd68a1e48 100644 --- a/internal/tests/cli/boundary/_targets.bash +++ b/internal/tests/cli/boundary/_targets.bash @@ -48,42 +48,12 @@ function update_address() { boundary targets update tcp -id $id -address $2 } -function assoc_host_sources() { - local id=$1 - local hst=$2 - boundary targets add-host-sources -id $id -host-source $hst -} - -function remove_host_sources() { - local id=$1 - local hst=$2 - boundary targets remove-host-sources -id $id -host-source $hst -} - function target_id_from_name() { local sid=$1 local name=$2 strip $(list_targets $sid | jq -c ".items[] | select(.name | contains(\"$name\")) | .[\"id\"]") } -function target_host_source_ids() { - local tid=$1 - boundary targets read -id $tid -format json | jq '.item.host_sources[].id' -} - -function target_has_host_source_id() { - local tid=$1 - local hsid=$2 - - ids=$(target_host_source_ids $tid) - for id in $ids; do - if [ $(strip "$id") == "$hsid" ]; then - return 0 - fi - done - return 1 -} - function has_default_target_actions() { local out=$1 local actions=('read' 'update' 'delete' 'add-host-sources' 'set-host-sources' 'remove-host-sources' 'authorize-session') diff --git a/internal/tests/cli/boundary/target.bats b/internal/tests/cli/boundary/target.bats index 1f61772ac..f609a2ad6 100644 --- a/internal/tests/cli/boundary/target.bats +++ b/internal/tests/cli/boundary/target.bats @@ -4,6 +4,7 @@ load _auth load _connect load _targets load _helpers +load _target_host_sources @test "boundary/login: can login as admin user" { @@ -69,14 +70,15 @@ load _helpers @test "boundary/target: admin user can add default host set to created target" { local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) - run assoc_host_sources $id $DEFAULT_HOST_SET + run add_target_host_sources $id $DEFAULT_HOST_SET echo "$output" [ "$status" -eq 0 ] } @test "boundary/target: created target has default host set" { local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) - run target_has_host_source_id $id $DEFAULT_HOST_SET + local format="json" + run target_has_host_source_id $id $format $DEFAULT_HOST_SET echo "$output" [ "$status" -eq 0 ] } @@ -108,7 +110,7 @@ load _helpers @test "boundary/target: cannot assign an host source to a target with an address" { local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME_WITH_ADDR) - run assoc_host_sources $id $DEFAULT_HOST_SET + run add_target_host_sources $id $DEFAULT_HOST_SET [ "$status" -eq 1 ] } @@ -116,7 +118,7 @@ load _helpers local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME_WITH_ADDR) run update_address $id "null" [ "$status" -eq 0 ] - run assoc_host_sources $id $DEFAULT_HOST_SET + run add_target_host_sources $id $DEFAULT_HOST_SET [ "$status" -eq 0 ] } @@ -128,7 +130,7 @@ load _helpers @test "boundary/target: can assign an an address to a target after deleting host source" { local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME_WITH_ADDR) - run remove_host_sources $id $DEFAULT_HOST_SET + run remove_target_host_sources $id $DEFAULT_HOST_SET [ "$status" -eq 0 ] run update_address $id "localhost" [ "$status" -eq 0 ] diff --git a/internal/tests/cli/boundary/target_host_sources.bats b/internal/tests/cli/boundary/target_host_sources.bats new file mode 100644 index 000000000..f8bf2d9dd --- /dev/null +++ b/internal/tests/cli/boundary/target_host_sources.bats @@ -0,0 +1,239 @@ +#!/usr/bin/env bats + +load _auth +load _hosts +load _helpers +load _target_host_sources +load _targets +load _host_catalogs +load _host_sets + +export NEW_HOST1='test-for-add-host-source-1' +export NEW_HOST2='test-for-add-host-source-2' +export NEW_HOST_CATALOG='test-host-catalog' +export NEW_HOST_SET1='test-host-set-1' +export NEW_HOST_SET2='test-host-set-2' +export TGT_NAME='test-target' +export TGT_DEFAULT_PORT='22' + +@test "boundary/login: can login as admin user" { + run login $DEFAULT_LOGIN + [ "$status" -eq 0 ] +} + +@test "boundary/target: admin user can create target" { + run create_tcp_target $DEFAULT_P_ID $TGT_DEFAULT_PORT $TGT_NAME + echo $output + [ "$status" -eq 0 ] +} + +@test "boundary/host-catalogs: can create $NEW_HOST_CATALOG host catalog in default project scope" { + run create_host_catalog $NEW_HOST_CATALOG $DEFAULT_P_ID + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/hosts: can create multiple hosts in created host catalog" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + run create_host $NEW_HOST1 $hcid + echo "$output" + [ "$status" -eq 0 ] + + run create_host $NEW_HOST2 $hcid + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/host-sets: can create multiple host sets in a host catalog" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + run create_host_set $hcid $NEW_HOST_SET1 + echo "$output" + [ "$status" -eq 0 ] + + run create_host_set $hcid $NEW_HOST_SET2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/host-set/add-host: can associate multiple host sets with created hosts" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hid1=$(host_id $NEW_HOST1 $hcid) + local hid2=$(host_id $NEW_HOST2 $hcid) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + + run assoc_host_set_host $hid1 $hsid1 + echo "$output" + [ "$status" -eq 0 ] + + run assoc_host_set_host $hid2 $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/host-set/add-host: verify all host sets contain created hosts" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hid1=$(host_id $NEW_HOST1 $hcid) + local hid2=$(host_id $NEW_HOST2 $hcid) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + + run host_set_has_host_id $hid1 $hsid1 + echo "$output" + [ "$status" -eq 0 ] + + run host_set_has_host_id $hid2 $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can set host sources on a created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run set_target_host_sources $tid $hsid1 $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can remove host sources on a created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run remove_target_host_sources $tid $hsid1 $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can add created host set to created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run add_target_host_sources $tid $hsid1 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: validate $NEW_HOST1 host source present - JSON" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid=$(host_set_id $NEW_HOST_SET1 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + local format="json" + run target_has_host_source_id $tid $format $hsid + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: validate $NEW_HOST1 host source present - Table" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid=$(host_set_id $NEW_HOST_SET1 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + local format="table" + run target_has_host_source_id $tid $format $hsid + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can add another host set to created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run add_target_host_sources $tid $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: validate $NEW_HOST2 host source present - Table" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + local format="table" + run target_has_host_source_id $tid $format $hsid2 + echo "$output" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can remove $NEW_HOST_SET1 host set from created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run remove_target_host_sources $tid $hsid1 + [ "$status" -eq 0 ] +} + +@test "boundary/target: validate $NEW_HOST1 host source is not present on target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + local format="table" + run target_has_host_source_id $tid $format $hsid1 + echo "$output" + [ "$status" -eq 1 ] +} + +@test "boundary/target: can remove $NEW_HOST_SET2 host set from created target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run remove_target_host_sources $tid $hsid2 + [ "$status" -eq 0 ] +} + +@test "boundary/target: validate $NEW_HOST2 host source is not present on target" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + local tid=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + local format="json" + run target_has_host_source_id $tid $format $hsid2 + echo "$output" + [ "$status" -eq 1 ] +} + +@test "boundary/host: can delete all created hosts" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + local hid1=$(host_id $NEW_HOST1 $hcid) + local hid2=$(host_id $NEW_HOST2 $hcid) + + run delete_host $hid1 + echo "$output" + run has_status_code "$output" "204" + [ "$status" -eq 0 ] + + run delete_host $hid2 + echo "$output" + run has_status_code "$output" "204" + [ "$status" -eq 0 ] +} + +@test "boundary/host-set: can delete all created host sets" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + + local hsid1=$(host_set_id $NEW_HOST_SET1 $hcid) + run delete_host_set $hsid1 + echo "$output" + run has_status_code "$output" "204" + [ "$status" -eq 0 ] + + local hsid2=$(host_set_id $NEW_HOST_SET2 $hcid) + run delete_host_set $hsid2 + echo "$output" + run has_status_code "$output" "204" + [ "$status" -eq 0 ] +} + +@test "boundary/host-catalogs: can delete $NEW_HOST_CATALOG host catalog in default project scope" { + local hcid=$(host_catalog_id $NEW_HOST_CATALOG $DEFAULT_P_ID) + run delete_host_catalog $hcid + echo "$output" + run has_status_code "$output" "204" + [ "$status" -eq 0 ] +} + +@test "boundary/target: can delete target" { + local id=$(target_id_from_name $DEFAULT_P_ID $TGT_NAME) + run delete_target $id + run has_status_code "$output" "204" + [ "$status" -eq 0 ] +}