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

Commitf9ddd04

Browse files
Proposal tofix#154 (v2)
- The one way to generate ExecUtilException - RaiseError.UtilityExitedWithNonZeroCode- RaiseError is added- ExecUtilException::error is added (it contains the error data)- ExecUtilException::__str__ is updated- PostgresNode::psql and PostgresNode::safe_psql are updated- TestLocalOperations::test_exec_command_failure is updated- TestRemoteOperations::test_exec_command_failure is updated- TestRemoteOperations::test_makedirs_and_rmdirs_failure is updated
1 parent1c73113 commitf9ddd04

File tree

7 files changed

+110
-47
lines changed

7 files changed

+110
-47
lines changed

‎testgres/exceptions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class TestgresException(Exception):
99

1010
@six.python_2_unicode_compatible
1111
classExecUtilException(TestgresException):
12-
def__init__(self,message=None,command=None,exit_code=0,out=None):
12+
def__init__(self,message=None,command=None,exit_code=0,out=None,error=None):
1313
super(ExecUtilException,self).__init__(message)
1414

1515
self.message=message
1616
self.command=command
1717
self.exit_code=exit_code
1818
self.out=out
19+
self.error=error
1920

2021
def__str__(self):
2122
msg= []
@@ -24,13 +25,17 @@ def __str__(self):
2425
msg.append(self.message)
2526

2627
ifself.command:
27-
msg.append(u'Command: {}'.format(self.command))
28+
command_s=' '.join(self.command)ifisinstance(self.command,list)elseself.command,
29+
msg.append(u'Command: {}'.format(command_s))
2830

2931
ifself.exit_code:
3032
msg.append(u'Exit code: {}'.format(self.exit_code))
3133

34+
ifself.error:
35+
msg.append(u'---- Error:\n{}'.format(self.error))
36+
3237
ifself.out:
33-
msg.append(u'----\n{}'.format(self.out))
38+
msg.append(u'---- Out:\n{}'.format(self.out))
3439

3540
returnself.convert_and_join(msg)
3641

‎testgres/helpers/raise_error.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from ..exceptionsimportExecUtilException
2+
3+
4+
classRaiseError:
5+
defUtilityExitedWithNonZeroCode(cmd,exit_code,msg_arg,error,out):
6+
asserttype(exit_code)==int# noqa: E721
7+
8+
msg_arg_s=__class__._TranslateDataIntoString(msg_arg).strip()
9+
asserttype(msg_arg_s)==str# noqa: E721
10+
11+
ifmsg_arg_s=="":
12+
msg_arg_s="#no_error_message"
13+
14+
message="Utility exited with non-zero code. Error: `"+msg_arg_s+"`"
15+
raiseExecUtilException(
16+
message=message,
17+
command=cmd,
18+
exit_code=exit_code,
19+
out=out,
20+
error=error)
21+
22+
def_TranslateDataIntoString(data):
23+
iftype(data)==bytes:# noqa: E721
24+
return__class__._TranslateDataIntoString__FromBinary(data)
25+
26+
returnstr(data)
27+
28+
def_TranslateDataIntoString__FromBinary(data):
29+
asserttype(data)==bytes# noqa: E721
30+
31+
try:
32+
returndata.decode('utf-8')
33+
exceptUnicodeDecodeError:
34+
pass
35+
36+
return"#cannot_decode_text"
37+
38+
def_BinaryIsASCII(data):
39+
asserttype(data)==bytes# noqa: E721
40+
41+
forbindata:
42+
ifnot (b>=0andb<=127):
43+
returnFalse
44+
45+
returnTrue

