summaryrefslogtreecommitdiffstats
path: root/test/units/plugins/connection/test_psrp.py
blob: 38052e81d3854bebb84d39e69ad6e25ac5b2917e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# -*- coding: utf-8 -*-
# (c) 2018, Jordan Borean <jborean@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest
import sys

from io import StringIO
from unittest.mock import MagicMock

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


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

    # 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
        (
            {'_extras': {}},
            {
                '_psrp_auth': 'negotiate',
                '_psrp_cert_validation': True,
                '_psrp_configuration_name': 'Microsoft.PowerShell',
                '_psrp_connection_timeout': 30,
                '_psrp_message_encryption': 'auto',
                '_psrp_host': 'inventory_hostname',
                '_psrp_conn_kwargs': {
                    'server': 'inventory_hostname',
                    'port': 5986,
                    'username': None,
                    'password': None,
                    'ssl': True,
                    'path': 'wsman',
                    'auth': 'negotiate',
                    'cert_validation': True,
                    'connection_timeout': 30,
                    'encryption': 'auto',
                    'proxy': None,
                    '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,
                '_psrp_operation_timeout': 20,
                '_psrp_pass': None,
                '_psrp_path': 'wsman',
                '_psrp_port': 5986,
                '_psrp_proxy': None,
                '_psrp_protocol': 'https',
                '_psrp_user': None
            },
        ),
        # ssl=False when port defined to 5985
        (
            {'_extras': {}, 'ansible_port': '5985'},
            {
                '_psrp_port': 5985,
                '_psrp_protocol': 'http'
            },
        ),
        # ssl=True when port defined to not 5985
        (
            {'_extras': {}, 'ansible_port': 1234},
            {
                '_psrp_port': 1234,
                '_psrp_protocol': 'https'
            },
        ),
        # port 5986 when ssl=True
        (
            {'_extras': {}, 'ansible_psrp_protocol': 'https'},
            {
                '_psrp_port': 5986,
                '_psrp_protocol': 'https'
            },
        ),
        # port 5985 when ssl=False
        (
            {'_extras': {}, 'ansible_psrp_protocol': 'http'},
            {
                '_psrp_port': 5985,
                '_psrp_protocol': 'http'
            },
        ),
        # psrp extras
        (
            {'_extras': {'ansible_psrp_mock_test1': True}},
            {
                '_psrp_conn_kwargs': {
                    'server': 'inventory_hostname',
                    'port': 5986,
                    'username': None,
                    'password': None,
                    'ssl': True,
                    'path': 'wsman',
                    'auth': 'negotiate',
                    'cert_validation': True,
                    'connection_timeout': 30,
                    'encryption': 'auto',
                    'proxy': None,
                    '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,
                    'mock_test1': True
                },
            },
        ),
        # cert validation through string repr of bool
        (
            {'_extras': {}, 'ansible_psrp_cert_validation': 'ignore'},
            {
                '_psrp_cert_validation': False
            },
        ),
        # cert validation path
        (
            {'_extras': {}, 'ansible_psrp_cert_trust_path': '/path/cert.pem'},
            {
                '_psrp_cert_validation': '/path/cert.pem'
            },
        ),
    )

    # pylint bug: https://github.com/PyCQA/pylint/issues/511
    # pylint: disable=undefined-variable
    @pytest.mark.parametrize('options, expected',
                             ((o, e) for o, e in OPTIONS_DATA))
    def test_set_options(self, options, expected):
        pc = PlayContext()
        new_stdin = StringIO()

        conn = connection_loader.get('psrp', pc, new_stdin)
        conn.set_options(var_options=options)
        conn._build_kwargs()

        for attr, expected in expected.items():
            actual = getattr(conn, attr)
            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'