This commit is contained in:
95
testing/test-docker.sh
Executable file
95
testing/test-docker.sh
Executable file
@@ -0,0 +1,95 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
set -x
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
MAIN_DIR=$(cd "${SCRIPT_DIR}/.." && pwd)
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
function wait_for_container {
|
||||
container_id="$1"
|
||||
container_name="$(docker inspect "${container_id}" --format '{{ .Name }}')"
|
||||
echo "Waiting for container: ${container_name} [${container_id}]"
|
||||
waiting_done="false"
|
||||
while [[ "${waiting_done}" != "true" ]]; do
|
||||
container_state="$(docker inspect "${container_id}" --format '{{ .State.Status }}')"
|
||||
if [[ "${container_state}" == "running" ]]; then
|
||||
health_status="$(docker inspect "${container_id}" --format '{{ .State.Health.Status }}')"
|
||||
echo "${container_name}: container_state=${container_state}, health_status=${health_status}"
|
||||
if [[ ${health_status} == "healthy" ]]; then
|
||||
waiting_done="true"
|
||||
fi
|
||||
else
|
||||
echo "${container_name}: container_state=${container_state}"
|
||||
health_status="${container_state}"
|
||||
waiting_done="true"
|
||||
fi
|
||||
sleep 1;
|
||||
done;
|
||||
[ "${health_status}" == "healthy" ]
|
||||
}
|
||||
|
||||
#------------------------------------------------------------------------------------------------
|
||||
# build the executable
|
||||
title "Building amd64 executable"
|
||||
"${MAIN_DIR}/build.sh" amd64
|
||||
|
||||
#------------------------------------------------------------------------------------------------
|
||||
# build the docker image
|
||||
title "Building docker image"
|
||||
docker buildx build --no-cache --load -t gitea.jde.nz/public/simple-object-storage:test --platform linux/amd64 "${MAIN_DIR}"
|
||||
|
||||
#------------------------------------------------------------------------------------------------
|
||||
# run the docker container
|
||||
title "Running docker container"
|
||||
export LOCALCONFIG="${SCRIPT_DIR}/config.json"
|
||||
export COMPOSE_FILE="${SCRIPT_DIR}/compose.yaml"
|
||||
|
||||
[ -f "${LOCALCONFIG}" ] || die "Config file not found: ${LOCALCONFIG}"
|
||||
[ -f "${COMPOSE_FILE}" ] || die "Compose file not found: ${COMPOSE_FILE}"
|
||||
|
||||
#------------------------------------------------------------------------------------------------
|
||||
title "Running tests"
|
||||
|
||||
# clear any existing containers.
|
||||
docker compose -f "${COMPOSE_FILE}" down
|
||||
docker compose -f "${COMPOSE_FILE}" rm -v
|
||||
|
||||
LOCALCONFIG=${LOCALCONFIG} docker compose -f "${COMPOSE_FILE}" up -d
|
||||
|
||||
# wait until healthy.
|
||||
if ! wait_for_container "sos-test"; then
|
||||
echo "----------------------------------------"
|
||||
echo "Container sos-test is not healthy"
|
||||
echo "----------------------------------------"
|
||||
docker logs sos-test
|
||||
docker inspect sos-test
|
||||
die "Container sos-test is not healthy"
|
||||
fi
|
||||
|
||||
# run the tests.
|
||||
"${SCRIPT_DIR}/test.sh" "http://localhost:7703"
|
||||
RESULT=$?
|
||||
|
||||
# clean up.
|
||||
docker compose -f "${COMPOSE_FILE}" down
|
||||
docker compose -f "${COMPOSE_FILE}" rm -v
|
||||
|
||||
exit $RESULT
|
||||
|
Reference in New Issue
Block a user