‎testgres/node.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
importos
44
importrandom
55
importsignal
6-
importsubprocess
76
importthreading
87
importtempfile
98
fromqueueimportQueue
@@ -987,6 +986,25 @@ def psql(self,
987986
>>> psql(query='select 3', ON_ERROR_STOP=1)
988987
"""
989988

989+
returnself._psql(
990+
ignore_errors=True,
991+
query=query,
992+
filename=filename,
993+
dbname=dbname,
994+
username=username,
995+
input=input,
996+
**variables
997+
)
998+
999+
def_psql(
1000+
self,
1001+
ignore_errors,
1002+
query=None,
1003+
filename=None,
1004+
dbname=None,
1005+
username=None,
1006+
input=None,
1007+
**variables):
9901008
dbname=dbnameordefault_dbname()
9911009

9921010
psql_params= [
@@ -1017,20 +1035,8 @@ def psql(self,
10171035

10181036
# should be the last one
10191037
psql_params.append(dbname)
1020-
ifnotself.os_ops.remote:
1021-
# start psql process
1022-
process=subprocess.Popen(psql_params,
1023-
stdin=subprocess.PIPE,
1024-
stdout=subprocess.PIPE,
1025-
stderr=subprocess.PIPE)
1026-
1027-
# wait until it finishes and get stdout and stderr
1028-
out,err=process.communicate(input=input)
1029-
returnprocess.returncode,out,err
1030-
else:
1031-
status_code,out,err=self.os_ops.exec_command(psql_params,verbose=True,input=input)
10321038

1033-
returnstatus_code,out,err
1039+
returnself.os_ops.exec_command(psql_params,verbose=True,input=input,ignore_errors=ignore_errors)
10341040

10351041
@method_decorator(positional_args_hack(['dbname','query']))
10361042
defsafe_psql(self,query=None,expect_error=False,**kwargs):
@@ -1051,21 +1057,17 @@ def safe_psql(self, query=None, expect_error=False, **kwargs):
10511057
Returns:
10521058
psql's output as str.
10531059
"""
1060+
asserttype(kwargs)==dict# noqa: E721
1061+
assertnot ("ignore_errors"inkwargs.keys())
10541062

10551063
# force this setting
10561064
kwargs['ON_ERROR_STOP']=1
10571065
try:
1058-
ret,out,err=self.psql(query=query,**kwargs)
1066+
ret,out,err=self._psql(ignore_errors=False,query=query,**kwargs)
10591067
exceptExecUtilExceptionase:
1060-
ret=e.exit_code
1061-
out=e.out
1062-
err=e.message
1063-
ifret:
1064-
ifexpect_error:
1065-
out= (errorb'').decode('utf-8')
1066-
else:
1067-
raiseQueryException((errorb'').decode('utf-8'),query)
1068-
elifexpect_error:
1068+
raiseQueryException(e.message,query)
1069+
1070+
ifexpect_error:
10691071
assertFalse,"Exception was expected, but query finished successfully: `{}` ".format(query)
10701072

10711073
returnout

‎testgres/operations/local_ops.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ..exceptionsimportExecUtilException
1313
from .os_opsimportConnectionParams,OsOperations,pglib,get_default_encoding
14+
from ..helpers.raise_errorimportRaiseError
1415

1516
try:
1617
fromshutilimportwhichasfind_executable
@@ -47,14 +48,6 @@ def __init__(self, conn_params=None):
4748
self.remote=False
4849
self.username=conn_params.usernameorgetpass.getuser()
4950

50-
@staticmethod
51-
def_raise_exec_exception(message,command,exit_code,output):
52-
"""Raise an ExecUtilException."""
53-
raiseExecUtilException(message=message.format(output),
54-
command=' '.join(command)ifisinstance(command,list)elsecommand,
55-
exit_code=exit_code,
56-
out=output)
57-
5851
@staticmethod
5952
def_process_output(encoding,temp_file_path):
6053
"""Process the output of a command from a temporary file."""
@@ -120,11 +113,20 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
120113
"""
121114
Execute a command in a subprocess and handle the output based on the provided parameters.
122115
"""
116+
asserttype(expect_error)==bool# noqa: E721
117+
asserttype(ignore_errors)==bool# noqa: E721
118+
123119
process,output,error=self._run_command(cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding)
124120
ifget_process:
125121
returnprocess
126122
ifnotignore_errorsand ((process.returncode!=0orhas_errors(output=output,error=error))andnotexpect_error):
127-
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,errororoutput)
123+
RaiseError.UtilityExitedWithNonZeroCode(
124+
cmd=cmd,
125+
exit_code=process.returncode,
126+
msg_arg=errororoutput,
127+
error=error,
128+
out=output
129+
)
128130

