From b3156c555309a92760118b610c021184e55ebc5e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 7 Sep 2015 00:39:28 -0400 Subject: [PATCH] Travis testing --- .travis.yml | 6 +++++ Makefile | 23 +++++++++++----- tests/bin/docker | 49 +++++++++++++++++++++++++++++++++ tests/bin/id | 2 ++ tests/bin/redis-cli | 2 ++ tests/service.bats | 28 +++++++++++++++++++ tests/setup.sh | 15 +++++++++++ tests/test_helper.bash | 58 +++++++++++++++++++++++++++++++++++++++ tests/unit/service.bats | 60 ----------------------------------------- 9 files changed, 177 insertions(+), 66 deletions(-) create mode 100644 .travis.yml create mode 100755 tests/bin/docker create mode 100755 tests/bin/id create mode 100644 tests/bin/redis-cli create mode 100644 tests/service.bats create mode 100644 tests/setup.sh create mode 100644 tests/test_helper.bash delete mode 100644 tests/unit/service.bats diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1ee4a8e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +sudo: required +language: bash +env: + - DOKKU_VERSION=master +before_install: make setup +script: make test diff --git a/Makefile b/Makefile index 5341517..93a7332 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,24 @@ shellcheck: ifeq ($(shell shellcheck > /dev/null 2>&1 ; echo $$?),127) ifeq ($(shell uname),Darwin) - brew install shellcheck + brew install shellcheck else - sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse' - sudo apt-get update && sudo apt-get install -y shellcheck + sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse' + sudo apt-get update -qq && sudo apt-get install -qq -y shellcheck endif endif bats: +ifeq ($(shell bats > /dev/null 2>&1 ; echo $$?),127) +ifeq ($(shell uname),Darwin) git clone https://github.com/sstephenson/bats.git /tmp/bats cd /tmp/bats && sudo ./install.sh /usr/local rm -rf /tmp/bats +else + sudo add-apt-repository ppa:duggan/bats --yes + sudo apt-get update -qq && sudo apt-get install -qq -y bats +endif +endif ci-dependencies: shellcheck bats @@ -21,10 +28,14 @@ lint: # SC2068: Double quote array expansions, otherwise they're like $* and break on spaces. - https://github.com/koalaman/shellcheck/wiki/SC2068 # SC2086: Double quote to prevent globbing and word splitting - https://github.com/koalaman/shellcheck/wiki/SC2086 @echo linting... - @$(QUIET) find . -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | xargs shellcheck -e SC2046,SC2068,SC2086 + @$(QUIET) find ./ -maxdepth 1 -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | xargs shellcheck -e SC2046,SC2068,SC2086 unit-tests: @echo running unit tests... - @$(QUIET) bats tests/unit + @$(QUIET) bats tests -test: ci-dependencies lint unit-tests +setup: + bash tests/setup.sh + $(MAKE) ci-dependencies + +test: setup lint unit-tests diff --git a/tests/bin/docker b/tests/bin/docker new file mode 100755 index 0000000..0c7a378 --- /dev/null +++ b/tests/bin/docker @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +case "$1" in + stop) + echo "testid" + ;; + start) + echo "testid" + ;; + kill) + echo "testid" + ;; + run) + echo "testid" + ;; + rm) + echo "testid" + ;; + ps) + if [[ $@ = *"no-trunc"* ]]; then + echo "1479bbd60ade8a92617d2aeb4935bd3ff3179bd0fd71c22c3102c421f4bc221f" + exit 0 + else + echo "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" + echo "testid redis:3.0.3 \"/entrypoint.sh redi 5 minutes ago Up 5 minutes 6379/tcp dokku.redis.l" + fi + ;; + exec) + echo "exec called with $@" + ;; + inspect) + if [[ $@ = *"IPAddress"* ]]; then + echo "172.17.0.34" + exit 0 + fi + + # running + echo "true" + ;; + images) + echo "REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE" + echo "redis 3.0.3 9216d5a4eec8 13 days ago 109.3 MB" + ;; + pull) + exit 0 + ;; + *) + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" + ;; +esac diff --git a/tests/bin/id b/tests/bin/id new file mode 100755 index 0000000..e1382cb --- /dev/null +++ b/tests/bin/id @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +echo "dokku" diff --git a/tests/bin/redis-cli b/tests/bin/redis-cli new file mode 100644 index 0000000..742e13d --- /dev/null +++ b/tests/bin/redis-cli @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +exit 0 diff --git a/tests/service.bats b/tests/service.bats new file mode 100644 index 0000000..03c13d5 --- /dev/null +++ b/tests/service.bats @@ -0,0 +1,28 @@ +#!/usr/bin/env bats +load test_helper + +@test "(service) dokku" { + dokku $PLUGIN_COMMAND_PREFIX:create l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:info l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:stop l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:stop l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:expose l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:restart l + assert_success + + dokku $PLUGIN_COMMAND_PREFIX:info l + assert_success + + dokku --force $PLUGIN_COMMAND_PREFIX:destroy l + assert_success +} diff --git a/tests/setup.sh b/tests/setup.sh new file mode 100644 index 0000000..a29514f --- /dev/null +++ b/tests/setup.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test_helper.bash" + +if [[ ! -d $DOKKU_ROOT ]]; then + git clone https://github.com/progrium/dokku.git $DOKKU_ROOT > /dev/null +fi + +cd $DOKKU_ROOT +echo "Dokku version $DOKKU_VERSION" +git checkout $DOKKU_VERSION > /dev/null +cd - + +rm -rf $DOKKU_ROOT/plugins/service +mkdir -p $DOKKU_ROOT/plugins/service +find ./ -maxdepth 1 -type f -exec cp '{}' $DOKKU_ROOT/plugins/service \; diff --git a/tests/test_helper.bash b/tests/test_helper.bash new file mode 100644 index 0000000..2107363 --- /dev/null +++ b/tests/test_helper.bash @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +export DOKKU_QUIET_OUTPUT=1 +export DOKKU_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/dokku" +export DOKKU_VERSION=${DOKKU_VERSION:-"master"} +export PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/bin:$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/dokku:$PATH" +export PLUGIN_COMMAND_PREFIX="postgres" +export PLUGIN_PATH="$DOKKU_ROOT/plugins" +export PLUGIN_DATA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/fixtures" + +mkdir -p $PLUGIN_DATA_ROOT + +flunk() { + { if [ "$#" -eq 0 ]; then cat - + else echo "$*" + fi + } + return 1 +} + +assert_equal() { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_exit_status() { + assert_equal "$status" "$1" +} + +assert_success() { + if [ "$status" -ne 0 ]; then + flunk "command failed with exit status $status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_exists() { + if [ ! -f "$1" ]; then + flunk "expected file to exist: $1" + fi +} + +assert_contains() { + if [[ "$1" != *"$2"* ]]; then + flunk "expected $2 to be in: $1" + fi +} + +assert_output() { + local expected + if [ $# -eq 0 ]; then expected="$(cat -)" + else expected="$1" + fi + assert_equal "$expected" "$output" +} diff --git a/tests/unit/service.bats b/tests/unit/service.bats deleted file mode 100644 index 0e4d4d7..0000000 --- a/tests/unit/service.bats +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env bats -export SERVICE=postgres - -flunk() { - { if [ "$#" -eq 0 ]; then cat - - else echo "$*" - fi - } - return 1 -} - -assert_success() { - if [ "$status" -ne 0 ]; then - flunk "command failed with exit status $status" - elif [ "$#" -gt 0 ]; then - assert_output "$1" - fi -} - -assert_equal() { - if [ "$1" != "$2" ]; then - { echo "expected: $1" - echo "actual: $2" - } | flunk - fi -} - -assert_output() { - local expected - if [ $# -eq 0 ]; then expected="$(cat -)" - else expected="$1" - fi - assert_equal "$expected" "$output" -} - -@test "(service) dokku" { - dokku $SERVICE:create l - assert_success - - dokku $SERVICE:info l - assert_success - - dokku $SERVICE:stop l - assert_success - - dokku $SERVICE:stop l - assert_success - - dokku $SERVICE:expose l - assert_success - - dokku $SERVICE:restart l - assert_success - - dokku $SERVICE:info l - assert_success - - dokku --force $SERVICE:destroy l - assert_success -}