Skip to content
Prev Previous commit
Next Next commit
TestOsOpsCommon is updated
New tests:
- test_exec_command_success
- test_exec_command_failure
- test_exec_command_failure__expect_error
  • Loading branch information
dmitry-lipetsk committed Apr 2, 2025
commit e43a38484ee4a82c0dff69f21fab7d8d6839d6da
55 changes: 0 additions & 55 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import re
import logging

from ..testgres import ExecUtilException
from ..testgres import LocalOperations

from .helpers.run_conditions import RunConditions


class TestLocalOperations:

Expand All @@ -33,58 +30,6 @@ def test_mkdtemp__custom(self):
os.rmdir(path)
assert not os.path.exists(path)

def test_exec_command_success(self):
"""
Test exec_command for successful command execution.
"""
RunConditions.skip_if_windows()

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.
"""
RunConditions.skip_if_windows()

cmd = "nonexistent_command"
while True:
try:
self.operations.exec_command(cmd, wait_exit=True, shell=True)
except ExecUtilException as e:
assert type(e.exit_code) == int # noqa: E721
assert e.exit_code == 127

assert type(e.message) == str # noqa: E721
assert type(e.error) == bytes # noqa: E721

assert e.message.startswith("Utility exited with non-zero code (127). Error:")
assert "nonexistent_command" in e.message
assert "not found" in e.message
assert b"nonexistent_command" in e.error
assert b"not found" in e.error
break
raise Exception("We wait an exception!")

def test_exec_command_failure__expect_error(self):
"""
Test exec_command for command execution failure.
"""
RunConditions.skip_if_windows()

cmd = "nonexistent_command"

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

assert exit_status == 127
assert result == b''
assert type(error) == bytes # noqa: E721
assert b"nonexistent_command" in error
assert b"not found" in error

def test_read__unknown_file(self):
"""
Test LocalOperations::read with unknown file.
Expand Down
61 changes: 61 additions & 0 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tempfile

from ..testgres import InvalidOperationException
from ..testgres import ExecUtilException


class TestOsOpsCommon:
Expand All @@ -28,6 +29,66 @@ def os_ops(self, request: pytest.FixtureRequest) -> OsOperations:
assert isinstance(request.param, OsOperations)
return request.param

def test_exec_command_success(self, os_ops: OsOperations):
"""
Test exec_command for successful command execution.
"""
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

cmd = ["sh", "-c", "python3 --version"]

response = os_ops.exec_command(cmd)

assert b'Python 3.' in response

def test_exec_command_failure(self, os_ops: OsOperations):
"""
Test exec_command for command execution failure.
"""
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

cmd = ["sh", "-c", "nonexistent_command"]

while True:
try:
os_ops.exec_command(cmd)
except ExecUtilException as e:
assert type(e.exit_code) == int # noqa: E721
assert e.exit_code == 127

assert type(e.message) == str # noqa: E721
assert type(e.error) == bytes # noqa: E721

assert e.message.startswith("Utility exited with non-zero code (127). Error:")
assert "nonexistent_command" in e.message
assert "not found" in e.message
assert b"nonexistent_command" in e.error
assert b"not found" in e.error
break
raise Exception("We wait an exception!")

def test_exec_command_failure__expect_error(self, os_ops: OsOperations):
"""
Test exec_command for command execution failure.
"""
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

cmd = ["sh", "-c", "nonexistent_command"]

exit_status, result, error = os_ops.exec_command(cmd, verbose=True, expect_error=True)

assert exit_status == 127
assert result == b''
assert type(error) == bytes # noqa: E721
assert b"nonexistent_command" in error
assert b"not found" in error

def test_listdir(self, os_ops: OsOperations):
"""
Test listdir for listing directory contents.
Expand Down
46 changes: 0 additions & 46 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,6 @@ def setup(self):
ssh_key=os.getenv('RDBMS_TESTPOOL_SSHKEY'))
self.operations = RemoteOperations(conn_params)

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)

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, verbose=True, wait_exit=True)
except ExecUtilException as e:
assert type(e.exit_code) == int # noqa: E721
assert e.exit_code == 127

assert type(e.message) == str # noqa: E721
assert type(e.error) == bytes # noqa: E721

assert e.message.startswith("Utility exited with non-zero code (127). Error:")
assert "nonexistent_command" in e.message
assert "not found" in e.message
assert b"nonexistent_command" in e.error
assert b"not found" in e.error
break
raise Exception("We wait an exception!")

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 exit_status == 127
assert result == b''
assert type(error) == bytes # noqa: E721
assert b"nonexistent_command" in error
assert b"not found" in error

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