From a91353742d31693697eab701c018ad4c251b8ada Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sun, 11 Jul 2021 09:43:16 +0200 Subject: [PATCH] add test for interacting with a swarm stack --- .circleci/config.yml | 9 ++++- test/default/{test.sh => run.sh} | 8 ++-- test/swarm/docker-compose.yml | 63 ++++++++++++++++++++++++++++++++ test/swarm/run.sh | 36 ++++++++++++++++++ 4 files changed, 112 insertions(+), 4 deletions(-) rename test/default/{test.sh => run.sh} (64%) create mode 100644 test/swarm/docker-compose.yml create mode 100755 test/swarm/run.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 01129a5..8e854fe 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,8 +14,15 @@ jobs: - run: name: Run tests command: | - for test in test/**/test.sh; do + for test in test/**/run.sh; do + echo "################################################" + echo "Now running $test" + echo "################################################" + echo "" /bin/sh $test + echo "" + echo "$test passed" + echo "" done build: diff --git a/test/default/test.sh b/test/default/run.sh similarity index 64% rename from test/default/test.sh rename to test/default/run.sh index 550cfec..cabb40f 100755 --- a/test/default/test.sh +++ b/test/default/run.sh @@ -13,12 +13,14 @@ docker run --rm -it \ -v default_backup_data:/data alpine \ ash -c 'tar -xf /data/backup/test.tar.gz && test -f /backup/app_data/offen.db' +echo "[TEST:PASS] Found relevant files in untared backup." + if [ "$(docker-compose ps -q | wc -l)" != "3" ]; then - echo "Expected all containers to be running post backup, instead seen:" + echo "[TEST:FAIL] Expected all containers to be running post backup, instead seen:" docker-compose ps exit 1 fi -docker-compose down --volumes +echo "[TEST:PASS] All containers running post backup." -echo "Test passed" +docker-compose down --volumes diff --git a/test/swarm/docker-compose.yml b/test/swarm/docker-compose.yml new file mode 100644 index 0000000..a6f2aa7 --- /dev/null +++ b/test/swarm/docker-compose.yml @@ -0,0 +1,63 @@ +# Copyright 2020-2021 - Offen Authors +# SPDX-License-Identifier: Unlicense + +version: '3.8' + +services: + minio: + image: minio/minio:RELEASE.2020-08-04T23-10-51Z + deploy: + restart_policy: + condition: on-failure + environment: + MINIO_ROOT_USER: test + MINIO_ROOT_PASSWORD: test + MINIO_ACCESS_KEY: test + MINIO_SECRET_KEY: GMusLtUmILge2by+z890kQ + entrypoint: /bin/ash -c 'mkdir -p /data/backup && minio server /data' + volumes: + - backup_data:/data + + backup: &default_backup_service + image: offen/docker-volume-backup:canary + depends_on: + - minio + deploy: + restart_policy: + condition: on-failure + environment: + AWS_ACCESS_KEY_ID: test + AWS_SECRET_ACCESS_KEY: GMusLtUmILge2by+z890kQ + AWS_ENDPOINT: minio:9000 + AWS_ENDPOINT_PROTO: http + AWS_S3_BUCKET_NAME: backup + BACKUP_FILENAME: test.tar.gz + BACKUP_CRON_EXPRESSION: 0 0 5 31 2 ? + volumes: + - pg_data:/backup/pg_data:ro + - /var/run/docker.sock:/var/run/docker.sock + + offen: + image: offen/offen:latest + labels: + - docker-volume-backup.stop-during-backup=true + deploy: + replicas: 2 + restart_policy: + condition: on-failure + + pg: + image: postgres:12.2-alpine + environment: + POSTGRES_PASSWORD: example + labels: + - docker-volume-backup.stop-during-backup=true + volumes: + - pg_data:/var/lib/postgresql/data + deploy: + restart_policy: + condition: on-failure + +volumes: + backup_data: + pg_data: diff --git a/test/swarm/run.sh b/test/swarm/run.sh new file mode 100755 index 0000000..54ab16d --- /dev/null +++ b/test/swarm/run.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +set -e + +cd $(dirname $0) + +docker swarm init + +docker stack deploy --compose-file=docker-compose.yml test_stack + +while [ -z $(docker ps -q -f name=backup) ]; do + echo "[TEST:INFO] Backup container not ready yet. Retrying." + sleep 1 +done + +sleep 5 + +docker exec $(docker ps -q -f name=backup) backup + +docker run --rm -it \ + -v test_stack_backup_data:/data alpine \ + ash -c 'tar -xf /data/backup/test.tar.gz && test -f /backup/pg_data/pg_stat/global.stat' + +echo "[TEST:PASS] Found relevant files in untared backup." + +if [ "$(docker ps -q | wc -l)" != "5" ]; then + echo "[TEST:FAIL] Expected all containers to be running post backup, instead seen:" + docker ps -a + exit 1 +fi + +echo "[TEST:PASS] All containers running post backup." + +docker stack rm test_stack + +docker swarm leave --force