diff options
author | Jordan Borean <jborean93@gmail.com> | 2023-10-05 20:55:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 20:55:47 +0200 |
commit | 976067c15fea8c416fc41d264a221535c6f38872 (patch) | |
tree | 44006c74ad5b077b7e66b5f6131c72e27ce190f2 /test/units/plugins | |
parent | Add test coverage for winrm (#81910) (diff) | |
download | ansible-976067c15fea8c416fc41d264a221535c6f38872.tar.xz ansible-976067c15fea8c416fc41d264a221535c6f38872.zip |
Add more winrm coverage (#81923)
Diffstat (limited to 'test/units/plugins')
-rw-r--r-- | test/units/plugins/connection/test_winrm.py | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/test/units/plugins/connection/test_winrm.py b/test/units/plugins/connection/test_winrm.py index d21d48f7a2..215a4d8989 100644 --- a/test/units/plugins/connection/test_winrm.py +++ b/test/units/plugins/connection/test_winrm.py @@ -11,7 +11,7 @@ import pytest from io import StringIO from unittest.mock import MagicMock -from ansible.errors import AnsibleConnectionFailure +from ansible.errors import AnsibleConnectionFailure, AnsibleError from ansible.module_utils.common.text.converters import to_bytes from ansible.playbook.play_context import PlayContext from ansible.plugins.loader import connection_loader @@ -459,3 +459,103 @@ class TestWinRMKerbAuth(object): conn.exec_command('cmd', in_data=None, sudoable=True) assert str(e.value) == "winrm connection error: msg" + + def test_exec_command_get_output_timeout(self, monkeypatch): + requests_exc = pytest.importorskip("requests.exceptions") + + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + + mock_proto = MagicMock() + mock_proto.run_command.return_value = "command_id" + mock_proto.get_command_output.side_effect = requests_exc.Timeout("msg") + + conn._connected = True + conn._winrm_host = 'hostname' + + monkeypatch.setattr(conn, "_winrm_connect", lambda: mock_proto) + + with pytest.raises(AnsibleConnectionFailure) as e: + conn.exec_command('cmd', in_data=None, sudoable=True) + + assert str(e.value) == "winrm connection error: msg" + + def test_exec_command_with_timeout(self, monkeypatch): + requests_exc = pytest.importorskip("requests.exceptions") + + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + + mock_proto = MagicMock() + mock_proto.run_command.side_effect = requests_exc.Timeout("msg") + + conn._connected = True + conn._winrm_host = 'hostname' + + monkeypatch.setattr(conn, "_winrm_connect", lambda: mock_proto) + + with pytest.raises(AnsibleConnectionFailure) as e: + conn.exec_command('cmd', in_data=None, sudoable=True) + + assert str(e.value) == "winrm connection error: msg" + + def test_connect_failure_auth_401(self, monkeypatch): + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + conn.set_options(var_options={"ansible_winrm_transport": "basic", "_extras": {}}) + + mock_proto = MagicMock() + mock_proto.open_shell.side_effect = ValueError("Custom exc Code 401") + + mock_proto_init = MagicMock() + mock_proto_init.return_value = mock_proto + monkeypatch.setattr(winrm, "Protocol", mock_proto_init) + + with pytest.raises(AnsibleConnectionFailure, match="the specified credentials were rejected by the server"): + conn.exec_command('cmd', in_data=None, sudoable=True) + + def test_connect_failure_other_exception(self, monkeypatch): + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + conn.set_options(var_options={"ansible_winrm_transport": "basic", "_extras": {}}) + + mock_proto = MagicMock() + mock_proto.open_shell.side_effect = ValueError("Custom exc") + + mock_proto_init = MagicMock() + mock_proto_init.return_value = mock_proto + monkeypatch.setattr(winrm, "Protocol", mock_proto_init) + + with pytest.raises(AnsibleConnectionFailure, match="basic: Custom exc"): + conn.exec_command('cmd', in_data=None, sudoable=True) + + def test_connect_failure_operation_timed_out(self, monkeypatch): + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + conn.set_options(var_options={"ansible_winrm_transport": "basic", "_extras": {}}) + + mock_proto = MagicMock() + mock_proto.open_shell.side_effect = ValueError("Custom exc Operation timed out") + + mock_proto_init = MagicMock() + mock_proto_init.return_value = mock_proto + monkeypatch.setattr(winrm, "Protocol", mock_proto_init) + + with pytest.raises(AnsibleError, match="the connection attempt timed out"): + conn.exec_command('cmd', in_data=None, sudoable=True) + + def test_connect_no_transport(self): + pc = PlayContext() + new_stdin = StringIO() + conn = connection_loader.get('winrm', pc, new_stdin) + conn.set_options(var_options={"_extras": {}}) + conn._build_winrm_kwargs() + conn._winrm_transport = [] + + with pytest.raises(AnsibleError, match="No transport found for WinRM connection"): + conn._winrm_connect() |