Files
simple-object-server/test.sh
2025-05-25 12:36:29 +12:00

274 lines
8.6 KiB
Bash
Executable File

#! /bin/bash
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
}
# 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 ~/.config/simple_object_storage/config.json
CONFIG_PATH="${HOME}/.config/simple_object_storage/config.json"
if [ ! -f "${CONFIG_PATH}" ]; then
echo "config file not found at ${CONFIG_PATH}"
exit 1
fi
CONFIG=$(cat "${CONFIG_PATH}")
# get the host and port from the config
HOST=$(echo $CONFIG | jq -r '.host')
PORT=$(echo $CONFIG | jq -r '.port')
# 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"
fi
echo "Simple Object Storage server is running at ${BASE_URL}"
# 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 <<EOF
{
"labels": ["${BASE_TAG}"],
"tags": ["test1"],
"description": "Example file",
"custom_field": "custom value"
}
EOF
)
# upload this script as an object
echo "uploading ${SCRIPT_DIR}/${SCRIPT_NAME} to ${BASE_TAG}:test1"
UPLOAD_RESPONSE=$(curl -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata=${METADATA_JSON}" \
"http://localhost:8123/upload")
echo "upload response: ${UPLOAD_RESPONSE}"
OBJECT_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
# check the hash matches.
CHECK_HASH=$(curl -s "${BASE_URL}/hash/${BASE_TAG}:test1" | jq -r '.hash')
[ "${OBJECT_HASH}" != "${CHECK_HASH}" ] && die "hash does not match: ${OBJECT_HASH} != ${CHECK_HASH}"
# get md5sum of this file
MD5SUM=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME} | awk '{print $1}')
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
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
die "failed to download ${BASE_TAG}:test1"
fi
# get md5sum of the downloaded file
MD5SUM_DOWNLOADED1=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1 | awk '{print $1}')
echo "md5sum of ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1 is ${MD5SUM_DOWNLOADED1}"
[ "${MD5SUM}" != "${MD5SUM_DOWNLOADED1}" ] && die "md5sums do not match"
MD5SUM_DOWNLOADED2=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2 | awk '{print $1}')
[ "${MD5SUM}" != "${MD5SUM_DOWNLOADED2}" ] && die "md5sums do not match"
rm ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1
rm ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2
# 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
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}")
if ! echo "${DELETE_RESPONSE}" | jq -r '.result' | grep -q 'error'; then
die "failed to verify ${OBJECT_HASH} is deleted"
fi
# Test 1: Verify extra metadata fields are preserved
title "Testing metadata field preservation"
# Upload with extra metadata fields
EXTRA_METADATA_JSON=$(cat <<EOF
{
"labels": ["${BASE_TAG}"],
"tags": ["test2"],
"description": "Test with extra fields",
"custom_field": "custom value",
"extra_field1": "value1",
"extra_field2": "value2"
}
EOF
)
echo "uploading with extra metadata fields"
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")
UPLOAD_EXIT_CODE=$?
echo "Upload response: ${UPLOAD_RESPONSE}"
echo "Upload exit code: ${UPLOAD_EXIT_CODE}"
if [ ${UPLOAD_EXIT_CODE} -ne 0 ]; then
die "Failed to upload object: curl returned ${UPLOAD_EXIT_CODE}"
fi
if ! echo "${UPLOAD_RESPONSE}" | jq -e . >/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 "${BASE_URL}/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")
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
# Clean up
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${BASE_URL}/deleteobject?hash=${OBJECT_HASH}" > /dev/null
# Test 2: Verify tag versioning behavior
title "Testing tag versioning behavior"
# Upload first version with tag 'latest'
FIRST_METADATA_JSON=$(cat <<EOF
{
"labels": ["${BASE_TAG}"],
"tags": ["latest", "v1"],
"description": "First version"
}
EOF
)
echo "uploading first version with tag 'latest'"
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")
FIRST_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
# Store first version's metadata before uploading second version
FIRST_METADATA=$(curl -s "${BASE_URL}/meta/${BASE_TAG}:latest")
echo "First version metadata response: ${FIRST_METADATA}"
if ! echo "${FIRST_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'v1'; then
die "First version does not have v1 tag"
fi
# Upload second version with same tag 'latest'
SECOND_METADATA_JSON=$(cat <<EOF
{
"labels": ["${BASE_TAG}"],
"tags": ["latest", "v2"],
"description": "Second version"
}
EOF
)
if [ ! -f "${SCRIPT_DIR}/test_1GB_file_upload.sh" ]; then
die "test_1GB_file_upload.sh not found"
fi
echo "uploading second version with tag 'latest'"
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")
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}")
echo "First version metadata response: ${FIRST_METADATA}"
if ! echo "${FIRST_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'v1'; then
die "First version does not have v1 tag"
fi
# Verify first version's metadata no longer has the latest tag
if echo "${FIRST_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'latest'; then
die "First version still has latest tag"
fi
# Verify second version has the correct tags: v2 and latest
SECOND_METADATA=$(curl -s "${BASE_URL}/meta/${BASE_TAG}:latest")
echo "Second version metadata response: ${SECOND_METADATA}"
if ! echo "${SECOND_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'v2'; then
die "Second version does not have v2 tag"
fi
if ! echo "${SECOND_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'latest'; then
die "Second version does not have latest tag"
fi
# 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
title "ALL TESTS PASSED"