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

Commit1309442

Browse files
Total refactoring of os_ops::execute_command
Main - 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.
1 parentb0f90d9 commit1309442

File tree

7 files changed

+134
-121
lines changed

7 files changed

+134
-121
lines changed

‎testgres/operations/local_ops.py‎

Lines changed: 15 additions & 20 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,28 @@ 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,
141-
msg_arg=errororoutput,
142138
error=error,
143-
out=output
144-
)
139+
out=output)
145140

146141
ifverbose:
147142
returnprocess.returncode,output,error
148-
else:
149-
returnoutput
143+
144+
returnoutput
150145

151146
# Environment setup
152147
defenviron(self,var_name):

‎testgres/operations/raise_error.py‎

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,27 @@
11
from ..exceptionsimportExecUtilException
2-
from .helpersimportHelpers
32

43

54
classRaiseError:
65
@staticmethod
7-
defUtilityExitedWithNonZeroCode(cmd,exit_code,msg_arg,error,out):
6+
defUtilityExitedWithNonZeroCode(cmd,exit_code,error,out):
87
asserttype(exit_code)==int# noqa: E721
98

10-
msg_arg_s=__class__._TranslateDataIntoString(msg_arg).strip()
11-
asserttype(msg_arg_s)==str# noqa: E721
12-
13-
ifmsg_arg_s=="":
14-
msg_arg_s="#no_error_message"
15-
16-
message="Utility exited with non-zero code. Error: `"+msg_arg_s+"`"
179
raiseExecUtilException(
18-
message=message,
10+
message="Utility exited with non-zero code.",
1911
command=cmd,
2012
exit_code=exit_code,
2113
out=out,
2214
error=error)
2315

2416
@staticmethod
25-
def_TranslateDataIntoString(data):
26-
iftype(data)==bytes:# noqa: E721
27-
return__class__._TranslateDataIntoString__FromBinary(data)
28-
29-
returnstr(data)
30-
31-
@staticmethod
32-
def_TranslateDataIntoString__FromBinary(data):
33-
asserttype(data)==bytes# noqa: E721
34-
35-
try:
36-
returndata.decode(Helpers.GetDefaultEncoding())
37-
exceptUnicodeDecodeError:
38-
pass
39-
40-
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
17+
defCommandExecutionError(cmd,exit_code,message,error,out):
18+
asserttype(exit_code)==int# noqa: E721
19+
asserttype(message)==str# noqa: E721
20+
assertmessage!=""
4921

50-
returnTrue
22+
raiseExecUtilException(
23+
message=message,
24+
command=cmd,
25+
exit_code=exit_code,
26+
out=out,
27+
error=error)

‎testgres/operations/remote_ops.py‎

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -100,41 +100,39 @@ 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,
130-
msg_arg=error,
128+
exit_code=process.returncode,
131129
error=error,
132-
out=result)
130+
out=output)
133131

134132
ifverbose:
135-
returnexit_status,result,error
136-
else:
137-
returnresult
133+
returnprocess.returncode,output,error
134+
135+
returnoutput
138136

139137
# Environment setup
140138
defenviron(self,var_name:str)->str:
@@ -165,8 +163,30 @@ def find_executable(self, executable):
165163

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

171191
defset_env(self,var_name:str,var_val:str):
172192
"""
@@ -251,15 +271,21 @@ def mkdtemp(self, prefix=None):
251271
else:
252272
command= ["mktemp","-d"]
253273

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

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

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

262-
temp_dir=result.strip()
288+
temp_dir=exec_output.strip()
263289
returntemp_dir
264290

265291
defmkstemp(self,prefix=None):
@@ -273,15 +299,21 @@ def mkstemp(self, prefix=None):
273299
else:
274300
command= ["mktemp"]
275301

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

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

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

284-
temp_file=result.strip()
316+
temp_file=exec_output.strip()
285317
returntemp_file
286318

287319
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."
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
@@ -38,10 +38,11 @@ def test_exec_command_failure(self):
3838
try:
3939
self.operations.exec_command(cmd,verbose=True,wait_exit=True)
4040
exceptExecUtilExceptionase:
41-
error=e.message
41+
asserte.message=="Utility exited with non-zero code."
42+
asserttype(e.error)==bytes# noqa: E721
43+
asserte.error.strip()==b"bash: line 1: nonexistent_command: command not found"
4244
break
4345
raiseException("We wait an exception!")
44-
asserterror=='Utility exited with non-zero code. Error: `bash: line 1: nonexistent_command: command not found`'
4546

4647
deftest_exec_command_failure__expect_error(self):
4748
"""
@@ -108,10 +109,11 @@ def test_makedirs_and_rmdirs_failure(self):
108109
try:
109110
self.operations.rmdirs(path,verbose=True)
110111
exceptExecUtilExceptionase:
111-
error=e.message
112+
asserte.message=="Utility exited with non-zero code."
113+
asserttype(e.error)==bytes# noqa: E721
114+
asserte.error.strip()==b"rm: cannot remove '/root/test_dir': Permission denied"
112115
break
113116
raiseException("We wait an exception!")
114-
asserterror=="Utility exited with non-zero code. Error: `rm: cannot remove '/root/test_dir': Permission denied`"
115117

116118
deftest_listdir(self):
117119
"""

‎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