Files
versitygw/tests/commands/head_bucket.sh

112 lines
3.5 KiB
Bash

#!/usr/bin/env bash
# Copyright 2024 Versity Software
# This file is licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
source ./tests/drivers/file.sh
source ./tests/report.sh
# params: client, bucket name
# fail for invalid params, return
# 0 - bucket exists
# 1 - bucket does not exist
# 2 - misc error
head_bucket() {
log 6 "head_bucket '$1' '$2'"
if ! check_param_count "head_bucket" "client, bucket name" 2 $#; then
return 2
fi
local exit_code=0
if [[ $1 == 's3api' ]] || [[ $1 == 's3' ]]; then
bucket_info=$(send_command aws --no-verify-ssl s3api head-bucket --bucket "$2" 2>&1) || exit_code=$?
elif [[ $1 == "s3cmd" ]]; then
bucket_info=$(send_command s3cmd "${S3CMD_OPTS[@]}" --no-check-certificate info "s3://$2" 2>&1) || exit_code=$?
elif [[ $1 == 'mc' ]]; then
bucket_info=$(send_command mc --insecure stat "$MC_ALIAS"/"$2" 2>&1) || exit_code=$?
elif [[ $1 == 'rest' ]]; then
bucket_info=$(head_bucket_rest "$2" 2>&1) || exit_code=$?
log 5 "head bucket rest exit code: $exit_code"
return $exit_code
else
log 2 "invalid command type $1"
fi
if [ $exit_code -ne 0 ]; then
if [[ "$bucket_info" == *"404"* ]] || [[ "$bucket_info" == *"does not exist"* ]]; then
exit_code=1
else
exit_code=2
fi
echo "error getting bucket info: $bucket_info" >&2
return $exit_code
fi
echo "$bucket_info"
return 0
}
head_bucket_rest() {
log 6 "head_bucket_rest '$1'"
if ! check_param_count_gt "bucket, callback, params (optional)" 1 $#; then
return 2
fi
if ! result_file=$(get_file_name 2>&1); then
log 2 "error getting result file: $result_file"
return 1
fi
if ! result=$(COMMAND_LOG="$COMMAND_LOG" BUCKET_NAME="$1" OUTPUT_FILE="$TEST_FILE_FOLDER/$result_file" ./tests/rest_scripts/head_bucket.sh 2>&1); then
log 2 "error getting head bucket: $result"
return 2
fi
local callback_code=0
if [ "$2" != "" ]; then
callback_result="$($2 "$result" "$TEST_FILE_FOLDER/$result_file" 2>&1)" || callback_code=$?
echo "$callback_result"
return "$callback_code"
fi
response_data="$(cat "$TEST_FILE_FOLDER/$result_file")"
if [ "$result" == "200" ]; then
echo "$response_data"
return 0
fi
if [ "$result" == "404" ]; then
echo "$response_data"
return 1
fi
echo "unexpected response code '$result' ($(cat "$TEST_FILE_FOLDER/$result_file"))" >&2
return 2
}
head_bucket_rest_expect_success() {
if ! check_param_count_v2 "bucket name, params" 2 $#; then
return 1
fi
env_vars="BUCKET_NAME=$1 $2"
if ! send_rest_command_expect_success "$env_vars" "./tests/rest_scripts/head_bucket.sh" "200"; then
log 2 "error sending REST command"
return 1
fi
return 0
}
head_bucket_rest_expect_error() {
if ! check_param_count_v2 "bucket name, params, response code, message" 4 $#; then
return 1
fi
env_vars="BUCKET_NAME=$1 $2"
if ! send_rest_command_expect_header_error "$env_vars" "./tests/rest_scripts/head_bucket.sh" "$3" "$4"; then
log 2 "error sending REST command and checking error"
return 1
fi
return 0
}