Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e76232f

Browse files
committed
Fix tests for pg_dump and pg_restore
2 parents 5e3c0f3 + 7bcb4f3 commit e76232f

11 files changed

+638
-475
lines changed

.gitignore

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
*.pyc
2-
dist
3-
tags
4-
*.egg-info/
52
*.egg
6-
Dockerfile
7-
.coverage
3+
*.egg-info/
4+
dist
5+
86
env/
97
venv/
108
build/
9+
10+
.coverage
1111
coverage.xml
12+
13+
Dockerfile
14+
15+
*~
1216
*.swp
17+
tags

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,9 @@ with testgres.get_new_node('master') as master:
154154
# start a new node
155155
master.init().start()
156156

157-
# initialize default database for TPC-B
158-
master.pgbench_run(options=['-i'])
159-
160-
# run benchmark for 20 seconds and show results
161-
print(master.pgbench_run(options=['-T', '20']))
157+
# initialize default DB and run bench for 10 seconds
158+
res = master.pgbench_init(scale=2).pgbench_run(time=10)
159+
print(res)
162160
```
163161

164162

testgres/backup.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
from six import raise_from
88

99
from .consts import \
10-
DATA_DIR as _DATA_DIR, \
11-
BACKUP_LOG_FILE as _BACKUP_LOG_FILE, \
12-
DEFAULT_XLOG_METHOD as _DEFAULT_XLOG_METHOD
10+
DATA_DIR, \
11+
PG_CONF_FILE, \
12+
BACKUP_LOG_FILE, \
13+
DEFAULT_XLOG_METHOD
1314

1415
from .exceptions import BackupException
1516

1617
from .utils import \
1718
get_bin_path, \
18-
default_username as _default_username, \
19-
execute_utility as _execute_utility
19+
default_username, \
20+
execute_utility
2021

2122

2223
class NodeBackup(object):
@@ -26,13 +27,13 @@ class NodeBackup(object):
2627

2728
@property
2829
def log_file(self):
29-
return os.path.join(self.base_dir, _BACKUP_LOG_FILE)
30+
return os.path.join(self.base_dir, BACKUP_LOG_FILE)
3031

3132
def __init__(self,
3233
node,
3334
base_dir=None,
3435
username=None,
35-
xlog_method=_DEFAULT_XLOG_METHOD):
36+
xlog_method=DEFAULT_XLOG_METHOD):
3637
"""
3738
Create a new backup.
3839
@@ -47,7 +48,7 @@ def __init__(self,
4748
raise BackupException('Node must be running')
4849

4950
# Set default arguments
50-
username = username or _default_username()
51+
username = username or default_username()
5152
base_dir = base_dir or tempfile.mkdtemp()
5253

5354
# public
@@ -58,7 +59,7 @@ def __init__(self,
5859
# private
5960
self._available = True
6061

61-
data_dir = os.path.join(self.base_dir, _DATA_DIR)
62+
data_dir = os.path.join(self.base_dir, DATA_DIR)
6263

6364
# yapf: disable
6465
_params = [
@@ -69,7 +70,7 @@ def __init__(self,
6970
"-D", data_dir,
7071
"-X", xlog_method
7172
]
72-
_execute_utility(_params, self.log_file)
73+
execute_utility(_params, self.log_file)
7374

7475
def __enter__(self):
7576
return self
@@ -97,8 +98,8 @@ def _prepare_dir(self, destroy):
9798
if available:
9899
dest_base_dir = tempfile.mkdtemp()
99100

100-
data1 = os.path.join(self.base_dir, _DATA_DIR)
101-
data2 = os.path.join(dest_base_dir, _DATA_DIR)
101+
data1 = os.path.join(self.base_dir, DATA_DIR)
102+
data2 = os.path.join(dest_base_dir, DATA_DIR)
102103

103104
try:
104105
# Copy backup to new data dir
@@ -139,8 +140,8 @@ def spawn_primary(self, name=None, destroy=True, use_logging=False):
139140
# New nodes should always remove dir tree
140141
node._should_rm_dirs = True
141142

142-
node.append_conf("postgresql.conf", "\n")
143-
node.append_conf("postgresql.conf", "port = {}".format(node.port))
143+
node.append_conf(PG_CONF_FILE, "\n")
144+
node.append_conf(PG_CONF_FILE, "port = {}".format(node.port))
144145

145146
return node
146147

testgres/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .utils import \
1717
get_bin_path, \
18-
execute_utility as _execute_utility
18+
execute_utility
1919

2020

2121
def cached_initdb(data_dir, initdb_logfile, initdb_params=[]):
@@ -26,7 +26,7 @@ def cached_initdb(data_dir, initdb_logfile, initdb_params=[]):
2626
def call_initdb(initdb_dir):
2727
try:
2828
_params = [get_bin_path("initdb"), "-D", initdb_dir, "-N"]
29-
_execute_utility(_params + initdb_params, initdb_logfile)
29+
execute_utility(_params + initdb_params, initdb_logfile)
3030
except ExecUtilException as e:
3131
raise_from(InitNodeException("Failed to run initdb"), e)
3232

testgres/connection.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
from enum import Enum
1313

1414
from .exceptions import QueryException
15-
from .utils import default_username as _default_username
15+
16+
from .utils import \
17+
default_dbname, \
18+
default_username
19+
1620

1721
# export these exceptions
1822
InternalError = pglib.InternalError
@@ -32,26 +36,34 @@ class NodeConnection(object):
3236
Transaction wrapper returned by Node
3337
"""
3438

35-
def __init__(self,
36-
parent_node,
37-
dbname,
38-
host="127.0.0.1",
39-
username=None,
40-
password=None):
39+
def __init__(self, node, dbname=None, username=None, password=None):
4140

42-
# Use default user if not specified
43-
username = username or _default_username()
41+
# Set default arguments
42+
dbname = dbname or default_dbname()
43+
username = username or default_username()
4444

45-
self.parent_node = parent_node
45+
self._node = node
4646

47-
self.connection = pglib.connect(
47+
self._connection = pglib.connect(
4848
database=dbname,
4949
user=username,
50-
port=parent_node.port,
51-
host=host,
52-
password=password)
50+
password=password,
51+
host=node.host,
52+
port=node.port)
53+
54+
self._cursor = self.connection.cursor()
55+
56+
@property
57+
def node(self):
58+
return self._node
59+
60+
@property
61+
def connection(self):
62+
return self._connection
5363

54-
self.cursor = self.connection.cursor()
64+
@property
65+
def cursor(self):
66+
return self._cursor
5567

5668
def __enter__(self):
5769
return self
@@ -73,7 +85,7 @@ def begin(self, isolation_level=IsolationLevel.ReadCommitted):
7385

7486
# Get index of isolation level
7587
level_idx = isolation_level.value
76-
assert(level_idx in range(4))
88+
assert level_idx in range(4)
7789

7890
# Replace isolation level with its name
7991
isolation_level = levels[level_idx]

testgres/consts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
DATA_DIR = "data"
55
LOGS_DIR = "logs"
66

7+
RECOVERY_CONF_FILE = "recovery.conf"
8+
PG_CONF_FILE = "postgresql.conf"
9+
HBA_CONF_FILE = "pg_hba.conf"
10+
711
# names for log files
812
PG_LOG_FILE = "postgresql.log"
913
UTILS_LOG_FILE = "utils.log"
1014
BACKUP_LOG_FILE = "backup.log"
1115

12-
# default argument value
16+
# default argument values
1317
DEFAULT_XLOG_METHOD = "fetch"
18+
DEFAULT_DUMP_FORMAT = "plain"

testgres/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestgresLogger(threading.Thread):
1010
"""
11-
Helper class to implement reading from postgresql.log
11+
Helper class to implement reading from log files.
1212
"""
1313

1414
def __init__(self, node_name, log_file_name):

0 commit comments

Comments
 (0)