mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add tests for issue 64588
This commit is contained in:
parent
92b46fb4ff
commit
2577728579
1 changed files with 47 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue