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

Commit0ffd5f0

Browse files
Total refactoring of os_ops::execute_command (#203)
* Total refactoring of os_ops::execute_commandMain - We check only an exit code to detect an error. - If someone utility returns a result through an exit code, a caller side should set ignore_errors=true and process this case itself. - If expect_error is true and no errors occurred, we raise an InvalidOperationException.* The old behaviour of RaiseError.UtilityExitedWithNonZeroCode is restoredLet's rollback the new code to avoid problems with probackup2' tests.
1 parent7177212 commit0ffd5f0

File tree

7 files changed

+141
-96
lines changed

7 files changed

+141
-96
lines changed

‎testgres/operations/local_ops.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@
2323
fromdistutilsimportrmtree
2424

2525
CMD_TIMEOUT_SEC=60
26-
error_markers= [b'error',b'Permission denied',b'fatal']
27-
err_out_markers= [b'Failure']
28-
29-
30-
defhas_errors(output=None,error=None):
31-
ifoutput:
32-
ifisinstance(output,str):
33-
output=output.encode(get_default_encoding())
34-
returnany(markerinoutputformarkerinerr_out_markers)
35-
iferror:
36-
ifisinstance(error,str):
37-
error=error.encode(get_default_encoding())
38-
returnany(markerinerrorformarkerinerror_markers)
39-
returnFalse
4026

4127

4228
classLocalOperations(OsOperations):
@@ -134,19 +120,29 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
134120
process,output,error=self._run_command(cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding)
135121
ifget_process:
136122
returnprocess
137-
ifnotignore_errorsand ((process.returncode!=0orhas_errors(output=output,error=error))andnotexpect_error):
123+
124+
ifexpect_error:
125+
ifprocess.returncode==0:
126+
raiseInvalidOperationException("We expected an execution error.")
127+
elifignore_errors:
128+
pass
129+
elifprocess.returncode==0:
130+
pass
131+
else:
132+
assertnotexpect_error
133+
assertnotignore_errors
134+
assertprocess.returncode!=0
138135
RaiseError.UtilityExitedWithNonZeroCode(
139136
cmd=cmd,
140137
exit_code=process.returncode,
141138
msg_arg=errororoutput,
142139
error=error,
143-
out=output
144-
)
140+
out=output)
145141

146142
ifverbose:
147143
returnprocess.returncode,output,error
148-
else:
149-
returnoutput
144+
145+
returnoutput
150146

151147
# Environment setup
152148
defenviron(self,var_name):

‎testgres/operations/raise_error.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ class RaiseError:
77
defUtilityExitedWithNonZeroCode(cmd,exit_code,msg_arg,error,out):
88
asserttype(exit_code)==int# noqa: E721
99

10-
msg_arg_s=__class__._TranslateDataIntoString(msg_arg).strip()
10+
msg_arg_s=__class__._TranslateDataIntoString(msg_arg)
1111
asserttype(msg_arg_s)==str# noqa: E721
1212

13+
msg_arg_s=msg_arg_s.strip()
1314
ifmsg_arg_s=="":
1415
msg_arg_s="#no_error_message"
1516

16-
message="Utility exited with non-zero code. Error: `"+msg_arg_s+"`"
17+
message="Utility exited with non-zero code ("+str(exit_code)+"). Error: `"+msg_arg_s+"`"
18+
raiseExecUtilException(
19+
message=message,
20+
command=cmd,
21+
exit_code=exit_code,
22+
out=out,
23+
error=error)
24+
25+
@staticmethod
26+
defCommandExecutionError(cmd,exit_code,message,error,out):
27+
asserttype(exit_code)==int# noqa: E721
28+
asserttype(message)==str# noqa: E721
29+
assertmessage!=""
30+
1731
raiseExecUtilException(
1832
message=message,
1933
command=cmd,
@@ -23,6 +37,9 @@ def UtilityExitedWithNonZeroCode(cmd, exit_code, msg_arg, error, out):
2337

2438
@staticmethod
2539
def_TranslateDataIntoString(data):
40+
ifdataisNone:
41+
return""
42+
2643
iftype(data)==bytes:# noqa: E721
2744
return__class__._TranslateDataIntoString__FromBinary(data)
2845

@@ -38,13 +55,3 @@ def _TranslateDataIntoString__FromBinary(data):
3855
pass
3956

4057
return"#cannot_decode_text"
41-
42-
@staticmethod
43-
def_BinaryIsASCII(data):
44-
asserttype(data)==bytes# noqa: E721
45-
46-
forbindata:
47-
ifnot (b>=0andb<=127):
48-
returnFalse
49-
50-
returnTrue

‎testgres/operations/remote_ops.py

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -100,41 +100,40 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
100100
returnprocess
101101

102102
try:
103-
result,error=process.communicate(input=input_prepared,timeout=timeout)
103+
output,error=process.communicate(input=input_prepared,timeout=timeout)
104104
exceptsubprocess.TimeoutExpired:
105105
process.kill()
106106
raiseExecUtilException("Command timed out after {} seconds.".format(timeout))
107107

108-
exit_status=process.returncode
109-
110-
asserttype(result)==bytes# noqa: E721
108+
asserttype(output)==bytes# noqa: E721
111109
asserttype(error)==bytes# noqa: E721
112110

113-
ifnoterror:
114-
error_found=False
115-
else:
116-
error_found=exit_status!=0orany(
117-
markerinerrorformarkerin [b'error',b'Permission denied',b'fatal',b'No such file or directory']
118-
)
119-
120-
asserttype(error_found)==bool# noqa: E721
121-
122111
ifencoding:
123-
result=result.decode(encoding)
112+
output=output.decode(encoding)
124113
error=error.decode(encoding)
125114

126-
ifnotignore_errorsanderror_foundandnotexpect_error:
115+
ifexpect_error:
116+
ifprocess.returncode==0:
117+
raiseInvalidOperationException("We expected an execution error.")
118+
elifignore_errors:
119+
pass
120+
elifprocess.returncode==0:
121+
pass
122+
else:
123+
assertnotexpect_error
124+
assertnotignore_errors
125+
assertprocess.returncode!=0
127126
RaiseError.UtilityExitedWithNonZeroCode(
128127
cmd=cmd,
129-
exit_code=exit_status,
128+
exit_code=process.returncode,
130129
msg_arg=error,
131130
error=error,
132-
out=result)
131+
out=output)
133132

134133
ifverbose:
135-
returnexit_status,result,error
136-
else:
137-
returnresult
134+
returnprocess.returncode,output,error
135+
136+
returnoutput
138137

139138
# Environment setup
140139
defenviron(self,var_name:str)->str:
@@ -165,8 +164,30 @@ def find_executable(self, executable):
165164

166165
defis_executable(self,file):
167166
# Check if the file is executable
168-
is_exec=self.exec_command("test -x {} && echo OK".format(file))
169-
returnis_exec==b"OK\n"
167+
command= ["test","-x",file]
168+
169+
exit_status,output,error=self.exec_command(cmd=command,encoding=get_default_encoding(),ignore_errors=True,verbose=True)
170+
171+
asserttype(output)==str# noqa: E721
172+
asserttype(error)==str# noqa: E721
173+
174+
ifexit_status==0:
175+
returnTrue
176+
177+
ifexit_status==1:
178+
returnFalse
179+
180+
errMsg="Test operation returns an unknown result code: {0}. File name is [{1}].".format(
181+
exit_status,
182+
file)
183+
184+
RaiseError.CommandExecutionError(
185+
cmd=command,
186+
exit_code=exit_status,
187+
msg_arg=errMsg,
188+
error=error,
189+
out=output
190+
)
170191

