From daaf72962f8e357272dcca37b2e9219e83b9ae6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4der?= Date: Wed, 11 Dec 2019 11:48:29 +0100 Subject: [PATCH] Adds a test that validates the initializer yml files --- hooks/test | 13 +++---------- test.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) create mode 100755 test.sh diff --git a/hooks/test b/hooks/test index 1dd5538..a66db12 100755 --- a/hooks/test +++ b/hooks/test @@ -2,21 +2,14 @@ . hooks/common -run_test() { - echo "🐳🐳🐳 Testing '${1}'" - VERSION="${1}" docker-compose run netbox ./manage.py test - docker-compose down -v - echo "🐳🐳🐳 Done testing '${1}'" -} - # test on builds of 'branches' if [ "${BUILD}" == "branches" ] \ || [ "${DEBUG}" == "true" ]; then - run_test latest - run_test snapshot + ./test.sh latest + ./test.sh snapshot # test on bulds of 'this' (i.e. pull request) elif [ "${BUILD}" == "this" ]; then - run_test latest + ./test.sh latest else echo "🐳🐳🐳 No tests are implemented for build '${BUILD}'." fi diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..d6d784a --- /dev/null +++ b/test.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# exit when a command exits with an exit code != 0 +set -e + +# version is used by `docker-compose.yml` do determine the tag +# of the Docker Image that is to be used +export VERSION=${VERSION-latest} + +test_netbox_unit_tests() { + echo "⏱ Running Netbox Unit Tests" + docker-compose run --rm netbox ./manage.py test +} + +test_initializers() { + echo "🏗 Testing Initializers" + + mkdir initializers_test + ( + cd initializers + for script in *.yml; do + sed -E 's/^# //' "${script}" > "../initializers_test/${script}" + done + ) + mv initializers initializers_original + mv initializers_test initializers + + docker-compose run --rm netbox ./manage.py check +} + +test_cleanup() { + echo "💣 Cleaning Up" + docker-compose down -v + rm -rf initializers + mv initializers_original initializers +} + +echo "🐳🐳🐳 Start testing '${VERSION}'" + +# Make sure the cleanup script is executed +trap test_cleanup EXIT ERR + +test_netbox_unit_tests +test_initializers + +echo "🐳🐳🐳 Done testing '${VERSION}'"