145 lines
5.0 KiB
Bash
Executable File
145 lines
5.0 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 every action in the README.md file, leaving the system in the same state it was found
|
|
# and print the output of each action
|
|
|
|
# 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={"labeltag":"${BASE_TAG}:test1","description":"Example file","tags":["test","example"],"custom_field":"custom value"}' \
|
|
"http://localhost:8123/upload")
|
|
|
|
echo "upload response: ${UPLOAD_RESPONSE}"
|
|
|
|
OBJECT_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
|
|
|
|
|
|
#OBJECT_HASH=$(curl -s "${BASE_URL}/upload?token=${WRITE_TOKEN}&labeltag=${BASE_TAG}:test1&filename=${SCRIPT_NAME}" -T ${SCRIPT_DIR}/${SCRIPT_NAME} | jq -r '.hash')
|
|
echo "received hash ${OBJECT_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"
|
|
|
|
# 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 tags
|
|
echo "deleting tag ${BASE_TAG}:test1"
|
|
if ! curl -s "${BASE_URL}/deletetag?token=${WRITE_TOKEN}&labeltag=${BASE_TAG}:test1" | jq -r '.result' | grep -q 'success'; then
|
|
die "failed to delete tag ${BASE_TAG}:test1"
|
|
fi
|
|
|
|
# testing we CANT download via the label:tag
|
|
echo "testing we CANT download via the label:tag"
|
|
if curl -s "${BASE_URL}/object/${BASE_TAG}:test1" | jq -r '.result' | grep -q 'success'; then
|
|
die "downloaded via the label:tag"
|
|
fi
|
|
|
|
# testing we can still download via the hash
|
|
echo "testing we can still download via the hash"
|
|
if ! curl -s "${BASE_URL}/exists/${OBJECT_HASH}" | jq -r '.result' | grep -q 'success'; then
|
|
die "failed to check exists via the hash"
|
|
fi
|
|
|
|
# download the object
|
|
echo "downloading ${OBJECT_HASH} to ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded4"
|
|
if ! curl -s "${BASE_URL}/object/${OBJECT_HASH}" -o ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded4; then
|
|
die "failed to download ${OBJECT_HASH}"
|
|
fi
|
|
|
|
MD5SUM_DOWNLOADED4=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded4 | awk '{print $1}')
|
|
MD5SUM=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME} | awk '{print $1}')
|
|
[ "${MD5SUM}" != "${MD5SUM_DOWNLOADED4}" ] && die "md5sums do not match"
|
|
|
|
# delete the downloaded file
|
|
rm ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded4
|
|
|
|
# delete the object
|
|
echo "deleting ${OBJECT_HASH}"
|
|
if ! curl -s "${BASE_URL}/deleteobject?token=${WRITE_TOKEN}&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
|
|
|
|
title "ALL TESTS PASSED"
|