171192
defset_env(self,var_name:str,var_val:str):
172193
"""
@@ -251,15 +272,21 @@ def mkdtemp(self, prefix=None):
251272
else:
252273
command= ["mktemp","-d"]
253274

254-
exit_status,result,error=self.exec_command(command,verbose=True,encoding=get_default_encoding(),ignore_errors=True)
275+
exec_exitcode,exec_output,exec_error=self.exec_command(command,verbose=True,encoding=get_default_encoding(),ignore_errors=True)
255276

256-
asserttype(result)==str# noqa: E721
257-
asserttype(error)==str# noqa: E721
277+
asserttype(exec_exitcode)==int# noqa: E721
278+
asserttype(exec_output)==str# noqa: E721
279+
asserttype(exec_error)==str# noqa: E721
258280

259-
ifexit_status!=0:
260-
raiseExecUtilException("Could not create temporary directory. Error code: {0}. Error message: {1}".format(exit_status,error))
281+
ifexec_exitcode!=0:
282+
RaiseError.CommandExecutionError(
283+
cmd=command,
284+
exit_code=exec_exitcode,
285+
message="Could not create temporary directory.",
286+
error=exec_error,
287+
out=exec_output)
261288

262-
temp_dir=result.strip()
289+
temp_dir=exec_output.strip()
263290
returntemp_dir
264291

265292
defmkstemp(self,prefix=None):
@@ -273,15 +300,21 @@ def mkstemp(self, prefix=None):
273300
else:
274301
command= ["mktemp"]
275302

276-
exit_status,result,error=self.exec_command(command,verbose=True,encoding=get_default_encoding(),ignore_errors=True)
303+
exec_exitcode,exec_output,exec_error=self.exec_command(command,verbose=True,encoding=get_default_encoding(),ignore_errors=True)
277304

278-
asserttype(result)==str# noqa: E721
279-
asserttype(error)==str# noqa: E721
305+
asserttype(exec_exitcode)==int# noqa: E721
306+
asserttype(exec_output)==str# noqa: E721
307+
asserttype(exec_error)==str# noqa: E721
280308

281-
ifexit_status!=0:
282-
raiseExecUtilException("Could not create temporary file. Error code: {0}. Error message: {1}".format(exit_status,error))
309+
ifexec_exitcode!=0:
310+
RaiseError.CommandExecutionError(
311+
cmd=command,
312+
exit_code=exec_exitcode,
313+
message="Could not create temporary file.",
314+
error=exec_error,
315+
out=exec_output)
283316

284-
temp_file=result.strip()
317+
temp_file=exec_output.strip()
285318
returntemp_file
286319

287320
defcopytree(self,src,dst):

‎testgres/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .configimporttestgres_configastconf
1919
from .operations.os_opsimportOsOperations
2020
from .operations.remote_opsimportRemoteOperations
21+
from .operations.helpersimportHelpersasOsHelpers
2122

2223
# rows returned by PG_CONFIG
2324
_pg_config_data= {}
@@ -79,13 +80,13 @@ def execute_utility2(os_ops: OsOperations, args, logfile=None, verbose=False, ig
7980
asserttype(verbose)==bool# noqa: E721
8081
asserttype(ignore_errors)==bool# noqa: E721
8182

82-
exit_status,out,error=os_ops.exec_command(args,verbose=True,ignore_errors=ignore_errors)
83-
# decode result
83+
exit_status,out,error=os_ops.exec_command(
84+
args,
85+
verbose=True,
86+
ignore_errors=ignore_errors,
87+
encoding=OsHelpers.GetDefaultEncoding())
88+
8489
out=''ifnotoutelseout
85-
ifisinstance(out,bytes):
86-
out=out.decode('utf-8')
87-
ifisinstance(error,bytes):
88-
error=error.decode('utf-8')
8990

9091
# write new log entry if possible
9192
iflogfile:

‎tests/test_local.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ def test_exec_command_failure(self):
4040
try:
4141
self.operations.exec_command(cmd,wait_exit=True,shell=True)
4242
exceptExecUtilExceptionase:
43-
error=e.message
43+
asserte.message=="Utility exited with non-zero code (127). Error: `/bin/sh: 1: nonexistent_command: not found`"
44+
asserttype(e.error)==bytes# noqa: E721
45+
asserte.error.strip()==b"/bin/sh: 1: nonexistent_command: not found"
4446
break
4547
raiseException("We wait an exception!")
46-
asserterror=="Utility exited with non-zero code. Error: `/bin/sh: 1: nonexistent_command: not found`"
4748

4849
deftest_exec_command_failure__expect_error(self):
4950
"""

