Add some integration tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
IamTheFij 2023-08-02 13:59:59 -07:00
parent 9cdf37c680
commit eb80a81255
5 changed files with 150 additions and 0 deletions

39
itest/bootstrap-tests.sh Executable file
View File

@ -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

35
itest/docker-compose.yml Normal file
View File

@ -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

27
itest/run.sh Executable file
View File

@ -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/*

29
itest/test-backup.hcl Normal file
View File

@ -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 = "/"
}
}
}

20
itest/validate-tests.sh Executable file
View File

@ -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