netbox-docker/test.sh

120 lines
3.2 KiB
Bash
Raw Normal View History

#!/bin/bash
# Runs the original NetBox unit tests and tests whether all initializers work.
2020-10-17 19:24:35 +02: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
# exit when a command exits with an exit code != 0
set -e
2023-03-16 21:44:08 +01:00
source ./build-functions/gh-functions.sh
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
2021-02-08 12:16:04 +01:00
exit 1
2020-01-17 18:10:36 +01:00
else
echo "⚠️ Would 'exit 1' here, but DEBUG is '${DEBUG}'."
fi
fi
# The docker compose command to use
2023-03-16 21:44:08 +01:00
doco="docker compose --file docker-compose.test.yml --file docker-compose.test.override.yml --project-name netbox_docker_test"
test_setup() {
2023-03-16 21:44:08 +01:00
gh_echo "::group:: Test setup"
echo "🏗 Setup up test environment"
$doco up --detach --quiet-pull --wait --force-recreate --renew-anon-volumes --no-start
$doco start postgres
$doco start redis
$doco start redis-cache
2023-03-16 21:44:08 +01:00
gh_echo "::endgroup::"
}
test_netbox_unit_tests() {
2023-03-16 21:44:08 +01:00
gh_echo "::group:: Netbox unit tests"
echo "⏱ Running NetBox Unit Tests"
2021-09-13 08:18:23 +02:00
$doco run --rm netbox /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py test
2023-03-16 21:44:08 +01:00
gh_echo "::endgroup::"
}
test_compose_db_setup() {
2023-03-16 21:44:08 +01:00
gh_echo "::group:: Netbox DB migrations"
echo "⏱ Running NetBox DB migrations"
$doco run --rm netbox /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py migrate
2023-03-16 21:44:08 +01:00
gh_echo "::endgroup::"
}
test_netbox_start() {
gh_echo "::group:: Start Netbox service"
echo "⏱ Starting NetBox services"
$doco up --detach --wait
gh_echo "::endgroup::"
}
test_netbox_web() {
gh_echo "::group:: Web service test"
echo "⏱ Starting web service test"
RESP_CODE=$(
curl \
--silent \
--output /dev/null \
--write-out '%{http_code}' \
--request GET \
--connect-timeout 5 \
--max-time 10 \
--retry 5 \
--retry-delay 0 \
--retry-max-time 40 \
http://127.0.0.1:8000/login/
2023-03-16 21:44:08 +01:00
)
if [ "$RESP_CODE" == "200" ]; then
echo "Webservice running"
else
echo "⚠️ Got response code '$RESP_CODE' but expected '200'"
exit 1
fi
gh_echo "::endgroup::"
}
test_cleanup() {
echo "💣 Cleaning Up"
2023-03-16 21:44:08 +01:00
gh_echo "::group:: Docker compose logs"
$doco logs --no-color
gh_echo "::endgroup::"
gh_echo "::group:: Docker compose down"
$doco down --volumes
2023-03-16 21:44:08 +01:00
gh_echo "::endgroup::"
}
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_compose_db_setup
2023-03-16 21:44:08 +01:00
test_netbox_start
test_netbox_web
echo "🐳🐳🐳 Done testing '${IMAGE}'"