Skip to content

Commit f9b6bdb

Browse files
author
v.shepard
committed
PBCKP-152
1 parent 2bc17f0 commit f9b6bdb

File tree

11 files changed

+697
-424
lines changed

11 files changed

+697
-424
lines changed

testgres/backup.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44

5-
from shutil import rmtree, copytree
65
from six import raise_from
76

87
from .enums import XLogMethod
@@ -14,8 +13,6 @@
1413
PG_CONF_FILE, \
1514
BACKUP_LOG_FILE
1615

17-
from .defaults import default_username
18-
1916
from .exceptions import BackupException
2017

2118
from .utils import \
@@ -80,7 +77,7 @@ def __init__(self,
8077
"-D", data_dir,
8178
"-X", xlog_method.value
8279
] # yapf: disable
83-
execute_utility(_params, self.log_file, hostname=node.hostname, ssh_key=node.ssh_key)
80+
execute_utility(_params, self.log_file, self.os_ops)
8481

8582
def __enter__(self):
8683
return self
@@ -113,7 +110,7 @@ def _prepare_dir(self, destroy):
113110

114111
try:
115112
# Copy backup to new data dir
116-
copytree(data1, data2)
113+
self.os_ops.copytree(data1, data2)
117114
except Exception as e:
118115
raise_from(BackupException('Failed to copy files'), e)
119116
else:

testgres/cache.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from six import raise_from
66

7-
from .os_ops import OsOperations
7+
from .op_ops.local_ops import LocalOperations
8+
from .op_ops.os_ops import OsOperations
89
from .config import testgres_config
910

1011
from .consts import XLOG_CONTROL_FILE
@@ -20,16 +21,15 @@
2021
execute_utility
2122

2223

23-
def cached_initdb(data_dir, logfile=None, hostname='localhost', ssh_key=None, params=None):
24+
def cached_initdb(data_dir, logfile=None, params=None, os_ops: OsOperations = LocalOperations()):
2425
"""
2526
Perform initdb or use cached node files.
2627
"""
27-
os_ops = OsOperations(hostname=hostname, ssh_key=ssh_key)
2828

2929
def call_initdb(initdb_dir, log=None):
3030
try:
3131
_params = [get_bin_path("initdb"), "-D", initdb_dir, "-N"]
32-
execute_utility(_params + (params or []), log, hostname=hostname, ssh_key=ssh_key)
32+
execute_utility(_params + (params or []), log, os_ops)
3333
except ExecUtilException as e:
3434
raise_from(InitNodeException("Failed to run initdb"), e)
3535

@@ -42,7 +42,7 @@ def call_initdb(initdb_dir, log=None):
4242
# Initialize cached initdb
4343

4444
if not os_ops.path_exists(cached_data_dir) or \
45-
not os_ops.listdir(cached_data_dir):
45+
not os_ops.listdir(cached_data_dir):
4646
call_initdb(cached_data_dir)
4747

4848
try:
@@ -60,7 +60,7 @@ def call_initdb(initdb_dir, log=None):
6060

6161
# XXX: build new WAL segment with our system id
6262
_params = [get_bin_path("pg_resetwal"), "-D", data_dir, "-f"]
63-
execute_utility(_params, logfile, hostname=hostname, ssh_key=ssh_key)
63+
execute_utility(_params, logfile, os_ops)
6464

6565
except ExecUtilException as e:
6666
msg = "Failed to reset WAL for system id"

testgres/config.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from contextlib import contextmanager
88
from shutil import rmtree
9-
from tempfile import mkdtemp
109

10+
from .op_ops.local_ops import LocalOperations
1111
from .consts import TMP_CACHE
1212

1313

@@ -137,12 +137,9 @@ def copy(self):
137137

138138

139139
@atexit.register
140-
def _rm_cached_initdb_dirs(os_ops=None):
140+
def _rm_cached_initdb_dirs(os_ops=LocalOperations()):
141141
for d in cached_initdb_dirs:
142-
if os_ops:
143-
os_ops.rmtree(d, ignore_errors=True)
144-
else:
145-
rmtree(d, ignore_errors=True)
142+
os_ops.rmdirs(d, ignore_errors=True)
146143

147144

148145
def push_config(**options):
@@ -205,4 +202,4 @@ def configure_testgres(**options):
205202

206203

207204
# NOTE: assign initial cached dir for initdb
208-
testgres_config.cached_initdb_dir = mkdtemp(prefix=TMP_CACHE)
205+
testgres_config.cached_initdb_dir = testgres_config.os_ops.mkdtemp(prefix=TMP_CACHE)

testgres/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self,
3737

3838
# Set default arguments
3939
dbname = dbname or default_dbname()
40-
username = username or default_username()
40+
username = username or default_username(node.os_ops)
4141

4242
self._node = node
4343

testgres/defaults.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import datetime
2-
import getpass
32
import os
43
import struct
54
import uuid
65

6+
from .op_ops.local_ops import LocalOperations
7+
78

89
def default_dbname():
910
"""
@@ -13,15 +14,11 @@ def default_dbname():
1314
return 'postgres'
1415

1516

16-
def default_username(os_ops=None):
17+
def default_username(os_ops=LocalOperations()):
1718
"""
1819
Return default username (current user).
1920
"""
20-
if os_ops:
21-
user = os_ops.get_user()
22-
else:
23-
user = getpass.getuser()
24-
return user
21+
return os_ops.get_user()
2522

2623

2724
def generate_app_name():
@@ -32,7 +29,7 @@ def generate_app_name():
3229
return 'testgres-{}'.format(str(uuid.uuid4()))
3330

3431

35-
def generate_system_id(os_ops=None):
32+
def generate_system_id(os_ops=LocalOperations()):
3633
"""
3734
Generate a new 64-bit unique system identifier for node.
3835
"""
@@ -47,10 +44,7 @@ def generate_system_id(os_ops=None):
4744
system_id = 0
4845
system_id |= (secs << 32)
4946
system_id |= (usecs << 12)
50-
if os_ops:
51-
system_id |= (os_ops.get_pid() & 0xFFF)
52-
else:
53-
system_id |= (os.getpid() & 0xFFF)
47+
system_id |= (os_ops.get_pid() & 0xFFF)
5448

5549
# pack ULL in native byte order
5650
return struct.pack('=Q', system_id)

0 commit comments

Comments
 (0)