-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue with multiple db creation and add test for this (#443)
* fix issue with multiple db creation and add test for this
- Loading branch information
Showing
9 changed files
with
195 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
version: '2.1' | ||
|
||
volumes: | ||
pg-db-data-dir: | ||
pg-db-schema-dir: | ||
|
||
|
||
services: | ||
pg-database: | ||
image: 'kartoza/postgis:${TAG:-manual-build}' | ||
restart: 'always' | ||
# You can optionally mount to volume, to play with the persistence and | ||
# observe how the node will behave after restarts. | ||
volumes: | ||
- pg-db-data-dir:/var/lib/postgresql | ||
- ./tests:/tests | ||
- ../utils:/lib/utils | ||
environment: | ||
POSTGRES_DB: 'gis,data' | ||
POSTGRES_PASS: 'docker' | ||
ALL_DATABASES: TRUE | ||
SCHEMA_NAME: demo | ||
healthcheck: | ||
interval: 60s | ||
timeout: 30s | ||
retries: 3 | ||
test: "PGPASSWORD=docker pg_isready -h 127.0.0.1 -U docker -d gis" | ||
|
||
pg-schema: | ||
image: 'kartoza/postgis:${TAG:-manual-build}' | ||
restart: 'always' | ||
# You can optionally mount to volume, to play with the persistence and | ||
# observe how the node will behave after restarts. | ||
volumes: | ||
- pg-db-schema-dir:/var/lib/postgresql | ||
- ./tests:/tests | ||
- ../utils:/lib/utils | ||
environment: | ||
POSTGRES_DB: 'gis,data' | ||
POSTGRES_PASS: 'docker' | ||
ALL_DATABASES: FALSE | ||
SCHEMA_NAME: demo | ||
healthcheck: | ||
interval: 60s | ||
timeout: 30s | ||
retries: 3 | ||
test: "PGPASSWORD=docker pg_isready -h 127.0.0.1 -U docker -d gis" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env bash | ||
|
||
# exit immediately if test fails | ||
set -e | ||
|
||
source ../test-env.sh | ||
if [[ $(dpkg -l | grep "docker-compose") > /dev/null ]];then | ||
VERSION='docker-compose' | ||
else | ||
VERSION='docker compose' | ||
fi | ||
|
||
|
||
# Run service as root | ||
${VERSION} up -d pg-database | ||
|
||
if [[ -n "${PRINT_TEST_LOGS}" ]]; then | ||
${VERSION} logs -f & | ||
fi | ||
|
||
sleep 30 | ||
|
||
# Preparing all databases and all schemas | ||
until ${VERSION} exec -T pg-database pg_isready; do | ||
sleep 1 | ||
done; | ||
|
||
# Execute tests | ||
${VERSION} exec -T pg-database /bin/bash /tests/test_schemas.sh | ||
|
||
|
||
${VERSION} down -v | ||
|
||
|
||
# Run service for pg-schema | ||
${VERSION} up -d pg-schema | ||
|
||
if [[ -n "${PRINT_TEST_LOGS}" ]]; then | ||
${VERSION} logs -f & | ||
fi | ||
|
||
sleep 30 | ||
|
||
# Preparing all databases and single schema | ||
until ${VERSION} exec -T pg-schema pg_isready; do | ||
sleep 1 | ||
done; | ||
|
||
# Execute tests | ||
${VERSION} exec -T pg-schema /bin/bash /tests/test_schemas.sh | ||
|
||
|
||
${VERSION} down -v |
Empty file.
50 changes: 50 additions & 0 deletions
50
scenario_tests/multiple_databases/tests/test_schema_existence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
import psycopg2 | ||
import os | ||
|
||
|
||
class TestSchemaExistence(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.db_host = os.environ.get('POSTGRES_HOST', 'localhost') | ||
self.db_port = os.environ.get('POSTGRES_PORT', '5432') | ||
self.db_user = os.environ.get('POSTGRES_USER', 'docker') | ||
self.db_pass = os.environ.get('POSTGRES_PASS', 'docker') | ||
self.db_names = os.environ.get('POSTGRES_DB', '').split(',') | ||
self.schemas = os.environ.get('SCHEMA_NAME', '').split(',') | ||
self.all_databases = os.environ.get('ALL_DATABASES', 'TRUE').lower() == 'true' | ||
|
||
def connect_to_db(self, db_name): | ||
try: | ||
conn = psycopg2.connect( | ||
dbname=db_name, | ||
user=self.db_user, | ||
password=self.db_pass, | ||
host=self.db_host, | ||
port=self.db_port | ||
) | ||
return conn | ||
except psycopg2.Error as e: | ||
self.fail(f"Failed to connect to the database: {e}") | ||
|
||
def test_schema_existence(self): | ||
for idx, db_name in enumerate(self.db_names): | ||
conn = self.connect_to_db(db_name) | ||
cursor = conn.cursor() | ||
|
||
for schema in self.schemas: | ||
query = f"SELECT schema_name, catalog_name FROM information_schema.schemata \ | ||
WHERE schema_name = '{schema}' and catalog_name = '{db_name}';" | ||
cursor.execute(query) | ||
exists = cursor.fetchone() | ||
|
||
if not self.all_databases and idx > 0: | ||
self.assertIsNone(exists, f"Schema '{schema}' should not exist in database '{db_name}'") | ||
else: | ||
self.assertIsNotNone(exists, f"Schema '{schema}' does not exist in database '{db_name}'") | ||
|
||
conn.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/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_schema_existence.TestSchemaExistence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters