Skip to content

Commit 1c90723

Browse files
committed
fix rm files on cleanup()
1 parent bd428b0 commit 1c90723

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

testgres/testgres.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,11 @@ def __init__(self,
292292
if not node.status():
293293
raise BackupException('Node must be running')
294294

295-
# set default arguments
295+
# Set default arguments
296296
username = username or default_username()
297297
base_dir = base_dir or tempfile.mkdtemp()
298298

299-
# create directory if needed
299+
# Create directory if needed
300300
if base_dir and not os.path.exists(base_dir):
301301
os.makedirs(base_dir)
302302

@@ -350,7 +350,7 @@ def _prepare_dir(self, destroy):
350350
else:
351351
base_dir = self.base_dir
352352

353-
# update value
353+
# Update value
354354
self.available = available
355355

356356
return base_dir
@@ -370,12 +370,15 @@ def spawn_primary(self, name, destroy=True, use_logging=False):
370370

371371
base_dir = self._prepare_dir(destroy)
372372

373-
# build a new PostgresNode
373+
# Build a new PostgresNode
374374
node = PostgresNode(name=name,
375375
base_dir=base_dir,
376376
master=self.original_node,
377377
use_logging=use_logging)
378378

379+
# New nodes should always remove dir tree
380+
node.should_rm_dirs = True
381+
379382
node.append_conf("postgresql.conf", "\n")
380383
node.append_conf("postgresql.conf", "port = {}".format(node.port))
381384

@@ -1149,13 +1152,14 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11491152
util: utility to be executed (str).
11501153
args: arguments for utility (list).
11511154
logfile: stores stdout and stderr (str).
1155+
write_to_pipe: do we care about stdout?
11521156
11531157
Returns:
11541158
stdout of executed utility.
11551159
"""
11561160

1157-
with open(logfile, "a") as file_out, \
1158-
open(os.devnull, "w") as devnull: # hack for 2.7
1161+
# we can't use subprocess.DEVNULL on 2.7
1162+
with open(os.devnull, "w") as devnull:
11591163

11601164
# choose file according to options
11611165
stdout_file = subprocess.PIPE if write_to_pipe else devnull
@@ -1169,10 +1173,16 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11691173
out, _ = process.communicate()
11701174
out = '' if not out else out.decode('utf-8')
11711175

1172-
# write new log entry
1173-
file_out.write(''.join(map(lambda x: str(x) + ' ', [util] + args)))
1174-
file_out.write('\n')
1175-
file_out.write(out)
1176+
# write new log entry if possible
1177+
try:
1178+
with open(logfile, "a") as file_out:
1179+
# write util name + args
1180+
file_out.write(''.join(map(lambda x: str(x) + ' ',
1181+
[util] + args)))
1182+
file_out.write('\n')
1183+
file_out.write(out)
1184+
except FileNotFoundError:
1185+
pass
11761186

11771187
if process.returncode:
11781188
error_text = (

tests/test_simple.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
#!/usr/bin/env python
22

3-
import unittest
3+
import os
44
import re
55
import six
6-
import tempfile
7-
import logging.config
86
import subprocess
7+
import tempfile
98
import testgres
9+
import unittest
10+
11+
import logging.config
1012

1113
from distutils.version import LooseVersion
1214

13-
from testgres import InitNodeException, \
14-
StartNodeException, ExecUtilException, \
15-
BackupException, QueryException, CatchUpException
15+
from testgres import \
16+
InitNodeException, \
17+
StartNodeException, \
18+
ExecUtilException, \
19+
BackupException, \
20+
QueryException, \
21+
CatchUpException
1622

1723
from testgres import get_new_node, get_pg_config, configure_testgres
1824
from testgres import bound_ports
@@ -266,6 +272,7 @@ def test_dump(self):
266272

267273
# take a new dump
268274
dump = node1.dump('postgres')
275+
self.assertTrue(os.path.isfile(dump))
269276

270277
with get_new_node('node2') as node2:
271278
node2.init().start().restore('postgres', dump)
@@ -274,6 +281,9 @@ def test_dump(self):
274281
'select * from test order by val asc')
275282
self.assertListEqual(res, [(1, ), (2, )])
276283

284+
# finally, remove dump
285+
os.remove(dump)
286+
277287
def test_users(self):
278288
with get_new_node('master') as node:
279289
node.init().start()

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