From eb80a8125578795676bafdcec8533489c6e3dd56 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 2 Aug 2023 13:59:59 -0700 Subject: [PATCH] Add some integration tests --- itest/bootstrap-tests.sh | 39 +++++++++++++++++++++++++++++++++++++++ itest/docker-compose.yml | 35 +++++++++++++++++++++++++++++++++++ itest/run.sh | 27 +++++++++++++++++++++++++++ itest/test-backup.hcl | 29 +++++++++++++++++++++++++++++ itest/validate-tests.sh | 20 ++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100755 itest/bootstrap-tests.sh create mode 100644 itest/docker-compose.yml create mode 100755 itest/run.sh create mode 100644 itest/test-backup.hcl create mode 100755 itest/validate-tests.sh diff --git a/itest/bootstrap-tests.sh b/itest/bootstrap-tests.sh new file mode 100755 index 0000000..7eb46f1 --- /dev/null +++ b/itest/bootstrap-tests.sh @@ -0,0 +1,39 @@ +#! /bin/bash +set -ex + +# Create flat file +echo "Hello" > /data/test.txt + +# Create Sqlite database +touch /data/test_database.db +sqlite3 /data/test_database.db <<-EOF +CREATE TABLE test_table ( + id integer PRIMARY KEY, + data text NOT NULL, +); + +INSERT INTO test_table(data) +VALUES ("Test row"); +EOF + +# Create MySql database +mysql --user "$MYSQL_USER" --password "$MYSQL_PWD" main <<-EOF +CREATE TABLE test_table ( + id integer AUTO_INCREMENT PRIMARY KEY, + data text NOT NULL, +); + +INSERT INTO test_table(data) +VALUES ("Test row"); +EOF + +# Create Postgresql database +pgsql --username "$PGSQL_USER" --dbname main <<-EOF +CREATE TABLE test_table ( + id integer PRIMARY KEY, + data text NOT NULL, +); + +INSERT INTO test_table(data) +VALUES ("Test row"); +EOF diff --git a/itest/docker-compose.yml b/itest/docker-compose.yml new file mode 100644 index 0000000..2ad6fea --- /dev/null +++ b/itest/docker-compose.yml @@ -0,0 +1,35 @@ +--- +version: 3.9 + +services: + mysql: + image: mysql + environment: + MYSQL_ROOT_PASSWORD: shhh + MYSQL_DATABASE: main + + bootstrap: + image: alpine + command: /bootstrap-tests.sh + environment: + MYSQL_HOST: mysql + MYSQL_USER: root + MYSQL_PWD: shhh + volumes: + - ./bootstrap-tests.sh:/bootstrap-tests.sh + - ./data:/data + + main: + build: + context: .. + command: + volumes: + - ./repo:/repo + - ./data:/data + - ./test-backup.hcl:/test-backup.hcl + + validate: + image: alpine + command: /validate-tests.sh + volumes: + ./validate-tests.sh:/validate-tests.sh diff --git a/itest/run.sh b/itest/run.sh new file mode 100755 index 0000000..28673a1 --- /dev/null +++ b/itest/run.sh @@ -0,0 +1,27 @@ +#! /bin/bash +set -ex + +echo Clean everything +docker-compose down -v +rm -fr ./repo/* ./data/* + +echo Boostrap databases and data +docker-compose up -d mysql +docker-compose run bootstrap /bootstrap-tests.sh + +echo Run backup job +docker-compose run main -backup IntegrationTest -once /test-backup.hcl + +echo Clean data +docker-compose down -v +rm -fr ./data/* + +echo Run restore +docker-compose run main -restore IntegrationTest -once /test-backup.hcl + +echo Validate data +docker-compose run validate /validate-tests.sh + +echo Clean all again +docker-compose down -v +rm -fr ./repo/* ./data/* diff --git a/itest/test-backup.hcl b/itest/test-backup.hcl new file mode 100644 index 0000000..4a6a89f --- /dev/null +++ b/itest/test-backup.hcl @@ -0,0 +1,29 @@ +job "IntegrationTest" { + schedule = "@daily" + + config { + repo = "/repo" + passphrase = "shh" + } + + mysql { + hostname = env("MYSQL_HOST") + database = "main" + username = env("MYSQL_USER") + password = env("MYSQL_PWD") + dump_to = "/tmp/mysql.sql" + } + + sqlite { + path = "/data/test_database.db" + dump_to = "/data/test_database.db.bak" + } + + backup { + paths = ["/data"] + + restore_opts { + Target = "/" + } + } +} diff --git a/itest/validate-tests.sh b/itest/validate-tests.sh new file mode 100755 index 0000000..d03de36 --- /dev/null +++ b/itest/validate-tests.sh @@ -0,0 +1,20 @@ +#! /bin/bash +set -ex + +# Check flat file +test -f /data/test.txt +grep "^Hello" /data/test.txt + +# Check Sqlite database +test -f /data/test_database.db +sqlite3 /data/test_database.db "select data from test_table where id = 1" | grep "^Test row" + +# Check MySql database +mysql --user "$MYSQL_USER" --password "$MYSQL_PWD" main <<-EOF | grep "^Test row" +select data from test_table where id = 1; +EOF + +# Check Postgresql database +pgsql --username "$PGSQL_USER" --dbname main <<-EOF | grep "^Test row" +select data from test_table where id = 1; +EOF