Add tests for issue 64588

This commit is contained in:
jeanluc 2023-06-30 20:35:12 +02:00 committed by Pedro Algarvio
parent 92b46fb4ff
commit 2577728579

View file

@ -4,7 +4,7 @@ import types
import pytest
import salt.client.ssh.shell as shell
from tests.support.mock import patch
from tests.support.mock import MagicMock, PropertyMock, patch
@pytest.fixture
@ -52,3 +52,49 @@ def test_ssh_shell_exec_cmd(caplog):
ret = _shell.exec_cmd("ls {}".format(passwd))
assert not any([x for x in ret if passwd in str(x)])
assert passwd not in caplog.text
def test_ssh_shell_exec_cmd_waits_for_term_close_before_reading_exit_status():
"""
Ensure that the terminal is always closed before accessing its exitstatus.
"""
term = MagicMock()
has_unread_data = PropertyMock(side_effect=(True, True, False))
exitstatus = PropertyMock(
side_effect=lambda *args: 0 if term._closed is True else None
)
term.close.side_effect = lambda *args, **kwargs: setattr(term, "_closed", True)
type(term).has_unread_data = has_unread_data
type(term).exitstatus = exitstatus
term.recv.side_effect = (("hi ", ""), ("there", ""), (None, None), (None, None))
shl = shell.Shell({}, "localhost")
with patch("salt.utils.vt.Terminal", autospec=True, return_value=term):
stdout, stderr, retcode = shl.exec_cmd("do something")
assert stdout == "hi there"
assert stderr == ""
assert retcode == 0
def test_ssh_shell_exec_cmd_returns_status_code_with_highest_bit_set_if_process_dies():
"""
Ensure that if a child process dies as the result of a signal instead of exiting
regularly, the shell returns the signal code encoded in the lowest seven bits with
the highest one set, not None.
"""
term = MagicMock()
term.exitstatus = None
term.signalstatus = 9
has_unread_data = PropertyMock(side_effect=(True, True, False))
type(term).has_unread_data = has_unread_data
term.recv.side_effect = (
("", "leave me alone"),
("", " please"),
(None, None),
(None, None),
)
shl = shell.Shell({}, "localhost")
with patch("salt.utils.vt.Terminal", autospec=True, return_value=term):
stdout, stderr, retcode = shl.exec_cmd("do something")
assert stdout == ""
assert stderr == "leave me alone please"
assert retcode == 137