2021-05-28 18:13:28 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
if ! command -v newman &> /dev/null
|
|
|
|
then
|
|
|
|
echo "Newman could not be found. Run 'npm install -g newman' first."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-12-06 10:28:23 +03:00
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
--migration)
|
|
|
|
MIGRATION=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
script_path=$(realpath "${BASH_SOURCE[0]}")
|
|
|
|
script_dir=$(dirname "$script_path")
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
echo "Compiling."
|
|
|
|
(cd "$script_dir/.." || exit 1; CGO_ENABLED=0 go build)
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
cd "$script_dir" || exit 1
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
# Download previous release (when upgrade testing)
|
2022-12-06 10:28:23 +03:00
|
|
|
initial_run_exe="../wakapi"
|
|
|
|
if [[ $MIGRATION -eq 1 ]]; then
|
2022-12-04 10:02:36 +03:00
|
|
|
if [ ! -f wakapi_linux_amd64.zip ]; then
|
2022-12-06 10:28:23 +03:00
|
|
|
echo "Downloading latest release"
|
2022-12-04 10:02:36 +03:00
|
|
|
curl https://github.com/muety/wakapi/releases/latest/download/wakapi_linux_amd64.zip -O -L
|
|
|
|
fi
|
|
|
|
unzip -o wakapi_linux_amd64.zip
|
2022-12-06 10:28:23 +03:00
|
|
|
initial_run_exe="./wakapi"
|
2022-12-04 10:02:36 +03:00
|
|
|
echo "Running tests with release version"
|
|
|
|
fi
|
|
|
|
|
2022-12-27 12:37:53 +03:00
|
|
|
cleanup() {
|
|
|
|
if [ -n "$pid" ] && ps -p "$pid" > /dev/null; then
|
|
|
|
kill -TERM "$pid"
|
|
|
|
fi
|
|
|
|
if [ "${docker_down-0}" -eq 1 ]; then
|
2022-12-27 12:42:55 +03:00
|
|
|
docker compose -f "$script_dir/docker-compose.yml" down
|
2022-12-27 12:37:53 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
# Initialise test data
|
|
|
|
case $1 in
|
2022-12-27 06:39:32 +03:00
|
|
|
postgres|mysql|mariadb)
|
2022-12-27 12:42:55 +03:00
|
|
|
docker compose -f "$script_dir/docker-compose.yml" down
|
2022-12-27 06:39:32 +03:00
|
|
|
docker volume rm "testing_wakapi-$1"
|
|
|
|
|
|
|
|
docker_down=1
|
2022-12-27 12:42:55 +03:00
|
|
|
docker compose -f "$script_dir/docker-compose.yml" up --wait -d "$1"
|
2022-12-27 12:58:17 +03:00
|
|
|
|
|
|
|
config="config.$1.yml"
|
2022-12-27 06:39:32 +03:00
|
|
|
if [ "$1" == "mariadb" ]; then
|
|
|
|
config="config.mysql.yml"
|
|
|
|
fi
|
|
|
|
|
2022-12-27 07:35:11 +03:00
|
|
|
db_port=0
|
|
|
|
if [ "$1" == "postgres" ]; then
|
2022-12-27 12:37:53 +03:00
|
|
|
db_port=55432
|
2022-12-27 07:35:11 +03:00
|
|
|
else
|
2022-12-27 12:37:53 +03:00
|
|
|
db_port=53306
|
2022-12-27 06:39:32 +03:00
|
|
|
fi
|
2022-12-27 07:35:11 +03:00
|
|
|
|
|
|
|
for _ in $(seq 0 30); do
|
|
|
|
if netstat -tulpn 2>/dev/null | grep "LISTEN" | tr -s ' ' | cut -d' ' -f4 | grep -E ":$db_port$" > /dev/null; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
2022-12-27 06:39:32 +03:00
|
|
|
;;
|
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
sqlite|*)
|
|
|
|
rm -f wakapi_testing.db
|
|
|
|
|
|
|
|
echo "Creating database and schema ..."
|
|
|
|
sqlite3 wakapi_testing.db < schema.sql
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
echo "Importing seed data ..."
|
|
|
|
sqlite3 wakapi_testing.db < data.sql
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-27 06:39:32 +03:00
|
|
|
config="config.sqlite.yml"
|
2022-12-04 10:02:36 +03:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
wait_for_wakapi () {
|
|
|
|
counter=0
|
|
|
|
echo "Waiting for Wakapi to come up ..."
|
|
|
|
until curl --output /dev/null --silent --get --fail http://localhost:3000/api/health; do
|
|
|
|
if [ "$counter" -ge 5 ]; then
|
|
|
|
echo "Waited for 5s, but Wakapi failed to come up ..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
printf '.'
|
|
|
|
sleep 1
|
|
|
|
counter=$((counter+1))
|
|
|
|
done
|
2022-12-06 10:28:23 +03:00
|
|
|
sleep 1
|
2022-12-04 10:02:36 +03:00
|
|
|
printf "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run tests
|
|
|
|
echo "Running Wakapi testing instance in background ..."
|
2022-12-27 06:39:32 +03:00
|
|
|
echo "Configuration file: $config"
|
2022-12-06 10:28:23 +03:00
|
|
|
"$initial_run_exe" -config "$config" &
|
2022-12-04 10:02:36 +03:00
|
|
|
pid=$!
|
2022-12-06 10:28:23 +03:00
|
|
|
wait_for_wakapi
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-27 06:39:32 +03:00
|
|
|
if [ "$1" == "sqlite" ]; then
|
|
|
|
echo "Running test collection ..."
|
|
|
|
newman run "wakapi_api_tests.postman_collection.json"
|
|
|
|
exit_code=$?
|
|
|
|
else
|
|
|
|
exit_code=0
|
|
|
|
fi
|
2021-05-28 18:13:28 +03:00
|
|
|
|
|
|
|
echo "Shutting down Wakapi ..."
|
2021-09-17 14:21:09 +03:00
|
|
|
kill -TERM $pid
|
2021-05-28 18:13:28 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
# Run upgrade tests
|
2022-12-06 10:28:23 +03:00
|
|
|
if [[ $MIGRATION -eq 1 ]]; then
|
|
|
|
echo "Running migrations with build"
|
|
|
|
../wakapi -config "$config" &
|
2022-12-04 10:02:36 +03:00
|
|
|
pid=$!
|
|
|
|
|
|
|
|
wait_for_wakapi
|
|
|
|
echo "Shutting down Wakapi ..."
|
|
|
|
kill -TERM $pid
|
|
|
|
fi
|
2021-09-18 15:24:16 +03:00
|
|
|
|
2022-12-04 10:02:36 +03:00
|
|
|
echo "Exiting with status $exit_code"
|
2022-02-17 14:20:22 +03:00
|
|
|
exit $exit_code
|