2019-12-11 04:48:29 -06:00
|
|
|
#!/bin/bash
|
2021-02-04 14:48:08 -06:00
|
|
|
# Runs the original NetBox unit tests and tests whether all initializers work.
|
2020-10-17 12:24:35 -05:00
|
|
|
# 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
|
2019-12-11 04:48:29 -06:00
|
|
|
|
|
|
|
# exit when a command exits with an exit code != 0
|
|
|
|
set -e
|
|
|
|
|
2020-10-17 12:24:35 -05:00
|
|
|
# IMAGE is used by `docker-compose.yml` do determine the tag
|
2019-12-11 04:48:29 -06:00
|
|
|
# of the Docker Image that is to be used
|
2020-10-17 12:24:35 -05:00
|
|
|
if [ "${1}x" != "x" ]; then
|
|
|
|
# Use the command line argument
|
|
|
|
export IMAGE="netboxcommunity/netbox:${1}"
|
|
|
|
else
|
|
|
|
export IMAGE="${IMAGE-netboxcommunity/netbox:latest}"
|
|
|
|
fi
|
2019-12-23 10:53:19 -06:00
|
|
|
|
2020-10-17 12:24:35 -05:00
|
|
|
# Ensure that an IMAGE is defined
|
2020-01-17 11:10:36 -06:00
|
|
|
if [ -z "${IMAGE}" ]; then
|
|
|
|
echo "⚠️ No image defined"
|
|
|
|
|
|
|
|
if [ -z "${DEBUG}" ]; then
|
2021-02-08 05:16:04 -06:00
|
|
|
exit 1
|
2020-01-17 11:10:36 -06:00
|
|
|
else
|
|
|
|
echo "⚠️ Would 'exit 1' here, but DEBUG is '${DEBUG}'."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# The docker compose command to use
|
2020-11-10 08:23:07 -06:00
|
|
|
doco="docker-compose --file docker-compose.test.yml --project-name netbox_docker_test_${1}"
|
2019-12-11 04:48:29 -06:00
|
|
|
|
2020-01-17 10:21:47 -06:00
|
|
|
INITIALIZERS_DIR=".initializers"
|
2019-12-11 04:48:29 -06:00
|
|
|
|
2020-01-17 10:21:47 -06:00
|
|
|
test_setup() {
|
|
|
|
echo "🏗 Setup up test environment"
|
|
|
|
if [ -d "${INITIALIZERS_DIR}" ]; then
|
|
|
|
rm -rf "${INITIALIZERS_DIR}"
|
|
|
|
fi
|
2019-12-11 04:48:29 -06:00
|
|
|
|
2020-01-17 10:21:47 -06:00
|
|
|
mkdir "${INITIALIZERS_DIR}"
|
2019-12-11 04:48:29 -06:00
|
|
|
(
|
|
|
|
cd initializers
|
|
|
|
for script in *.yml; do
|
2021-02-08 05:16:04 -06:00
|
|
|
sed -E 's/^# //' "${script}" >"../${INITIALIZERS_DIR}/${script}"
|
2019-12-11 04:48:29 -06:00
|
|
|
done
|
|
|
|
)
|
2020-01-17 10:21:47 -06:00
|
|
|
}
|
2019-12-11 04:48:29 -06:00
|
|
|
|
2020-01-17 10:21:47 -06:00
|
|
|
test_netbox_unit_tests() {
|
2021-02-04 14:48:08 -06:00
|
|
|
echo "⏱ Running NetBox Unit Tests"
|
2021-09-13 01:18:23 -05:00
|
|
|
$doco run --rm netbox /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py test
|
2020-01-17 10:21:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
test_initializers() {
|
|
|
|
echo "🏭 Testing Initializers"
|
|
|
|
export INITIALIZERS_DIR
|
2021-09-13 01:18:23 -05:00
|
|
|
$doco run --rm netbox /opt/netbox/docker-entrypoint.sh ./manage.py check
|
2019-12-11 04:48:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
test_cleanup() {
|
|
|
|
echo "💣 Cleaning Up"
|
2019-12-23 10:53:19 -06:00
|
|
|
$doco down -v
|
|
|
|
|
2020-01-17 10:21:47 -06:00
|
|
|
if [ -d "${INITIALIZERS_DIR}" ]; then
|
|
|
|
rm -rf "${INITIALIZERS_DIR}"
|
2019-12-23 10:53:19 -06:00
|
|
|
fi
|
2019-12-11 04:48:29 -06:00
|
|
|
}
|
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
echo "🐳🐳🐳 Start testing '${IMAGE}'"
|
2019-12-11 04:48:29 -06:00
|
|
|
|
|
|
|
# Make sure the cleanup script is executed
|
|
|
|
trap test_cleanup EXIT ERR
|
2020-01-17 10:21:47 -06:00
|
|
|
test_setup
|
2019-12-11 04:48:29 -06:00
|
|
|
|
2020-10-17 15:10:33 -05:00
|
|
|
test_netbox_unit_tests
|
2019-12-11 04:48:29 -06:00
|
|
|
test_initializers
|
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
echo "🐳🐳🐳 Done testing '${IMAGE}'"
|