Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf6bfe17

Browse files
author
v.shepard
committed
PBCKP-152 fix failed tests
1 parentac77ef7 commitf6bfe17

12 files changed

+182
-75
lines changed

‎testgres/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
First, \
4747
Any
4848

49+
importoperations
50+
from .operations.os_opsimportOsOperations
51+
from .operations.local_opsimportLocalOperations
52+
from .operations.remote_opsimportRemoteOperations
53+
4954
__all__= [
5055
"get_new_node",
5156
"NodeBackup",
@@ -56,4 +61,5 @@
5661
"PostgresNode","NodeApp",
5762
"reserve_port","release_port","bound_ports","get_bin_path","get_pg_config","get_pg_version",
5863
"First","Any",
64+
"OsOperations","LocalOperations","RemoteOperations","operations"
5965
]

‎testgres/cache.py

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

55
fromsiximportraise_from
66

7-
from .os_ops.local_opsimportLocalOperations
8-
from .os_ops.os_opsimportOsOperations
97
from .configimporttestgres_config
108

119
from .constsimportXLOG_CONTROL_FILE
@@ -20,6 +18,9 @@
2018
get_bin_path, \
2119
execute_utility
2220

21+
from .operations.local_opsimportLocalOperations
22+
from .operations.os_opsimportOsOperations
23+
2324

2425
defcached_initdb(data_dir,logfile=None,params=None,os_ops:OsOperations=LocalOperations()):
2526
"""
@@ -38,7 +39,7 @@ def call_initdb(initdb_dir, log=None):
3839
call_initdb(data_dir,logfile)
3940
else:
4041
# Fetch cached initdb dir
41-
cached_data_dir=testgres_config.cached_initdb_dir()
42+
cached_data_dir=testgres_config.cached_initdb_dir
4243

4344
# Initialize cached initdb
4445

‎testgres/config.py

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

77
fromcontextlibimportcontextmanager
88

9-
from .os_ops.local_opsimportLocalOperations
109
from .constsimportTMP_CACHE
10+
from .operations.local_opsimportLocalOperations
1111

1212

1313
classGlobalConfig(object):

‎testgres/connection.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,15 @@ def rollback(self):
102102
returnself
103103

104104
defexecute(self,query,*args):
105+
self.cursor.execute(query,args)
105106
try:
106-
withself.connection.cursor()ascursor:
107-
cursor.execute(query,args)
108-
try:
109-
res=cursor.fetchall()
110-
111-
# pg8000 might return tuples
112-
ifisinstance(res,tuple):
113-
res= [tuple(t)fortinres]
114-
115-
returnres
116-
except (pglib.ProgrammingError,pglib.InternalError)ase:
117-
# An error occurred while trying to fetch results (e.g., no results to fetch)
118-
print(f"Error fetching results:{e}")
119-
returnNone
120-
except (pglib.Error,Exception)ase:
121-
# Handle other database errors
107+
res=self.cursor.fetchall()
108+
# pg8000 might return tuples
109+
ifisinstance(res,tuple):
110+
res= [tuple(t)fortinres]
111+
112+
returnres
113+
exceptExceptionase:
122114
print(f"Error executing query:{e}")
123115
returnNone
124116

‎testgres/defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
importstruct
33
importuuid
44

5-
from .os_ops.local_opsimportLocalOperations
5+
from .operations.local_opsimportLocalOperations
66

77

88
defdefault_dbname():

‎testgres/node.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
importpsutil
1010
importtime
1111

12-
from .os_ops.local_opsimportLocalOperations
13-
from .os_ops.remote_opsimportRemoteOperations
14-
1512
try:
1613
fromcollections.abcimportIterable
1714
exceptImportError:
@@ -99,6 +96,9 @@
9996

10097
from .backupimportNodeBackup
10198

99+
from .operations.local_opsimportLocalOperations
100+
from .operations.remote_opsimportRemoteOperations
101+
102102
InternalError=pglib.InternalError
103103
ProgrammingError=pglib.ProgrammingError
104104
OperationalError=pglib.OperationalError
@@ -201,7 +201,7 @@ def pid(self):
201201

202202
ifself.status():
203203
pid_file=os.path.join(self.data_dir,PG_PID_FILE)
204-
lines=self.os_ops.readlines(pid_file,num_lines=1)
204+
lines=self.os_ops.readlines(pid_file)
205205
pid=int(lines[0])iflineselseNone
206206
returnpid
207207

@@ -433,7 +433,8 @@ def _collect_special_files(self):
433433
ifnotself.os_ops.path_exists(f):
434434
continue
435435

436-
lines=b''.join(self.os_ops.readlines(f,num_lines,encoding='utf-8'))
436+
file_lines=self.os_ops.readlines(f,num_lines,binary=True,encoding=None)
437+
lines=b''.join(file_lines)
437438

438439
# fill list
439440
result.append((f,lines))
@@ -498,7 +499,7 @@ def default_conf(self,
498499
]
499500

500501
# write filtered lines
501-
self.os_ops.write(hba_conf_file,lines,truncate=True)
502+
self.os_ops.write(hba_conf,lines,truncate=True)
502503

503504
# replication-related settings
504505
ifallow_streaming:
@@ -960,11 +961,9 @@ def psql(self,
960961
psql_params.append(dbname)
961962

962963
# start psql process
963-
process=self.os_ops.exec_command(psql_params)
964+
status_code,out,err=self.os_ops.exec_command(psql_params,shell=False,verbose=True,input=input)
964965

965-
# wait until it finishes and get stdout and stderr
966-
out,err=process.communicate(input=input)
967-
returnprocess.returncode,out,err
966+
returnstatus_code,out,err
968967

969968
@method_decorator(positional_args_hack(['dbname','query']))
970969
defsafe_psql(self,query=None,expect_error=False,**kwargs):
@@ -1348,7 +1347,7 @@ def pgbench(self,
13481347
# should be the last one
13491348
_params.append(dbname)
13501349

1351-
proc=self.os_ops.exec_command(_params,wait_exit=True)
1350+
proc=self.os_ops.exec_command(_params,stdout=stdout,stderr=stderr,wait_exit=True,shell=False,proc=True)
13521351

13531352
returnproc
13541353

‎testgres/os_ops/local_ops.pyrenamed to‎testgres/operations/local_ops.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ def __init__(self, username=None):
1919
self.username=usernameorself.get_user()
2020

2121
# Command execution
22-
defexec_command(self,cmd,wait_exit=False,verbose=False,expect_error=False):
23-
ifisinstance(cmd,list):
24-
cmd=" ".join(cmd)
22+
defexec_command(self,cmd,wait_exit=False,verbose=False,
23+
expect_error=False,encoding=None,shell=True,text=False,
24+
input=None,stdout=subprocess.PIPE,stderr=subprocess.PIPE,proc=None):
2525
log.debug(f"os_ops.exec_command: `{cmd}`; remote={self.remote}")
2626
# Source global profile file + execute command
2727
try:
28+
ifproc:
29+
returnsubprocess.Popen(cmd,
30+
shell=shell,
31+
stdin=inputorsubprocess.PIPE,
32+
stdout=stdout,
33+
stderr=stderr)
2834
process=subprocess.run(
2935
cmd,
30-
shell=True,
31-
text=True,
32-
stdout=subprocess.PIPE,
33-
stderr=subprocess.PIPE,
36+
input=input,
37+
shell=shell,
38+
text=text,
39+
stdout=stdout,
40+
stderr=stderr,
3441
timeout=CMD_TIMEOUT_SEC,
3542
)
3643
exit_status=process.returncode
@@ -39,11 +46,11 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False):
3946

4047
ifexpect_error:
4148
raiseException(result,error)
42-
ifexit_status!=0or"error"inerror.lower():
49+
ifexit_status!=0or"error"inerror.lower().decode(encodingor'utf-8'):# Decode error for comparison
4350
log.error(
44-
f"Problem in executing command: `{cmd}`\nerror:{error}\nexit_code:{exit_status}"
51+
f"Problem in executing command: `{cmd}`\nerror:{error.decode(encodingor'utf-8')}\nexit_code:{exit_status}"
52+
# Decode for logging
4553
)
46-
exit(1)
4754

4855
ifverbose:
4956
returnexit_status,result,error
@@ -152,9 +159,9 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
152159
"""
153160
mode="wb"ifbinaryelse"w"
154161
ifnottruncate:
155-
mode="a"+mode
162+
mode="ab"ifbinaryelse"a"
156163
ifread_and_write:
157-
mode="r+"+mode
164+
mode="r+b"ifbinaryelse"r+"
158165

