Skip to content

Total refactoring of os_ops::execute_command #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
The old behaviour of RaiseError.UtilityExitedWithNonZeroCode is restored
Let's rollback the new code to avoid problems with probackup2' tests.
  • Loading branch information
dmitry-lipetsk committed Mar 1, 2025
commit d843df62f1d038905dd6a12d420edc9be670d4bf
1 change: 1 addition & 0 deletions testgres/operations/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
RaiseError.UtilityExitedWithNonZeroCode(
cmd=cmd,
exit_code=process.returncode,
msg_arg=error or output,
error=error,
out=output)

Expand Down
34 changes: 32 additions & 2 deletions testgres/operations/raise_error.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from ..exceptions import ExecUtilException
from .helpers import Helpers


class RaiseError:
@staticmethod
def UtilityExitedWithNonZeroCode(cmd, exit_code, error, out):
def UtilityExitedWithNonZeroCode(cmd, exit_code, msg_arg, error, out):
assert type(exit_code) == int # noqa: E721

msg_arg_s = __class__._TranslateDataIntoString(msg_arg)
assert type(msg_arg_s) == str # noqa: E721

msg_arg_s = msg_arg_s.strip()
if msg_arg_s == "":
msg_arg_s = "#no_error_message"

message = "Utility exited with non-zero code (" + str(exit_code) + "). Error: `" + msg_arg_s + "`"
raise ExecUtilException(
message="Utility exited with non-zero code.",
message=message,
command=cmd,
exit_code=exit_code,
out=out,
Expand All @@ -25,3 +34,24 @@ def CommandExecutionError(cmd, exit_code, message, error, out):
exit_code=exit_code,
out=out,
error=error)

@staticmethod
def _TranslateDataIntoString(data):
if data is None:
return ""

if type(data) == bytes: # noqa: E721
return __class__._TranslateDataIntoString__FromBinary(data)

return str(data)

@staticmethod
def _TranslateDataIntoString__FromBinary(data):
assert type(data) == bytes # noqa: E721

try:
return data.decode(Helpers.GetDefaultEncoding())
except UnicodeDecodeError:
pass

return "#cannot_decode_text"
3 changes: 2 additions & 1 deletion testgres/operations/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
RaiseError.UtilityExitedWithNonZeroCode(
cmd=cmd,
exit_code=process.returncode,
msg_arg=error,
error=error,
out=output)

Expand Down Expand Up @@ -180,7 +181,7 @@ def is_executable(self, file):
exit_status,
file)

RaiseError.UtilityExitedWithNonZeroCode(
RaiseError.CommandExecutionError(
cmd=command,
exit_code=exit_status,
msg_arg=errMsg,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_exec_command_failure(self):
try:
self.operations.exec_command(cmd, wait_exit=True, shell=True)
except ExecUtilException as e:
assert e.message == "Utility exited with non-zero code."
assert e.message == "Utility exited with non-zero code (127). Error: `/bin/sh: 1: nonexistent_command: not found`"
assert type(e.error) == bytes # noqa: E721
assert e.error.strip() == b"/bin/sh: 1: nonexistent_command: not found"
break
Expand Down
4 changes: 2 additions & 2 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_exec_command_failure(self):
try:
self.operations.exec_command(cmd, verbose=True, wait_exit=True)
except ExecUtilException as e:
assert e.message == "Utility exited with non-zero code."
assert e.message == "Utility exited with non-zero code (127). Error: `bash: line 1: nonexistent_command: command not found`"
assert type(e.error) == bytes # noqa: E721
assert e.error.strip() == b"bash: line 1: nonexistent_command: command not found"
break
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_makedirs_and_rmdirs_failure(self):
try:
self.operations.rmdirs(path, verbose=True)
except ExecUtilException as e:
assert e.message == "Utility exited with non-zero code."
assert e.message == "Utility exited with non-zero code (1). Error: `rm: cannot remove '/root/test_dir': Permission denied`"
assert type(e.error) == bytes # noqa: E721
assert e.error.strip() == b"rm: cannot remove '/root/test_dir': Permission denied"
break
Expand Down