Skip to content

RemoteOperations::exec_command must not raise an exception when 'expect_error' is True (#159) #160

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
Next Next commit
test_local.py and test_exec_command_failure__expect_error are added
test_local.py contains set of test for LocalOperations
 - test_exec_command_success
 - test_exec_command_failure
 - test_exec_command_failure__expect_error

Changes in TestRemoteOperations:
 - test_exec_command_failure exptects an exception
 - new test test_exec_command_failure__expect_error was added

TestRemoteOperations::test_exec_command_failure__expect_error will fail because RemoteOperations::exec_command does not handle the 'expect_error' parameter correctly.
  • Loading branch information
dmitry-lipetsk committed Dec 7, 2024
commit 22c649dcaa8129bc240d49b24f2e363832e70d9a
46 changes: 46 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from testgres import ExecUtilException
from testgres import LocalOperations


class TestLocalOperations:

@pytest.fixture(scope="function", autouse=True)
def setup(self):
self.operations = LocalOperations()

def test_exec_command_success(self):
"""
Test exec_command for successful command execution.
"""
cmd = "python3 --version"
response = self.operations.exec_command(cmd, wait_exit=True, shell=True)

assert b'Python 3.' in response

def test_exec_command_failure(self):
"""
Test exec_command for command execution failure.
"""
cmd = "nonexistent_command"
while True:
try:
self.operations.exec_command(cmd, wait_exit=True, shell=True)
except ExecUtilException as e:
error = e.message
break
raise Exception("We wait an exception!")
assert error == "Utility exited with non-zero code. Error `b'/bin/sh: 1: nonexistent_command: not found\\n'`"

def test_exec_command_failure__expect_error(self):
"""
Test exec_command for command execution failure.
"""
cmd = "nonexistent_command"

exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True, shell=True, expect_error=True)

assert error == b'/bin/sh: 1: nonexistent_command: not found\n'
assert exit_status == 127
assert result == b''
23 changes: 19 additions & 4 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@ def test_exec_command_failure(self):
Test exec_command for command execution failure.
"""
cmd = "nonexistent_command"
try:
exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True)
except ExecUtilException as e:
error = e.message
while True:
try:
self.operations.exec_command(cmd, verbose=True, wait_exit=True)
except ExecUtilException as e:
error = e.message
break
raise Exception("We wait an exception!")
assert error == b'Utility exited with non-zero code. Error: bash: line 1: nonexistent_command: command not found\n'

def test_exec_command_failure__expect_error(self):
"""
Test exec_command for command execution failure.
"""
cmd = "nonexistent_command"

exit_status, result, error = self.operations.exec_command(cmd, verbose=True, wait_exit=True, shell=True, expect_error=True)

assert error == b'bash: line 1: nonexistent_command: command not found\n'
assert exit_status == 127
assert result == b''

def test_is_executable_true(self):
"""
Test is_executable for an existing executable.
Expand Down