|
20 | 20 | from pyinfra.api import Config, MaskString, State, StringCommand |
21 | 21 | from pyinfra.api.connect import connect_all |
22 | 22 | from pyinfra.api.connectors.ssh import _get_sftp_connection |
23 | | -from pyinfra.api.exceptions import PyinfraError |
| 23 | +from pyinfra.api.exceptions import ConnectError, PyinfraError |
24 | 24 |
|
25 | 25 | from ..util import make_inventory |
26 | 26 |
|
@@ -650,3 +650,24 @@ def test_get_file_su_user(self, fake_sftp_client, fake_ssh_client): |
650 | 650 | fake_sftp_client.from_transport().getfo.assert_called_with( |
651 | 651 | '/tmp/pyinfra-e9c0d3c8ffca943daa0e75511b0a09c84b59c508', fake_open(), |
652 | 652 | ) |
| 653 | + |
| 654 | + @patch('pyinfra.api.connectors.ssh.SSHClient') |
| 655 | + @patch('pyinfra.api.connectors.ssh.SFTPClient') |
| 656 | + def test_get_sftp_fail(self, fake_sftp_client, fake_ssh_client): |
| 657 | + inventory = make_inventory(hosts=('anotherhost',)) |
| 658 | + State(inventory, Config()) |
| 659 | + host = inventory.get_host('anotherhost') |
| 660 | + host.connect() |
| 661 | + |
| 662 | + def raise_exception(*args, **kwargs): |
| 663 | + raise SSHException() |
| 664 | + |
| 665 | + fake_sftp_client.from_transport.side_effect = raise_exception |
| 666 | + |
| 667 | + fake_open = mock_open(read_data='test!') |
| 668 | + with patch('pyinfra.api.util.open', fake_open, create=True): |
| 669 | + with self.assertRaises(ConnectError): |
| 670 | + host.put_file( |
| 671 | + 'not-a-file', 'not-another-file', |
| 672 | + print_output=True, |
| 673 | + ) |
0 commit comments