Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
File renamed without changes.
84 changes: 40 additions & 44 deletions src/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 48 additions & 1 deletion tests/test_testgres_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading