Skip to content

Commit

Permalink
Merge pull request #394 from Limmen/ssh
Browse files Browse the repository at this point in the history
unit test ssh_util
  • Loading branch information
Limmen authored Jul 18, 2024
2 parents 90d9d1f + 415183b commit a433e4f
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions simulation-system/libs/csle-common/tests/test_ssh_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from csle_common.util.ssh_util import SSHUtil
from unittest.mock import patch, MagicMock
import pytest


class TestSSHUtilSuite:
"""
Test suite for ssh_util
"""

@pytest.fixture(autouse=True)
def mock_sleep(self):
"""
Mock time.sleep to avoid delays
"""
with patch("time.sleep", return_value=None):
yield

@patch("csle_common.util.ssh_util.SSHUtil.execute_ssh_cmd")
def test_execute_ssh_cmds(self, mock_execute_ssh_cmd) -> None:
"""
Test the method that executes a list of commands over an ssh connection to the emulation
:param mock_execute_ssh_cmd: mock_execute_ssh_cmd
:return: None
"""
mock_execute_ssh_cmd.return_value = (b"output", b"error", 1.0)
cmds = ["ls", "pwd", "whoami"]
conn = MagicMock()
results = SSHUtil.execute_ssh_cmds(cmds, conn)
mock_execute_ssh_cmd.assert_called()
assert results == [(b"output", b"error", 1.0)] * len(cmds)

def test_execute_ssh_cmd(self) -> None:
"""
Test the method that executes an action on the emulation over a ssh connection
:return: None
"""
conn = MagicMock()
mock_transport = MagicMock()
mock_session = MagicMock()
mock_session.exit_status_ready.return_value = True
mock_session.recv_ready.return_value = True
mock_session.recv_stderr_ready.return_value = True
mock_session.recv.side_effect = [b"output", b""]
mock_session.recv_stderr.side_effect = [b"error", b""]
conn.get_transport.return_value = mock_transport
mock_transport.open_session.return_value = mock_session

with pytest.raises(ConnectionError, match="Connection failed"):
SSHUtil.execute_ssh_cmd("ls", conn)

0 comments on commit a433e4f

Please sign in to comment.