:-'Generic Commit'

This commit is contained in:
Your Name
2025-05-29 18:37:41 +12:00
parent 2444180a96
commit 73297c40b9
6 changed files with 84 additions and 59 deletions

68
test.sh
View File

@@ -3,6 +3,11 @@
set -euo pipefail
HOSTURL="$1"
if [ -z "${HOSTURL}" ]; then
HOSTURL="http://localhost:8123"
fi
#------------------------------------------------------------------------------------------------
SCRIPT_DIR=$(dirname "$0")
@@ -44,30 +49,25 @@ if ! command -v jq &> /dev/null; then
fi
# read ~/.config/simple_object_storage/config.json
CONFIG_PATH="${SCRIPT_DIR}/.docker/config.json"
CONFIG_PATH="${SCRIPT_DIR}/.test-docker/config.json"
if [ ! -f "${CONFIG_PATH}" ]; then
echo "config file not found at ${CONFIG_PATH}"
exit 1
fi
CONFIG=$(cat "${CONFIG_PATH}")
HOST=localhost
PORT=8123
# extract the first write token from the config
WRITE_TOKEN=$(echo "$CONFIG" | jq -r '.write_tokens[0]')
BASE_URL="http://${HOST}:${PORT}"
BASE_TAG="autotest"
# test if server is running
if ! curl -s "${BASE_URL}/status" | jq -r '.result' | grep -q 'success'; then
die "server is not running"
if ! curl -s "${HOSTURL}/status" | jq -r '.result' | grep -q 'success'; then
die "server is not running on ${HOSTURL}"
fi
echo "Simple Object Storage server is running at ${BASE_URL}"
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
@@ -92,7 +92,7 @@ UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata=${METADATA_JSON}" \
"http://localhost:8123/upload")
"${HOSTURL}/upload")
echo "upload response: ${UPLOAD_RESPONSE}"
@@ -102,7 +102,7 @@ OBJECT_HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash')
title "2: Check sos hash matches"
# check the hash matches.
CMD="${BASE_URL}/hash/${BASE_TAG}:test1"
CMD="${HOSTURL}/hash/${BASE_TAG}:test1"
echo "checking hash via ${CMD}"
CHECK_HASH=$(curl -s "${CMD}" | jq -r '.hash')
[ "${OBJECT_HASH}" != "${CHECK_HASH}" ] && die "hash does not match: ${OBJECT_HASH} != ${CHECK_HASH}"
@@ -117,13 +117,13 @@ echo "md5sum of ${SCRIPT_DIR}/${SCRIPT_NAME} is ${MD5SUM}"
# download the object
echo "downloading ${OBJECT_HASH} to ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1"
if ! curl -s "${BASE_URL}/object/${OBJECT_HASH}" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1"; then
if ! curl -s "${HOSTURL}/object/${OBJECT_HASH}" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1"; then
die "failed to download ${OBJECT_HASH}"
fi
# download the object again via the label:tag
echo "downloading ${BASE_TAG}:test1 to ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2"
if ! curl -s "${BASE_URL}/object/${BASE_TAG}:test1" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2"; then
if ! curl -s "${HOSTURL}/object/${BASE_TAG}:test1" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2"; then
die "failed to download ${BASE_TAG}:test1"
fi
@@ -156,7 +156,7 @@ UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata=${METADATA_JSON}" \
"http://localhost:8123/upload")
"${HOSTURL}/upload")
echo "upload response: ${UPLOAD_RESPONSE}"
@@ -167,7 +167,7 @@ if [ ! "${OBJECT_HASH}" == "${OBJECT_HASH2}" ]; then
fi
# test the description and custom field matches our new ones.
METADATA_RESPONSE=$(curl "http://localhost:8123/meta/${OBJECT_HASH2}")
METADATA_RESPONSE=$(curl "${HOSTURL}/meta/${OBJECT_HASH2}")
echo "metadata response: ${METADATA_RESPONSE}"
@@ -181,7 +181,7 @@ fi
# download via the label:tag
echo "downloading ${LABELTAG} to ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3"
if ! curl -s "${BASE_URL}/object/${LABELTAG}" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3"; then
if ! curl -s "${HOSTURL}/object/${LABELTAG}" -o "${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3"; then
die "failed to download ${LABELTAG}"
fi
@@ -199,13 +199,13 @@ title "4: Delete the object"
# delete the object
echo "deleting ${OBJECT_HASH}"
if ! curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${OBJECT_HASH}" | jq -r '.result' | grep -q 'success'; then
if ! curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${OBJECT_HASH}" | jq -r '.result' | grep -q 'success'; then
die "failed to delete ${OBJECT_HASH}"
fi
# verify the object is deleted
echo "verifying ${OBJECT_HASH} is deleted"
DELETE_RESPONSE=$(curl -s "${BASE_URL}/object/${OBJECT_HASH}")
DELETE_RESPONSE=$(curl -s "${HOSTURL}/object/${OBJECT_HASH}")
if ! echo "${DELETE_RESPONSE}" | jq -r '.result' | grep -q 'error'; then
die "failed to verify ${OBJECT_HASH} is deleted"
fi
@@ -230,7 +230,7 @@ UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata=${EXTRA_METADATA_JSON}" \
"http://localhost:8123/upload")
"${HOSTURL}/upload")
UPLOAD_EXIT_CODE=$?
echo "Upload response: ${UPLOAD_RESPONSE}"
echo "Upload exit code: ${UPLOAD_EXIT_CODE}"
@@ -248,12 +248,12 @@ echo "Received hash: ${OBJECT_HASH}"
# Verify the object exists
echo "Verifying object exists..."
EXISTS_RESPONSE=$(curl -s "${BASE_URL}/exists/${BASE_TAG}:test2")
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 "${BASE_URL}/meta/${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}"
@@ -277,7 +277,7 @@ fi
title "6: Test tag versioning behavior"
# Clean up
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${OBJECT_HASH}" > /dev/null
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${OBJECT_HASH}" > /dev/null
# Upload first version with tag 'latest'
FIRST_METADATA_JSON=$(cat <<EOF
@@ -293,12 +293,12 @@ UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata=${FIRST_METADATA_JSON}" \
"http://localhost:8123/upload")
"${HOSTURL}/upload")
FIRST_HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash')
# Store first version's metadata before uploading second version
FIRST_METADATA=$(curl -s "${BASE_URL}/meta/${FIRST_HASH}")
FIRST_METADATA=$(curl -s "${HOSTURL}/meta/${FIRST_HASH}")
echo "First version metadata response: ${FIRST_METADATA}"
if ! echo "${FIRST_METADATA}" | jq -r '.metadata.labeltags[]' | grep -q "${BASE_TAG}:v1"; then
die "First version does not have v1 tag"
@@ -322,12 +322,12 @@ UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/test_1GB_file_upload.sh" \
-F "metadata=${SECOND_METADATA_JSON}" \
"http://localhost:8123/upload")
"${HOSTURL}/upload")
SECOND_HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash')
# Verify first version's metadata still has v1 tag
FIRST_METADATA=$(curl -s "${BASE_URL}/meta/${FIRST_HASH}")
FIRST_METADATA=$(curl -s "${HOSTURL}/meta/${FIRST_HASH}")
echo "First version metadata response: ${FIRST_METADATA}"
if ! echo "${FIRST_METADATA}" | jq -r '.metadata.labeltags[]' | grep -q "${BASE_TAG}:v1"; then
die "First version does not have v1 tag"
@@ -339,7 +339,7 @@ if echo "${FIRST_METADATA}" | jq -r '.metadata.labeltags[]' | grep -q "${BASE_TA
fi
# Verify second version has the correct tags: v2 and latest
SECOND_METADATA=$(curl -s "${BASE_URL}/meta/${SECOND_HASH}")
SECOND_METADATA=$(curl -s "${HOSTURL}/meta/${SECOND_HASH}")
echo "Second version metadata response: ${SECOND_METADATA}"
if ! echo "${SECOND_METADATA}" | jq -r '.metadata.labeltags[]' | grep -q "${BASE_TAG}:latest"; then
die "Second version does not have latest tag"
@@ -353,8 +353,8 @@ title "7: Test rate limiting behavior"
# Clean up
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${FIRST_HASH}" > /dev/null
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${SECOND_HASH}" > /dev/null
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${FIRST_HASH}" > /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"
@@ -362,13 +362,13 @@ 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\"]}" "${BASE_URL}/upload")
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\"]}" "${BASE_URL}/upload")
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
@@ -379,16 +379,16 @@ 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\"]}" "${BASE_URL}/upload")
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 "${BASE_URL}/hash/test:latest" | jq -r '.hash')
TODELHASH=$(curl -s "${HOSTURL}/hash/test:latest" | jq -r '.hash')
echo "deleting test:latest ${TODELHASH}"
if ! curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${TODELHASH}" | jq -r '.result' | grep -q 'success'; then
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