Esempio n. 1
0
    def test_remote_exec_timeout(self):
        # Setup
        ssh_mock = Mock()
        sftp_mock = Mock()
        ssh_mock.open_sftp.return_value = sftp_mock
        stdout_mock = Mock()
        stdout_mock.channel.exit_status_ready.return_value = False
        ssh_mock.exec_command.return_value = (None, stdout_mock, None)

        # Run
        with self.assertRaises(RemoteExecTimeout):
            remote_exec(ssh_mock, "script")

        sftp_mock.remove.assert_called()
Esempio n. 2
0
    def test_remote_exec_error(self):
        # Setup
        ssh_mock = Mock()
        sftp_mock = Mock()
        ssh_mock.open_sftp.return_value = sftp_mock
        stdout_mock = Mock()
        stdout_mock.channel.exit_status_ready.return_value = True
        stdout_mock.channel.recv_exit_status.return_value = 1
        stderr_mock = Mock()
        stderr_mock.read.return_value = "error message"
        ssh_mock.exec_command.return_value = (None, stdout_mock, stderr_mock)

        # Run
        with self.assertRaises(RemoteExecException):
            remote_exec(ssh_mock, "script")

        sftp_mock.remove.assert_called()
Esempio n. 3
0
    def test_remote_exec_reuse_sftp(self):
        # Setup
        ssh_mock = Mock()
        sftp_mock = Mock()
        stdout_mock = Mock()
        stdout_mock.channel.exit_status_ready.return_value = True
        stdout_mock.channel.recv_exit_status.return_value = 0
        ssh_mock.exec_command.side_effect = [(None, stdout_mock, None)]

        # Run
        retval = remote_exec(ssh_mock, "script", reuse_sftp=sftp_mock)

        # Assert
        self.assertEqual(0, retval)
        sftp_mock.open.assert_called()
        sftp_mock.remove.assert_called()
        sftp_mock.close.assert_not_called()
Esempio n. 4
0
    def test_remote_exec(self):
        # Setup
        ssh_mock = Mock()
        sftp_mock = Mock()
        ssh_mock.open_sftp.return_value = sftp_mock
        stdout_mock = Mock()
        stdout_mock.channel.exit_status_ready.return_value = True
        stdout_mock.channel.recv_exit_status.return_value = 0
        ssh_mock.exec_command.return_value = (None, stdout_mock, None)

        # Run
        retval = remote_exec(ssh_mock, "script")

        # Assert
        self.assertEqual(0, retval)
        sftp_mock.open.assert_called()
        sftp_mock.remove.assert_called()
        sftp_mock.close.assert_called()
Esempio n. 5
0
    def test_remote_exec_with_params(self):
        # Setup
        ssh_mock = Mock()
        sftp_mock = Mock()
        ssh_mock.open_sftp.return_value = sftp_mock
        stdout_mock = Mock()
        stdout_mock.channel.exit_status_ready.return_value = True
        stdout_mock.channel.recv_exit_status.return_value = 0
        ssh_mock.exec_command.return_value = (None, stdout_mock, None)
        self.mocks["uuid"].uuid4.return_value = "a_very_predictable_uuid"

        # Run
        retval = remote_exec(ssh_mock, "script", params="resume")

        # Assert
        self.assertEqual(0, retval)
        expected_command = "/tmp/.a_very_predictable_uuid resume"
        ssh_mock.exec_command.assert_called_with(expected_command)
        sftp_mock.open.assert_called()
        sftp_mock.remove.assert_called()
        sftp_mock.close.assert_called()