Skip to content

Commit a4092af

Browse files
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.
1 parent 2c26deb commit a4092af

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

testgres/node.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,11 +1626,6 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):
16261626

16271627
name, var = line.partition('=')[::2]
16281628
name = name.strip()
1629-
var = var.strip()
1630-
1631-
# Handle quoted values and remove escaping
1632-
if var.startswith("'") and var.endswith("'"):
1633-
var = var[1:-1].replace("''", "'")
16341629

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

16411636
for option in options:
16421637
value = options[option]
1643-
if isinstance(value, str):
1644-
value = value.replace("'", "\\'")
1638+
valueType = type(value)
1639+
1640+
if valueType == str:
1641+
value = __class__._escape_config_value(value)
1642+
elif valueType == bool:
1643+
value = "on" if value else "off"
1644+
16451645
current_options[option] = value
16461646

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

16521651
for directive in current_directives:
16531652
auto_conf += directive + "\n"
@@ -1695,6 +1694,28 @@ def _get_bin_path(self, filename):
16951694
bin_path = get_bin_path(filename)
16961695
return bin_path
16971696

1697+
def _escape_config_value(value):
1698+
result = "'"
1699+
1700+
for ch in value:
1701+
if (ch == "'"):
1702+
result += "\\'"
1703+
elif (ch == "\n"):
1704+
result += "\\n"
1705+
elif (ch == "\r"):
1706+
result += "\\r"
1707+
elif (ch == "\t"):
1708+
result += "\\t"
1709+
elif (ch == "\b"):
1710+
result += "\\b"
1711+
elif (ch == "\\"):
1712+
result += "\\\\"
1713+
else:
1714+
result += ch
1715+
1716+
result += "'"
1717+
return result
1718+
16981719

16991720
class NodeApp:
17001721

tests/test_simple.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,13 +1062,35 @@ def test_simple_with_bin_dir(self):
10621062
pass # Expected error
10631063

10641064
def test_set_auto_conf(self):
1065+
# elements contain [property id, value, storage value]
1066+
testData = [
1067+
["archive_command",
1068+
"cp '%p' \"/mnt/server/archivedir/%f\"",
1069+
"'cp \\'%p\\' \"/mnt/server/archivedir/%f\""],
1070+
["restore_command",
1071+
'cp "/mnt/server/archivedir/%f" \'%p\'',
1072+
"'cp \"/mnt/server/archivedir/%f\" \\'%p\\''"],
1073+
["log_line_prefix",
1074+
"'\n\r\t\b\\\"",
1075+
"'\\\'\\n\\r\\t\\b\\\\\""],
1076+
["log_connections",
1077+
True,
1078+
"on"],
1079+
["log_disconnections",
1080+
False,
1081+
"off"],
1082+
["autovacuum_max_workers",
1083+
3,
1084+
"3"]
1085+
]
1086+
10651087
with get_new_node() as node:
10661088
node.init().start()
10671089

1068-
options = {
1069-
"archive_command": "cp '%p' \"/mnt/server/archivedir/%f\"",
1070-
'restore_command': 'cp "/mnt/server/archivedir/%f" \'%p\'',
1071-
}
1090+
options = {}
1091+
1092+
for x in testData:
1093+
options[x[0]] = x[1]
10721094

10731095
node.set_auto_conf(options)
10741096
node.stop()
@@ -1077,16 +1099,13 @@ def test_set_auto_conf(self):
10771099
auto_conf_path = f"{node.data_dir}/postgresql.auto.conf"
10781100
with open(auto_conf_path, "r") as f:
10791101
content = f.read()
1080-
self.assertIn(
1081-
"archive_command = 'cp \\'%p\\' \"/mnt/server/archivedir/%f\"",
1082-
content,
1083-
"archive_command stored wrong"
1084-
)
1085-
self.assertIn(
1086-
"restore_command = 'cp \"/mnt/server/archivedir/%f\" \\'%p\\''",
1087-
content,
1088-
"restore_command stored wrong"
1089-
)
1102+
1103+
for x in testData:
1104+
self.assertIn(
1105+
x[0] + " = " + x[2],
1106+
content,
1107+
x[0] + " stored wrong"
1108+
)
10901109

10911110

10921111
if __name__ == '__main__':

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy