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/commands b/commands index ac168b5..03027c2 100755 --- a/commands +++ b/commands @@ -36,7 +36,7 @@ case "$1" in echo "$ID" > "$SERVICE_ROOT/ID" dokku_log_verbose_quiet "Waiting for container to be ready" - sleep 10 + docker run --rm --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" aanand/wait > /dev/null dokku_log_info2 "$PLUGIN_SERVICE container created: $SERVICE" dokku "$PLUGIN_COMMAND_PREFIX:info" "$SERVICE" diff --git a/install b/install index a32d3b8..51c0387 100755 --- a/install +++ b/install @@ -7,6 +7,7 @@ if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q "$PLUGIN_IMAGE_VERSION" fi docker pull svendowideit/ambassador:latest +docker pull aanand/wait:latest mkdir -p "$PLUGIN_DATA_ROOT" || echo "Failed to create $PLUGIN_SERVICE directory" chown dokku:dokku "$PLUGIN_DATA_ROOT" diff --git a/tests/bin/docker b/tests/bin/docker index 87eeee5..03995b4 100755 --- a/tests/bin/docker +++ b/tests/bin/docker @@ -1,4 +1,9 @@ #!/usr/bin/env bash +if [[ $ECHO_DOCKER_COMMAND == "true" ]]; then + echo "$(basename "$0") $*" + exit 0 +fi + case "$1" in stop) echo "testid" @@ -42,6 +47,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 +75,9 @@ case "$1" in pull) exit 0 ;; + logs) + echo "1:M 08 Sep 07:36:43.544 # Server started, Redis version 3.0.3" + ;; *) exit "$DOKKU_NOT_IMPLEMENTED_EXIT" ;; diff --git a/tests/bin/redis-cli b/tests/bin/redis-cli deleted file mode 100644 index 742e13d..0000000 --- a/tests/bin/redis-cli +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash -exit 0 diff --git a/tests/redis_alias.bats b/tests/redis_alias.bats new file mode 100755 index 0000000..6d556af --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_connect.bats b/tests/redis_connect.bats new file mode 100755 index 0000000..1a6b68c --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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 run -it --link dokku.redis.l:redis --rm redis sh -c exec redis-cli -h "$REDIS_PORT_6379_TCP_ADDR" -p "$REDIS_PORT_6379_TCP_PORT"' +} + diff --git a/tests/redis_create.bats b/tests/redis_create.bats new file mode 100755 index 0000000..7ecb87b --- /dev/null +++ b/tests/redis_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/redis_destroy.bats b/tests/redis_destroy.bats new file mode 100755 index 0000000..a506820 --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_expose.bats b/tests/redis_expose.bats new file mode 100755 index 0000000..31ab350 --- /dev/null +++ b/tests/redis_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[*]}" "Redis service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) success when not providing a custom port" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" l + [[ "${lines[*]}" =~ exposed\ on\ port\(s\)\ [[:digit:]]+ ]] +} + +@test "($PLUGIN_COMMAND_PREFIX:expose) success when providing a custom port" { + run dokku "$PLUGIN_COMMAND_PREFIX:expose" l 4242 + assert_contains "${lines[*]}" "exposed on port(s) 4242" +} diff --git a/tests/redis_info.bats b/tests/redis_info.bats new file mode 100755 index 0000000..1fe00b7 --- /dev/null +++ b/tests/redis_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[*]}" "Redis service not_existing_service does not exist" +} + +@test "($PLUGIN_COMMAND_PREFIX:info) success" { + run dokku "$PLUGIN_COMMAND_PREFIX:info" l + assert_contains "${lines[*]}" "DSN: redis://172.17.0.34:6379/0" +} diff --git a/tests/redis_link.bats b/tests/redis_link.bats new file mode 100755 index 0000000..047945f --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_list.bats b/tests/redis_list.bats new file mode 100755 index 0000000..eff2c68 --- /dev/null +++ b/tests/redis_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 + run dokku "$PLUGIN_COMMAND_PREFIX:list" + assert_contains "${lines[*]}" "l (running), exposed port(s): 6379->4242" +} + +@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 Redis services" + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 +} diff --git a/tests/redis_logs.bats b/tests/redis_logs.bats new file mode 100755 index 0000000..33754e0 --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_restart.bats b/tests/redis_restart.bats new file mode 100755 index 0000000..492da85 --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_start.bats b/tests/redis_start.bats new file mode 100755 index 0000000..cee7b3a --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_stop.bats b/tests/redis_stop.bats new file mode 100755 index 0000000..3d3f39f --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_unexpose.bats b/tests/redis_unexpose.bats new file mode 100755 index 0000000..befc303 --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/redis_unlink.bats b/tests/redis_unlink.bats new file mode 100755 index 0000000..18ef349 --- /dev/null +++ b/tests/redis_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[*]}" "Redis 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/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/test_helper.bash b/tests/test_helper.bash index 43ce430..65164cc 100644 --- a/tests/test_helper.bash +++ b/tests/test_helper.bash @@ -7,7 +7,8 @@ export PLUGIN_COMMAND_PREFIX="redis" 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 -