Skip to content

Commit

Permalink
COSI-25: refactor-tests-for-provisioner-server
Browse files Browse the repository at this point in the history
This commit refactors the test file by:
- Extracting common test data and parameters into helper functions
- Reducing repetitive code in BeforeEach/AfterEach blocks
- Using local variables or dereferencing pointers to avoid
  addressability errors for string constants
- Improving readability and maintainability of the tests
- used AWS CLI delay and retries to check user and bucket deletion
  • Loading branch information
anurag4DSB committed Dec 13, 2024
1 parent 10f4962 commit 4bb5259
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 497 deletions.
61 changes: 23 additions & 38 deletions .github/scripts/e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ SECRET_NAME="object-storage-access-secret"
NAMESPACE="default"
ADMIN_ACCESS_KEY_ID=D4IT2AWSB588GO5J9T00
ADMIN_SECRET_ACCESS_KEY=UEEu8tYlsOGGrgf4DAiSZD6apVNPUWqRiPG0nTB6
ATTEMPTS=12 # Total AWS CLI attempts (2 minutes / 10 seconds per attempt)
DELAY=10 # Delay between AWS CLI requests attempts in seconds


# Error handling function
error_handler() {
Expand Down Expand Up @@ -101,10 +104,6 @@ sleep 5

log_and_run echo "Verifying bucket creation..."


ATTEMPTS=12 # Total attempts (2 minutes / 10 seconds per attempt)
DELAY=10 # Delay between attempts in seconds

for ((i=1; i<=$ATTEMPTS; i++)); do
log_and_run aws --endpoint-url "$S3_ENDPOINT" s3 ls
BUCKET_FOUND=$(aws --endpoint-url "$S3_ENDPOINT" s3api list-buckets --query "Buckets[?starts_with(Name, '$BUCKET_CLASS_NAME')].Name" --output text)
Expand Down Expand Up @@ -220,23 +219,12 @@ log_and_run kubectl delete -f cosi-examples/bucketaccess.yaml
log_and_run echo "Verifying IAM user '$IAM_USER_NAME' deletion..."
log_and_run aws --endpoint-url "$IAM_ENDPOINT" iam get-user --user-name "$IAM_USER_NAME"

# Retry logic for checking user deletion

for ((i=1; i<=$ATTEMPTS; i++)); do
USER_EXISTS="$(aws --endpoint-url "$IAM_ENDPOINT" iam get-user --user-name "$IAM_USER_NAME" 2>&1 || true)"

if [[ "$USER_EXISTS" == *"NoSuchEntity"* ]]; then
log_and_run echo "IAM user '$IAM_USER_NAME' successfully deleted."
break
else
log_and_run echo "Attempt $i: IAM user '$IAM_USER_NAME' still exists. Retrying in $DELETE_DELAY seconds..."
sleep $DELAY
fi
done
USER_EXISTS="$(aws --endpoint-url "$IAM_ENDPOINT" iam get-user --user-name "$IAM_USER_NAME" --retry-mode standard --max-attempts 12 --delay $DELAY 2>&1 || true)"

if [[ "$USER_EXISTS" != *"NoSuchEntity"* ]]; then
log_and_run echo "IAM user '$IAM_USER_NAME' was not deleted."
exit 1
if [[ "$USER_EXISTS" == *"NoSuchEntity"* ]]; then
log_and_run echo "IAM user '$IAM_USER_NAME' successfully deleted."
else
log_and_run echo "IAM user '$IAM_USER_NAME' still exists after retries."
fi

# Step 13: Test deletion bucket with deletion policy set
Expand Down Expand Up @@ -278,23 +266,20 @@ log_and_run kubectl delete -f cosi-examples/bucketclaim-deletion-policy.yaml

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."
# Check if the bucket has been deleted
log_and_run aws s3 ls --endpoint-url "$S3_ENDPOINT"
AWS_MAX_ATTEMPTS=$ATTEMPTS
AWS_RETRY_DELAY=$DELAY
# Run head-bucket to check if the bucket has been deleted
BUCKET_HEAD_RESULT="$(log_and_run aws --endpoint-url "$S3_ENDPOINT" s3api head-bucket --bucket "$BUCKET_TO_BE_DELETED" 2>&1 || true)"

# Log the actual error result for debugging purposes
log_and_run echo "head-bucket result: $BUCKET_HEAD_RESULT"

# Check if the result contains the "Not Found" error message
if [[ "$BUCKET_HEAD_RESULT" == *"Not Found"* ]]; then
log_and_run echo "Bucket with name '$BUCKET_TO_BE_DELETED' was successfully deleted (Not Found error)."
else
log_and_run echo "Bucket with name '$BUCKET_TO_BE_DELETED' was not deleted after $ATTEMPTS attempts. Error: $BUCKET_HEAD_RESULT"
exit 1
fi

log_and_run echo "Bucket deletion verified successfully."

log_and_run echo "All verifications for object-storage-access-secret passed successfully."
Loading

0 comments on commit 4bb5259

Please sign in to comment.