Skip to content

Commit 5e9ecbc

Browse files
Merge pull request #153 from postgrespro/fix-set-auto-conf
Fix set_auto_conf with single quotes
2 parents 655a386 + 1335e51 commit 5e9ecbc

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

testgres/node.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,23 +1626,31 @@ 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-
var = var.strip('"')
1631-
var = var.strip("'")
16321629

1633-
# remove options specified in rm_options list
1630+
# Remove options specified in rm_options list
16341631
if name in rm_options:
16351632
continue
16361633

16371634
current_options[name] = var
16381635

16391636
for option in options:
1640-
current_options[option] = options[option]
1637+
assert type(option) == str # noqa: E721
1638+
assert option != ""
1639+
assert option.strip() == option
1640+
1641+
value = options[option]
1642+
valueType = type(value)
1643+
1644+
if valueType == str:
1645+
value = __class__._escape_config_value(value)
1646+
elif valueType == bool:
1647+
value = "on" if value else "off"
1648+
1649+
current_options[option] = value
16411650

16421651
auto_conf = ''
16431652
for option in current_options:
1644-
auto_conf += "{0} = '{1}'\n".format(
1645-
option, current_options[option])
1653+
auto_conf += option + " = " + str(current_options[option]) + "\n"
16461654

16471655
for directive in current_directives:
16481656
auto_conf += directive + "\n"
@@ -1690,6 +1698,30 @@ def _get_bin_path(self, filename):
16901698
bin_path = get_bin_path(filename)
16911699
return bin_path
16921700

1701+
def _escape_config_value(value):
1702+
assert type(value) == str # noqa: E721
1703+
1704+
result = "'"
1705+
1706+
for ch in value:
1707+
if ch == "'":
1708+
result += "\\'"
1709+
elif ch == "\n":
1710+
result += "\\n"
1711+
elif ch == "\r":
1712+
result += "\\r"
1713+
elif ch == "\t":
1714+
result += "\\t"
1715+
elif ch == "\b":
1716+
result += "\\b"
1717+
elif ch == "\\":
1718+
result += "\\\\"
1719+
else:
1720+
result += ch
1721+
1722+
result += "'"
1723+
return result
1724+
16931725

16941726
class NodeApp:
16951727

tests/test_simple.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,52 @@ def test_simple_with_bin_dir(self):
10611061
except FileNotFoundError:
10621062
pass # Expected error
10631063

1064+
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+
1087+
with get_new_node() as node:
1088+
node.init().start()
1089+
1090+
options = {}
1091+
1092+
for x in testData:
1093+
options[x[0]] = x[1]
1094+
1095+
node.set_auto_conf(options)
1096+
node.stop()
1097+
node.slow_start()
1098+
1099+
auto_conf_path = f"{node.data_dir}/postgresql.auto.conf"
1100+
with open(auto_conf_path, "r") as f:
1101+
content = f.read()
1102+
1103+
for x in testData:
1104+
self.assertIn(
1105+
x[0] + " = " + x[2],
1106+
content,
1107+
x[0] + " stored wrong"
1108+
)
1109+
10641110

10651111
if __name__ == '__main__':
10661112
if os.environ.get('ALT_CONFIG'):

0 commit comments

Comments
 (0)