‎tests/test_remote.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ def test_exec_command_failure(self):
4040
try:
4141
self.operations.exec_command(cmd,verbose=True,wait_exit=True)
4242
exceptExecUtilExceptionase:
43-
error=e.message
43+
asserte.message=="Utility exited with non-zero code (127). Error: `bash: line 1: nonexistent_command: command not found`"
44+
asserttype(e.error)==bytes# noqa: E721
45+
asserte.error.strip()==b"bash: line 1: nonexistent_command: command not found"
4446
break
4547
raiseException("We wait an exception!")
46-
asserterror=='Utility exited with non-zero code. Error: `bash: line 1: nonexistent_command: command not found`'
4748

4849
deftest_exec_command_failure__expect_error(self):
4950
"""
@@ -114,10 +115,11 @@ def test_makedirs_and_rmdirs_failure(self):
114115
try:
115116
self.operations.rmdirs(path,verbose=True)
116117
exceptExecUtilExceptionase:
117-
error=e.message
118+
asserte.message=="Utility exited with non-zero code (1). Error: `rm: cannot remove '/root/test_dir': Permission denied`"
119+
asserttype(e.error)==bytes# noqa: E721
120+
asserte.error.strip()==b"rm: cannot remove '/root/test_dir': Permission denied"
118121
break
119122
raiseException("We wait an exception!")
120-
asserterror=="Utility exited with non-zero code. Error: `rm: cannot remove '/root/test_dir': Permission denied`"
121123

122124
deftest_listdir(self):
123125
"""

‎tests/test_simple_remote.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,32 @@ def test_init__unk_LANG_and_LC_CTYPE(self):
178178
assertos.environ.get("LC_CTYPE")==unkData[1]
179179
assertnot ("LC_COLLATE"inos.environ.keys())
180180

181-
whileTrue:
181+
assertos.getenv('LANG')==unkData[0]
182+
assertos.getenv('LANGUAGE')isNone
183+
assertos.getenv('LC_CTYPE')==unkData[1]
184+
assertos.getenv('LC_COLLATE')isNone
185+
186+
exc:ExecUtilException=None
187+
with__class__.helper__get_node()asnode:
182188
try:
183-
with__class__.helper__get_node():
184-
pass
185-
exceptExecUtilExceptionase:
186-
#
187-
# Example of an error message:
188-
#
189-
# warning: setlocale: LC_CTYPE: cannot change locale (UNKNOWN_CTYPE): No such file or directory
190-
# postgres (PostgreSQL) 14.12
191-
#
192-
errMsg=str(e)
193-
194-
logging.info("Error message is: {0}".format(errMsg))
195-
196-
assert"LC_CTYPE"inerrMsg
197-
assertunkData[1]inerrMsg
198-
assert"warning: setlocale: LC_CTYPE: cannot change locale ("+unkData[1]+"): No such file or directory"inerrMsg
199-
assert ("postgres"inerrMsg)or ("PostgreSQL"inerrMsg)
200-
break
189+
node.init()# IT RAISES!
190+
exceptInitNodeExceptionase:
191+
exc=e.__cause__
192+
assertexcisnotNone
193+
assertisinstance(exc,ExecUtilException)
194+
195+
ifexcisNone:
201196
raiseException("We expected an error!")
197+
198+
assertisinstance(exc,ExecUtilException)
199+
200+
errMsg=str(exc)
201+
logging.info("Error message is {0}: {1}".format(type(exc).__name__,errMsg))
202+
203+
assert"warning: setlocale: LC_CTYPE: cannot change locale ("+unkData[1]+")"inerrMsg
204+
assert"initdb: error: invalid locale settings; check LANG and LC_* environment variables"inerrMsg
205+
continue
206+
202207
finally:
203208
__class__.helper__restore_envvar("LANG",prev_LANG)
204209
__class__.helper__restore_envvar("LANGUAGE",prev_LANGUAGE)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp