From e6af6efd8a8804f0883c10ac05067235835d4e2f Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sat, 12 Feb 2022 20:28:38 +0100 Subject: [PATCH] Add test setup for notification feature (#61) * Add test case for notification feature * Fix template data * bash is needed to interpret test * Do not use bashisms in test * Only print FullPath * Fix assertion --- test/notifications/.gitignore | 1 + test/notifications/docker-compose.yml | 36 +++++++++++++++++ test/notifications/notifications.tmpl | 7 ++++ test/notifications/run.sh | 58 +++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 test/notifications/.gitignore create mode 100644 test/notifications/docker-compose.yml create mode 100644 test/notifications/notifications.tmpl create mode 100755 test/notifications/run.sh diff --git a/test/notifications/.gitignore b/test/notifications/.gitignore new file mode 100644 index 0000000..4083037 --- /dev/null +++ b/test/notifications/.gitignore @@ -0,0 +1 @@ +local diff --git a/test/notifications/docker-compose.yml b/test/notifications/docker-compose.yml new file mode 100644 index 0000000..0423c72 --- /dev/null +++ b/test/notifications/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3' + +services: + backup: &default_backup_service + image: offen/docker-volume-backup:${TEST_VERSION} + restart: always + environment: + BACKUP_FILENAME: test.tar.gz + BACKUP_CRON_EXPRESSION: 0 0 5 31 2 ? + BACKUP_PRUNING_PREFIX: test + NOTIFICATION_LEVEL: info + NOTIFICATION_URLS: gotify://gotify/${GOTIFY_TOKEN}?disableTLS=true + volumes: + - ./local:/archive + - app_data:/backup/app_data:ro + - ./notifications.tmpl:/etc/dockervolumebackup/notifications.d/notifications.tmpl + + offen: + image: offen/offen:latest + labels: + - docker-volume-backup.stop-during-backup=true + volumes: + - app_data:/var/opt/offen + + gotify: + image: gotify/server + ports: + - 8080:80 + environment: + - GOTIFY_DEFAULTUSER_PASS=custom + volumes: + - gotify_data:/app/data + +volumes: + app_data: + gotify_data: diff --git a/test/notifications/notifications.tmpl b/test/notifications/notifications.tmpl new file mode 100644 index 0000000..1771da1 --- /dev/null +++ b/test/notifications/notifications.tmpl @@ -0,0 +1,7 @@ +{{ define "title_success" -}} +Successful test run, yay! +{{- end }} + +{{ define "body_success" -}} +Backing up {{ .Stats.BackupFile.FullPath }} succeeded. +{{- end }} diff --git a/test/notifications/run.sh b/test/notifications/run.sh new file mode 100755 index 0000000..35ba183 --- /dev/null +++ b/test/notifications/run.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +set -e + +cd $(dirname $0) + +mkdir -p local + +docker-compose up -d +sleep 5 + +GOTIFY_TOKEN=$(curl -sSLX POST -H 'Content-Type: application/json' -d '{"name":"test"}' http://admin:custom@localhost:8080/application | jq -r '.token') + +docker-compose down + +GOTIFY_TOKEN=$GOTIFY_TOKEN docker-compose up -d + +echo "[TEST:INFO] Set up Gotify application using token $GOTIFY_TOKEN" + +docker-compose exec backup backup + +tar -xf ./local/test.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db +echo "[TEST:PASS] Found relevant files in untared local backup." + +if [ "$(docker-compose ps -q | wc -l)" != "3" ]; then + echo "[TEST:FAIL] Expected all containers to be running post backup, instead seen:" + docker-compose ps + exit 1 +fi + +echo "[TEST:PASS] All containers running post backup." + +MESSAGE=$(curl -sSL http://admin:custom@localhost:8080/message | jq -r '.messages[0]') + + +case "$MESSAGE" in + *"Successful test run, yay!"*) + echo "[TEST:PASS] Custom notification title was used" + ;; + *) + echo "[TEST:FAIL] Expected custom title to be used in notification, instead seen:" + echo $MESSAGE + exit 1 + ;; +esac + +case "$MESSAGE" in + *"Backing up /tmp/test.tar.gz succeeded."*) + echo "[TEST:PASS] Custom notification body was used" + ;; + *) + echo "[TEST:FAIL] Expected custom body to be used in notification, instead seen:" + echo $MESSAGE + exit 1 + ;; +esac + +docker-compose down --volumes