Skip to content

Fix set_auto_conf with single quotes #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 4, 2024
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
46 changes: 39 additions & 7 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,23 +1626,31 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):

name, var = line.partition('=')[::2]
name = name.strip()
var = var.strip()
var = var.strip('"')
var = var.strip("'")

# remove options specified in rm_options list
# Remove options specified in rm_options list
if name in rm_options:
continue

current_options[name] = var

for option in options:
current_options[option] = options[option]
assert type(option) == str # noqa: E721
assert option != ""
assert option.strip() == option

value = options[option]
valueType = type(value)

if valueType == str:
value = __class__._escape_config_value(value)
elif valueType == bool:
value = "on" if value else "off"

current_options[option] = value

auto_conf = ''
for option in current_options:
auto_conf += "{0} = '{1}'\n".format(
option, current_options[option])
auto_conf += option + " = " + str(current_options[option]) + "\n"

for directive in current_directives:
auto_conf += directive + "\n"
Expand Down Expand Up @@ -1690,6 +1698,30 @@ def _get_bin_path(self, filename):
bin_path = get_bin_path(filename)
return bin_path

def _escape_config_value(value):
assert type(value) == str # noqa: E721

result = "'"

for ch in value:
if ch == "'":
result += "\\'"
elif ch == "\n":
result += "\\n"
elif ch == "\r":
result += "\\r"
elif ch == "\t":
result += "\\t"
elif ch == "\b":
result += "\\b"
elif ch == "\\":
result += "\\\\"
else:
result += ch

result += "'"
return result


class NodeApp:

Expand Down
46 changes: 46 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,52 @@ def test_simple_with_bin_dir(self):
except FileNotFoundError:
pass # Expected error

def test_set_auto_conf(self):
# elements contain [property id, value, storage value]
testData = [
["archive_command",
"cp '%p' \"/mnt/server/archivedir/%f\"",
"'cp \\'%p\\' \"/mnt/server/archivedir/%f\""],
["restore_command",
'cp "/mnt/server/archivedir/%f" \'%p\'',
"'cp \"/mnt/server/archivedir/%f\" \\'%p\\''"],
["log_line_prefix",
"'\n\r\t\b\\\"",
"'\\\'\\n\\r\\t\\b\\\\\""],
["log_connections",
True,
"on"],
["log_disconnections",
False,
"off"],
["autovacuum_max_workers",
3,
"3"]
]

with get_new_node() as node:
node.init().start()

options = {}

for x in testData:
options[x[0]] = x[1]

node.set_auto_conf(options)
node.stop()
node.slow_start()

auto_conf_path = f"{node.data_dir}/postgresql.auto.conf"
with open(auto_conf_path, "r") as f:
content = f.read()

for x in testData:
self.assertIn(
x[0] + " = " + x[2],
content,
x[0] + " stored wrong"
)


if __name__ == '__main__':
if os.environ.get('ALT_CONFIG'):
Expand Down