Merge pull request #58 from hasnat/add_tests

Try tests via docker-compose
This commit is contained in:
will Farrell
2021-02-25 21:43:38 -07:00
committed by GitHub
7 changed files with 113 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
.github
.gitignore
.dockerignore
Dockerfile
tests
+13
View File
@@ -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
```
+11
View File
@@ -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
+42
View File
@@ -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
Executable
+16
View File
@@ -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
+9
View File
@@ -0,0 +1,9 @@
FROM alpine:latest
RUN apk --update add bash docker
WORKDIR /app
COPY . .
ENTRYPOINT ["/app/entrypoint.sh"]
+17
View File
@@ -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