From aad79a458a6fa2c719bca4c880f9342a07ba0204 Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Mon, 28 Jan 2019 09:53:41 -0800 Subject: [PATCH] Refactor pre/post script tests --- tests/scripts/post-backup.sh | 4 -- tests/scripts/post-restore.sh | 3 -- tests/scripts/pre-backup.sh | 7 ---- tests/test-pre-scripts.sh | 41 ++++++++++--------- .../backup/after/post-backup.sh | 6 +++ .../backup/before/pre-backup.sh | 6 +++ tests/test-pre-scripts/create-test-data.sql | 7 ++++ .../restore/after/post-restore.sh | 6 +++ .../restore/before}/pre-restore.sh | 0 9 files changed, 47 insertions(+), 33 deletions(-) delete mode 100755 tests/scripts/post-backup.sh delete mode 100755 tests/scripts/post-restore.sh delete mode 100755 tests/scripts/pre-backup.sh create mode 100755 tests/test-pre-scripts/backup/after/post-backup.sh create mode 100755 tests/test-pre-scripts/backup/before/pre-backup.sh create mode 100644 tests/test-pre-scripts/create-test-data.sql create mode 100755 tests/test-pre-scripts/restore/after/post-restore.sh rename tests/{scripts => test-pre-scripts/restore/before}/pre-restore.sh (100%) diff --git a/tests/scripts/post-backup.sh b/tests/scripts/post-backup.sh deleted file mode 100755 index 827f073..0000000 --- a/tests/scripts/post-backup.sh +++ /dev/null @@ -1,4 +0,0 @@ -set -e - -# Clear dump -rm /data/dump.txt diff --git a/tests/scripts/post-restore.sh b/tests/scripts/post-restore.sh deleted file mode 100755 index 2c7f8c7..0000000 --- a/tests/scripts/post-restore.sh +++ /dev/null @@ -1,3 +0,0 @@ -set -e - -cat /data/dump.txt > /mydb.txt diff --git a/tests/scripts/pre-backup.sh b/tests/scripts/pre-backup.sh deleted file mode 100755 index 3b86ba7..0000000 --- a/tests/scripts/pre-backup.sh +++ /dev/null @@ -1,7 +0,0 @@ -set -e - -# Pretend we have some database -echo "insert foo into bar;" > /mydb.txt - -# Let's dump that db -cat /mydb.txt > /data/dump.txt diff --git a/tests/test-pre-scripts.sh b/tests/test-pre-scripts.sh index 91b00ec..8482406 100755 --- a/tests/test-pre-scripts.sh +++ b/tests/test-pre-scripts.sh @@ -9,10 +9,7 @@ if [ "$IN_CONTAINER" != "true" ] ; then -e IN_CONTAINER=true \ -e SKIP_ON_START=true \ -v "$(pwd)/test-pre-scripts.sh:/test.sh" \ - -v "$(pwd)/scripts/pre-backup.sh:/scripts/backup/before/1.sh" \ - -v "$(pwd)/scripts/post-backup.sh:/scripts/backup/after/1.sh" \ - -v "$(pwd)/scripts/pre-restore.sh:/scripts/restore/before/1.sh" \ - -v "$(pwd)/scripts/post-restore.sh:/scripts/restore/after/1.sh" \ + -v "$(pwd)/test-pre-scripts:/scripts" \ $image \ bash -c "/test.sh" else @@ -22,45 +19,51 @@ else type cron type crontab + echo "Install sqlite3" + apt-get update + apt-get install -y --no-install-recommends sqlite3 + echo "Create test data..." - mkdir -p /data && echo Test > /data/test.txt + mkdir -p /data + touch /data/test_database.db + sqlite3 /data/test_database.db < /scripts/create-test-data.sql echo "Making backup..." /backup.sh + echo "Verify intermediary file is gone" + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + echo "Delete test data..." rm -fr /data/* - rm -fr /mydb.txt echo "Verify deleted..." - test -f /data/test.txt && exit 1 || echo "Gone" - test -f /mydb.txt && exit 1 || echo "Gone" + test -f /data/test_database.db && exit 1 || echo "Gone" echo "Restore backup..." /restore.sh - echo "Verify pre-post script backups..." - test -f /mydb.txt - cat /mydb.txt + echo "Verify restored files exist..." + test -f /data/test_database.db + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + sqlite3 /data/test_database.db "select data from test_table where id = 1" echo "Delete test data again..." rm -fr /data/* - rm -fr /mydb.txt echo "Verify deleted..." - test -f /data/test.txt && exit 1 || echo "Gone" - test -f /mydb.txt && exit 1 || echo "Gone" + test -f /data/test_database.db && exit 1 || echo "Gone" echo "Simulate a restart with RESTORE_ON_EMPTY_START..." RESTORE_ON_EMPTY_START=true /start.sh echo "Verify restore happened..." - test -f /data/test.txt - cat /data/test.txt + test -f /data/test_database.db + test -f /data/test_database.db.bak && exit 1 || echo "Gone" + sqlite3 /data/test_database.db "select data from test_table where id = 1" - echo "Verify pre-post script backups..." - test -f /mydb.txt - cat /mydb.txt + echo "Delete test data..." + rm -fr /data/* echo "Verify restore with incorrect passphrase fails..." echo "Fail to restore backup..." diff --git a/tests/test-pre-scripts/backup/after/post-backup.sh b/tests/test-pre-scripts/backup/after/post-backup.sh new file mode 100755 index 0000000..a606858 --- /dev/null +++ b/tests/test-pre-scripts/backup/after/post-backup.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Remove backed up copy +rm test_database.db.bak diff --git a/tests/test-pre-scripts/backup/before/pre-backup.sh b/tests/test-pre-scripts/backup/before/pre-backup.sh new file mode 100755 index 0000000..b0da835 --- /dev/null +++ b/tests/test-pre-scripts/backup/before/pre-backup.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Dump the SQLite database +sqlite3 test_database.db ".backup test_database.db.bak" diff --git a/tests/test-pre-scripts/create-test-data.sql b/tests/test-pre-scripts/create-test-data.sql new file mode 100644 index 0000000..0d63db8 --- /dev/null +++ b/tests/test-pre-scripts/create-test-data.sql @@ -0,0 +1,7 @@ +CREATE TABLE test_table ( + id integer PRIMARY KEY, + data text NOT NULL +); + +INSERT INTO test_table (data) +VALUES ("Test row"); diff --git a/tests/test-pre-scripts/restore/after/post-restore.sh b/tests/test-pre-scripts/restore/after/post-restore.sh new file mode 100755 index 0000000..7ede126 --- /dev/null +++ b/tests/test-pre-scripts/restore/after/post-restore.sh @@ -0,0 +1,6 @@ +set -e + +cd /data + +# Restore the backedup database +mv test_database.db.bak test_database.db diff --git a/tests/scripts/pre-restore.sh b/tests/test-pre-scripts/restore/before/pre-restore.sh similarity index 100% rename from tests/scripts/pre-restore.sh rename to tests/test-pre-scripts/restore/before/pre-restore.sh