11# coding: utf-8
22
3- import io
43import os
54import random
6- import shutil
75import signal
86import threading
97from queue import Queue
108
119import psutil
12- import subprocess
1310import time
1411
15- from op_ops .local_ops import LocalOperations
16- from op_ops .os_ops import OsOperations
17- from op_ops .remote_ops import RemoteOperations
12+ from .os_ops .local_ops import LocalOperations
13+ from .os_ops .remote_ops import RemoteOperations
1814
1915try :
2016from collections .abc import Iterable
3228
3329from shutil import rmtree
3430from six import raise_from ,iteritems ,text_type
35- from tempfile import mkstemp ,mkdtemp
3631
3732from .enums import \
3833NodeStatus , \
9691eprint , \
9792get_bin_path , \
9893get_pg_version , \
99- file_tail , \
10094reserve_port , \
10195release_port , \
10296execute_utility , \
@@ -163,6 +157,7 @@ def __init__(self, name=None, port=None, base_dir=None,
163157else :
164158self .os_ops = RemoteOperations (host ,hostname ,ssh_key )
165159
160+ testgres_config .os_ops = self .os_ops
166161# defaults for __exit__()
167162self .cleanup_on_good_exit = testgres_config .node_cleanup_on_good_exit
168163self .cleanup_on_bad_exit = testgres_config .node_cleanup_on_bad_exit
@@ -289,7 +284,7 @@ def base_dir(self):
289284self ._base_dir = self .os_ops .mkdtemp (prefix = TMP_NODE )
290285
291286# NOTE: it's safe to create a new dir
292- if not self .os_ops .exists (self ._base_dir ):
287+ if not self .os_ops .path_exists (self ._base_dir ):
293288self .os_ops .makedirs (self ._base_dir )
294289
295290return self ._base_dir
@@ -299,7 +294,7 @@ def logs_dir(self):
299294path = os .path .join (self .base_dir ,LOGS_DIR )
300295
301296# NOTE: it's safe to create a new dir
302- if not self .os_ops .exists (path ):
297+ if not self .os_ops .path_exists (path ):
303298self .os_ops .makedirs (path )
304299
305300return path
@@ -628,7 +623,7 @@ def status(self):
628623"-D" ,self .data_dir ,
629624"status"
630625 ]# yapf: disable
631- execute_utility (_params ,self .utils_log_file )
626+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
632627return NodeStatus .Running
633628
634629except ExecUtilException as e :
@@ -650,7 +645,7 @@ def get_control_data(self):
650645_params += ["-D" ]if self ._pg_version >= PgVer ('9.5' )else []
651646_params += [self .data_dir ]
652647
653- data = execute_utility (_params ,self .utils_log_file )
648+ data = execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
654649
655650out_dict = {}
656651
@@ -713,7 +708,7 @@ def start(self, params=[], wait=True):
713708 ]+ params # yapf: disable
714709
715710try :
716- execute_utility (_params ,self .utils_log_file )
711+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
717712except ExecUtilException as e :
718713msg = 'Cannot start node'
719714files = self ._collect_special_files ()
@@ -744,7 +739,7 @@ def stop(self, params=[], wait=True):
744739"stop"
745740 ]+ params # yapf: disable
746741
747- execute_utility (_params ,self .utils_log_file )
742+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
748743
749744self ._maybe_stop_logger ()
750745self .is_started = False
@@ -786,7 +781,7 @@ def restart(self, params=[]):
786781 ]+ params # yapf: disable
787782
788783try :
789- execute_utility (_params ,self .utils_log_file )
784+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
790785except ExecUtilException as e :
791786msg = 'Cannot restart node'
792787files = self ._collect_special_files ()
@@ -813,7 +808,7 @@ def reload(self, params=[]):
813808"reload"
814809 ]+ params # yapf: disable
815810
816- execute_utility (_params ,self .utils_log_file )
811+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
817812
818813return self
819814
@@ -835,7 +830,7 @@ def promote(self, dbname=None, username=None):
835830"promote"
836831 ]# yapf: disable
837832
838- execute_utility (_params ,self .utils_log_file )
833+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
839834
840835# for versions below 10 `promote` is asynchronous so we need to wait
841836# until it actually becomes writable
@@ -870,7 +865,7 @@ def pg_ctl(self, params):
870865"-w" # wait
871866 ]+ params # yapf: disable
872867
873- return execute_utility (_params ,self .utils_log_file )
868+ return execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
874869
875870def free_port (self ):
876871"""
@@ -1035,10 +1030,9 @@ def dump(self,
10351030# Generate tmpfile or tmpdir
10361031def tmpfile ():
10371032if format == DumpFormat .Directory :
1038- fname = mkdtemp (prefix = TMP_DUMP )
1033+ fname = self . os_ops . mkdtemp (prefix = TMP_DUMP )
10391034else :
1040- fd ,fname = mkstemp (prefix = TMP_DUMP )
1041- os .close (fd )
1035+ fname = self .os_ops .mkstemp (prefix = TMP_DUMP )
10421036return fname
10431037
10441038# Set default arguments
@@ -1056,7 +1050,7 @@ def tmpfile():
10561050"-F" ,format .value
10571051 ]# yapf: disable
10581052
1059- execute_utility (_params ,self .utils_log_file )
1053+ execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
10601054
10611055return filename
10621056
@@ -1085,7 +1079,7 @@ def restore(self, filename, dbname=None, username=None):
10851079
10861080# try pg_restore if dump is binary formate, and psql if not
10871081try :
1088- execute_utility (_params ,self .utils_log_name )
1082+ execute_utility (_params ,self .utils_log_name , os_ops = self . os_ops )
10891083except ExecUtilException :
10901084self .psql (filename = filename ,dbname = dbname ,username = username )
10911085
@@ -1417,7 +1411,7 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
14171411# should be the last one
14181412_params .append (dbname )
14191413
1420- return execute_utility (_params ,self .utils_log_file )
1414+ return execute_utility (_params ,self .utils_log_file , os_ops = self . os_ops )
14211415
14221416def connect (self ,
14231417dbname = None ,