From 7b55d67b34ce5af4eddcfd32dd3f36ba6d336418 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Mon, 27 Jan 2020 17:17:55 -0500 Subject: [PATCH] Add some tests and drone to run them --- .drone.yml | 24 ++++++++++++++++++++++++ Makefile | 21 +++++++++++++++++++++ compose-check.sh | 9 +++++---- tests/docker-compose.bad.yml | 1 + tests/docker-compose.yml | 6 ++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 .drone.yml create mode 100644 Makefile create mode 100644 tests/docker-compose.bad.yml create mode 100644 tests/docker-compose.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..56513ec --- /dev/null +++ b/.drone.yml @@ -0,0 +1,24 @@ +--- +kind: pipeline +name: test + +steps: + + - name: shellcheck + image: python:3-alpine + commands: + - apk install shellcheck + - make all + + - name: notify + image: drillster/drone-email + settings: + host: + from_secret: SMTP_HOST # pragma: whitelist secret + username: + from_secret: SMTP_USER # pragma: whitelist secret + password: + from_secret: SMTP_PASS # pragma: whitelist secret + from: drone@iamthefij.com + when: + status: [changed, failure] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c20cc9e --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +.PHONY: all test clean + +all: test shellcheck + +test: test-negative test-positive + +.PHONY: shellcheck +shellcheck: + shellcheck *.sh + +.PHONY: test-positive +test-positive: + @echo "Check valid compose file." + ./compose-check.sh tests/docker-compose.yml || { echo 'fail'; exit 1; } + +.PHONY: test-negative +test-negative: + @echo "Check bad file. Should error." + ./compose-check.sh tests/docker-compose.bad.yml && { echo 'fail'; exit 1; } || echo 'ok' + @echo "Check multiple files. Should error." + ./compose-check.sh tests/docker-compose* && { echo 'fail'; exit 1; } || echo 'ok' diff --git a/compose-check.sh b/compose-check.sh index 5cb2f05..b038955 100755 --- a/compose-check.sh +++ b/compose-check.sh @@ -6,15 +6,16 @@ check_file() { local file=$1 docker-compose -f "$file" config -q 2>&1 \ | sed "/variable is not set. Defaulting/d" - return ${PIPESTATUS[0]} + return "${PIPESTATUS[0]}" } check_files() { - local all_files=$@ + local all_files=( "$@" ) has_error=0 - for file in $all_files ; do + for file in "${all_files[@]}" ; do if [[ -f "$file" ]]; then if ! check_file "$file" ; then + echo "ERROR: $file" has_error=1 fi fi @@ -22,7 +23,7 @@ check_files() { return $has_error } -if ! check_files $@ ; then +if ! check_files "$@" ; then echo "To ignore, use --no-verify" fi diff --git a/tests/docker-compose.bad.yml b/tests/docker-compose.bad.yml new file mode 100644 index 0000000..87de008 --- /dev/null +++ b/tests/docker-compose.bad.yml @@ -0,0 +1 @@ +not: valid diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..be64343 --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,6 @@ +--- +version: '2' + +services: + main: + image: busybox