From 537fc6c45c556e1d3aae37a1f4f2aa95fb5064fe Mon Sep 17 00:00:00 2001 From: Anurag Mittal Date: Wed, 11 Dec 2024 23:18:48 +0100 Subject: [PATCH] COSI-32: add-e2e-tests-for-delete-bucket --- .github/scripts/cleanup_cosi_resources.sh | 1 + .github/scripts/e2e_tests.sh | 58 +++++++++++++++++++ .../bucketclaim-delete-on-claim-removal.yaml | 8 +++ .../bucketclass-delete-on-claim-removal.yaml | 9 +++ 4 files changed, 76 insertions(+) create mode 100644 cosi-examples/bucketclaim-delete-on-claim-removal.yaml create mode 100644 cosi-examples/bucketclass-delete-on-claim-removal.yaml diff --git a/.github/scripts/cleanup_cosi_resources.sh b/.github/scripts/cleanup_cosi_resources.sh index 9f18984f..c2d6cef1 100755 --- a/.github/scripts/cleanup_cosi_resources.sh +++ b/.github/scripts/cleanup_cosi_resources.sh @@ -57,6 +57,7 @@ log_and_run kubectl delete -f cosi-examples/bucketaccessclass.yaml --all || { ec log_and_run echo "Deleting Bucket Class and Bucket Claim..." log_and_run kubectl delete -f cosi-examples/bucketclass.yaml || { echo "Bucket Class not found." | tee -a "$LOG_FILE"; } log_and_run kubectl delete -f cosi-examples/bucketclaim.yaml || { echo "Bucket Claim not found." | tee -a "$LOG_FILE"; } +log_and_run kubectl delete -f cosi-examples/bucketclass-delete-on-claim-removal.yaml || { echo "Bucket Class not found." | tee -a "$LOG_FILE"; } log_and_run echo "Deleting s3-secret-for-cosi secret..." log_and_run kubectl delete secret s3-secret-for-cosi --namespace=default || { echo "Secret s3-secret-for-cosi not found." | tee -a "$LOG_FILE"; } diff --git a/.github/scripts/e2e_tests.sh b/.github/scripts/e2e_tests.sh index 1dc46582..8b828928 100755 --- a/.github/scripts/e2e_tests.sh +++ b/.github/scripts/e2e_tests.sh @@ -239,4 +239,62 @@ if [[ "$USER_EXISTS" != *"NoSuchEntity"* ]]; then exit 1 fi +# Step 13: Test deletion bucket with deletion policy set + +log_and_run echo "Applying Bucket Class with deletion policy and respective Bucket Claim..." +log_and_run kubectl apply -f cosi-examples/bucketclass-delete-on-claim-removal.yaml +log_and_run kubectl apply -f cosi-examples/bucketclaim-delete-on-claim-removal.yaml + +log_and_run echo "Listing all S3 buckets before deletion..." +log_and_run aws s3 ls --endpoint-url "$S3_ENDPOINT" + +BUCKET_CLASS_NAME="bucket-class-delete-on-claim-removal" + +log_and_run echo "Verifying bucket creation with prefix '$BUCKET_CLASS_NAME'..." + +for ((i=1; i<=$ATTEMPTS; i++)); do + log_and_run aws --endpoint-url "$S3_ENDPOINT" s3 ls + BUCKET_TO_BE_DELETED=$(aws --endpoint-url "$S3_ENDPOINT" s3api list-buckets --query "Buckets[?starts_with(Name, '$BUCKET_CLASS_NAME')].Name" --output text) + + if [ -n "$BUCKET_TO_BE_DELETED" ]; then + log_and_run echo "Bucket created with prefix '$BUCKET_CLASS_NAME': $BUCKET_TO_BE_DELETED" + break + else + log_and_run echo "Attempt $i: Bucket with prefix '$BUCKET_CLASS_NAME' not found. Retrying in $DELAY seconds..." + sleep $DELAY + fi +done + +if [ -z "$BUCKET_TO_BE_DELETED" ]; then + log_and_run echo "Bucket with prefix '$BUCKET_CLASS_NAME' was not created." + exit 1 +fi + +log_and_run echo "Deleting Bucket Claim..." +log_and_run kubectl delete -f cosi-examples/bucketclaim-deletion-policy.yaml + +# Check if the bucket with name $BUCKET_TO_BE_DELETED exists by doing a head bucket. +# If bucket exists, retry with ATTEMPTS and DELAY. If bucket is not found, test success. + +log_and_run echo "Verifying bucket deletion with name '$BUCKET_TO_BE_DELETED'..." + +for ((i=1; i<=$ATTEMPTS; i++)); do + BUCKET_HEAD_RESULT=$(aws --endpoint-url "$S3_ENDPOINT" s3api head-bucket --bucket "$BUCKET_TO_BE_DELETED" 2>&1 || true) + + if [[ "$BUCKET_HEAD_RESULT" == *"Not Found"* ]]; then + log_and_run echo "Bucket with name '$BUCKET_TO_BE_DELETED' not found. Bucket deletion successful." + break + else + log_and_run echo "Attempt $i: Bucket with name '$BUCKET_TO_BE_DELETED' still exists. Retrying in $DELAY seconds..." + sleep $DELAY + fi +done + +if [[ "$BUCKET_HEAD_RESULT" != *"Not Found"* ]]; then + log_and_run echo "Bucket with name '$BUCKET_TO_BE_DELETED' was not deleted after $ATTEMPTS attempts." + exit 1 +fi + +log_and_run echo "Bucket deletion verified successfully." + log_and_run echo "All verifications for object-storage-access-secret passed successfully." diff --git a/cosi-examples/bucketclaim-delete-on-claim-removal.yaml b/cosi-examples/bucketclaim-delete-on-claim-removal.yaml new file mode 100644 index 00000000..85b90fe0 --- /dev/null +++ b/cosi-examples/bucketclaim-delete-on-claim-removal.yaml @@ -0,0 +1,8 @@ +kind: BucketClaim +apiVersion: objectstorage.k8s.io/v1alpha1 +metadata: + name: bucket-claim-auto-delete-s3-bucket +spec: + bucketClassName: bucket-class-delete-on-claim-removal + protocols: + - s3 diff --git a/cosi-examples/bucketclass-delete-on-claim-removal.yaml b/cosi-examples/bucketclass-delete-on-claim-removal.yaml new file mode 100644 index 00000000..0634bc03 --- /dev/null +++ b/cosi-examples/bucketclass-delete-on-claim-removal.yaml @@ -0,0 +1,9 @@ +kind: BucketClass +apiVersion: objectstorage.k8s.io/v1alpha1 +metadata: + name: bucket-class-delete-on-claim-removal +driverName: cosi.scality.com +deletionPolicy: Delete +parameters: + objectStorageSecretName: s3-secret-for-cosi + objectStorageSecretNamespace: default