From ce208079ed9b6d1729fbdd938aaf39f6fef1f769 Mon Sep 17 00:00:00 2001 From: Rizky Maulana Nugraha Date: Wed, 18 Aug 2021 23:25:42 +0000 Subject: [PATCH] add unittests for not matching environment --- .../datadir_init/docker-compose.yml | 4 ++-- .../datadir_init/test_custom_waldir.sh | 2 +- .../datadir_init/tests/test_datadir.py | 23 ++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/scenario_tests/datadir_init/docker-compose.yml b/scenario_tests/datadir_init/docker-compose.yml index aa8c8c5..9a1c4f0 100644 --- a/scenario_tests/datadir_init/docker-compose.yml +++ b/scenario_tests/datadir_init/docker-compose.yml @@ -112,7 +112,7 @@ services: - ./tests:/tests - ../utils:/lib/utils environment: - TEST_CLASS: TestCustomWALdir + TEST_CLASS: TestCustomWALdirNotMatch DATADIR: /opt/mypostgis/data POSTGRES_INITDB_WALDIR: /opt/waldir POSTGRES_PASS: 'docker' @@ -132,7 +132,7 @@ services: - ./tests:/tests - ../utils:/lib/utils environment: - TEST_CLASS: TestCustomWALdir + TEST_CLASS: TestCustomWALdirNotMatch DATADIR: /opt/mypostgis/data POSTGRES_INITDB_WALDIR: /opt/waldir POSTGRES_PASS: 'docker' diff --git a/scenario_tests/datadir_init/test_custom_waldir.sh b/scenario_tests/datadir_init/test_custom_waldir.sh index 0209dd2..63e48ff 100644 --- a/scenario_tests/datadir_init/test_custom_waldir.sh +++ b/scenario_tests/datadir_init/test_custom_waldir.sh @@ -101,7 +101,7 @@ sleep 60 warning_text="Can't proceed because \"/opt/mypostgis/data/pg_wal\" directory is empty." while true; do if [[ -n "$(docker-compose logs $service | grep 'Error')" && \ - -n "$(docker-compose logs $service | grep \"$warning_text\")" ]]; then + -n "$(docker-compose logs $service | grep "$warning_text")" ]]; then break fi sleep 5 diff --git a/scenario_tests/datadir_init/tests/test_datadir.py b/scenario_tests/datadir_init/tests/test_datadir.py index 175af65..1bef318 100644 --- a/scenario_tests/datadir_init/tests/test_datadir.py +++ b/scenario_tests/datadir_init/tests/test_datadir.py @@ -88,5 +88,26 @@ class TestCustomWALdir(TestCollationBase): with self.db.cursor() as c: datadir_location = self.fetch_datadir_location(c) pg_wal_symlink = os.path.join(datadir_location, 'pg_wal') + # In this correct setup, pg_wal symlink must resolve to the + # correct POSTGRES_INITDB_WALDIR location self.assertTrue( - Path(pg_wal_symlink).resolve().match(postgres_initdb_waldir)) \ No newline at end of file + Path(pg_wal_symlink).resolve().match(postgres_initdb_waldir)) + + +class TestCustomWALdirNotMatch(TestCollationBase): + + def test_check_pg_wal_symlink(self): + self.db.conn.autocommit = True + postgres_initdb_waldir = os.environ.get('POSTGRES_INITDB_WALDIR') + with self.db.cursor() as c: + datadir_location = self.fetch_datadir_location(c) + pg_wal_symlink = os.path.join(datadir_location, 'pg_wal') + # In this wrong setup, pg_wal symlink and POSTGRES_INITDB_WALDIR + # must resolve to a different path to raise the warning + self.assertFalse( + Path(pg_wal_symlink).resolve().match(postgres_initdb_waldir)) + + # It has different path, but if this unittests runs, that means + # postgres was able to start and the pg_wal symlink contains correct + # data + self.assertTrue(os.listdir(pg_wal_symlink))