| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2018, Jordan Borean <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
import sys |
| 9 |
import typing as t |
| 10 |
|
| 11 |
from unittest.mock import MagicMock |
| 12 |
|
| 13 |
from ansible.playbook.play_context import PlayContext |
| 14 |
from ansible.plugins.loader import connection_loader |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(autouse=True) |
| 18 |
def psrp_connection(): |
| 19 |
"""Imports the psrp connection plugin with a mocked pypsrp module for testing""" |
| 20 |
|
| 21 |
# Take a snapshot of sys.modules before we manipulate it |
| 22 |
orig_modules = sys.modules.copy() |
| 23 |
try: |
| 24 |
sys.modules["pypsrp.complex_objects"] = MagicMock() |
| 25 |
sys.modules["pypsrp.exceptions"] = MagicMock() |
| 26 |
sys.modules["pypsrp.host"] = MagicMock() |
| 27 |
sys.modules["pypsrp.powershell"] = MagicMock() |
| 28 |
sys.modules["pypsrp.shell"] = MagicMock() |
| 29 |
sys.modules["pypsrp.wsman"] = MagicMock() |
| 30 |
sys.modules["requests.exceptions"] = MagicMock() |
| 31 |
|
| 32 |
from ansible.plugins.connection import psrp |
| 33 |
|
| 34 |
# Take a copy of the original import state vars before we set to an ok import |
| 35 |
orig_has_psrp = psrp.HAS_PYPSRP |
| 36 |
orig_psrp_imp_err = psrp.PYPSRP_IMP_ERR |
| 37 |
|
| 38 |
yield psrp |
| 39 |
|
| 40 |
psrp.HAS_PYPSRP = orig_has_psrp |
| 41 |
psrp.PYPSRP_IMP_ERR = orig_psrp_imp_err |
| 42 |
finally: |
| 43 |
# Restore sys.modules back to our pre-shenanigans |
| 44 |
sys.modules = orig_modules |
| 45 |
|
| 46 |
|
| 47 |
class TestConnectionPSRP(object): |
| 48 |
|
| 49 |
OPTIONS_DATA: tuple[tuple[dict[str, t.Any], dict[str, t.Any]], ...] = ( |
| 50 |
# default options |
| 51 |
( |
| 52 |
{}, |
| 53 |
{ |
| 54 |
'_psrp_auth': 'negotiate', |
| 55 |
'_psrp_runspace_kwargs': { |
| 56 |
'configuration_name': 'Microsoft.PowerShell', |
| 57 |
}, |
| 58 |
'_psrp_host': 'inventory_hostname', |
| 59 |
'_psrp_conn_kwargs': { |
| 60 |
'server': 'inventory_hostname', |
| 61 |
'port': 5986, |
| 62 |
'username': None, |
| 63 |
'password': None, |
| 64 |
'ssl': True, |
| 65 |
'path': 'wsman', |
| 66 |
'auth': 'negotiate', |
| 67 |
'cert_validation': True, |
| 68 |
'connection_timeout': 30, |
| 69 |
'encryption': 'auto', |
| 70 |
'proxy': None, |
| 71 |
'no_proxy': False, |
| 72 |
'max_envelope_size': 153600, |
| 73 |
'operation_timeout': 20, |
| 74 |
'certificate_key_pem': None, |
| 75 |
'certificate_pem': None, |
| 76 |
'credssp_auth_mechanism': 'auto', |
| 77 |
'credssp_disable_tlsv1_2': False, |
| 78 |
'credssp_minimum_version': 2, |
| 79 |
'negotiate_delegate': None, |
| 80 |
'negotiate_hostname_override': None, |
| 81 |
'negotiate_send_cbt': True, |
| 82 |
'negotiate_service': 'host', |
| 83 |
'read_timeout': 30, |
| 84 |
'reconnection_backoff': 2.0, |
| 85 |
'reconnection_retries': 0, |
| 86 |
}, |
| 87 |
'_psrp_port': 5986, |
| 88 |
'_psrp_user': None |
| 89 |
}, |
| 90 |
), |
| 91 |
# ssl=False when port defined to 5985 |
| 92 |
( |
| 93 |
{'ansible_port': '5985'}, |
| 94 |
{ |
| 95 |
'_psrp_port': 5985, |
| 96 |
'_psrp_conn_kwargs': {'ssl': False}, |
| 97 |
}, |
| 98 |
), |
| 99 |
# ssl=True when port defined to not 5985 |
| 100 |
( |
| 101 |
{'ansible_port': 1234}, |
| 102 |
{ |
| 103 |
'_psrp_port': 1234, |
| 104 |
'_psrp_conn_kwargs': {'ssl': True}, |
| 105 |
}, |
| 106 |
), |
| 107 |
# port 5986 when ssl=True |
| 108 |
( |
| 109 |
{'ansible_psrp_protocol': 'https'}, |
| 110 |
{ |
| 111 |
'_psrp_port': 5986, |
| 112 |
'_psrp_conn_kwargs': {'ssl': True}, |
| 113 |
}, |
| 114 |
), |
| 115 |
# port 5985 when ssl=False |
| 116 |
( |
| 117 |
{'ansible_psrp_protocol': 'http'}, |
| 118 |
{ |
| 119 |
'_psrp_port': 5985, |
| 120 |
'_psrp_conn_kwargs': {'ssl': False}, |
| 121 |
}, |
| 122 |
), |
| 123 |
# psrp extras |
| 124 |
( |
| 125 |
{'ansible_psrp_mock_test1': True}, |
| 126 |
{ |
| 127 |
'_psrp_conn_kwargs': { |
| 128 |
'server': 'inventory_hostname', |
| 129 |
'port': 5986, |
| 130 |
'username': None, |
| 131 |
'password': None, |
| 132 |
'ssl': True, |
| 133 |
'path': 'wsman', |
| 134 |
'auth': 'negotiate', |
| 135 |
'cert_validation': True, |
| 136 |
'connection_timeout': 30, |
| 137 |
'encryption': 'auto', |
| 138 |
'proxy': None, |
| 139 |
'no_proxy': False, |
| 140 |
'max_envelope_size': 153600, |
| 141 |
'operation_timeout': 20, |
| 142 |
'certificate_key_pem': None, |
| 143 |
'certificate_pem': None, |
| 144 |
'credssp_auth_mechanism': 'auto', |
| 145 |
'credssp_disable_tlsv1_2': False, |
| 146 |
'credssp_minimum_version': 2, |
| 147 |
'negotiate_delegate': None, |
| 148 |
'negotiate_hostname_override': None, |
| 149 |
'negotiate_send_cbt': True, |
| 150 |
'negotiate_service': 'host', |
| 151 |
'read_timeout': 30, |
| 152 |
'reconnection_backoff': 2.0, |
| 153 |
'reconnection_retries': 0, |
| 154 |
}, |
| 155 |
}, |
| 156 |
), |
| 157 |
# cert validation through string repr of bool |
| 158 |
( |
| 159 |
{'ansible_psrp_cert_validation': 'ignore'}, |
| 160 |
{ |
| 161 |
'_psrp_conn_kwargs': {'cert_validation': False}, |
| 162 |
}, |
| 163 |
), |
| 164 |
# cert validation path |
| 165 |
( |
| 166 |
{'ansible_psrp_cert_trust_path': '/path/cert.pem'}, |
| 167 |
{ |
| 168 |
'_psrp_conn_kwargs': {'cert_validation': '/path/cert.pem'}, |
| 169 |
}, |
| 170 |
), |
| 171 |
# ignore proxy boolean value |
| 172 |
( |
| 173 |
{'ansible_psrp_ignore_proxy': 'true'}, |
| 174 |
{ |
| 175 |
'_psrp_conn_kwargs': {'no_proxy': True}, |
| 176 |
} |
| 177 |
), |
| 178 |
# ignore proxy false-ish value |
| 179 |
( |
| 180 |
{'ansible_psrp_ignore_proxy': 'n'}, |
| 181 |
{ |
| 182 |
'_psrp_conn_kwargs': {'no_proxy': False}, |
| 183 |
} |
| 184 |
), |
| 185 |
# ignore proxy true-ish value |
| 186 |
( |
| 187 |
{'ansible_psrp_ignore_proxy': 'y'}, |
| 188 |
{ |
| 189 |
'_psrp_conn_kwargs': {'no_proxy': True}, |
| 190 |
} |
| 191 |
), |
| 192 |
) |
| 193 |
|
| 194 |
@pytest.mark.parametrize('options, expected', list(OPTIONS_DATA)) |
| 195 |
def test_set_options(self, options, expected): |
| 196 |
pc = PlayContext() |
| 197 |
|
| 198 |
conn = connection_loader.get('psrp', pc) |
| 199 |
conn.set_options(var_options=options) |
| 200 |
conn._build_kwargs() |
| 201 |
|
| 202 |
for attr, expected in expected.items(): |
| 203 |
actual = getattr(conn, attr) |
| 204 |
|
| 205 |
if attr == '_psrp_conn_kwargs': |
| 206 |
for k, v in expected.items(): |
| 207 |
actual_v = actual[k] |
| 208 |
assert actual_v == v, \ |
| 209 |
f"psrp Protocol kwarg '{k}', actual '{actual_v}' != expected '{v}'" |
| 210 |
|
| 211 |
else: |
| 212 |
assert actual == expected, \ |
| 213 |
"psrp attr '%s', actual '%s' != expected '%s'"\ |
| 214 |
% (attr, actual, expected) |