netbox-docker/test.sh

87 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
2020-10-17 19:24:35 +02:00
# Runs the original Netbox unit tests and tests whether all initializers work.
# Usage:
# ./test.sh latest
# ./test.sh v2.9.7
# ./test.sh develop-2.10
# IMAGE='netboxcommunity/netbox:latest' ./test.sh
# IMAGE='netboxcommunity/netbox:v2.9.7' ./test.sh
# IMAGE='netboxcommunity/netbox:develop-2.10' ./test.sh
# export IMAGE='netboxcommunity/netbox:latest'; ./test.sh
# export IMAGE='netboxcommunity/netbox:v2.9.7'; ./test.sh
# export IMAGE='netboxcommunity/netbox:develop-2.10'; ./test.sh
# exit when a command exits with an exit code != 0
set -e
2020-10-17 19:24:35 +02:00
# IMAGE is used by `docker-compose.yml` do determine the tag
# of the Docker Image that is to be used
2020-10-17 19:24:35 +02:00
if [ "${1}x" != "x" ]; then
# Use the command line argument
export IMAGE="netboxcommunity/netbox:${1}"
else
export IMAGE="${IMAGE-netboxcommunity/netbox:latest}"
fi
2020-10-17 19:24:35 +02:00
# Ensure that an IMAGE is defined
2020-01-17 18:10:36 +01:00
if [ -z "${IMAGE}" ]; then
echo "⚠️ No image defined"
if [ -z "${DEBUG}" ]; then
exit 1;
else
echo "⚠️ Would 'exit 1' here, but DEBUG is '${DEBUG}'."
fi
fi
# The docker compose command to use
doco="docker-compose -f docker-compose.test.yml"
INITIALIZERS_DIR=".initializers"
test_setup() {
echo "🏗 Setup up test environment"
if [ -d "${INITIALIZERS_DIR}" ]; then
rm -rf "${INITIALIZERS_DIR}"
fi
mkdir "${INITIALIZERS_DIR}"
(
cd initializers
for script in *.yml; do
sed -E 's/^# //' "${script}" > "../${INITIALIZERS_DIR}/${script}"
done
)
}
test_netbox_unit_tests() {
echo "⏱ Running Netbox Unit Tests"
$doco run --rm netbox ./manage.py test
}
test_initializers() {
echo "🏭 Testing Initializers"
export INITIALIZERS_DIR
$doco run --rm netbox ./manage.py check
}
test_cleanup() {
echo "💣 Cleaning Up"
$doco down -v
if [ -d "${INITIALIZERS_DIR}" ]; then
rm -rf "${INITIALIZERS_DIR}"
fi
}
echo "🐳🐳🐳 Start testing '${IMAGE}'"
# Make sure the cleanup script is executed
trap test_cleanup EXIT ERR
test_setup
2020-10-17 22:10:33 +02:00
test_netbox_unit_tests
test_initializers
echo "🐳🐳🐳 Done testing '${IMAGE}'"