diff --git a/tests/.env b/tests/.env new file mode 100644 index 0000000..74a35dc --- /dev/null +++ b/tests/.env @@ -0,0 +1 @@ +AUTOHEAL_CONTAINER_LABEL=autoheal-test \ No newline at end of file diff --git a/tests/README.md b/tests/README.md index 4fafbf8..92932ee 100644 --- a/tests/README.md +++ b/tests/README.md @@ -11,3 +11,14 @@ Currently setup to a very basic exit 1 on invalid restart and exit 0 on valid re cd tests ./tests.sh ``` + +## Run tests in CI +``` +cd tests +export "AUTOHEAL_CONTAINER_LABEL=autoheal-123456" +./tests.sh "MY_UNIQUE_BUILD_NUMBER_123456" +``` + +This enables the tests to only restart containers within the test spec by using +unique docker-compose project names and autoheal labels (as long as you replace +123456 by a unique number) diff --git a/tests/docker-compose.autoheal.yml b/tests/docker-compose.autoheal.yml index 1f55a83..d5c72b9 100644 --- a/tests/docker-compose.autoheal.yml +++ b/tests/docker-compose.autoheal.yml @@ -3,9 +3,10 @@ version: '3.7' services: watch-autoheal: - container_name: watch-autoheal build: watch-autoheal restart: "no" volumes: - "/var/run/docker.sock:/var/run/docker.sock" + environment: + COMPOSE_PROJECT_NAME: $COMPOSE_PROJECT_NAME network_mode: none diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index f35b431..ca0a152 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -3,10 +3,12 @@ version: '3.7' services: should-keep-restarting: + # this container should be restarted by autoheal because its unhealthy and has the autoheal label image: alpine - container_name: should-keep-restarting network_mode: none restart: "no" + labels: + - "$AUTOHEAL_CONTAINER_LABEL=true" healthcheck: test: exit 1 interval: 3s @@ -15,11 +17,13 @@ services: start_period: 5s command: tail -f /dev/null - shouldnt-restart: + shouldnt-restart-healthy: + # this container shouldn't be restarted by autoheal because its healthy image: alpine - container_name: shouldnt-restart network_mode: none restart: "no" + labels: + - "$AUTOHEAL_CONTAINER_LABEL=true" healthcheck: test: exit 0 interval: 2s @@ -28,14 +32,25 @@ services: start_period: 1s command: tail -f /dev/null + shouldnt-restart-no-label: + # this container shouldn't be restarted by autoheal because its missing the autoheal label + image: alpine + network_mode: none + restart: "no" + healthcheck: + test: exit 1 + interval: 3s + timeout: 1s + retries: 1 + start_period: 5s + command: tail -f /dev/null autoheal: - container_name: autoheal build: context: ../ restart: unless-stopped environment: - AUTOHEAL_CONTAINER_LABEL: "all" + AUTOHEAL_CONTAINER_LABEL: "${AUTOHEAL_CONTAINER_LABEL:-all}" AUTOHEAL_INTERVAL: "10" volumes: - "/var/run/docker.sock:/var/run/docker.sock" diff --git a/tests/tests.sh b/tests/tests.sh index 83af29b..703e3a5 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -1,13 +1,16 @@ #!/usr/bin/env bash set -euxo pipefail -COMPOSE_PROJECT_NAME=autoheal-test +COMPOSE_PROJECT_NAME=${1:-autoheal-test} +export COMPOSE_PROJECT_NAME function cleanup() { exit_status=$? echo "exit was $exit_status" - docker-compose -f docker-compose.autoheal.yml -f docker-compose.yml rm -f || true + # stop autoheal first, to stop it restarting the test containers while we try to stop them + docker-compose stop autoheal + docker-compose -f docker-compose.autoheal.yml -f docker-compose.yml down || true exit "$exit_status" } trap cleanup EXIT diff --git a/tests/watch-autoheal/entrypoint.sh b/tests/watch-autoheal/entrypoint.sh index 2f77c6b..e411ff1 100755 --- a/tests/watch-autoheal/entrypoint.sh +++ b/tests/watch-autoheal/entrypoint.sh @@ -3,14 +3,17 @@ set -euxo pipefail listenToDockerEvents() { -docker events --filter 'container=should-keep-restarting' --filter 'container=shouldnt-restart' --filter 'event=restart' | while read LOGLINE -do - echo "$LOGLINE" - # may be more elaborate checks here. - [[ "${LOGLINE}" == *"container restart "*"name=shouldnt-restart"* ]] && echo "ERR: No restarts expected on shouldnt-restart container!" && pkill -9 docker && exit 1 - [[ "${LOGLINE}" == *"container restart "*"name=should-keep-restarting"* ]] && echo "OK: Expected restart on should-keep-restarting container!" && pkill -9 docker && exit 0 -done - + local expected_restarts + local LOGLINE + expected_restarts=0 + docker events --filter 'com.docker.compose.service=should-keep-restarting' --filter 'com.docker.compose.service=shouldnt-restart-*' --filter 'event=restart' | while read -r LOGLINE + do + echo "$LOGLINE" + # may be more elaborate checks here. + [[ $LOGLINE == *"container restart "*"com.docker.compose.service=shouldnt-restart-"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "ERR: No restarts expected on shouldnt-restart-* containers!" 1>&2 && pkill -9 docker && exit 1 + [[ $LOGLINE == *"container restart "*"com.docker.compose.service=should-keep-restarting"* && $LOGLINE == *"com.docker.compose.project=$COMPOSE_PROJECT_NAME"* ]] && echo "OK: Expected restart on should-keep-restarting container!" && pkill -9 docker && expected_restarts=$((expected_restarts + 1)) + [[ $expected_restarts == 1 ]] && echo "OK: All expected restarts happened" && exit 0 + done } export -f listenToDockerEvents