Files
SpacetimeDB/test/lib.include
T
Kim Altintop 4b9f30f5a0 ci: Exercise the right discipline to restart docker containers (#632)
One does not simply restart a docker container. But when you do,
SpacetimeDB also restarts.
2023-12-07 19:32:19 +01:00

119 lines
2.4 KiB
Bash

#!/bin/bash
# Runs a test with the assumption that it will return a zero result code
run_test() {
set +e
"$@" > "$TEST_OUT" 2>&1
RESULT=$?
cat "$TEST_OUT"
set -e
return "$RESULT"
}
# Runs a test with the assumption that it will return a non-zero result code
run_fail_test() {
if "$@" > "$TEST_OUT" 2>&1 ; then
cat "$TEST_OUT"
return 1
fi
cat "$TEST_OUT"
return 0
}
# This resets the spacetime config for a new test run
reset_config() {
SPACETIME_CONFIG_FILE="$(mktemp)"
export SPACETIME_CONFIG_FILE
cp "$RESET_SPACETIME_CONFIG" "$SPACETIME_CONFIG_FILE"
}
# This deletes the project from the previous test run
reset_project() {
PROJECT_PATH="$(mktemp -d)"
rmdir "$PROJECT_PATH"
cp -rp "$RESET_PROJECT_PATH" "$PROJECT_PATH"
export PROJECT_PATH
}
# This cleans up the temporary project directory after a test
clear_project() {
rm -rf "$PROJECT_PATH"
}
# This cleans up the temporary config file after a test
clear_config() {
rm -f "$SPACETIME_CONFIG_FILE"
}
random_string() {
if [[ "$OSTYPE" == "darwin"* ]]; then
echo $RANDOM | md5 -q | head -c 20
else
echo $RANDOM | md5sum | head -c 20
fi
}
spacetime_publish() {
RETURN_DIR=$PWD
cd "$SPACETIME_DIR"
set +e
run_test spacetime publish "$@"
RESULT_CODE=$?
cd "$RETURN_DIR" || exit 1
set -e
return "$RESULT_CODE"
}
fsed() {
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i.sed_bak "$@"
rm -f rm *.sed_bak
else
sed -i "$@"
fi
}
restart_docker() {
# Behold!
#
# You thought stop/start restarts? How wrong. Restart restarts.
docker compose restart
# The suspense!
#
# Wait until compose believes the health probe succeeds.
#
# The container may decide to recompile, or grab a coffee at crates.io, or
# whatever. In any case, restart doesn't mean the server is up yet.
docker compose up --no-recreate --detach --wait-timeout 60
# Belts and suspenders!
#
# The health probe runs inside the container, but that doesn't mean we can
# reach it from outside. Ping until we get through.
ping
}
ping() {
local retries=5
local success=0
while true
do
curl -sf http://127.0.0.1:3000/database/ping && { success=1; break; } || echo "Server down"
retries=$((retries - 1))
if [ $retries -gt 0 ]
then
sleep 5
else
break
fi
done
if [ $success -lt 1 ]
then
echo "Server at 127.0.0.1:3000 not responding"
exit 127
else
echo "Server up after $((5 - retries)) retries"
fi
}
# vim: noexpandtab tabstop=4 shiftwidth=4