159166
withopen(filename,mode)asfile:
160167
ifisinstance(data,list):
@@ -174,26 +181,26 @@ def touch(self, filename):
174181
withopen(filename,"a"):
175182
os.utime(filename,None)
176183

177-
defread(self,filename):
178-
withopen(filename,"r")asfile:
184+
defread(self,filename,encoding=None):
185+
withopen(filename,"r",encoding=encoding)asfile:
179186
returnfile.read()
180187

181-
defreadlines(self,filename,num_lines=0,encoding=None):
188+
defreadlines(self,filename,num_lines=0,binary=False,encoding=None):
182189
"""
183190
Read lines from a local file.
184191
If num_lines is greater than 0, only the last num_lines lines will be read.
185192
"""
186193
assertnum_lines>=0
187-
194+
mode='rb'ifbinaryelse'r'
188195
ifnum_lines==0:
189-
withopen(filename,"r",encoding=encoding)asfile:
196+
withopen(filename,mode,encoding=encoding)asfile:# open in binary mode
190197
returnfile.readlines()
191198

192199
else:
193200
bufsize=8192
194201
buffers=1
195202

196-
withopen(filename,"r",encoding=encoding)asfile:
203+
withopen(filename,mode,encoding=encoding)asfile:# open in binary mode
197204
file.seek(0,os.SEEK_END)
198205
end_pos=file.tell()
199206

@@ -205,7 +212,7 @@ def readlines(self, filename, num_lines=0, encoding=None):
205212
cur_lines=len(lines)
206213

