Skip to content

Commit 2b5d7ea

Browse files
committed
add unix_sockets=True to init() and default_conf()
1 parent 9648aa2 commit 2b5d7ea

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

testgres/backup.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def __init__(self,
3232
base_dir=None,
3333
username=None,
3434
xlog_method=_DEFAULT_XLOG_METHOD):
35+
"""
36+
Create a new backup.
37+
38+
Args:
39+
node: PostgresNode we're going to backup.
40+
base_dir: where should we store it?
41+
username: database user name.
42+
xlog_method: none | fetch | stream (see docs)
43+
"""
3544

3645
if not node.status():
3746
raise BackupException('Node must be running')
@@ -43,6 +52,7 @@ def __init__(self,
4352
# public
4453
self.original_node = node
4554
self.base_dir = base_dir
55+
self.username = username
4656

4757
# private
4858
self._available = True
@@ -148,7 +158,8 @@ def spawn_replica(self, name, destroy=True, use_logging=False):
148158
"""
149159

150160
node = self.spawn_primary(name, destroy, use_logging=use_logging)
151-
node._create_recovery_conf(self.original_node)
161+
node._create_recovery_conf(username=self.username,
162+
master=self.original_node)
152163

153164
return node
154165

testgres/node.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,20 @@ def utils_log_name(self):
112112
def pg_log_name(self):
113113
return os.path.join(self.data_dir, _PG_LOG_FILE)
114114

115-
@property
116-
def connstr(self):
117-
return "port={}".format(self.port)
115+
def _create_recovery_conf(self, username, master):
116+
# yapf: disable
117+
conninfo = (
118+
u"user={} "
119+
u"port={} "
120+
u"host={} "
121+
u"application_name={}"
122+
).format(username, master.port, master.host, master.name)
118123

119-
def _create_recovery_conf(self, root_node):
120-
line = ("primary_conninfo='{} application_name={}'\n"
121-
"standby_mode=on\n").format(root_node.connstr, self.name)
124+
# yapf: disable
125+
line = (
126+
"primary_conninfo='{}'\n"
127+
"standby_mode=on\n"
128+
).format(conninfo)
122129

123130
self.append_conf("recovery.conf", line)
124131

@@ -175,13 +182,18 @@ def print_node_file(node_file):
175182

176183
return error_text
177184

178-
def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
185+
def init(self,
186+
fsync=False,
187+
unix_sockets=True,
188+
allow_streaming=False,
189+
initdb_params=[]):
179190
"""
180191
Perform initdb for this node.
181192
182193
Args:
183-
allow_streaming: should this node add a hba entry for replication?
184194
fsync: should this node use fsync to keep data safe?
195+
unix_sockets: should we enable UNIX sockets?
196+
allow_streaming: should this node add a hba entry for replication?
185197
initdb_params: parameters for initdb (list).
186198
187199
Returns:
@@ -196,22 +208,25 @@ def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
196208
_cached_initdb(self.data_dir, initdb_log, initdb_params)
197209

198210
# initialize default config files
199-
self.default_conf(allow_streaming=allow_streaming, fsync=fsync)
211+
self.default_conf(fsync=fsync,
212+
unix_sockets=unix_sockets,
213+
allow_streaming=allow_streaming)
200214

201215
return self
202216

203217
def default_conf(self,
204-
allow_streaming=False,
205218
fsync=False,
219+
unix_sockets=True,
220+
allow_streaming=False,
206221
log_statement='all'):
207222
"""
208223
Apply default settings to this node.
209224
210225
Args:
211-
allow_streaming: should this node add a hba entry for replication?
212226
fsync: should this node use fsync to keep data safe?
213-
log_statement: one of ('all', 'off', 'mod', 'ddl'), look at
214-
PostgreSQL docs for more information
227+
unix_sockets: should we enable UNIX sockets?
228+
allow_streaming: should this node add a hba entry for replication?
229+
log_statement: one of ('all', 'off', 'mod', 'ddl').
215230
216231
Returns:
217232
This instance of PostgresNode.
@@ -258,6 +273,9 @@ def get_auth_method(t):
258273

259274
# overwrite postgresql.conf file
260275
with io.open(postgres_conf, "w") as conf:
276+
# remove old lines
277+
conf.truncate()
278+
261279
if not fsync:
262280
conf.write(u"fsync = off\n")
263281

@@ -287,6 +305,10 @@ def get_auth_method(t):
287305
wal_keep_segments,
288306
wal_level))
289307

308+
# disable UNIX sockets if asked to
309+
if not unix_sockets:
310+
conf.write(u"unix_socket_directories = ''\n")
311+
290312
return self
291313

292314
def append_conf(self, filename, string):
@@ -784,7 +806,7 @@ def replicate(self,
784806
"""
785807

786808
backup = self.backup(username=username, xlog_method=xlog_method)
787-
return backup.spawn_replica(name, use_logging=use_logging)
809+
return backup.spawn_replica(name=name, use_logging=use_logging)
788810

789811
def catchup(self, dbname='postgres', username=None):
790812
"""

tests/test_simple.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,17 @@ def test_config(self):
590590
# restore setting
591591
configure_testgres(cache_pg_config=True)
592592

593+
def test_unix_sockets(self):
594+
with get_new_node('node').init(unix_sockets=False) as node:
595+
node.start()
596+
597+
node.execute('postgres', 'select 1')
598+
node.safe_psql('postgres', 'select 1')
599+
600+
r = node.replicate('r').start()
601+
r.execute('postgres', 'select 1')
602+
r.safe_psql('postgres', 'select 1')
603+
593604
def test_isolation_levels(self):
594605
with get_new_node('node').init().start() as node:
595606
with node.connect() as con:

0 commit comments

Comments
 (0)