update workflow with test for init script (#402)

pull/405/head
mazano 2022-12-20 22:03:34 +02:00 zatwierdzone przez GitHub
rodzic e7fd886c38
commit c46a7b6a7b
10 zmienionych plików z 155 dodań i 0 usunięć

Wyświetl plik

@ -38,6 +38,7 @@ jobs:
- collations
- extensions
- logical_replication
- init_scripts
include:
- distro: debian
imageVersion: bullseye

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

@ -0,0 +1,5 @@
#!/bin/bash
export PGPASSWORD=${POSTGRES_PASS}
psql -d gis -p 5432 -U docker -h localhost -f /tests/init.sql

Wyświetl plik

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

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -0,0 +1,2 @@
compressed_size=337
uncompressed_size=734

Wyświetl plik

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

Wyświetl plik

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