Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/knut

crates/knut-pipelines/tests/testmatrix_wiring.rs

//! Wiring proof for knut-pipelines' `functional_status` test-matrix surface
//! (the newest feature: the event sink reports each `on_event` into the nornir
//! matrix). INJECT-AND-ASSERT (project LAW): drive the REAL `StdoutEventSink`
//! over a real event and assert on the REAL emitted row — offline, in-process.
//!
//! One test per build (single test per binary → no race on the process-global
//! functional buffer `--features testmatrix` records into):
//!   * default build → the emit is a compiled-out no-op; assert the public
//!     wrapper links and the sink still writes its line.
//!   * `--features testmatrix` → assert `on_event` records a green
//!     `event_sink`/`on_event` row carrying the event message.

use knut_pipelines::{EventLevel, PipelineEventSink, PipelineRunEvent, StdoutEventSink};

fn event(msg: &str) -> PipelineRunEvent {
    PipelineRunEvent {
        timestamp: None,
        level: EventLevel::Info,
        message: msg.to_string(),
        element: Some("stage-a".to_string()),
    }
}

/// Default build: `testmatrix` OFF → `functional_status` is an inlined no-op.
/// Prove it links and the sink it is wired into still writes the event line.
#[cfg(not(feature = "testmatrix"))]
#[test]
fn functional_status_is_a_linked_noop_and_sink_writes() {
    knut_pipelines::functional_status("knut-pipelines/event_sink", "on_event", true, "noop");

    let mut buf: Vec<u8> = Vec::new();
    {
        let mut sink = StdoutEventSink::to_writer(&mut buf);
        sink.on_event(&event("stage-a materialized"))
            .expect("sink writes");
        assert_eq!(sink.count(), 1, "the sink counted the event");
    }
    let out = String::from_utf8(buf).expect("utf8");
    assert!(
        out.contains("stage-a materialized"),
        "line written: {out:?}"
    );
}

/// `--features testmatrix`: `on_event` must record a green
/// `event_sink`/`on_event` row carrying the event message.
#[cfg(feature = "testmatrix")]
#[test]
fn on_event_records_a_green_event_sink_row() {
    use nornir_testmatrix::{drain_functional_rows, status};

    let _ = drain_functional_rows();

    let mut buf: Vec<u8> = Vec::new();
    {
        let mut sink = StdoutEventSink::to_writer(&mut buf);
        sink.on_event(&event("stage-a materialized"))
            .expect("sink writes");
    }

    let rows = drain_functional_rows();
    let row = rows
        .iter()
        .find(|r| r.suite == "knut-pipelines/event_sink" && r.test_name == "on_event")
        .unwrap_or_else(|| panic!("on_event must emit an event_sink row; got {rows:?}"));

    assert!(
        status::is_green(&row.status),
        "a successful write is a GREEN row: {row:?}"
    );
    assert_eq!(
        row.aspect, "functional",
        "recorded under the functional aspect"
    );
    assert_eq!(
        row.message, "stage-a materialized",
        "detail carries the event message: {:?}",
        row.message
    );
}