207214
ifcur_lines>=num_linesorpos==0:
208-
returnlines[-num_lines:]
215+
returnlines[-num_lines:]# get last num_lines from lines
209216

210217
buffers=int(
211218
buffers*max(2,int(num_lines/max(cur_lines,1)))
File renamed without changes.

‎testgres/os_ops/remote_ops.pyrenamed to‎testgres/operations/remote_ops.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
importio
12
importos
23
importtempfile
34
fromcontextlibimportcontextmanager
@@ -65,30 +66,41 @@ def connect(self):
6566
returnssh
6667

6768
# Command execution
68-
defexec_command(
69-
self,cmd,wait_exit=False,verbose=False,expect_error=False,encoding="utf-8"
70-
):
69+
defexec_command(self,cmd,wait_exit=False,verbose=False,
70+
expect_error=False,encoding=None,shell=True,text=False,
71+
input=None,stdout=None,stderr=None,proc=None):
7172
ifisinstance(cmd,list):
7273
cmd=" ".join(cmd)
7374
log.debug(f"os_ops.exec_command: `{cmd}`; remote={self.remote}")
7475
# Source global profile file + execute command
7576
try:
7677
cmd=f"source /etc/profile.d/custom.sh;{cmd}"
7778
withself.ssh_connect()asssh:
78-
stdin,stdout,stderr=ssh.exec_command(cmd)
79+
ifinput:
80+
# encode input and feed it to stdin
81+
stdin,stdout,stderr=ssh.exec_command(cmd)
82+
stdin.write(input)
83+
stdin.flush()
84+
else:
85+
stdin,stdout,stderr=ssh.exec_command(cmd)
7986
exit_status=0
8087
ifwait_exit:
8188
exit_status=stdout.channel.recv_exit_status()
82-
result=stdout.read().decode(encoding)
83-
error=stderr.read().decode(encoding)
89+
ifencoding:
90+
result=stdout.read().decode(encoding)
91+
error=stderr.read().decode(encoding)
92+
else:
93+
# Save as binary string
94+
result=io.BytesIO(stdout.read()).getvalue()
95+
error=io.BytesIO(stderr.read()).getvalue()
96+
error_str=stderr.read()
8497

8598
ifexpect_error:
8699
raiseException(result,error)
87-
ifexit_status!=0or"error"inerror.lower():
100+
ifexit_status!=0or'error'inerror_str:
88101
log.error(
89102
f"Problem in executing command: `{cmd}`\nerror:{error}\nexit_code:{exit_status}"
90103
)
91-
exit(1)
92104

93105
ifverbose:
94106
returnexit_status,result,error
@@ -203,9 +215,9 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
203215
"""
204216
mode="wb"ifbinaryelse"w"
205217
ifnottruncate:
206-
mode="a"+mode
218+
mode="ab"ifbinaryelse"a"
207219
ifread_and_write:
208-
mode="r+"+mode
220+
mode="r+b"ifbinaryelse"r+"
209221

210222
withtempfile.NamedTemporaryFile(mode=mode)astmp_file:
211223
ifisinstance(data,list):
@@ -229,17 +241,28 @@ def touch(self, filename):
229241
"""
230242
self.exec_command(f"touch{filename}")
231243

232-
defread(self,filename,encoding="utf-8"):
244+
defread(self,filename,binary=False,encoding=None):
233245
cmd=f"cat{filename}"
234-
returnself.exec_command(cmd,encoding=encoding)
246+
result=self.exec_command(cmd,encoding=encoding)
247+
248+
ifnotbinaryandresult:
249+
result=result.decode(encodingor'utf-8')
250+
251+
returnresult
235252

236-
defreadlines(self,filename,num_lines=0,encoding=None):
237-
encoding=encodingor"utf-8"
253+
defreadlines(self,filename,num_lines=0,binary=False,encoding=None):
238254
ifnum_lines>0:
239255
cmd=f"tail -n{num_lines}{filename}"
240-
lines=self.exec_command(cmd,encoding)
241256
else:
242-
lines=self.read(filename,encoding=encoding).splitlines()
257+
cmd=f"cat{filename}"
258+
259+
result=self.exec_command(cmd,encoding=encoding)
260+
261+
ifnotbinaryandresult:
262+
lines=result.decode(encodingor'utf-8').splitlines()
263+
else:
264+
lines=result.splitlines()
265+
243266
returnlines
244267

245268
defisfile(self,remote_file):

‎testgres/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
fromcontextlibimportcontextmanager
1414
frompackaging.versionimportVersion
1515

16-
from .os_ops.remote_opsimportRemoteOperations
17-
1816
try:
1917
fromshutilimportwhichasfind_executable
2018
exceptImportError:
2119
fromdistutils.spawnimportfind_executable
2220
fromsiximportiteritems
2321

2422
fromfabricimportConnection
25-
from .os_ops.local_opsimportLocalOperations
26-
from .os_ops.os_opsimportOsOperations
23+
24+
from .operations.remote_opsimportRemoteOperations
25+
from .operations.local_opsimportLocalOperations
26+
from .operations.os_opsimportOsOperations
2727

2828
from .configimporttestgres_config
2929
from .exceptionsimportExecUtilException

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp