From d28193e8c959a03d04e4e3bc19f9dc5d75c318a4 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Sat, 14 Nov 2026 22:31:23 +0300 Subject: [PATCH] PostgresNode::set_auto_conf is updated (postgres_configuration) New implementation of PostgresNode::set_auto_conf starts using testgres.postgres_configuration objects to update configuration files. Amen. --- pyproject.toml | 1 + {tests/helpers => src/impl}/pg_cfg_os_ops.py | 0 src/node.py | 84 ++++++++++---------- tests/requirements.txt | 2 +- tests/test_testgres_common.py | 49 +++++++++++- 5 files changed, 90 insertions(+), 46 deletions(-) rename {tests/helpers => src/impl}/pg_cfg_os_ops.py (100%) diff --git a/pyproject.toml b/pyproject.toml index 7c51b331..b7c5d79f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,7 @@ dependencies = [ "psutil", "packaging", "testgres.os_ops>=3.3.3,<4.0.0", + "testgres.postgres_configuration>=0.5.0,<0.6.0", ] [project.urls] diff --git a/tests/helpers/pg_cfg_os_ops.py b/src/impl/pg_cfg_os_ops.py similarity index 100% rename from tests/helpers/pg_cfg_os_ops.py rename to src/impl/pg_cfg_os_ops.py diff --git a/src/node.py b/src/node.py index 5458e830..d13630e9 100644 --- a/src/node.py +++ b/src/node.py @@ -58,6 +58,7 @@ from .impl.port_manager__generic2 import PortManager__Generic2 from .impl.error_utils import ErrorUtils from .impl import internal_utils +from .impl import pg_cfg_os_ops from .logger import TestgresLogger @@ -77,6 +78,10 @@ from testgres.operations.os_ops import OsProcessController from testgres.operations.local_ops import LocalOperations +from testgres.postgres_configuration import PostgresConfiguration +from testgres.postgres_configuration import PostgresConfigurationReader +from testgres.postgres_configuration import PostgresConfigurationWriter + import ipaddress import logging import signal @@ -2438,9 +2443,9 @@ def pgbench_table_checksums( def set_auto_conf( self, - options, + options: typing.Dict[str, typing.Any], config='postgresql.auto.conf', - rm_options={}, + rm_options: typing.Optional[typing.Iterable[str]] = None, ): """ Update or remove configuration options in the specified configuration file, @@ -2460,56 +2465,47 @@ def set_auto_conf( assert isinstance(self._os_ops, OsOperations) # parse postgresql.auto.conf - path = self._os_ops.build_path(self.data_dir, config) - - lines = self._os_ops.readlines(path) - current_options = {} - current_directives = [] - for line in lines: - - # ignore comments - if line.startswith('#'): - continue - - if line.strip() == '': - continue + pgCfgOsOps = pg_cfg_os_ops.PgCfgOsOps( + self._os_ops, + "utf-8", + ) - if line.startswith('include'): - current_directives.append(line) - continue + pgCfg = PostgresConfiguration( + self.data_dir, + cfgOsOps=pgCfgOsOps, + strategy=PostgresConfiguration.Strategy.Standard, + ) - name, var = line.partition('=')[::2] - name = name.strip() + pgCfgFile = PostgresConfigurationReader.LoadConfigurationFile( + pgCfg, + config, + ) + assert pgCfgFile is not None - # Remove options specified in rm_options list - if name in rm_options: + if rm_options is None: + pass + else: + assert rm_options is not None + for optionName in rm_options: + assert type(optionName) is str + pgCfgFile.SetOptionValue(optionName, None) continue - current_options[name] = var - - for option in options: - assert type(option) is str - assert option != "" - assert option.strip() == option - - value = options[option] - valueType = type(value) + for optionName in options: + assert type(optionName) is str + assert optionName != "" + assert optionName.strip() == optionName - if valueType is str: - value = __class__._escape_config_value(value) - elif valueType is bool: - value = "on" if value else "off" + optionValue = options[optionName] - current_options[option] = value - - auto_conf = '' - for option in current_options: - auto_conf += option + " = " + str(current_options[option]) + "\n" - - for directive in current_directives: - auto_conf += directive + "\n" + pgCfgFile.SetOptionValue( + optionName, + optionValue, + ) + continue - self._os_ops.write(path, auto_conf, truncate=True) + PostgresConfigurationWriter.WriteConfiguration(pgCfg) + return def upgrade_from( self, diff --git a/tests/requirements.txt b/tests/requirements.txt index b85c6074..fbd5b4fa 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,4 +4,4 @@ pytest-xdist psutil psycopg2 testgres.os_ops>=3.3.3,<4.0.0 -testgres.postgres_configuration>=0.2.2,<1.0.0 +testgres.postgres_configuration>=0.5.0,<0.6.0 diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index f5ca3e7f..bae1ce64 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -6,7 +6,6 @@ from tests.helpers.global_data import PostgresNodeServices from tests.helpers.global_data import OsOperations from tests.helpers.global_data import PortManager -from tests.helpers.pg_cfg_os_ops import PgCfgOsOps from tests.helpers.pg_msg_builder import PgMsgBuilder from tests.conftest_helpers import TestServices @@ -26,6 +25,7 @@ from src.defaults import default_username from src.defaults import default_username2 from src.config import testgres_config as tconf +from src.impl.pg_cfg_os_ops import PgCfgOsOps from src import ProcessType from src import NodeStatus from src import IsolationLevel @@ -3183,6 +3183,53 @@ def helper__call_and_check_pgbench_table_checksums( )) return ok + def test_set_auto_conf( + self, + node_svc: PostgresNodeService, + ): + # elements contain [property id, value, storage value] + options = { + "autovacuum_max_workers": 3, + "max_connections": 123, + } + + with __class__.helper__get_node(node_svc) as node: + node.init() + + auto_conf_path = node.os_ops.build_path( + node.data_dir, + "postgresql.auto.conf", + ) + + node.set_auto_conf( + options, + config=auto_conf_path, + ) + content = node.os_ops.read( + auto_conf_path, + encoding="utf-8", + ) + assert type(content) is str + assert "autovacuum_max_workers = 3" in content + assert "max_connections = 123" in content + + node.set_auto_conf( + { + "max_connections": None, + }, + config=auto_conf_path, + rm_options=["autovacuum_max_workers"], + ) + content = node.os_ops.read( + auto_conf_path, + encoding="utf-8", + ) + assert type(content) is str + assert "autovacuum_max_workers" not in content + assert "max_connections" not in content + + return + class tag_rmdirs_protector: _os_ops: OsOperations _cwd: str