From ec53605ea33f000191a86203c5cd15f9f93c3b6a Mon Sep 17 00:00:00 2001 From: Luke McCrone Date: Tue, 23 Apr 2024 20:00:49 -0300 Subject: [PATCH] test: delete tags, get location, some reorganization --- tests/commands/delete_object_tagging.sh | 21 ++++++ tests/commands/get_bucket_location.sh | 68 ++++++++++++++++++ tests/commands/get_bucket_tagging.sh | 31 ++++++++ tests/run.sh | 7 +- tests/test_aws.sh | 10 +++ tests/test_common.sh | 55 +++++++++++++-- tests/test_mc.sh | 8 +++ tests/test_s3cmd.sh | 4 ++ tests/util.sh | 94 ++++++++++++++++--------- 9 files changed, 258 insertions(+), 40 deletions(-) create mode 100644 tests/commands/delete_object_tagging.sh create mode 100644 tests/commands/get_bucket_location.sh create mode 100644 tests/commands/get_bucket_tagging.sh diff --git a/tests/commands/delete_object_tagging.sh b/tests/commands/delete_object_tagging.sh new file mode 100644 index 00000000..bf59a382 --- /dev/null +++ b/tests/commands/delete_object_tagging.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +delete_object_tagging() { + if [[ $# -ne 3 ]]; then + echo "delete object tagging command missing command type, bucket, key" + return 1 + fi + if [[ $1 == 'aws' ]]; then + error=$(aws --no-verify-ssl s3api delete-object-tagging --bucket "$2" --key "$3" 2>&1) || delete_result=$? + elif [[ $1 == 'mc' ]]; then + error=$(mc --insecure tag remove "$MC_ALIAS/$2/$3") || delete_result=$? + else + echo "delete-object-tagging command not implemented for '$1'" + return 1 + fi + if [[ $delete_result -ne 0 ]]; then + echo "error deleting object tagging: $error" + return 1 + fi + return 0 +} \ No newline at end of file diff --git a/tests/commands/get_bucket_location.sh b/tests/commands/get_bucket_location.sh new file mode 100644 index 00000000..b2dff87f --- /dev/null +++ b/tests/commands/get_bucket_location.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +get_bucket_location() { + if [[ $# -ne 2 ]]; then + echo "get bucket location command requires command type, bucket name" + return 1 + fi + if [[ $1 == 'aws' ]]; then + get_bucket_location_aws "$2" || get_result=$? + elif [[ $1 == 's3cmd' ]]; then + get_bucket_location_s3cmd "$2" || get_result=$? + elif [[ $1 == 'mc' ]]; then + get_bucket_location_mc "$2" || get_result=$? + else + echo "command type '$1' not implemented for get_bucket_location" + return 1 + fi + if [[ $get_result -ne 0 ]]; then + return 1 + fi + location=$(echo "$location_json" | jq -r '.LocationConstraint') + export location +} + +get_bucket_location_aws() { + if [[ $# -ne 1 ]]; then + echo "get bucket location (aws) requires bucket name" + return 1 + fi + location_json=$(aws --no-verify-ssl s3api get-bucket-location --bucket "$1") || location_result=$? + if [[ $location_result -ne 0 ]]; then + echo "error getting bucket location: $location" + return 1 + fi + bucket_location=$(echo "$location_json" | jq -r '.LocationConstraint') + export bucket_location + return 0 +} + +get_bucket_location_s3cmd() { + if [[ $# -ne 1 ]]; then + echo "get bucket location (s3cmd) requires bucket name" + return 1 + fi + info=$(s3cmd --no-check-certificate info "s3://$1") || results=$? + if [[ $results -ne 0 ]]; then + echo "error getting s3cmd info: $info" + return 1 + fi + bucket_location=$(echo "$info" | grep -o 'Location:.*' | awk '{print $2}') + export bucket_location + return 0 +} + +get_bucket_location_mc() { + if [[ $# -ne 1 ]]; then + echo "get bucket location (mc) requires bucket name" + return 1 + fi + info=$(mc --insecure stat "$MC_ALIAS/$1") || results=$? + if [[ $results -ne 0 ]]; then + echo "error getting s3cmd info: $info" + return 1 + fi + bucket_location=$(echo "$info" | grep -o 'Location:.*' | awk '{print $2}') + export bucket_location + return 0 +} \ No newline at end of file diff --git a/tests/commands/get_bucket_tagging.sh b/tests/commands/get_bucket_tagging.sh new file mode 100644 index 00000000..1ac5fe9c --- /dev/null +++ b/tests/commands/get_bucket_tagging.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +# get bucket tags +# params: bucket +# export 'tags' on success, return 1 for error +get_bucket_tagging() { + if [ $# -ne 2 ]; then + echo "get bucket tag command missing command type, bucket name" + return 1 + fi + local result + if [[ $1 == 'aws' ]]; then + tags=$(aws --no-verify-ssl s3api get-bucket-tagging --bucket "$2" 2>&1) || result=$? + elif [[ $1 == 'mc' ]]; then + tags=$(mc --insecure tag list "$MC_ALIAS"/"$2" 2>&1) || result=$? + else + echo "invalid command type $1" + return 1 + fi + log 5 "Tags: $tags" + tags=$(echo "$tags" | grep -v "InsecureRequestWarning") + if [[ $result -ne 0 ]]; then + if [[ $tags =~ "No tags found" ]] || [[ $tags =~ "The TagSet does not exist" ]]; then + export tags= + return 0 + fi + echo "error getting bucket tags: $tags" + return 1 + fi + export tags +} \ No newline at end of file diff --git a/tests/run.sh b/tests/run.sh index 826b90df..2c72d30c 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -66,26 +66,27 @@ fi case $command_type in aws) + echo "Running aws tests ..." "$HOME"/bin/bats ./tests/test_aws.sh || exit_code=$? if [[ $exit_code -eq 0 ]]; then "$HOME"/bin/bats ./tests/test_user_aws.sh || exit_code=$? fi ;; aws-posix) + echo "Running aws posix-specific tests ..." "$HOME"/bin/bats ./tests/test_aws_posix.sh || exit_code=$? ;; s3cmd) + echo "Running s3cmd tests ..." "$HOME"/bin/bats ./tests/test_s3cmd.sh || exit_code=$? if [[ $exit_code -eq 0 ]]; then "$HOME"/bin/bats ./tests/test_user_s3cmd.sh || exit_code=$? fi ;; mc) + echo "Running mc tests ..." "$HOME"/bin/bats ./tests/test_mc.sh || exit_code=$? ;; - user) - "$HOME"/bin/bats ./tests/test_user_aws.sh || exit_code=$? - ;; esac exit $exit_code diff --git a/tests/test_aws.sh b/tests/test_aws.sh index 7ccedbe8..502c60c3 100755 --- a/tests/test_aws.sh +++ b/tests/test_aws.sh @@ -6,6 +6,7 @@ source ./tests/util_aws.sh source ./tests/util_bucket_create.sh source ./tests/util_file.sh source ./tests/test_common.sh +source ./tests/commands/delete_object_tagging.sh # test creation and deletion of bucket on versitygw @test "test_create_delete_bucket_aws" { @@ -205,6 +206,7 @@ source ./tests/test_common.sh delete_test_files $bucket_file } +# ADDED # test multi-part upload abort @test "test-multi-part-upload-abort" { local bucket_file="bucket-file" @@ -443,4 +445,12 @@ source ./tests/test_common.sh value=$(echo "$metadata" | jq '.[]') [[ $key == "\"$test_key\"" ]] || fail "keys doesn't match (expected $key, actual \"$test_key\")" [[ $value == "\"$test_value\"" ]] || fail "values doesn't match (expected $value, actual \"$test_value\")" +} + +@test "test_delete_object_tagging" { + test_common_delete_object_tagging "aws" +} + +@test "test_get_bucket_location" { + test_common_get_bucket_location "aws" } \ No newline at end of file diff --git a/tests/test_common.sh b/tests/test_common.sh index e9423380..651a3d91 100644 --- a/tests/test_common.sh +++ b/tests/test_common.sh @@ -2,6 +2,9 @@ source ./tests/util.sh source ./tests/util_file.sh +source ./tests/commands/delete_object_tagging.sh +source ./tests/commands/get_bucket_location.sh +source ./tests/commands/get_bucket_tagging.sh # common test for creating, deleting buckets # param: "aws" or "s3cmd" @@ -159,14 +162,14 @@ test_common_set_get_delete_bucket_tags() { setup_bucket "$1" "$BUCKET_ONE_NAME" || local result=$? [[ $result -eq 0 ]] || fail "Failed to create bucket '$BUCKET_ONE_NAME'" - get_bucket_tags "$1" "$BUCKET_ONE_NAME" || local get_result=$? + get_bucket_tagging "$1" "$BUCKET_ONE_NAME" || local get_result=$? [[ $get_result -eq 0 ]] || fail "Error getting bucket tags first time" - check_bucket_tags_empty "$1" || local check_result=$? + check_bucket_tags_empty "$1" "$BUCKET_ONE_NAME" || local check_result=$? [[ $check_result -eq 0 ]] || fail "error checking if bucket tags are empty" put_bucket_tag "$1" "$BUCKET_ONE_NAME" $key $value - get_bucket_tags "$1" "$BUCKET_ONE_NAME" || local get_result_two=$? + get_bucket_tagging "$1" "$BUCKET_ONE_NAME" || local get_result_two=$? [[ $get_result_two -eq 0 ]] || fail "Error getting bucket tags second time" local tag_set_key @@ -184,10 +187,10 @@ test_common_set_get_delete_bucket_tags() { fi delete_bucket_tags "$1" "$BUCKET_ONE_NAME" - get_bucket_tags "$1" "$BUCKET_ONE_NAME" || local get_result=$? + get_bucket_tagging "$1" "$BUCKET_ONE_NAME" || local get_result=$? [[ $get_result -eq 0 ]] || fail "Error getting bucket tags third time" - check_bucket_tags_empty "$1" || local check_result=$? + check_bucket_tags_empty "$1" "$BUCKET_ONE_NAME" || local check_result=$? [[ $check_result -eq 0 ]] || fail "error checking if bucket tags are empty" delete_bucket_or_contents "$1" "$BUCKET_ONE_NAME" } @@ -313,3 +316,45 @@ test_common_list_objects_file_count() { [[ $file_count == 1001 ]] || fail "file count should be 1001, is $file_count" delete_bucket_or_contents "$1" "$BUCKET_ONE_NAME" } + +test_common_delete_object_tagging() { + + [[ $# -eq 1 ]] || fail "test common delete object tagging requires command type" + + bucket_file="bucket_file" + tag_key="key" + tag_value="value" + + create_test_files "$bucket_file" || local created=$? + [[ $created -eq 0 ]] || fail "Error creating test files" + + setup_bucket "$1" "$BUCKET_ONE_NAME" || local setup_result=$? + [[ $setup_result -eq 0 ]] || fail "error setting up bucket" + + put_object "$1" "$test_file_folder"/"$bucket_file" "$BUCKET_ONE_NAME"/"$bucket_file" || local put_object=$? + [[ $put_object -eq 0 ]] || fail "Failed to add object to bucket" + + put_object_tag "$1" "$BUCKET_ONE_NAME" "$bucket_file" "$tag_key" "$tag_value" || put_result=$? + [[ $put_result -eq 0 ]] || fail "failed to add tags to object" + + get_and_verify_object_tags "$1" "$BUCKET_ONE_NAME" "$bucket_file" "$tag_key" "$tag_value" || get_result=$? + [[ $get_result -eq 0 ]] || fail "failed to get tags" + + delete_object_tagging "$1" "$BUCKET_ONE_NAME" "$bucket_file" || delete_result=$? + [[ $delete_result -eq 0 ]] || fail "error deleting object tagging" + + check_object_tags_empty "$1" "$BUCKET_ONE_NAME" "$bucket_file" || get_result=$? + [[ $get_result -eq 0 ]] || fail "failed to get tags" + + delete_bucket_or_contents "aws" "$BUCKET_TWO_NAME" + delete_test_files "$bucket_file" +} + +test_common_get_bucket_location() { + [[ $# -eq 1 ]] || fail "test common get bucket location missing command type" + setup_bucket "aws" "$BUCKET_ONE_NAME" || local setup_result=$? + [[ $setup_result -eq 0 ]] || fail "error setting up bucket" + get_bucket_location "aws" "$BUCKET_ONE_NAME" + # shellcheck disable=SC2154 + [[ $bucket_location == "null" ]] || [[ $bucket_location == "us-east-1" ]] || fail "wrong location: '$bucket_location'" +} diff --git a/tests/test_mc.sh b/tests/test_mc.sh index 6763606c..d47fa646 100755 --- a/tests/test_mc.sh +++ b/tests/test_mc.sh @@ -76,3 +76,11 @@ export RUN_MC=true [[ $bucket_info == *"does not exist"* ]] || fail "404 not returned for non-existent bucket info" delete_bucket_or_contents "mc" "$BUCKET_ONE_NAME" } + +@test "test_delete_object_tagging" { + test_common_delete_object_tagging "mc" +} + +@test "test_get_bucket_location" { + test_common_get_bucket_location "mc" +} diff --git a/tests/test_s3cmd.sh b/tests/test_s3cmd.sh index 49957786..679cd8de 100755 --- a/tests/test_s3cmd.sh +++ b/tests/test_s3cmd.sh @@ -71,3 +71,7 @@ export RUN_S3CMD=true [[ $bucket_info == *"404"* ]] || fail "404 not returned for non-existent bucket info" delete_bucket_or_contents "s3cmd" "$BUCKET_ONE_NAME" } + +@test "test_get_bucket_location" { + test_common_get_bucket_location "s3cmd" +} diff --git a/tests/util.sh b/tests/util.sh index 99309047..eb89019e 100644 --- a/tests/util.sh +++ b/tests/util.sh @@ -2,6 +2,7 @@ source ./tests/util_mc.sh source ./tests/logger.sh +source ./tests/commands/get_bucket_tagging.sh # delete an AWS bucket # param: bucket name @@ -596,40 +597,12 @@ put_bucket_tag() { return 0 } -# get bucket tags -# params: bucket -# export 'tags' on success, return 1 for error -get_bucket_tags() { - if [ $# -ne 2 ]; then - echo "get bucket tag command missing command type, bucket name" - return 1 - fi - local result - if [[ $1 == 'aws' ]]; then - tags=$(aws --no-verify-ssl s3api get-bucket-tagging --bucket "$2" 2>&1) || result=$? - elif [[ $1 == 'mc' ]]; then - tags=$(mc --insecure tag list "$MC_ALIAS"/"$2" 2>&1) || result=$? - else - echo "invalid command type $1" - return 1 - fi - log 5 "Tags: $tags" - tags=$(echo "$tags" | grep -v "InsecureRequestWarning") - if [[ $result -ne 0 ]]; then - if [[ $tags =~ "No tags found" ]] || [[ $tags =~ "The TagSet does not exist" ]]; then - export tags= - return 0 - fi - echo "error getting bucket tags: $tags" - return 1 - fi - export tags -} -check_bucket_tags_empty() { + +check_tags_empty() { if [[ $# -ne 1 ]]; then - echo "bucket tags empty check requires command type" - return 2 + echo "check tags empty requires command type" + return 1 fi if [[ $1 == 'aws' ]]; then if [[ $tags != "" ]]; then @@ -648,6 +621,34 @@ check_bucket_tags_empty() { return 0 } +check_object_tags_empty() { + if [[ $# -ne 3 ]]; then + echo "bucket tags empty check requires command type, bucket, and key" + return 2 + fi + get_object_tags "$1" "$2" "$3" || get_result=$? + if [[ $get_result -ne 0 ]]; then + echo "failed to get tags" + return 2 + fi + check_tags_empty "$1" || check_result=$? + return $check_result +} + +check_bucket_tags_empty() { + if [[ $# -ne 2 ]]; then + echo "bucket tags empty check requires command type, bucket" + return 2 + fi + get_bucket_tagging "$1" "$2" || get_result=$? + if [[ $get_result -ne 0 ]]; then + echo "failed to get tags" + return 2 + fi + check_tags_empty "$1" || check_result=$? + return $check_result +} + delete_bucket_tags() { if [ $# -ne 2 ]; then echo "delete bucket tag command missing command type, bucket name" @@ -690,6 +691,35 @@ put_object_tag() { return 0 } +get_and_verify_object_tags() { + if [[ $# -ne 5 ]]; then + echo "get and verify object tags missing command type, bucket, key, tag key, tag value" + return 1 + fi + get_object_tags "$1" "$2" "$3" || get_result=$? + if [[ $get_result -ne 0 ]]; then + echo "failed to get tags" + return 1 + fi + if [[ $1 == 'aws' ]]; then + tag_set_key=$(echo "$tags" | jq '.TagSet[0].Key') + tag_set_value=$(echo "$tags" | jq '.TagSet[0].Value') + if [[ $tag_set_key != '"'$4'"' ]]; then + echo "Key mismatch ($tag_set_key, \"$4\")" + return 1 + fi + if [[ $tag_set_value != '"'$5'"' ]]; then + echo "Value mismatch ($tag_set_value, \"$5\")" + return 1 + fi + else + read -r tag_set_key tag_set_value <<< "$(echo "$tags" | awk 'NR==2 {print $1, $3}')" + [[ $tag_set_key == "$4" ]] || fail "Key mismatch" + [[ $tag_set_value == "$5" ]] || fail "Value mismatch" + fi + return 0 +} + # get object tags # params: bucket # export 'tags' on success, return 1 for error