| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2015, Toshio Kuratomi <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
from io import StringIO |
| 22 |
from selectors import SelectorKey, EVENT_READ |
| 23 |
import pytest |
| 24 |
|
| 25 |
|
| 26 |
import unittest |
| 27 |
from unittest.mock import patch, MagicMock, PropertyMock |
| 28 |
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound |
| 29 |
import shlex |
| 30 |
from ansible.module_utils.common.text.converters import to_bytes |
| 31 |
from ansible.playbook.play_context import PlayContext |
| 32 |
from ansible.plugins.connection import ssh |
| 33 |
from ansible.plugins.loader import connection_loader, become_loader |
| 34 |
|
| 35 |
|
| 36 |
class TestConnectionBaseClass(unittest.TestCase): |
| 37 |
|
| 38 |
def test_plugins_connection_ssh_module(self): |
| 39 |
play_context = PlayContext() |
| 40 |
play_context.prompt = ( |
| 41 |
'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' |
| 42 |
) |
| 43 |
in_stream = StringIO() |
| 44 |
|
| 45 |
self.assertIsInstance(ssh.Connection(play_context, in_stream), ssh.Connection) |
| 46 |
|
| 47 |
def test_plugins_connection_ssh_basic(self): |
| 48 |
pc = PlayContext() |
| 49 |
new_stdin = StringIO() |
| 50 |
conn = ssh.Connection(pc, new_stdin) |
| 51 |
|
| 52 |
# connect just returns self, so assert that |
| 53 |
res = conn._connect() |
| 54 |
self.assertEqual(conn, res) |
| 55 |
|
| 56 |
conn.close() |
| 57 |
self.assertFalse(conn._connected) |
| 58 |
|
| 59 |
def test_plugins_connection_ssh_exec_command(self): |
| 60 |
pc = PlayContext() |
| 61 |
conn = connection_loader.get('ssh', pc) |
| 62 |
|
| 63 |
conn._build_command = MagicMock() |
| 64 |
conn._build_command.return_value = 'ssh something something' |
| 65 |
conn._run = MagicMock() |
| 66 |
conn._run.return_value = (0, 'stdout', 'stderr') |
| 67 |
conn.get_option = MagicMock() |
| 68 |
conn.get_option.return_value = True |
| 69 |
|
| 70 |
res, stdout, stderr = conn.exec_command('ssh') |
| 71 |
res, stdout, stderr = conn.exec_command('ssh', 'this is some data') |
| 72 |
|
| 73 |
def test_plugins_connection_ssh__examine_output(self): |
| 74 |
pc = PlayContext() |
| 75 |
become_success_token = b'BECOME-SUCCESS-abcdefghijklmnopqrstuvxyz' |
| 76 |
|
| 77 |
conn = connection_loader.get('ssh', pc) |
| 78 |
conn.set_become_plugin(become_loader.get('sudo')) |
| 79 |
|
| 80 |
conn.become.check_password_prompt = MagicMock() |
| 81 |
conn.become.check_success = MagicMock() |
| 82 |
conn.become.check_incorrect_password = MagicMock() |
| 83 |
conn.become.check_missing_password = MagicMock() |
| 84 |
|
| 85 |
def _check_password_prompt(line): |
| 86 |
return b'foo' in line |
| 87 |
|
| 88 |
def _check_become_success(line): |
| 89 |
return become_success_token in line |
| 90 |
|
| 91 |
def _check_incorrect_password(line): |
| 92 |
return b'incorrect password' in line |
| 93 |
|
| 94 |
def _check_missing_password(line): |
| 95 |
return b'bad password' in line |
| 96 |
|
| 97 |
# test examining output for prompt |
| 98 |
conn._flags = dict( |
| 99 |
become_prompt=False, |
| 100 |
become_success=False, |
| 101 |
become_error=False, |
| 102 |
become_nopasswd_error=False, |
| 103 |
) |
| 104 |
|
| 105 |
pc.prompt = True |
| 106 |
|
| 107 |
# override become plugin |
| 108 |
conn.become.prompt = True |
| 109 |
conn.become.check_password_prompt = MagicMock(side_effect=_check_password_prompt) |
| 110 |
conn.become.check_success = MagicMock(side_effect=_check_become_success) |
| 111 |
conn.become.check_incorrect_password = MagicMock(side_effect=_check_incorrect_password) |
| 112 |
conn.become.check_missing_password = MagicMock(side_effect=_check_missing_password) |
| 113 |
|
| 114 |
def get_option(option): |
| 115 |
assert option == 'become_pass' |
| 116 |
return 'password' |
| 117 |
|
| 118 |
conn.become.get_option = get_option |
| 119 |
output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\nfoo\nline 3\nthis should be the remainder', False) |
| 120 |
self.assertEqual(output, b'line 1\nline 2\nline 3\n') |
| 121 |
self.assertEqual(unprocessed, b'this should be the remainder') |
| 122 |
self.assertTrue(conn._flags['become_prompt']) |
| 123 |
self.assertFalse(conn._flags['become_success']) |
| 124 |
self.assertFalse(conn._flags['become_error']) |
| 125 |
self.assertFalse(conn._flags['become_nopasswd_error']) |
| 126 |
|
| 127 |
# test examining output for become prompt |
| 128 |
conn._flags = dict( |
| 129 |
become_prompt=False, |
| 130 |
become_success=False, |
| 131 |
become_error=False, |
| 132 |
become_nopasswd_error=False, |
| 133 |
) |
| 134 |
|
| 135 |
pc.prompt = False |
| 136 |
conn.become.prompt = False |
| 137 |
pc.success_key = str(become_success_token) |
| 138 |
conn.become.success = str(become_success_token) |
| 139 |
output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\n%s\nline 3\n' % become_success_token, False) |
| 140 |
self.assertEqual(output, b'line 1\nline 2\nline 3\n') |
| 141 |
self.assertEqual(unprocessed, b'') |
| 142 |
self.assertFalse(conn._flags['become_prompt']) |
| 143 |
self.assertTrue(conn._flags['become_success']) |
| 144 |
self.assertFalse(conn._flags['become_error']) |
| 145 |
self.assertFalse(conn._flags['become_nopasswd_error']) |
| 146 |
|
| 147 |
# test we dont detect become success from ssh debug: lines |
| 148 |
conn._flags = dict( |
| 149 |
become_prompt=False, |
| 150 |
become_success=False, |
| 151 |
become_error=False, |
| 152 |
become_nopasswd_error=False, |
| 153 |
) |
| 154 |
|
| 155 |
pc.prompt = False |
| 156 |
conn.become.prompt = True |
| 157 |
pc.success_key = str(become_success_token) |
| 158 |
conn.become.success = str(become_success_token) |
| 159 |
output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\ndebug1: %s\nline 3\n' % become_success_token, False) |
| 160 |
self.assertEqual(output, b'line 1\nline 2\ndebug1: %s\nline 3\n' % become_success_token) |
| 161 |
self.assertEqual(unprocessed, b'') |
| 162 |
self.assertFalse(conn._flags['become_success']) |
| 163 |
|
| 164 |
# test examining output for become failure |
| 165 |
conn._flags = dict( |
| 166 |
become_prompt=False, |
| 167 |
become_success=False, |
| 168 |
become_error=False, |
| 169 |
become_nopasswd_error=False, |
| 170 |
) |
| 171 |
|
| 172 |
pc.prompt = False |
| 173 |
conn.become.prompt = False |
| 174 |
pc.success_key = None |
| 175 |
output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\nincorrect password\n', True) |
| 176 |
self.assertEqual(output, b'line 1\nline 2\nincorrect password\n') |
| 177 |
self.assertEqual(unprocessed, b'') |
| 178 |
self.assertFalse(conn._flags['become_prompt']) |
| 179 |
self.assertFalse(conn._flags['become_success']) |
| 180 |
self.assertTrue(conn._flags['become_error']) |
| 181 |
self.assertFalse(conn._flags['become_nopasswd_error']) |
| 182 |
|
| 183 |
# test examining output for missing password |
| 184 |
conn._flags = dict( |
| 185 |
become_prompt=False, |
| 186 |
become_success=False, |
| 187 |
become_error=False, |
| 188 |
become_nopasswd_error=False, |
| 189 |
) |
| 190 |
|
| 191 |
pc.prompt = False |
| 192 |
conn.become.prompt = False |
| 193 |
pc.success_key = None |
| 194 |
output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nbad password\n', True) |
| 195 |
self.assertEqual(output, b'line 1\nbad password\n') |
| 196 |
self.assertEqual(unprocessed, b'') |
| 197 |
self.assertFalse(conn._flags['become_prompt']) |
| 198 |
self.assertFalse(conn._flags['become_success']) |
| 199 |
self.assertFalse(conn._flags['become_error']) |
| 200 |
self.assertTrue(conn._flags['become_nopasswd_error']) |
| 201 |
|
| 202 |
@patch('time.sleep') |
| 203 |
@patch('os.path.exists') |
| 204 |
def test_plugins_connection_ssh_put_file(self, mock_ospe, mock_sleep): |
| 205 |
pc = PlayContext() |
| 206 |
conn = connection_loader.get('ssh', pc) |
| 207 |
conn._build_command = MagicMock() |
| 208 |
conn._bare_run = MagicMock() |
| 209 |
|
| 210 |
mock_ospe.return_value = True |
| 211 |
conn._build_command.return_value = 'some command to run' |
| 212 |
conn._bare_run.return_value = (0, '', '') |
| 213 |
|
| 214 |
conn.set_option("host", "some_host") |
| 215 |
conn.set_option('reconnection_retries', 9) |
| 216 |
conn.set_option('ssh_transfer_method', None) # default is smart |
| 217 |
|
| 218 |
# Test when SFTP works |
| 219 |
expected_in_data = b' '.join((b'put', to_bytes(shlex.quote('/path/to/in/file')), to_bytes(shlex.quote('/path/to/dest/file')))) + b'\n' |
| 220 |
conn.put_file('/path/to/in/file', '/path/to/dest/file') |
| 221 |
conn._bare_run.assert_called_with('some command to run', expected_in_data, checkrc=False) |
| 222 |
|
| 223 |
# Test filenames with unicode |
| 224 |
expected_in_data = b' '.join((b'put', |
| 225 |
to_bytes(shlex.quote('/path/to/in/file/with/unicode-fö〩')), |
| 226 |
to_bytes(shlex.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n' |
| 227 |
conn.put_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩') |
| 228 |
conn._bare_run.assert_called_with('some command to run', expected_in_data, checkrc=False) |
| 229 |
|
| 230 |
# Test when SFTP doesn't work but SCP does |
| 231 |
conn._bare_run.side_effect = [(1, 'stdout', 'some errors'), (0, '', '')] |
| 232 |
conn.put_file('/path/to/in/file', '/path/to/dest/file') |
| 233 |
conn._bare_run.assert_called_with('some command to run', None, checkrc=False) |
| 234 |
conn._bare_run.side_effect = None |
| 235 |
|
| 236 |
# Test that a non-zero rc raises an error |
| 237 |
conn.set_option('ssh_transfer_method', 'sftp') |
| 238 |
conn._bare_run.return_value = (1, 'stdout', 'some errors') |
| 239 |
self.assertRaises(AnsibleError, conn.put_file, '/path/to/bad/file', '/remote/path/to/file') |
| 240 |
|
| 241 |
# Test that rc=255 raises an error |
| 242 |
conn._bare_run.return_value = (255, 'stdout', 'some errors') |
| 243 |
self.assertRaises(AnsibleConnectionFailure, conn.put_file, '/path/to/bad/file', '/remote/path/to/file') |
| 244 |
|
| 245 |
# Test that rc=256 raises an error |
| 246 |
conn._bare_run.return_value = (256, 'stdout', 'some errors') |
| 247 |
self.assertRaises(AnsibleError, conn.put_file, '/path/to/bad/file', '/remote/path/to/file') |
| 248 |
|
| 249 |
# Test that a not-found path raises an error |
| 250 |
mock_ospe.return_value = False |
| 251 |
conn._bare_run.return_value = (0, 'stdout', '') |
| 252 |
self.assertRaises(AnsibleFileNotFound, conn.put_file, '/path/to/bad/file', '/remote/path/to/file') |
| 253 |
|
| 254 |
@patch('time.sleep') |
| 255 |
def test_plugins_connection_ssh_fetch_file(self, mock_sleep): |
| 256 |
pc = PlayContext() |
| 257 |
conn = connection_loader.get('ssh', pc) |
| 258 |
conn._build_command = MagicMock() |
| 259 |
conn._bare_run = MagicMock() |
| 260 |
conn._load_name = 'ssh' |
| 261 |
|
| 262 |
conn._build_command.return_value = 'some command to run' |
| 263 |
conn._bare_run.return_value = (0, '', '') |
| 264 |
|
| 265 |
conn.set_option("host", "some_host") |
| 266 |
conn.set_option('reconnection_retries', 9) |
| 267 |
conn.set_option('ssh_transfer_method', None) # default is smart |
| 268 |
|
| 269 |
# Test when SFTP works |
| 270 |
expected_in_data = b' '.join((b'get', to_bytes(shlex.quote('/path/to/in/file')), to_bytes(shlex.quote('/path/to/dest/file')))) + b'\n' |
| 271 |
conn.set_options({}) |
| 272 |
conn.fetch_file('/path/to/in/file', '/path/to/dest/file') |
| 273 |
conn._bare_run.assert_called_with('some command to run', expected_in_data, checkrc=False) |
| 274 |
|
| 275 |
# Test when SFTP doesn't work but SCP does |
| 276 |
conn._bare_run.side_effect = [(1, 'stdout', 'some errors'), (0, '', '')] |
| 277 |
conn.fetch_file('/path/to/in/file', '/path/to/dest/file') |
| 278 |
conn._bare_run.assert_called_with('some command to run', None, checkrc=False) |
| 279 |
conn._bare_run.side_effect = None |
| 280 |
|
| 281 |
# Test when filename is unicode |
| 282 |
expected_in_data = b' '.join((b'get', |
| 283 |
to_bytes(shlex.quote('/path/to/in/file/with/unicode-fö〩')), |
| 284 |
to_bytes(shlex.quote('/path/to/dest/file/with/unicode-fö〩')))) + b'\n' |
| 285 |
conn.fetch_file(u'/path/to/in/file/with/unicode-fö〩', u'/path/to/dest/file/with/unicode-fö〩') |
| 286 |
conn._bare_run.assert_called_with('some command to run', expected_in_data, checkrc=False) |
| 287 |
conn._bare_run.side_effect = None |
| 288 |
|
| 289 |
# Test that a non-zero rc raises an error |
| 290 |
conn.set_option('ssh_transfer_method', 'sftp') |
| 291 |
conn._bare_run.return_value = (1, 'stdout', 'some errors') |
| 292 |
self.assertRaises(AnsibleError, conn.fetch_file, '/path/to/bad/file', '/remote/path/to/file') |
| 293 |
|
| 294 |
# Test that rc=255 raises an error |
| 295 |
conn._bare_run.return_value = (255, 'stdout', 'some errors') |
| 296 |
self.assertRaises(AnsibleConnectionFailure, conn.fetch_file, '/path/to/bad/file', '/remote/path/to/file') |
| 297 |
|
| 298 |
# Test that rc=256 raises an error |
| 299 |
conn._bare_run.return_value = (256, 'stdout', 'some errors') |
| 300 |
self.assertRaises(AnsibleError, conn.fetch_file, '/path/to/bad/file', '/remote/path/to/file') |
| 301 |
|
| 302 |
|
| 303 |
class MockSelector(object): |
| 304 |
def __init__(self): |
| 305 |
self.files_watched = 0 |
| 306 |
self.register = MagicMock(side_effect=self._register) |
| 307 |
self.unregister = MagicMock(side_effect=self._unregister) |
| 308 |
self.close = MagicMock() |
| 309 |
self.get_map = MagicMock() |
| 310 |
self.select = MagicMock() |
| 311 |
|
| 312 |
def _register(self, *args, **kwargs): |
| 313 |
self.files_watched += 1 |
| 314 |
|
| 315 |
def _unregister(self, *args, **kwargs): |
| 316 |
self.files_watched -= 1 |
| 317 |
|
| 318 |
|
| 319 |
@pytest.fixture |
| 320 |
def mock_run_env(request, mocker): |
| 321 |
pc = PlayContext() |
| 322 |
|
| 323 |
conn = connection_loader.get('ssh', pc) |
| 324 |
conn.set_become_plugin(become_loader.get('sudo')) |
| 325 |
conn._send_initial_data = MagicMock() |
| 326 |
conn._examine_output = MagicMock() |
| 327 |
conn._terminate_process = MagicMock() |
| 328 |
conn._load_name = 'ssh' |
| 329 |
conn.sshpass_pipe = [MagicMock(), MagicMock()] |
| 330 |
|
| 331 |
request.cls.pc = pc |
| 332 |
request.cls.conn = conn |
| 333 |
|
| 334 |
mock_popen_res = MagicMock() |
| 335 |
mock_popen_res.poll = MagicMock() |
| 336 |
mock_popen_res.wait = MagicMock() |
| 337 |
mock_popen_res.stdin = MagicMock() |
| 338 |
mock_popen_res.stdin.fileno.return_value = 1000 |
| 339 |
mock_popen_res.stdout = MagicMock() |
| 340 |
mock_popen_res.stdout.fileno.return_value = 1001 |
| 341 |
mock_popen_res.stderr = MagicMock() |
| 342 |
mock_popen_res.stderr.fileno.return_value = 1002 |
| 343 |
mock_popen_res.returncode = 0 |
| 344 |
request.cls.mock_popen_res = mock_popen_res |
| 345 |
|
| 346 |
mock_popen = mocker.patch('subprocess.Popen', return_value=mock_popen_res) |
| 347 |
request.cls.mock_popen = mock_popen |
| 348 |
|
| 349 |
request.cls.mock_selector = MockSelector() |
| 350 |
mocker.patch('selectors.DefaultSelector', lambda: request.cls.mock_selector) |
| 351 |
|
| 352 |
request.cls.mock_openpty = mocker.patch('pty.openpty') |
| 353 |
|
| 354 |
mocker.patch('fcntl.fcntl') |
| 355 |
mocker.patch('os.write') |
| 356 |
mocker.patch('os.close') |
| 357 |
|
| 358 |
|
| 359 |
@pytest.mark.usefixtures('mock_run_env') |
| 360 |
class TestSSHConnectionRun(object): |
| 361 |
# FIXME: |
| 362 |
# These tests are little more than a smoketest. Need to enhance them |
| 363 |
# a bit to check that they're calling the relevant functions and making |
| 364 |
# complete coverage of the code paths |
| 365 |
def test_no_escalation(self): |
| 366 |
self.mock_popen_res.stdout.read.side_effect = [b"my_stdout\n", b"second_line"] |
| 367 |
self.mock_popen_res.stderr.read.side_effect = [b"my_stderr"] |
| 368 |
self.mock_selector.select.side_effect = [ |
| 369 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 370 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 371 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 372 |
[]] |
| 373 |
self.mock_selector.get_map.side_effect = lambda: True |
| 374 |
|
| 375 |
return_code, b_stdout, b_stderr = self.conn._run([b"ssh"], b"this is input data") |
| 376 |
assert return_code == 0 |
| 377 |
assert b_stdout == b'my_stdout\nsecond_line' |
| 378 |
assert b_stderr == b'my_stderr' |
| 379 |
assert self.mock_selector.register.called is True |
| 380 |
assert self.mock_selector.register.call_count == 2 |
| 381 |
assert self.conn._send_initial_data.called is True |
| 382 |
assert self.conn._send_initial_data.call_count == 1 |
| 383 |
assert self.conn._send_initial_data.call_args[0][1] == b'this is input data' |
| 384 |
|
| 385 |
def _password_with_prompt_examine_output(self, sourice, state, b_chunk, sudoable): |
| 386 |
if state == 'awaiting_prompt': |
| 387 |
self.conn._flags['become_prompt'] = True |
| 388 |
else: |
| 389 |
assert state == 'awaiting_escalation' |
| 390 |
self.conn._flags['become_success'] = True |
| 391 |
return (b'', b'') |
| 392 |
|
| 393 |
def test_password_with_prompt(self): |
| 394 |
# test with password prompting enabled |
| 395 |
self.pc.password = None |
| 396 |
self.conn.become.prompt = b'Password:' |
| 397 |
self.conn._examine_output.side_effect = self._password_with_prompt_examine_output |
| 398 |
self.mock_popen_res.stdout.read.side_effect = [b"Password:", b"Success", b""] |
| 399 |
self.mock_popen_res.stderr.read.side_effect = [b""] |
| 400 |
self.mock_selector.select.side_effect = [ |
| 401 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 402 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 403 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ), |
| 404 |
(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 405 |
[]] |
| 406 |
self.mock_selector.get_map.side_effect = lambda: True |
| 407 |
|
| 408 |
return_code, b_stdout, b_stderr = self.conn._run([b"ssh"], b"this is input data") |
| 409 |
assert return_code == 0 |
| 410 |
assert b_stdout == b'' |
| 411 |
assert b_stderr == b'' |
| 412 |
assert self.mock_selector.register.called is True |
| 413 |
assert self.mock_selector.register.call_count == 2 |
| 414 |
assert self.conn._send_initial_data.called is True |
| 415 |
assert self.conn._send_initial_data.call_count == 1 |
| 416 |
assert self.conn._send_initial_data.call_args[0][1] == b'this is input data' |
| 417 |
|
| 418 |
def test_password_with_become(self): |
| 419 |
# test with some become settings |
| 420 |
self.pc.prompt = b'Password:' |
| 421 |
self.conn.become.prompt = b'Password:' |
| 422 |
self.pc.become = True |
| 423 |
self.pc.success_key = 'BECOME-SUCCESS-abcdefg' |
| 424 |
self.conn.become._id = 'abcdefg' |
| 425 |
self.conn._examine_output.side_effect = self._password_with_prompt_examine_output |
| 426 |
self.mock_popen_res.stdout.read.side_effect = [b"Password:", b"BECOME-SUCCESS-abcdefg", b"abc"] |
| 427 |
self.mock_popen_res.stderr.read.side_effect = [b"123"] |
| 428 |
self.mock_selector.select.side_effect = [ |
| 429 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 430 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 431 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 432 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 433 |
[]] |
| 434 |
self.mock_selector.get_map.side_effect = lambda: True |
| 435 |
|
| 436 |
return_code, b_stdout, b_stderr = self.conn._run([b"ssh"], b"this is input data") |
| 437 |
self.mock_popen_res.stdin.flush.assert_called_once_with() |
| 438 |
assert return_code == 0 |
| 439 |
assert b_stdout == b'abc' |
| 440 |
assert b_stderr == b'123' |
| 441 |
assert self.mock_selector.register.called is True |
| 442 |
assert self.mock_selector.register.call_count == 2 |
| 443 |
assert self.conn._send_initial_data.called is True |
| 444 |
assert self.conn._send_initial_data.call_count == 1 |
| 445 |
assert self.conn._send_initial_data.call_args[0][1] == b'this is input data' |
| 446 |
|
| 447 |
def test_password_without_data(self): |
| 448 |
# simulate no data input but Popen using new pty's fails |
| 449 |
self.mock_popen.return_value = None |
| 450 |
self.mock_popen.side_effect = [OSError(), self.mock_popen_res] |
| 451 |
|
| 452 |
# simulate no data input |
| 453 |
self.mock_openpty.return_value = (98, 99) |
| 454 |
self.mock_popen_res.stdout.read.side_effect = [b"some data", b"", b""] |
| 455 |
self.mock_popen_res.stderr.read.side_effect = [b""] |
| 456 |
self.mock_selector.select.side_effect = [ |
| 457 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 458 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 459 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 460 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 461 |
[]] |
| 462 |
self.mock_selector.get_map.side_effect = lambda: True |
| 463 |
|
| 464 |
return_code, b_stdout, b_stderr = self.conn._run([b"ssh"], b"") |
| 465 |
assert return_code == 0 |
| 466 |
assert b_stdout == b'some data' |
| 467 |
assert b_stderr == b'' |
| 468 |
assert self.mock_selector.register.called is True |
| 469 |
assert self.mock_selector.register.call_count == 2 |
| 470 |
assert self.conn._send_initial_data.called is False |
| 471 |
|
| 472 |
|
| 473 |
@pytest.mark.usefixtures('mock_run_env') |
| 474 |
class TestSSHConnectionRetries(object): |
| 475 |
def test_retry_then_success(self, monkeypatch): |
| 476 |
self.conn.set_option('host_key_checking', False) |
| 477 |
self.conn.set_option('reconnection_retries', 3) |
| 478 |
|
| 479 |
monkeypatch.setattr('time.sleep', lambda x: None) |
| 480 |
|
| 481 |
self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] |
| 482 |
self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] |
| 483 |
type(self.mock_popen_res).returncode = PropertyMock(side_effect=[255] * 3 + [0] * 4) |
| 484 |
|
| 485 |
self.mock_selector.select.side_effect = [ |
| 486 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 487 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 488 |
[], |
| 489 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 490 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 491 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 492 |
[] |
| 493 |
] |
| 494 |
self.mock_selector.get_map.side_effect = lambda: True |
| 495 |
|
| 496 |
self.conn._build_command = MagicMock() |
| 497 |
self.conn._build_command.return_value = 'ssh' |
| 498 |
|
| 499 |
return_code, b_stdout, b_stderr = self.conn.exec_command('ssh', 'some data') |
| 500 |
assert return_code == 0 |
| 501 |
assert b_stdout == b'my_stdout\nsecond_line' |
| 502 |
assert b_stderr == b'my_stderr' |
| 503 |
|
| 504 |
def test_multiple_failures(self, monkeypatch): |
| 505 |
self.conn.set_option('host_key_checking', False) |
| 506 |
self.conn.set_option('reconnection_retries', 9) |
| 507 |
|
| 508 |
monkeypatch.setattr('time.sleep', lambda x: None) |
| 509 |
|
| 510 |
self.mock_popen_res.stdout.read.side_effect = [b""] * 10 |
| 511 |
self.mock_popen_res.stderr.read.side_effect = [b""] * 10 |
| 512 |
type(self.mock_popen_res).returncode = PropertyMock(side_effect=[255] * 30) |
| 513 |
|
| 514 |
self.mock_selector.select.side_effect = [ |
| 515 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 516 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 517 |
[], |
| 518 |
] * 10 |
| 519 |
self.mock_selector.get_map.side_effect = lambda: True |
| 520 |
|
| 521 |
self.conn._build_command = MagicMock() |
| 522 |
self.conn._build_command.return_value = 'ssh' |
| 523 |
|
| 524 |
pytest.raises(AnsibleConnectionFailure, self.conn.exec_command, 'ssh', 'some data') |
| 525 |
assert self.mock_popen.call_count == 10 |
| 526 |
|
| 527 |
def test_abitrary_exceptions(self, monkeypatch): |
| 528 |
self.conn.set_option('host_key_checking', False) |
| 529 |
self.conn.set_option('reconnection_retries', 9) |
| 530 |
|
| 531 |
monkeypatch.setattr('time.sleep', lambda x: None) |
| 532 |
|
| 533 |
self.conn._build_command = MagicMock() |
| 534 |
self.conn._build_command.return_value = 'ssh' |
| 535 |
|
| 536 |
self.mock_popen.side_effect = [Exception('bad')] * 10 |
| 537 |
pytest.raises(Exception, self.conn.exec_command, 'ssh', 'some data') |
| 538 |
assert self.mock_popen.call_count == 10 |
| 539 |
|
| 540 |
def test_put_file_retries(self, monkeypatch): |
| 541 |
self.conn.set_option('host_key_checking', False) |
| 542 |
self.conn.set_option('reconnection_retries', 3) |
| 543 |
|
| 544 |
monkeypatch.setattr('time.sleep', lambda x: None) |
| 545 |
monkeypatch.setattr('ansible.plugins.connection.ssh.os.path.exists', lambda x: True) |
| 546 |
|
| 547 |
self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] |
| 548 |
self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] |
| 549 |
type(self.mock_popen_res).returncode = PropertyMock(side_effect=[255] * 4 + [0] * 4) |
| 550 |
|
| 551 |
self.mock_selector.select.side_effect = [ |
| 552 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 553 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 554 |
[], |
| 555 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 556 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 557 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 558 |
[] |
| 559 |
] |
| 560 |
self.mock_selector.get_map.side_effect = lambda: True |
| 561 |
|
| 562 |
self.conn._build_command = MagicMock() |
| 563 |
self.conn._build_command.return_value = 'sftp' |
| 564 |
|
| 565 |
return_code, b_stdout, b_stderr = self.conn.put_file('/path/to/in/file', '/path/to/dest/file') |
| 566 |
assert return_code == 0 |
| 567 |
assert b_stdout == b"my_stdout\nsecond_line" |
| 568 |
assert b_stderr == b"my_stderr" |
| 569 |
assert self.mock_popen.call_count == 2 |
| 570 |
|
| 571 |
def test_fetch_file_retries(self, monkeypatch): |
| 572 |
self.conn.set_option('host_key_checking', False) |
| 573 |
self.conn.set_option('reconnection_retries', 3) |
| 574 |
|
| 575 |
monkeypatch.setattr('time.sleep', lambda x: None) |
| 576 |
|
| 577 |
self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] |
| 578 |
self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] |
| 579 |
type(self.mock_popen_res).returncode = PropertyMock(side_effect=[255] * 4 + [0] * 4) |
| 580 |
|
| 581 |
self.mock_selector.select.side_effect = [ |
| 582 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 583 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 584 |
[], |
| 585 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 586 |
[(SelectorKey(self.mock_popen_res.stdout, 1001, [EVENT_READ], None), EVENT_READ)], |
| 587 |
[(SelectorKey(self.mock_popen_res.stderr, 1002, [EVENT_READ], None), EVENT_READ)], |
| 588 |
[] |
| 589 |
] |
| 590 |
self.mock_selector.get_map.side_effect = lambda: True |
| 591 |
|
| 592 |
self.conn._build_command = MagicMock() |
| 593 |
self.conn._build_command.return_value = 'sftp' |
| 594 |
|
| 595 |
return_code, b_stdout, b_stderr = self.conn.fetch_file('/path/to/in/file', '/path/to/dest/file') |
| 596 |
assert return_code == 0 |
| 597 |
assert b_stdout == b"my_stdout\nsecond_line" |
| 598 |
assert b_stderr == b"my_stderr" |
| 599 |
assert self.mock_popen.call_count == 2 |