Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for the psrp connection plugin #55061

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 91 additions & 6 deletions test/units/plugins/connection/test_psrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,65 @@
__metaclass__ = type

import pytest
import sys

from io import StringIO
from units.compat.mock import MagicMock

from ansible.playbook.play_context import PlayContext
from ansible.plugins.loader import connection_loader
from ansible.utils.display import Display

pytest.importorskip("pypsrp")

@pytest.fixture(autouse=True)
def psrp_connection():
"""Imports the psrp connection plugin with a mocked pypsrp module for testing"""

class TestConnectionWinRM(object):
# Take a snapshot of sys.modules before we manipulate it
orig_modules = sys.modules.copy()
try:
fake_pypsrp = MagicMock()
fake_pypsrp.FEATURES = [
'wsman_locale',
'wsman_read_timeout',
'wsman_reconnections',
]

fake_wsman = MagicMock()
fake_wsman.AUTH_KWARGS = {
"certificate": ["certificate_key_pem", "certificate_pem"],
"credssp": ["credssp_auth_mechanism", "credssp_disable_tlsv1_2",
"credssp_minimum_version"],
"negotiate": ["negotiate_delegate", "negotiate_hostname_override",
"negotiate_send_cbt", "negotiate_service"],
"mock": ["mock_test1", "mock_test2"],
}

sys.modules["pypsrp"] = fake_pypsrp
sys.modules["pypsrp.complex_objects"] = MagicMock()
sys.modules["pypsrp.exceptions"] = MagicMock()
sys.modules["pypsrp.host"] = MagicMock()
sys.modules["pypsrp.powershell"] = MagicMock()
sys.modules["pypsrp.shell"] = MagicMock()
sys.modules["pypsrp.wsman"] = fake_wsman
sys.modules["requests.exceptions"] = MagicMock()

from ansible.plugins.connection import psrp

# Take a copy of the original import state vars before we set to an ok import
orig_has_psrp = psrp.HAS_PYPSRP
orig_psrp_imp_err = psrp.PYPSRP_IMP_ERR

yield psrp

psrp.HAS_PYPSRP = orig_has_psrp
psrp.PYPSRP_IMP_ERR = orig_psrp_imp_err
finally:
# Restore sys.modules back to our pre-shenanigans
sys.modules = orig_modules


class TestConnectionPSRP(object):

OPTIONS_DATA = (
# default options
Expand Down Expand Up @@ -44,6 +93,18 @@ class TestConnectionWinRM(object):
'no_proxy': False,
'max_envelope_size': 153600,
'operation_timeout': 20,
'certificate_key_pem': None,
'certificate_pem': None,
'credssp_auth_mechanism': 'auto',
'credssp_disable_tlsv1_2': False,
'credssp_minimum_version': 2,
'negotiate_delegate': None,
'negotiate_hostname_override': None,
'negotiate_send_cbt': True,
'negotiate_service': 'WSMAN',
'read_timeout': 30,
'reconnection_backoff': 2.0,
'reconnection_retries': 0,
},
'_psrp_max_envelope_size': 153600,
'_psrp_ignore_proxy': False,
Expand Down Expand Up @@ -90,7 +151,7 @@ class TestConnectionWinRM(object):
),
# psrp extras
(
{'_extras': {'ansible_psrp_negotiate_delegate': True}},
{'_extras': {'ansible_psrp_mock_test1': True}},
{
'_psrp_conn_kwargs': {
'server': 'inventory_hostname',
Expand All @@ -107,8 +168,19 @@ class TestConnectionWinRM(object):
'no_proxy': False,
'max_envelope_size': 153600,
'operation_timeout': 20,
'negotiate_delegate': True,

'certificate_key_pem': None,
'certificate_pem': None,
'credssp_auth_mechanism': 'auto',
'credssp_disable_tlsv1_2': False,
'credssp_minimum_version': 2,
'negotiate_delegate': None,
'negotiate_hostname_override': None,
'negotiate_send_cbt': True,
'negotiate_service': 'WSMAN',
'read_timeout': 30,
'reconnection_backoff': 2.0,
'reconnection_retries': 0,
'mock_test1': True
},
},
),
Expand All @@ -128,7 +200,6 @@ class TestConnectionWinRM(object):
),
)

@pytest.mark.skip(reason="tests are not passing")
# pylint bug: https://github.com/PyCQA/pylint/issues/511
# pylint: disable=undefined-variable
@pytest.mark.parametrize('options, expected',
Expand All @@ -146,3 +217,17 @@ def test_set_options(self, options, expected):
assert actual == expected, \
"psrp attr '%s', actual '%s' != expected '%s'"\
% (attr, actual, expected)

def test_set_invalid_extras_options(self, monkeypatch):
pc = PlayContext()
new_stdin = StringIO()

conn = connection_loader.get('psrp', pc, new_stdin)
conn.set_options(var_options={'_extras': {'ansible_psrp_mock_test3': True}})

mock_display = MagicMock()
monkeypatch.setattr(Display, "warning", mock_display)
conn._build_kwargs()

assert mock_display.call_args[0][0] == \
'ansible_psrp_mock_test3 is unsupported by the current psrp version installed'