#! /bin/bash set -euo pipefail HOSTURL="${1:-http://127.0.0.1:7703}" #------------------------------------------------------------------------------------------------ SCRIPT_DIR=$(dirname "$0") SCRIPT_NAME=$(basename "$0") # FUNCTIONS function title() { echo "----------------------------------------" # Center the text local text="$1" local line_length=40 local text_length=${#text} local padding=$(( (line_length - text_length) / 2 )) printf "%*s%s%*s\n" $padding "" "$text" $padding "" echo "----------------------------------------" } function die() { title "error: $1" exit 1 } #------------------------------------------------------------------------------------------------ cat << EOF EOF title "TESTING ${HOSTURL}" cat << EOF EOF # Test 0: Verify the script is running title "0: Verify the server is running" # test jq is installed if ! command -v jq &> /dev/null; then echo "jq could not be found" echo "sudo apt-get install jq" exit 1 fi # read sos_config.json CONFIG_PATH="${SCRIPT_DIR}/sos_config.json" if [ ! -f "${CONFIG_PATH}" ]; then echo "config file not found at ${CONFIG_PATH}" exit 1 fi CONFIG=$(cat "${CONFIG_PATH}") # extract the first write token from the config WRITE_TOKEN=$(echo "$CONFIG" | jq -r '.write_tokens[0]') BASE_TAG="autotest" if ! command -v wget &> /dev/null; then echo "wget could not be found" exit 1 fi # test if server is running if ! wget -qO- "${HOSTURL}/status" | jq -r '.result' | grep -q 'success'; then wget "${HOSTURL}/status" die "server is not running on ${HOSTURL}" fi echo "Simple Object Storage server is running at ${HOSTURL}" # test every action in the README.md file, leaving the system in the same state it was found # and print the output of each action # Construct metadata JSON METADATA_JSON=$(cat </dev/null 2>&1; then die "Invalid JSON response from upload: ${UPLOAD_RESPONSE}" fi OBJECT_HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash') echo "Received hash: ${OBJECT_HASH}" # Verify the object exists echo "Verifying object exists..." EXISTS_RESPONSE=$(curl -s "${HOSTURL}/exists/${BASE_TAG}:test2") echo "Exists response: ${EXISTS_RESPONSE}" # Get metadata and verify extra fields are preserved echo "Retrieving metadata for ${BASE_TAG}:test2" METADATA_RESPONSE=$(curl -s "${HOSTURL}/meta/${BASE_TAG}:test2") CURL_EXIT_CODE=$? echo "Curl exit code: ${CURL_EXIT_CODE}" echo "Full metadata response: ${METADATA_RESPONSE}" if [ ${CURL_EXIT_CODE} -ne 0 ]; then die "Failed to retrieve metadata: curl returned ${CURL_EXIT_CODE}" fi if ! echo "${METADATA_RESPONSE}" | jq -e . >/dev/null 2>&1; then die "Invalid JSON response: ${METADATA_RESPONSE}" fi if ! echo "${METADATA_RESPONSE}" | jq -r '.metadata.extra_field1' | grep -q 'value1'; then die "extra_field1 not preserved in metadata" fi if ! echo "${METADATA_RESPONSE}" | jq -r '.metadata.extra_field2' | grep -q 'value2'; then die "extra_field2 not preserved in metadata" fi #------------------------------------------------------------------------------------------------ title "6: Test tag versioning behavior" # Clean up curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${OBJECT_HASH}" > /dev/null # Upload first version with tag 'latest' FIRST_METADATA_JSON=$(cat < /dev/null curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${SECOND_HASH}" > /dev/null # Use a known invalid token INVALID_TOKEN="invalid_token" # Make 5 requests with an invalid token for i in {1..5}; do echo "Attempt $i with invalid token" RESPONSE=$(curl -s -X PUT -H "Authorization: Bearer ${INVALID_TOKEN}" -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" -F "metadata={\"labeltags\":[\"test:latest\"]}" "${HOSTURL}/upload") echo "Response: ${RESPONSE}" done # Now try a request with a valid token - should be rate limited echo "Attempting request with valid token (should be rate limited)" RESPONSE=$(curl -s -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" -F "metadata={\"labeltags\":[\"test:latest\"]}" "${HOSTURL}/upload") if ! echo "${RESPONSE}" | jq -r '.error' | grep -q "Too many authentication attempts"; then die "Expected rate limit error, got: ${RESPONSE}" fi echo "Sleeping for 3 seconds to allow rate limit to reset" echo "(Normally 5 mins, but we set to 2s for this test!)" sleep 3 # Now try a request with a valid token - should be rate limited echo "Attempting request with valid token (should NOT be rate limited)" RESPONSE=$(curl -s -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" -F "metadata={\"labeltags\":[\"test:latest\"]}" "${HOSTURL}/upload") if echo "${RESPONSE}" | jq -r '.error' | grep -q "Too many authentication attempts"; then die "Expected no rate limit error, got: ${RESPONSE}" fi # delete the object TODELHASH=$(curl -s "${HOSTURL}/hash/test:latest" | jq -r '.hash') echo "deleting test:latest ${TODELHASH}" if ! curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${TODELHASH}" | jq -r '.result' | grep -q 'success'; then die "failed to delete ${TODELHASH}" fi #------------------------------------------------------------------------------------------------ title "ALL TESTS PASSED"