|
| 1 | +# This file is part of parallel-ssh. |
| 2 | +# |
| 3 | +# Copyright (C) 2014-2025 Panos Kittenis and contributors. |
| 4 | +# |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation, version 2.1. |
| 8 | +# |
| 9 | +# This library is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public |
| 15 | +# License along with this library; if not, write to the Free Software |
| 16 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + |
| 18 | + |
| 19 | +import unittest |
| 20 | +from unittest.mock import patch, MagicMock |
| 21 | + |
| 22 | +from pssh.clients import ParallelSSHClient, SSHClient |
| 23 | +from pssh.exceptions import InvalidAPIUseError, UnknownHostError |
| 24 | + |
| 25 | + |
| 26 | +class APIUseTest(unittest.TestCase): |
| 27 | + |
| 28 | + @patch('gevent.socket') |
| 29 | + @patch('pssh.clients.native.single.Session') |
| 30 | + def test_kbd_interactive_enabled_single_clients(self, mock_sess, mock_sock): |
| 31 | + self.assertRaises(UnknownHostError, SSHClient, |
| 32 | + 'fakehost', password='fake_pass', keyboard_interactive=True, num_retries=0, |
| 33 | + timeout=.1, |
| 34 | + retry_delay=.1, |
| 35 | + _auth_thread_pool=False, |
| 36 | + allow_agent=False, |
| 37 | + ) |
| 38 | + self.assertRaises(InvalidAPIUseError, SSHClient, 'fakehost', keyboard_interactive=True) |
| 39 | + |
| 40 | + @patch('gevent.socket') |
| 41 | + @patch('pssh.clients.native.single.Session') |
| 42 | + def test_kbd_interactive_enabled_parallel_clients(self, mock_sess, mock_sock): |
| 43 | + client = ParallelSSHClient( |
| 44 | + ['fakehost'], password='fake_pass', keyboard_interactive=True, num_retries=0, |
| 45 | + timeout=.1, |
| 46 | + retry_delay=.1, |
| 47 | + allow_agent=False, |
| 48 | + ) |
| 49 | + self.assertRaises(UnknownHostError, client.run_command, 'echo me') |
| 50 | + self.assertRaises(InvalidAPIUseError, ParallelSSHClient, ['fakehost'], keyboard_interactive=True) |
| 51 | + |
| 52 | + @patch('gevent.socket.socket') |
| 53 | + @patch('pssh.clients.native.single.Session') |
| 54 | + def test_kbd_interactive_enabled_and_used(self, mock_sess, mock_sock): |
| 55 | + sess = MagicMock() |
| 56 | + mock_sess.return_value = sess |
| 57 | + kbd_auth = MagicMock() |
| 58 | + password_auth = MagicMock() |
| 59 | + sess.userauth_keyboardinteractive = kbd_auth |
| 60 | + sess.userauth_password = password_auth |
| 61 | + my_user = "my_user" |
| 62 | + my_pass = "fake_pass" |
| 63 | + |
| 64 | + client = SSHClient( |
| 65 | + '127.0.0.1', port=1234, user=my_user, password=my_pass, keyboard_interactive=True, num_retries=0, |
| 66 | + timeout=.1, |
| 67 | + retry_delay=.1, |
| 68 | + _auth_thread_pool=False, |
| 69 | + allow_agent=False, |
| 70 | + identity_auth=False, |
| 71 | + ) |
| 72 | + sess.userauth_keyboardinteractive.assert_called_once_with(my_user, my_pass) |
| 73 | + sess.userauth_password.assert_not_called() |
| 74 | + self.assertEqual(client.user, my_user) |
| 75 | + self.assertEqual(client.password, my_pass) |
0 commit comments