diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c604a48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +tests/dokku +tests/fixtures diff --git a/tests/bin/docker b/tests/bin/docker index 931db29..95ca40b 100755 --- a/tests/bin/docker +++ b/tests/bin/docker @@ -1,4 +1,13 @@ #!/usr/bin/env bash +# shellcheck source=../../config +# shellcheck disable=SC1091 +source "$(dirname "$0")/../../config" + +if [[ $ECHO_DOCKER_COMMAND == "true" ]]; then + echo "$(basename "$0") $*" + exit 0 +fi + case "$1" in stop) echo "testid" @@ -42,6 +51,15 @@ case "$1" in exit 0 fi + if [[ $@ =~ \{\{\.State\..*\}\} ]]; then + if [[ $@ =~ \{\{\.State\.Running\}\} ]]; then + echo "true" + else + echo "false" + fi + exit 0 + fi + # running echo "true" ;; @@ -61,6 +79,9 @@ case "$1" in pull) exit 0 ;; + logs) + echo "$PLUGIN_SERVICE $PLUGIN_IMAGE_VERSION" + ;; *) exit "$DOKKU_NOT_IMPLEMENTED_EXIT" ;; diff --git a/tests/service.bats b/tests/service.bats deleted file mode 100644 index 03c13d5..0000000 --- a/tests/service.bats +++ /dev/null @@ -1,28 +0,0 @@ -#!/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/service_alias.bats b/tests/service_alias.bats new file mode 100755 index 0000000..570f469 --- /dev/null +++ b/tests/service_alias.bats @@ -0,0 +1,32 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:alias) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:alias" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:alias) error when alias is missing" { + run dokku "$PLUGIN_COMMAND_PREFIX:alias" l + assert_contains "${lines[*]}" "Please specify an alias for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:alias) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:alias" not_existing_service MY_ALIAS + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:alias) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:alias" l MY_ALIAS + new_alias=$(cat "$PLUGIN_DATA_ROOT/l/ALIAS") + [[ $new_alias == "MY_ALIAS" ]] +} + diff --git a/tests/service_connect.bats b/tests/service_connect.bats new file mode 100755 index 0000000..c3581c9 --- /dev/null +++ b/tests/service_connect.bats @@ -0,0 +1,29 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + export ECHO_DOCKER_COMMAND="false" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + export ECHO_DOCKER_COMMAND="false" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:connect) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:connect" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:connect) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:connect" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:connect) success" { + export ECHO_DOCKER_COMMAND="true" + run dokku "$PLUGIN_COMMAND_PREFIX:connect" l + assert_output 'docker exec -it dokku.mongo.l mongo l' +} + diff --git a/tests/service_create.bats b/tests/service_create.bats new file mode 100755 index 0000000..7ecb87b --- /dev/null +++ b/tests/service_create.bats @@ -0,0 +1,12 @@ +#!/usr/bin/env bats +load test_helper + +@test "($PLUGIN_COMMAND_PREFIX:create) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:create" l + assert_contains "${lines[*]}" "container created: l" +} + +@test "($PLUGIN_COMMAND_PREFIX:create) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:create" + assert_contains "${lines[*]}" "Please specify a name for the service" +} diff --git a/tests/service_destroy.bats b/tests/service_destroy.bats new file mode 100755 index 0000000..4c1fc94 --- /dev/null +++ b/tests/service_destroy.bats @@ -0,0 +1,27 @@ +#!/usr/bin/env bats +load test_helper + +@test "($PLUGIN_COMMAND_PREFIX:destroy) success with --force" { + dokku "$PLUGIN_COMMAND_PREFIX:create" l + run dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l + assert_contains "${lines[*]}" "container deleted: l" +} + +@test "($PLUGIN_COMMAND_PREFIX:destroy) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:destroy" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:destroy) error when container does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:destroy" non_existing_container + assert_contains "${lines[*]}" "service non_existing_container does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:destroy) error when container is linked to an app" { + dokku "$PLUGIN_COMMAND_PREFIX:create" l + dokku apps:create app + dokku "$PLUGIN_COMMAND_PREFIX:link" l app + run dokku "$PLUGIN_COMMAND_PREFIX:destroy" l + assert_contains "${lines[*]}" "Cannot delete linked service" + rm "$DOKKU_ROOT/app" -rf +} diff --git a/tests/service_expose.bats b/tests/service_expose.bats new file mode 100755 index 0000000..feecb85 --- /dev/null +++ b/tests/service_expose.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) success when not providing custom ports" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" l + [[ "${lines[*]}" =~ exposed\ on\ port\(s\)\ [[:digit:]]+ ]] +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) success when providing custom ports" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" l 4242 4243 4244 4245 + assert_contains "${lines[*]}" "exposed on port(s) 4242 4243 4244 4245" +} diff --git a/tests/service_info.bats b/tests/service_info.bats new file mode 100755 index 0000000..9b7d37a --- /dev/null +++ b/tests/service_info.bats @@ -0,0 +1,25 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:info) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:info" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:info) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:info" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:info) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:info" l + assert_contains "${lines[*]}" "DSN: mongodb://172.17.0.34:27017" +} diff --git a/tests/service_link.bats b/tests/service_link.bats new file mode 100755 index 0000000..df17c0d --- /dev/null +++ b/tests/service_link.bats @@ -0,0 +1,38 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 + dokku apps:create my_app >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 + rm "$DOKKU_ROOT/my_app" -rf +} + +@test "($PLUGIN_COMMAND_PREFIX:link) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:link" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:link) error when the app argument is missing" { + run dokku "$PLUGIN_COMMAND_PREFIX:link" l + assert_contains "${lines[*]}" "Please specify an app to run the command on" +} + +@test "($PLUGIN_COMMAND_PREFIX:link) error when the app does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:link" l not_existing_app + assert_contains "${lines[*]}" "App not_existing_app does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:link) error when the service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:link" not_existing_service my_app + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:link) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app + links=$(cat "$PLUGIN_DATA_ROOT/l/LINKS") + assert_equal "$links" "my_app" +} diff --git a/tests/service_list.bats b/tests/service_list.bats new file mode 100755 index 0000000..3a3b8fb --- /dev/null +++ b/tests/service_list.bats @@ -0,0 +1,28 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:list) with no exposed ports" { + run dokku "$PLUGIN_COMMAND_PREFIX:list" + assert_contains "${lines[*]}" "l (running)" +} + +@test "($PLUGIN_COMMAND_PREFIX:list) with exposed ports" { + dokku "$PLUGIN_COMMAND_PREFIX:expose" l 4242 4243 4244 4245 + run dokku "$PLUGIN_COMMAND_PREFIX:list" + assert_contains "${lines[*]}" "l (running), exposed port(s): 27017->4242 27018->4243 27019->4244 28017->4245" +} + +@test "($PLUGIN_COMMAND_PREFIX:list) when there are no services" { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 + run dokku "$PLUGIN_COMMAND_PREFIX:list" + assert_contains "${lines[*]}" "There are no MongoDB services" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} diff --git a/tests/service_logs.bats b/tests/service_logs.bats new file mode 100755 index 0000000..07d79f2 --- /dev/null +++ b/tests/service_logs.bats @@ -0,0 +1,34 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + export ECHO_DOCKER_COMMAND="false" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + export ECHO_DOCKER_COMMAND="false" + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:logs) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:logs" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:logs) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:logs" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:logs) success when not tailing" { + export ECHO_DOCKER_COMMAND="true" + run dokku "$PLUGIN_COMMAND_PREFIX:logs" l + assert_output "docker logs --tail 100 testid" +} + +@test "($PLUGIN_COMMAND_PREFIX:logs) success when tailing" { + export ECHO_DOCKER_COMMAND="true" + run dokku "$PLUGIN_COMMAND_PREFIX:logs" l -t + assert_output "docker logs --follow testid" +} diff --git a/tests/service_restart.bats b/tests/service_restart.bats new file mode 100755 index 0000000..ab20d29 --- /dev/null +++ b/tests/service_restart.bats @@ -0,0 +1,26 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:restart) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:restart" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:restart) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:restart" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:restart) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:restart" l + assert_success +} + diff --git a/tests/service_start.bats b/tests/service_start.bats new file mode 100755 index 0000000..69a0852 --- /dev/null +++ b/tests/service_start.bats @@ -0,0 +1,26 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:start) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:start" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:start) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:start" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:start) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:start" l + assert_success +} + diff --git a/tests/service_stop.bats b/tests/service_stop.bats new file mode 100755 index 0000000..4b2f71b --- /dev/null +++ b/tests/service_stop.bats @@ -0,0 +1,26 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:stop) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:stop" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:stop) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:stop" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:stop) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:stop" l + assert_success +} + diff --git a/tests/service_unexpose.bats b/tests/service_unexpose.bats new file mode 100755 index 0000000..02f0b8f --- /dev/null +++ b/tests/service_unexpose.bats @@ -0,0 +1,28 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 +} + +@test "($PLUGIN_COMMAND_PREFIX:unexpose) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:unexpose" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:unexpose) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:unexpose" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:unexpose) success" { + dokku "$PLUGIN_COMMAND_PREFIX:expose" l + run dokku "$PLUGIN_COMMAND_PREFIX:unexpose" l + [[ ! -f $PLUGIN_DATA_ROOT/PORT ]] + assert_contains "${lines[*]}" "Service l unexposed" +} + diff --git a/tests/service_unlink.bats b/tests/service_unlink.bats new file mode 100755 index 0000000..98233ad --- /dev/null +++ b/tests/service_unlink.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku apps:create my_app >&2 + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} + +teardown() { + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 + rm "$DOKKU_ROOT/my_app" -rf +} + +@test "($PLUGIN_COMMAND_PREFIX:unlink) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:unlink" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:unlink) error when the app argument is missing" { + run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l + assert_contains "${lines[*]}" "Please specify an app to run the command on" +} + +@test "($PLUGIN_COMMAND_PREFIX:unlink) error when the app does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l not_existing_app + assert_contains "${lines[*]}" "App not_existing_app does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:unlink) error when the service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:unlink" not_existing_service my_app + assert_contains "${lines[*]}" "service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:unlink) success" { + dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app >&2 + run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app + links=$(cat "$PLUGIN_DATA_ROOT/l/LINKS") + assert_equal "$links" "" +} diff --git a/tests/test_helper.bash b/tests/test_helper.bash index db0add8..a6de657 100644 --- a/tests/test_helper.bash +++ b/tests/test_helper.bash @@ -7,7 +7,8 @@ export PLUGIN_COMMAND_PREFIX="mongo" export PLUGIN_PATH="$DOKKU_ROOT/plugins" export PLUGIN_DATA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/fixtures" -mkdir -p $PLUGIN_DATA_ROOT +mkdir -p "$PLUGIN_DATA_ROOT" +rm -rf "${PLUGIN_DATA_ROOT:?}"/* flunk() { { if [ "$#" -eq 0 ]; then cat -