8000 Fix set_auto_conf with single quotes by demonolock · Pull Request #153 · postgrespro/testgres · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
Node.set_auto_conf is improved
- we do not touch existing values
- escaping of '\n', '\r', '\t', '\b' and '\\' is added
- translation of bool into 'on|off' is added

test_set_auto_conf is updated.
  • Loading branch information
dmitry-lipetsk committed Dec 3, 2024
commit a4092af44fae5a1a92e9c9fbfec449fd99c8e520
39 changes: 30 additions & 9 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,11 +1626,6 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):

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

# Handle quoted values and remove escaping
if var.startswith("'") and var.endswith("'"):
var = var[1:-1].replace("''", "'")

# Remove options specified in rm_options list
if name in rm_options:
Expand All @@ -1640,14 +1635,18 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):

for option in options:
value = options[option]
if isinstance(value, str):
value = value.replace("'", "\\'")
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 @@ -1695,6 +1694,28 @@ def _get_bin_path(self, filename):
bin_path = get_bin_path(filename)
return bin_path

def _escape_config_value(value):
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
47 changes: 33 additions & 14 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,13 +1062,35 @@ def test_simple_with_bin_dir(self):
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 = {
"archive_command": "cp '%p' \"/mnt/server/archivedir/%f\"",
'restore_command': 'cp "/mnt/server/archivedir/%f" \'%p\'',
}
options = {}

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

node.set_auto_conf(options)
node.stop()
Expand All @@ -1077,16 +1099,13 @@ def test_set_auto_conf(self):
auto_conf_path = f"{node.data_dir}/postgresql.auto.conf"
with open(auto_conf_path, "r") as f:
content = f.read()
self.assertIn(
"archive_command = 'cp \\'%p\\' \"/mnt/server/archivedir/%f\"",
content,
"archive_command stored wrong"
)
self.assertIn(
"restore_command = 'cp \"/mnt/server/archivedir/%f\" \\'%p\\''",
content,
"restore_command stored wrong"
)

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


if __name__ == '__main__':
Expand Down
0