129131
ifverbose:
130132
returnprocess.returncode,output,error

‎testgres/operations/remote_ops.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
raiseImportError("You must have psycopg2 or pg8000 modules installed")
1515

1616
from ..exceptionsimportExecUtilException
17+
from ..helpers.raise_errorimportRaiseError
1718
from .os_opsimportOsOperations,ConnectionParams,get_default_encoding
1819

1920
error_markers= [b'error',b'Permission denied',b'fatal',b'No such file or directory']
@@ -66,6 +67,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
6667
Args:
6768
- cmd (str): The command to be executed.
6869
"""
70+
asserttype(expect_error)==bool# noqa: E721
71+
asserttype(ignore_errors)==bool# noqa: E721
72+
6973
ssh_cmd= []
7074
ifisinstance(cmd,str):
7175
ssh_cmd= ['ssh',self.ssh_dest]+self.ssh_args+ [cmd]
@@ -100,10 +104,12 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
100104
error=error.decode(encoding)
101105

102106
ifnotignore_errorsanderror_foundandnotexpect_error:
103-
error=normalize_error(error)
104-
asserttype(error)==str# noqa: E721
105-
message="Utility exited with non-zero code. Error: "+error
106-
raiseExecUtilException(message=message,command=cmd,exit_code=exit_status,out=result)
107+
RaiseError.UtilityExitedWithNonZeroCode(
108+
cmd=cmd,
109+
exit_code=exit_status,
110+
msg_arg=error,
111+
error=error,
112+
out=result)
107113

108114
ifverbose:
109115
returnexit_status,result,error

‎tests/test_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_exec_command_failure(self):
3737
error=e.message
3838
break
3939
raiseException("We wait an exception!")
40-
asserterror=="Utility exited with non-zero code. Error `b'/bin/sh: 1: nonexistent_command: not found\\n'`"
40+
asserterror=="Utility exited with non-zero code. Error: `/bin/sh: 1: nonexistent_command: not found`"
4141

4242
deftest_exec_command_failure__expect_error(self):
4343
"""

‎tests/test_remote.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_exec_command_failure(self):
3737
error=e.message
3838
break
3939
raiseException("We wait an exception!")
40-
asserterror==b'Utility exited with non-zero code. Error: bash: line 1: nonexistent_command: command not found\n'
40+
asserterror=='Utility exited with non-zero code. Error:`bash: line 1: nonexistent_command: command not found`'
4141

4242
deftest_exec_command_failure__expect_error(self):
4343
"""
@@ -98,11 +98,14 @@ def test_makedirs_and_rmdirs_failure(self):
9898
self.operations.makedirs(path)
9999

100100
# Test rmdirs
101-
try:
102-
exit_status,result,error=self.operations.rmdirs(path,verbose=True)
103-
exceptExecUtilExceptionase:
104-
error=e.message
105-
asserterror==b"Utility exited with non-zero code. Error: rm: cannot remove '/root/test_dir': Permission denied\n"
101+
whileTrue:
102+
try:
103+
self.operations.rmdirs(path,verbose=True)
104+
exceptExecUtilExceptionase:
105+
error=e.message
106+
break
107+
raiseException("We wait an exception!")
108+
asserterror=="Utility exited with non-zero code. Error: `rm: cannot remove '/root/test_dir': Permission denied`"
106109

107110
deftest_listdir(self):
108111
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp