From cd0635e917cf17ea0b10f9be241ad1529028c380 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 20:48:31 +0300 Subject: [PATCH 1/7] TestUtils is added --- tests/test_testgres_local.py | 12 +----------- tests/test_utils.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 tests/test_utils.py diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index bef80d0f..b43ee9d3 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -30,7 +30,7 @@ # NOTE: those are ugly imports from ..testgres import bound_ports -from ..testgres.utils import PgVer, parse_pg_version +from ..testgres.utils import PgVer from ..testgres.node import ProcessProxy @@ -234,16 +234,6 @@ def test_upgrade_node(self): node_new.start() assert (b'Upgrade Complete' in res) - def test_parse_pg_version(self): - # Linux Mint - assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5" - # Linux Ubuntu - assert parse_pg_version("postgres (PostgreSQL) 12.17") == "12.17" - # Windows - assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4" - # Macos - assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9" - class tagPortManagerProxy: sm_prev_testgres_reserve_port = None sm_prev_testgres_release_port = None diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..bf4f30e3 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,13 @@ +from ..testgres.utils import parse_pg_version + + +class TestUtils: + def test_parse_pg_version(self): + # Linux Mint + assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5" + # Linux Ubuntu + assert parse_pg_version("postgres (PostgreSQL) 12.17") == "12.17" + # Windows + assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4" + # Macos + assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9" From 2ef2e006f8401b3c226292b44ade65e5d8dc8ca5 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 20:51:44 +0300 Subject: [PATCH 2/7] TestTestgresCommon::test_node_repr is added --- tests/test_testgres_common.py | 5 +++++ tests/test_testgres_local.py | 5 ----- tests/test_testgres_remote.py | 6 ------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index b286a1c6..aff0fa2b 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -106,6 +106,11 @@ def test_version_management(self, node_svc: PostgresNodeService): assert (isinstance(node.version, PgVer)) assert (node.version == PgVer(version)) + def test_node_repr(self, node_svc: PostgresNodeService): + with __class__.helper__get_node(node_svc).init() as node: + pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" + assert re.match(pattern, str(node)) is not None + def test_double_init(self, node_svc: PostgresNodeService): assert isinstance(node_svc, PostgresNodeService) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index b43ee9d3..91c61865 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -75,11 +75,6 @@ def rm_carriage_returns(out): class TestTestgresLocal: - def test_node_repr(self): - with get_new_node() as node: - pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" - assert re.match(pattern, str(node)) is not None - def test_custom_init(self): with get_new_node() as node: # enable page checksums diff --git a/tests/test_testgres_remote.py b/tests/test_testgres_remote.py index ef4bd0c8..7eab9eb7 100755 --- a/tests/test_testgres_remote.py +++ b/tests/test_testgres_remote.py @@ -1,6 +1,5 @@ # coding: utf-8 import os -import re import pytest import logging @@ -58,11 +57,6 @@ def implicit_fixture(self): testgres_config.set_os_ops(os_ops=prev_ops) assert testgres_config.os_ops is prev_ops - def test_node_repr(self): - with __class__.helper__get_node() as node: - pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" - assert re.match(pattern, str(node)) is not None - def test_custom_init(self): with __class__.helper__get_node() as node: # enable page checksums From d7f704d8d46c902745e955f00dffb6835015a6c7 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 21:04:53 +0300 Subject: [PATCH 3/7] test_get_pg_config2 moved to TestUtils --- tests/test_testgres_common.py | 28 -------------------- tests/test_utils.py | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index aff0fa2b..94ab9080 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -6,7 +6,6 @@ from ..testgres.node import PgVer from ..testgres.node import PostgresNode from ..testgres.utils import get_pg_version2 -from ..testgres.utils import get_pg_config2 from ..testgres.utils import file_tail from ..testgres.utils import get_bin_path2 from ..testgres import ProcessType @@ -1080,33 +1079,6 @@ def test_dump(self, node_svc: PostgresNodeService): res = node3.execute(query_select) assert (res == [(1, ), (2, )]) - def test_get_pg_config2(self, node_svc: PostgresNodeService): - assert isinstance(node_svc, PostgresNodeService) - - # check same instances - a = get_pg_config2(node_svc.os_ops, None) - b = get_pg_config2(node_svc.os_ops, None) - assert (id(a) == id(b)) - - # save right before config change - c1 = get_pg_config2(node_svc.os_ops, None) - - # modify setting for this scope - with scoped_config(cache_pg_config=False) as config: - # sanity check for value - assert not (config.cache_pg_config) - - # save right after config change - c2 = get_pg_config2(node_svc.os_ops, None) - - # check different instances after config change - assert (id(c1) != id(c2)) - - # check different instances - a = get_pg_config2(node_svc.os_ops, None) - b = get_pg_config2(node_svc.os_ops, None) - assert (id(a) != id(b)) - def test_pgbench(self, node_svc: PostgresNodeService): assert isinstance(node_svc, PostgresNodeService) diff --git a/tests/test_utils.py b/tests/test_utils.py index bf4f30e3..d4a4c9ad 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,7 +1,29 @@ +from .helpers.global_data import OsOpsDescr +from .helpers.global_data import OsOpsDescrs +from .helpers.global_data import OsOperations + from ..testgres.utils import parse_pg_version +from ..testgres.utils import get_pg_config2 +from ..testgres import scoped_config + +import pytest class TestUtils: + sm_os_ops_descrs: list[OsOpsDescr] = [ + OsOpsDescrs.sm_local_os_ops_descr, + OsOpsDescrs.sm_remote_os_ops_descr + ] + + @pytest.fixture( + params=[descr.os_ops for descr in sm_os_ops_descrs], + ids=[descr.sign for descr in sm_os_ops_descrs] + ) + def os_ops(self, request: pytest.FixtureRequest) -> OsOperations: + assert isinstance(request, pytest.FixtureRequest) + assert isinstance(request.param, OsOperations) + return request.param + def test_parse_pg_version(self): # Linux Mint assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5" @@ -11,3 +33,30 @@ def test_parse_pg_version(self): assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4" # Macos assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9" + + def test_get_pg_config2(self, os_ops: OsOperations): + assert isinstance(os_ops, OsOperations) + + # check same instances + a = get_pg_config2(os_ops, None) + b = get_pg_config2(os_ops, None) + assert (id(a) == id(b)) + + # save right before config change + c1 = get_pg_config2(os_ops, None) + + # modify setting for this scope + with scoped_config(cache_pg_config=False) as config: + # sanity check for value + assert not (config.cache_pg_config) + + # save right after config change + c2 = get_pg_config2(os_ops, None) + + # check different instances after config change + assert (id(c1) != id(c2)) + + # check different instances + a = get_pg_config2(os_ops, None) + b = get_pg_config2(os_ops, None) + assert (id(a) != id(b)) From cd806178821edb817e0c9c87e2af8d7f42de09f9 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 21:15:52 +0300 Subject: [PATCH 4/7] TestTestgresCommon.test_custom_init is added --- tests/test_testgres_common.py | 21 +++++++++++++++++++++ tests/test_testgres_local.py | 20 -------------------- tests/test_testgres_remote.py | 19 ------------------- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index 94ab9080..c384dfb2 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -110,6 +110,27 @@ def test_node_repr(self, node_svc: PostgresNodeService): pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" assert re.match(pattern, str(node)) is not None + def test_custom_init(self, node_svc: PostgresNodeService): + assert isinstance(node_svc, PostgresNodeService) + + with __class__.helper__get_node(node_svc) as node: + # enable page checksums + node.init(initdb_params=['-k']).start() + + with __class__.helper__get_node(node_svc) as node: + node.init( + allow_streaming=True, + initdb_params=['--auth-local=reject', '--auth-host=reject']) + + hba_file = os.path.join(node.data_dir, 'pg_hba.conf') + lines = node.os_ops.readlines(hba_file) + + # check number of lines + assert (len(lines) >= 6) + + # there should be no trust entries at all + assert not (any('trust' in s for s in lines)) + def test_double_init(self, node_svc: PostgresNodeService): assert isinstance(node_svc, PostgresNodeService) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index 91c61865..4a8aa669 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -75,26 +75,6 @@ def rm_carriage_returns(out): class TestTestgresLocal: - def test_custom_init(self): - with get_new_node() as node: - # enable page checksums - node.init(initdb_params=['-k']).start() - - with get_new_node() as node: - node.init( - allow_streaming=True, - initdb_params=['--auth-local=reject', '--auth-host=reject']) - - hba_file = os.path.join(node.data_dir, 'pg_hba.conf') - with open(hba_file, 'r') as conf: - lines = conf.readlines() - - # check number of lines - assert (len(lines) >= 6) - - # there should be no trust entries at all - assert not (any('trust' in s for s in lines)) - def test_pg_config(self): # check same instances a = get_pg_config() diff --git a/tests/test_testgres_remote.py b/tests/test_testgres_remote.py index 7eab9eb7..0fc760b3 100755 --- a/tests/test_testgres_remote.py +++ b/tests/test_testgres_remote.py @@ -57,25 +57,6 @@ def implicit_fixture(self): testgres_config.set_os_ops(os_ops=prev_ops) assert testgres_config.os_ops is prev_ops - def test_custom_init(self): - with __class__.helper__get_node() as node: - # enable page checksums - node.init(initdb_params=['-k']).start() - - with __class__.helper__get_node() as node: - node.init( - allow_streaming=True, - initdb_params=['--auth-local=reject', '--auth-host=reject']) - - hba_file = os.path.join(node.data_dir, 'pg_hba.conf') - lines = node.os_ops.readlines(hba_file) - - # check number of lines - assert (len(lines) >= 6) - - # there should be no trust entries at all - assert not (any('trust' in s for s in lines)) - def test_init__LANG_ะก(self): # PBCKP-1744 prev_LANG = os.environ.get("LANG") From 88ca53dd232e0dded2566d52acb79bf9fd0750de Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 21:39:29 +0300 Subject: [PATCH 5/7] TestConfig is added --- tests/test_config.py | 41 +++++++++++++++++++++++++++++++++++ tests/test_testgres_local.py | 37 +------------------------------ tests/test_testgres_remote.py | 38 ++------------------------------ 3 files changed, 44 insertions(+), 72 deletions(-) create mode 100644 tests/test_config.py diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 00000000..05702e9a --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,41 @@ +from ..testgres import TestgresConfig +from ..testgres import configure_testgres +from ..testgres import scoped_config +from ..testgres import pop_config + +from .. import testgres + +import pytest + + +class TestConfig: + def test_config_stack(self): + # no such option + with pytest.raises(expected_exception=TypeError): + configure_testgres(dummy=True) + + # we have only 1 config in stack + with pytest.raises(expected_exception=IndexError): + pop_config() + + d0 = TestgresConfig.cached_initdb_dir + d1 = 'dummy_abc' + d2 = 'dummy_def' + + with scoped_config(cached_initdb_dir=d1) as c1: + assert (c1.cached_initdb_dir == d1) + + with scoped_config(cached_initdb_dir=d2) as c2: + stack_size = len(testgres.config.config_stack) + + # try to break a stack + with pytest.raises(expected_exception=TypeError): + with scoped_config(dummy=True): + pass + + assert (c2.cached_initdb_dir == d2) + assert (len(testgres.config.config_stack) == stack_size) + + assert (c1.cached_initdb_dir == d1) + + assert (TestgresConfig.cached_initdb_dir == d0) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index 4a8aa669..412fcc9b 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -14,11 +14,7 @@ ExecUtilException, \ NodeApp -from ..testgres import \ - TestgresConfig, \ - configure_testgres, \ - scoped_config, \ - pop_config +from ..testgres import scoped_config from ..testgres import \ get_new_node @@ -100,37 +96,6 @@ def test_pg_config(self): b = get_pg_config() assert (id(a) != id(b)) - def test_config_stack(self): - # no such option - with pytest.raises(expected_exception=TypeError): - configure_testgres(dummy=True) - - # we have only 1 config in stack - with pytest.raises(expected_exception=IndexError): - pop_config() - - d0 = TestgresConfig.cached_initdb_dir - d1 = 'dummy_abc' - d2 = 'dummy_def' - - with scoped_config(cached_initdb_dir=d1) as c1: - assert (c1.cached_initdb_dir == d1) - - with scoped_config(cached_initdb_dir=d2) as c2: - stack_size = len(testgres.config.config_stack) - - # try to break a stack - with pytest.raises(expected_exception=TypeError): - with scoped_config(dummy=True): - pass - - assert (c2.cached_initdb_dir == d2) - assert (len(testgres.config.config_stack) == stack_size) - - assert (c1.cached_initdb_dir == d1) - - assert (TestgresConfig.cached_initdb_dir == d0) - def test_ports_management(self): assert bound_ports is not None assert type(bound_ports) == set # noqa: E721 diff --git a/tests/test_testgres_remote.py b/tests/test_testgres_remote.py index 0fc760b3..42592fa7 100755 --- a/tests/test_testgres_remote.py +++ b/tests/test_testgres_remote.py @@ -13,11 +13,8 @@ InitNodeException, \ ExecUtilException -from ..testgres.config import \ - TestgresConfig, \ - configure_testgres, \ - scoped_config, \ - pop_config, testgres_config +from ..testgres.config import scoped_config +from ..testgres.config import testgres_config from ..testgres import \ get_bin_path, \ @@ -168,37 +165,6 @@ def test_pg_config(self): b = get_pg_config() assert (id(a) != id(b)) - def test_config_stack(self): - # no such option - with pytest.raises(expected_exception=TypeError): - configure_testgres(dummy=True) - - # we have only 1 config in stack - with pytest.raises(expected_exception=IndexError): - pop_config() - - d0 = TestgresConfig.cached_initdb_dir - d1 = 'dummy_abc' - d2 = 'dummy_def' - - with scoped_config(cached_initdb_dir=d1) as c1: - assert (c1.cached_initdb_dir == d1) - - with scoped_config(cached_initdb_dir=d2) as c2: - stack_size = len(testgres.config.config_stack) - - # try to break a stack - with pytest.raises(expected_exception=TypeError): - with scoped_config(dummy=True): - pass - - assert (c2.cached_initdb_dir == d2) - assert (len(testgres.config.config_stack) == stack_size) - - assert (c1.cached_initdb_dir == d1) - - assert (TestgresConfig.cached_initdb_dir == d0) - @staticmethod def helper__get_node(name=None): svc = PostgresNodeServices.sm_remote From 18e2af3ad424e6d00759451d167cfa3a888da9d5 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 21:48:46 +0300 Subject: [PATCH 6/7] Code style [simplification] --- tests/test_testgres_local.py | 20 +++++++------------- tests/test_testgres_remote.py | 10 ++++------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index 412fcc9b..101db64e 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -9,20 +9,14 @@ from .. import testgres -from ..testgres import \ - StartNodeException, \ - ExecUtilException, \ - NodeApp - +from ..testgres import StartNodeException +from ..testgres import ExecUtilException +from ..testgres import NodeApp from ..testgres import scoped_config - -from ..testgres import \ - get_new_node - -from ..testgres import \ - get_bin_path, \ - get_pg_config, \ - get_pg_version +from ..testgres import get_new_node +from ..testgres import get_bin_path +from ..testgres import get_pg_config +from ..testgres import get_pg_version # NOTE: those are ugly imports from ..testgres import bound_ports diff --git a/tests/test_testgres_remote.py b/tests/test_testgres_remote.py index 42592fa7..e38099b7 100755 --- a/tests/test_testgres_remote.py +++ b/tests/test_testgres_remote.py @@ -9,16 +9,14 @@ from .. import testgres -from ..testgres.exceptions import \ - InitNodeException, \ - ExecUtilException +from ..testgres.exceptions import InitNodeException +from ..testgres.exceptions import ExecUtilException from ..testgres.config import scoped_config from ..testgres.config import testgres_config -from ..testgres import \ - get_bin_path, \ - get_pg_config +from ..testgres import get_bin_path +from ..testgres import get_pg_config # NOTE: those are ugly imports From 820fd78d3f074608c4bc18f206444e28f12a0379 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sun, 6 Apr 2025 21:50:48 +0300 Subject: [PATCH 7/7] test_testgres_local.py is updated [normalization] --- tests/test_testgres_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index 101db64e..9dbd455b 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -19,7 +19,7 @@ from ..testgres import get_pg_version # NOTE: those are ugly imports -from ..testgres import bound_ports +from ..testgres.utils import bound_ports from ..testgres.utils import PgVer from ..testgres.node import ProcessProxy