diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..efcc8aa --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.github +.gitignore +.dockerignore +Dockerfile +tests \ No newline at end of file diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..4fafbf8 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,13 @@ +# Docker Autoheal Tests + +Docker Compose is used to build and deploy test environment. + +test.sh waits on watch-autoheal exit code. + +Currently setup to a very basic exit 1 on invalid restart and exit 0 on valid restart. + +## Run tests +``` +cd tests +./tests.sh +``` diff --git a/tests/docker-compose.autoheal.yml b/tests/docker-compose.autoheal.yml new file mode 100644 index 0000000..1f55a83 --- /dev/null +++ b/tests/docker-compose.autoheal.yml @@ -0,0 +1,11 @@ +version: '3.7' + +services: + + watch-autoheal: + container_name: watch-autoheal + build: watch-autoheal + restart: "no" + volumes: + - "/var/run/docker.sock:/var/run/docker.sock" + network_mode: none diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..f35b431 --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,42 @@ +version: '3.7' + +services: + + should-keep-restarting: + image: alpine + container_name: should-keep-restarting + network_mode: none + restart: "no" + healthcheck: + test: exit 1 + interval: 3s + timeout: 1s + retries: 3 + start_period: 5s + command: tail -f /dev/null + + shouldnt-restart: + image: alpine + container_name: shouldnt-restart + network_mode: none + restart: "no" + healthcheck: + test: exit 0 + interval: 2s + timeout: 1s + retries: 1 + start_period: 1s + command: tail -f /dev/null + + + autoheal: + container_name: autoheal + build: + context: ../ + restart: unless-stopped + environment: + AUTOHEAL_CONTAINER_LABEL: "all" + AUTOHEAL_INTERVAL: "10" + volumes: + - "/var/run/docker.sock:/var/run/docker.sock" + network_mode: none diff --git a/tests/tests.sh b/tests/tests.sh new file mode 100755 index 0000000..83af29b --- /dev/null +++ b/tests/tests.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euxo pipefail + +COMPOSE_PROJECT_NAME=autoheal-test + +function cleanup() +{ + exit_status=$? + echo "exit was $exit_status" + docker-compose -f docker-compose.autoheal.yml -f docker-compose.yml rm -f || true + exit "$exit_status" +} +trap cleanup EXIT +docker-compose up --build -d +docker-compose -f docker-compose.autoheal.yml up --build --exit-code-from watch-autoheal watch-autoheal + diff --git a/tests/watch-autoheal/Dockerfile b/tests/watch-autoheal/Dockerfile new file mode 100644 index 0000000..9f28f04 --- /dev/null +++ b/tests/watch-autoheal/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:latest + +RUN apk --update add bash docker + + +WORKDIR /app +COPY . . + +ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/tests/watch-autoheal/entrypoint.sh b/tests/watch-autoheal/entrypoint.sh new file mode 100755 index 0000000..2f77c6b --- /dev/null +++ b/tests/watch-autoheal/entrypoint.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +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 + +} + +export -f listenToDockerEvents +timeout 60s bash -c listenToDockerEvents