112 lines
3.9 KiB
Bash
112 lines
3.9 KiB
Bash
#! /bin/bash
|
|
|
|
SCRIPT_DIR=$(dirname $0)
|
|
SCRIPT_NAME=$(basename $0)
|
|
|
|
|
|
function die() {
|
|
echo "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"
|
|
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"
|
|
curl -s "${BASE_URL}/object/${OBJECT_HASH}" -o ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded1
|
|
|
|
# download the object again via the label:tag
|
|
echo "downloading ${BASE_TAG}:test1 to ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2"
|
|
curl -s "${BASE_URL}/object/${BASE_TAG}:test1" -o ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded2
|
|
|
|
# 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}/object/${OBJECT_HASH}" -o ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3 | jq -r '.result' | grep -q 'success'; then
|
|
die "failed to download via the hash"
|
|
fi
|
|
|
|
MD5SUM_DOWNLOADED3=$(md5sum ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3 | awk '{print $1}')
|
|
[ "${MD5SUM}" != "${MD5SUM_DOWNLOADED3}" ] && die "md5sums do not match"
|
|
|
|
# delete the downloaded file
|
|
rm ${SCRIPT_DIR}/${SCRIPT_NAME}.downloaded3
|
|
|
|
# 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}")
|
|
echo "delete response: ${DELETE_RESPONSE}"
|
|
if ! echo "${DELETE_RESPONSE}" | jq -r '.result' | grep -q 'success'; then
|
|
die "failed to verify ${OBJECT_HASH} is deleted"
|
|
fi
|
|
|
|
|