Add tests for every implemented command

This commit is contained in:
Loïc Guitaut
2015-09-10 11:57:24 +02:00
parent 860e5c7f9b
commit ca88f2bdce
21 changed files with 456 additions and 33 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
tests/dokku
tests/fixtures

View File

@@ -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"
;;

View File

@@ -1,2 +0,0 @@
#!/usr/bin/env bash
exit 0

View File

@@ -1,2 +0,0 @@
#!/usr/bin/env bash
exit 0

View File

@@ -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
}

32
tests/service_alias.bats Executable file
View File

@@ -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" ]]
}

29
tests/service_connect.bats Executable file
View File

@@ -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.postgres.l psql -h localhost -U postgres l'
}

12
tests/service_create.bats Executable file
View File

@@ -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"
}

27
tests/service_destroy.bats Executable file
View File

@@ -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
}

30
tests/service_export.bats Executable file
View File

@@ -0,0 +1,30 @@
#!/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:export) error when there are no arguments" {
run dokku "$PLUGIN_COMMAND_PREFIX:export"
assert_contains "${lines[*]}" "Please specify a name for the service"
}
@test "($PLUGIN_COMMAND_PREFIX:export) error when service does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:export" not_existing_service
assert_contains "${lines[*]}" "service not_existing_service does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:export) success" {
export ECHO_DOCKER_COMMAND="true"
run dokku "$PLUGIN_COMMAND_PREFIX:export" l
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_output "docker exec dokku.postgres.l env PGPASSWORD=$password pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w l"
}

30
tests/service_expose.bats Executable file
View File

@@ -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 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"
}

26
tests/service_info.bats Executable file
View File

@@ -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: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
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "${lines[*]}" "DSN: postgres://postgres:$password@172.17.0.34:5432/l"
}

38
tests/service_link.bats Executable file
View File

@@ -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"
}

28
tests/service_list.bats Executable file
View File

@@ -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): 5432->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 Postgres services"
dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2
}

34
tests/service_logs.bats Executable file
View File

@@ -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"
}

26
tests/service_restart.bats Executable file
View File

@@ -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
}

26
tests/service_start.bats Executable file
View File

@@ -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
}

26
tests/service_stop.bats Executable file
View File

@@ -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
}

28
tests/service_unexpose.bats Executable file
View File

@@ -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"
}

39
tests/service_unlink.bats Executable file
View File

@@ -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" ""
}

View File

@@ -7,7 +7,8 @@ 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
mkdir -p "$PLUGIN_DATA_ROOT"
rm -rf "${PLUGIN_DATA_ROOT:?}"/*
flunk() {
{ if [ "$#" -eq 0 ]; then cat -