def test_remote_executor_run_error(mock_sshclient):
    executor = command_executor.RemoteExecutor(
        "127.0.0.1",
        username="******",
        key_filename="path/to/file.key",
        password="******",
        accept_unknown_host=True,
    )

    mock_load_host_keys = mock.MagicMock()
    mock_sshclient.return_value.load_system_host_keys = mock_load_host_keys
    mock_set_keys = mock.MagicMock()
    mock_sshclient.return_value.set_missing_host_key_policy = mock_set_keys
    mock_connect = mock.MagicMock()
    mock_sshclient.return_value.connect = mock_connect

    mock_in = mock.MagicMock()
    mock_out = mock.MagicMock()
    mock_out.read.return_value = b"outlog"
    mock_recv_exit_status = mock.MagicMock()
    mock_recv_exit_status.return_value = -1
    mock_out.channel.recv_exit_status = mock_recv_exit_status
    mock_err = mock.MagicMock()
    mock_err.read.return_value = b"errlog"
    mock_send = mock.MagicMock()
    mock_shutdown_write = mock.MagicMock()
    mock_in.channel.send = mock_send
    mock_in.channel.shutdown_write = mock_shutdown_write
    mock_exec_command = mock.MagicMock()
    mock_exec_command.return_value = (mock_in, mock_out, mock_err)
    mock_sshclient.return_value.exec_command = mock_exec_command

    with pytest.raises(RuntimeError,
                       match="An error has occured when executing.*"):
        executor._run_cmd("pwd", stdin="input")
def test_remote_executor_init():
    executor = command_executor.RemoteExecutor(
        "127.0.0.1",
        username="******",
        key_filename="path/to/file.key",
        password="******",
        accept_unknown_host=False,
    )

    assert executor.hostname == "127.0.0.1"
    assert executor.username == "dummy"
    assert executor.key_filename == "path/to/file.key"
    assert executor.password == "123456"
    assert isinstance(executor.missing_host_policy, paramiko.client.RejectPolicy)

    executor2 = command_executor.RemoteExecutor("127.0.0.1")

    assert isinstance(executor2.missing_host_policy, paramiko.client.WarningPolicy)
def test_remote_executor_run(mock_sshclient):
    executor = command_executor.RemoteExecutor(
        "127.0.0.1",
        username="******",
        key_filename="path/to/file.key",
        password="******",
        accept_unknown_host=True,
    )

    mock_load_host_keys = mock.MagicMock()
    mock_sshclient.return_value.load_system_host_keys = mock_load_host_keys
    mock_set_keys = mock.MagicMock()
    mock_sshclient.return_value.set_missing_host_key_policy = mock_set_keys
    mock_connect = mock.MagicMock()
    mock_sshclient.return_value.connect = mock_connect

    mock_in = mock.MagicMock()
    mock_out = mock.MagicMock()
    mock_out.read.return_value = b"outlog"
    mock_recv_exit_status = mock.MagicMock()
    mock_recv_exit_status.return_value = 0
    mock_out.channel.recv_exit_status = mock_recv_exit_status
    mock_err = mock.MagicMock()
    mock_err.read.return_value = b"errlog"
    mock_send = mock.MagicMock()
    mock_shutdown_write = mock.MagicMock()
    mock_in.channel.send = mock_send
    mock_in.channel.shutdown_write = mock_shutdown_write
    mock_exec_command = mock.MagicMock()
    mock_exec_command.return_value = (mock_in, mock_out, mock_err)
    mock_sshclient.return_value.exec_command = mock_exec_command

    out, err = executor._run_cmd("pwd", stdin="input")

    mock_load_host_keys.assert_called_once()
    assert mock_set_keys.call_count == 1
    assert isinstance(mock_set_keys.call_args[0][0],
                      paramiko.client.WarningPolicy)
    # mock_set_keys.assert_called_once_with(paramiko.client.WarningPolicy)
    mock_connect.assert_called_once_with(
        "127.0.0.1",
        username="******",
        password="******",
        port=22,
        key_filename="path/to/file.key",
    )
    mock_exec_command.assert_called_once_with("pwd")
    mock_send.assert_called_once_with("input")
    mock_shutdown_write.assert_called_once()
    mock_recv_exit_status.assert_called_once()

    assert out == "outlog"
    assert err == "errlog"