diff --git a/.github/workflows/build-latest.yaml b/.github/workflows/build-latest.yaml index f1ad6bd..02a2726 100644 --- a/.github/workflows/build-latest.yaml +++ b/.github/workflows/build-latest.yaml @@ -38,6 +38,7 @@ jobs: - collations - extensions - logical_replication + - init_scripts include: - distro: debian imageVersion: bullseye diff --git a/scenario_tests/init_scripts/docker-compose.yml b/scenario_tests/init_scripts/docker-compose.yml new file mode 100644 index 0000000..ea9a932 --- /dev/null +++ b/scenario_tests/init_scripts/docker-compose.yml @@ -0,0 +1,59 @@ +version: '2.1' +volumes: + default-pg-data-dir-md5: + new-pg-data-dir: + default-pg-data-dir-scram: + +services: + pg-default-md5: + image: 'kartoza/postgis:${TAG:-manual-build}' + volumes: + # By default persisted volumes should be in /var/lib/postgresql + - default-pg-data-dir-md5:/var/lib/postgresql + - ./tests/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./tests:/tests + - ../utils:/lib/utils + environment: + POSTGRES_PASS: 'docker' + PASSWORD_AUTHENTICATION: md5 + healthcheck: + interval: 60s + timeout: 30s + retries: 3 + test: "pg_isready" + + pg-new-md5: + image: 'kartoza/postgis:${TAG:-manual-build}' + volumes: + # Mount to new locations where there are no initial data + - new-pg-data-dir:/opt/mypostgis/data + - ./tests/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./tests:/tests + - ../utils:/lib/utils + environment: + DATADIR: /opt/mypostgis/data + POSTGRES_PASS: 'docker' + PASSWORD_AUTHENTICATION: md5 + healthcheck: + interval: 60s + timeout: 30s + retries: 3 + test: "pg_isready" + + pg-default-scram: + image: 'kartoza/postgis:${TAG:-manual-build}' + volumes: + # By default persisted volumes should be in /var/lib/postgresql + - default-pg-data-dir-scram:/var/lib/postgresql + - ./tests/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./tests:/tests + - ../utils:/lib/utils + environment: + POSTGRES_PASS: 'docker' + healthcheck: + interval: 60s + timeout: 30s + retries: 3 + test: "pg_isready" + + diff --git a/scenario_tests/init_scripts/test.sh b/scenario_tests/init_scripts/test.sh new file mode 100755 index 0000000..efbb530 --- /dev/null +++ b/scenario_tests/init_scripts/test.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# exit immediately if test fails +set -e + +source ../test-env.sh + +# Run service +docker compose up -d pg-default-md5 pg-new-md5 pg-default-scram + +if [[ -n "${PRINT_TEST_LOGS}" ]]; then + docker compose logs -f & +fi + +sleep 60 + +services=("pg-default-md5" "pg-new-md5" "pg-default-scram") + +for service in "${services[@]}"; do + + # Execute tests + until docker compose exec -T $service pg_isready; do + sleep 5 + echo "Wait service to be ready" + done; + echo "Execute test for $service" + docker compose exec -T $service /bin/bash /tests/test.sh + +done + + +docker compose down -v diff --git a/scenario_tests/init_scripts/tests/__init__.py b/scenario_tests/init_scripts/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scenario_tests/init_scripts/tests/init.sh b/scenario_tests/init_scripts/tests/init.sh new file mode 100644 index 0000000..3eab011 --- /dev/null +++ b/scenario_tests/init_scripts/tests/init.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +export PGPASSWORD=${POSTGRES_PASS} + +psql -d gis -p 5432 -U docker -h localhost -f /tests/init.sql diff --git a/scenario_tests/init_scripts/tests/init.sql b/scenario_tests/init_scripts/tests/init.sql new file mode 100644 index 0000000..98f6ca9 --- /dev/null +++ b/scenario_tests/init_scripts/tests/init.sql @@ -0,0 +1,20 @@ + CREATE TABLE IF NOT EXISTS test_init_table ( + id integer not null + constraint pkey primary key, + geom geometry(Point, 4326), + name varchar(30), + alias varchar(30), + description varchar(255) + ); + +INSERT INTO test_init_table (id, geom, name, alias, description) + VALUES + ( + 1, + st_setsrid(st_point(107.6097, 6.9120), 4326), + 'Bandung', + 'Paris van Java', + 'Asia-Africa conference was held here' + ) ON CONFLICT DO NOTHING; + + diff --git a/scenario_tests/init_scripts/tests/init.sql.gz b/scenario_tests/init_scripts/tests/init.sql.gz new file mode 100644 index 0000000..52473af Binary files /dev/null and b/scenario_tests/init_scripts/tests/init.sql.gz differ diff --git a/scenario_tests/init_scripts/tests/init.sql.gz.properties b/scenario_tests/init_scripts/tests/init.sql.gz.properties new file mode 100644 index 0000000..3f7d287 --- /dev/null +++ b/scenario_tests/init_scripts/tests/init.sql.gz.properties @@ -0,0 +1,2 @@ +compressed_size=337 +uncompressed_size=734 diff --git a/scenario_tests/init_scripts/tests/test.sh b/scenario_tests/init_scripts/tests/test.sh new file mode 100644 index 0000000..d98e96a --- /dev/null +++ b/scenario_tests/init_scripts/tests/test.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +source /scripts/env-data.sh + +# execute tests +pushd /tests + + +PGHOST=localhost \ +PGDATABASE=gis \ +PYTHONPATH=/lib \ + python3 -m unittest -v test_entrypoint.TestInitScript diff --git a/scenario_tests/init_scripts/tests/test_entrypoint.py b/scenario_tests/init_scripts/tests/test_entrypoint.py new file mode 100644 index 0000000..ffc17d9 --- /dev/null +++ b/scenario_tests/init_scripts/tests/test_entrypoint.py @@ -0,0 +1,22 @@ +import unittest +from utils.utils import DBConnection + + +class TestInitScript(unittest.TestCase): + + def setUp(self): + self.db = DBConnection() + + def test_read_data(self): + # create new table + self.db.conn.autocommit = True + with self.db.cursor() as c: + c.execute( + """ + SELECT * FROM test_init_table; + """ + ) + + rows = c.fetchall() + self.assertEqual(len(rows), 1) +