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

Commit71f54f5

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

File tree

7 files changed

+1086
-65
lines changed

7 files changed

+1086
-65
lines changed

‎testgres/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def call_initdb(initdb_dir, log=None):
3838
call_initdb(data_dir,logfile)
3939
else:
4040
# Fetch cached initdb dir
41-
cached_data_dir=testgres_config.cached_initdb_dir()
41+
cached_data_dir=testgres_config.cached_initdb_dir
4242

4343
# Initialize cached initdb
4444

‎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/node.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
@@ -708,7 +709,8 @@ def start(self, params=[], wait=True):
708709
]+params# yapf: disable
709710

710711
try:
711-
execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
712+
res=execute_utility(_params,self.utils_log_file,os_ops=self.os_ops)
713+
print()
712714
exceptExecUtilExceptionase:
713715
msg='Cannot start node'
714716
files=self._collect_special_files()
@@ -960,11 +962,9 @@ def psql(self,
960962
psql_params.append(dbname)
961963

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

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

969969
@method_decorator(positional_args_hack(['dbname','query']))
970970
defsafe_psql(self,query=None,expect_error=False,**kwargs):
@@ -1348,7 +1348,7 @@ def pgbench(self,
13481348
# should be the last one
13491349
_params.append(dbname)
13501350

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

13531353
returnproc
13541354

‎testgres/os_ops/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)))

‎testgres/os_ops/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):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp