faucet-cli 1.13.1

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#![cfg(feature = "notify")]
//! Integration tests for the notification delivery path (#280) — real HTTP via
//! wiremock. The unit tests in `src/notify/` cover the pure matching /
//! rendering / coalesce decisions; these cover the wire: Slack / webhook /
//! PagerDuty delivery, HMAC signing, PagerDuty trigger→resolve pairing,
//! leading-edge coalescing, and failure-is-swallowed.

use assert_cmd::Command;
use faucet_cli::notify::{
    ChannelSpec, EventKind, NotificationSpec, Notifier, NotifyEvent, PagerdutyConfig, Severity,
    SlackConfig, WebhookConfig,
};
use serde_json::Value;
use wiremock::matchers::{header_exists, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn rule(name: &str, on: Vec<EventKind>, channel: ChannelSpec) -> NotificationSpec {
    NotificationSpec {
        name: name.into(),
        on,
        min_severity: Severity::Info,
        dedupe_window_secs: None,
        dlq_threshold: None,
        channel,
    }
}

#[tokio::test]
async fn slack_delivery_posts_block_kit_body() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .respond_with(ResponseTemplate::new(200))
        .expect(1)
        .mount(&server)
        .await;

    let spec = rule(
        "slack",
        vec![EventKind::RunFailure],
        ChannelSpec::Slack(SlackConfig {
            webhook_url: format!("{}/hook", server.uri()),
            channel: Some("#ops".into()),
            username: None,
        }),
    );
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    n.emit(NotifyEvent::run_failure("orders", "", "sink", "boom"))
        .await;

    let reqs = server.received_requests().await.unwrap();
    assert_eq!(reqs.len(), 1);
    let body: Value = serde_json::from_slice(&reqs[0].body).unwrap();
    assert!(body["text"].as_str().unwrap().contains("failed"));
    assert_eq!(body["channel"], "#ops");
    assert!(body["blocks"].is_array());
}

#[tokio::test]
async fn webhook_delivery_signs_body() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .and(header_exists("X-Faucet-Signature"))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    let spec = rule(
        "wh",
        vec![],
        ChannelSpec::Webhook(WebhookConfig {
            url: format!("{}/hook", server.uri()),
            method: "POST".into(),
            headers: Default::default(),
            hmac_secret: Some("s3cr3t".into()),
            signature_header: "X-Faucet-Signature".into(),
            extra_fields: Default::default(),
        }),
    );
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    n.emit(NotifyEvent::dlq_threshold("p", "", 5)).await;

    let reqs = server.received_requests().await.unwrap();
    assert_eq!(reqs.len(), 1);
    let sig = reqs[0]
        .headers
        .get("X-Faucet-Signature")
        .unwrap()
        .to_str()
        .unwrap();
    assert_eq!(sig.len(), 64, "hex HMAC-SHA256 is 64 chars");
    let body: Value = serde_json::from_slice(&reqs[0].body).unwrap();
    assert_eq!(body["event"], "dlq_threshold");
}

#[tokio::test]
async fn pagerduty_trigger_then_resolve_pairing() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/enqueue"))
        .respond_with(ResponseTemplate::new(202))
        .mount(&server)
        .await;

    let spec = rule(
        "pd",
        vec![EventKind::RunFailure],
        ChannelSpec::Pagerduty(PagerdutyConfig {
            routing_key: "rk".into(),
            source: None,
            endpoint: Some(format!("{}/enqueue", server.uri())),
        }),
    );
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    // Failure opens the incident (trigger); success resolves it.
    n.emit(NotifyEvent::run_failure("orders", "r1", "sink", "boom"))
        .await;
    n.emit(NotifyEvent::run_success("orders", "r1", 3)).await;

    let reqs = server.received_requests().await.unwrap();
    assert_eq!(reqs.len(), 2, "one trigger + one resolve");
    let first: Value = serde_json::from_slice(&reqs[0].body).unwrap();
    let second: Value = serde_json::from_slice(&reqs[1].body).unwrap();
    assert_eq!(first["event_action"], "trigger");
    assert_eq!(second["event_action"], "resolve");
    assert_eq!(first["dedup_key"], second["dedup_key"]); // same incident
}

#[tokio::test]
async fn coalescing_drops_repeat_within_window() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .respond_with(ResponseTemplate::new(200))
        .expect(1) // only the first of two identical events is delivered
        .mount(&server)
        .await;

    let mut spec = rule(
        "slack",
        vec![EventKind::RunFailure],
        ChannelSpec::Slack(SlackConfig {
            webhook_url: format!("{}/hook", server.uri()),
            channel: None,
            username: None,
        }),
    );
    spec.dedupe_window_secs = Some(3600);
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    n.emit(NotifyEvent::run_failure("p", "", "sink", "boom"))
        .await;
    n.emit(NotifyEvent::run_failure("p", "", "sink", "boom"))
        .await;

    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn delivery_failure_is_swallowed() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .respond_with(ResponseTemplate::new(500))
        .mount(&server)
        .await;

    let spec = rule(
        "slack",
        vec![],
        ChannelSpec::Slack(SlackConfig {
            webhook_url: format!("{}/hook", server.uri()),
            channel: None,
            username: None,
        }),
    );
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    // Must not panic despite the 500; delivery is fire-and-forget.
    n.emit(NotifyEvent::run_failure("p", "", "sink", "boom"))
        .await;
    // Retried, so at least one request reached the server.
    assert!(!server.received_requests().await.unwrap().is_empty());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn end_to_end_run_notifies_on_success() {
    // Drives the real `faucet run` binary so the executor's notify emit path
    // (run_success + the run.rs wiring) is exercised end-to-end.
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.csv");
    std::fs::write(&input, "name\nalice\nbob\n").unwrap();
    let out = dir.path().join("out.jsonl");
    let cfg = format!(
        "version: 1\nname: notif_e2e\npipeline:\n  source:\n    type: csv\n    config:\n      path: \"{}\"\n  sink:\n    type: jsonl\n    config:\n      path: \"{}\"\nnotifications:\n  - name: slack\n    on: [run_success]\n    channel:\n      type: slack\n      config:\n        webhook_url: \"{}/hook\"\n",
        input.display(),
        out.display(),
        server.uri()
    );
    let cfg_path = dir.path().join("p.yaml");
    std::fs::write(&cfg_path, cfg).unwrap();

    // `.assert()` blocks until the child exits; run it off the async workers.
    let cfg_path2 = cfg_path.clone();
    tokio::task::spawn_blocking(move || {
        Command::cargo_bin("faucet")
            .unwrap()
            .args(["run"])
            .arg(&cfg_path2)
            .assert()
            .success();
    })
    .await
    .unwrap();

    // The notify emit is awaited inside the run, so by process exit it landed.
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

#[tokio::test]
async fn severity_floor_filters_delivery() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/hook"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;

    let mut spec = rule(
        "slack",
        vec![],
        ChannelSpec::Slack(SlackConfig {
            webhook_url: format!("{}/hook", server.uri()),
            channel: None,
            username: None,
        }),
    );
    spec.min_severity = Severity::Error;
    let n = Notifier::from_specs(&[spec]).unwrap().unwrap();
    // run_success is Info < Error → gated (no request); circuit_open is Critical → delivered.
    n.emit(NotifyEvent::run_success("p", "", 1)).await;
    assert!(server.received_requests().await.unwrap().is_empty());
    n.emit(NotifyEvent::circuit_open("p", "", 3, 30)).await;
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
}

/// #480 acceptance: the run identity threaded through `ExecuteOptions.run_id`
/// reaches the delivered webhook body, and a matrix row's own `invocation_id`
/// is distinct from it. Drives a real csv → jsonl pipeline through the executor
/// rather than emitting a synthetic event, so the whole propagation path
/// (options → `run_one_invocation` → `RunContext` → render → HTTP) is covered.
/// Column-profile drift (#708) under `on_drift: fail`: one `profile_drift`
/// event per finding, and the run itself is reported as a failure whose
/// `error_kind` is `profile_drift`.
#[cfg(all(feature = "source-csv", feature = "sink-jsonl"))]
#[tokio::test]
async fn executor_notifies_profile_drift_and_the_failed_run() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/cb"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.csv");
    let output = dir.path().join("out.jsonl");
    let cfg_yaml = format!(
        r#"
version: 1
name: drift_pipeline
pipeline:
  source: {{ type: csv, config: {{ path: "{}" }} }}
  sink: {{ type: jsonl, config: {{ path: "{}", append: false }} }}
  state: {{ type: file, config: {{ path: "{}" }} }}
profiling: {{ min_history: 2, window: 5, on_drift: fail }}
notifications:
  - name: cb
    on: [profile_drift, run_failure, run_success]
    channel:
      type: webhook
      config:
        url: "{}/cb"
"#,
        input.display(),
        output.display(),
        dir.path().join("state").display(),
        server.uri()
    );
    let cfg = faucet_cli::config::PipelineConfig::from_text(&cfg_yaml, &dir.path().join("p.yaml"))
        .expect("config parses");
    let run = |csv: &str| {
        std::fs::write(&input, csv).unwrap();
        let nodes = faucet_cli::expand::expand(&cfg).expect("expand");
        let notifier = faucet_cli::notify::Notifier::from_specs(&cfg.notifications)
            .unwrap()
            .expect("notifier built");
        faucet_cli::executor::run_expanded(
            nodes,
            faucet_cli::executor::ExecuteOptions {
                legacy_state_writes: false,
                pipeline_name: "drift_pipeline".into(),
                run_id: None,
                execution: None,
                concurrency: None,
                dry_run: false,
                limit: None,
                state_path_override: None,
                state_scope: Default::default(),
                shard: None,
                auth: Default::default(),
                clock: chrono::Utc::now().fixed_offset(),
                cancel: None,
                resilience: None,
                sla: None,
                reconcile: None,
                verify: None,
                rollback: None,
                #[cfg(feature = "lineage")]
                lineage: None,
                #[cfg(feature = "lineage")]
                lineage_cfg: None,
                notifier: Some(notifier),
                #[cfg(feature = "catalog")]
                catalog: None,
                usage: Default::default(),
                budget: None,
            },
        )
    };
    for _ in 0..2 {
        let summary = run("id,region
1,eu
2,us
3,eu
4,us
")
        .await
        .unwrap();
        assert!(!summary.had_failures(), "{summary:?}");
    }
    let summary = run("id,region
1,eu
2,latam
3,eu
4,latam
")
    .await
    .unwrap();
    assert!(summary.had_failures(), "{summary:?}");

    let events: Vec<Value> = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .map(|r| serde_json::from_slice(&r.body).unwrap())
        .collect();
    let kinds: Vec<&str> = events
        .iter()
        .map(|e| e["event"].as_str().unwrap())
        .collect();
    assert_eq!(
        kinds.iter().filter(|k| **k == "run_success").count(),
        2,
        "{kinds:?}"
    );
    let drift: Vec<&Value> = events
        .iter()
        .filter(|e| e["event"] == "profile_drift")
        .collect();
    assert!(
        drift
            .iter()
            .any(|e| e["details"]["column"] == "region" && e["details"]["metric"] == "new_value"),
        "{events:?}"
    );
    let failure = events
        .iter()
        .find(|e| e["event"] == "run_failure")
        .expect("the drifted run notifies a failure");
    assert_eq!(
        failure["details"]["error_kind"], "profile_drift",
        "{failure}"
    );
}

#[tokio::test]
async fn executor_propagates_submitted_run_id_into_the_payload() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/cb"))
        .respond_with(ResponseTemplate::new(200))
        .expect(1)
        .mount(&server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.csv");
    let output = dir.path().join("out.jsonl");
    std::fs::write(&input, "id,name\n1,alice\n2,bob\n").unwrap();

    let cfg_yaml = format!(
        r#"
version: 1
name: cb_pipeline
pipeline:
  source:
    type: csv
    config:
      path: "{}"
  sink:
    type: jsonl
    config:
      path: "{}"
notifications:
  - name: cb
    on: [run_success]
    channel:
      type: webhook
      config:
        url: "{}/cb"
        extra_fields:
          tenant: acme
"#,
        input.display(),
        output.display(),
        server.uri()
    );

    let cfg_path = dir.path().join("p.yaml");
    std::fs::write(&cfg_path, &cfg_yaml).unwrap();
    let cfg =
        faucet_cli::config::PipelineConfig::from_text(&cfg_yaml, &cfg_path).expect("config parses");
    let nodes = faucet_cli::expand::expand(&cfg).expect("expand");
    let notifier = faucet_cli::notify::Notifier::from_specs(&cfg.notifications)
        .unwrap()
        .expect("notifier built");

    let opts = faucet_cli::executor::ExecuteOptions {
        legacy_state_writes: false,
        pipeline_name: "cb_pipeline".into(),
        run_id: Some("submitted-run-42".into()),
        execution: None,
        concurrency: None,
        dry_run: false,
        limit: None,
        state_path_override: None,
        state_scope: Default::default(),
        shard: None,
        auth: Default::default(),
        clock: chrono::Utc::now().fixed_offset(),
        cancel: None,
        resilience: None,
        sla: None,
        reconcile: None,
        verify: None,
        rollback: None,
        #[cfg(feature = "lineage")]
        lineage: None,
        #[cfg(feature = "lineage")]
        lineage_cfg: None,
        notifier: Some(notifier),
        #[cfg(feature = "catalog")]
        catalog: None,
        usage: Default::default(),
        budget: None,
    };

    let summary = faucet_cli::executor::run_expanded(nodes, opts)
        .await
        .expect("run");
    assert!(!summary.had_failures(), "pipeline should succeed");

    let reqs = server.received_requests().await.unwrap();
    assert_eq!(reqs.len(), 1, "exactly one run_success callback");
    let body: Value = serde_json::from_slice(&reqs[0].body).unwrap();

    assert_eq!(body["event"], "run_success");
    // The submitted run id — what a caller correlates against.
    assert_eq!(body["run_id"], "submitted-run-42");
    // The per-invocation id is generated, so it must differ from the submitted id.
    assert!(body["invocation_id"].is_string());
    assert_ne!(body["invocation_id"], body["run_id"]);
    // Timing is populated for a real run.
    assert!(body["started_at"].is_string());
    assert!(body["finished_at"].is_string());
    assert!(body["duration_secs"].as_f64().unwrap() >= 0.0);
    // Static metadata merged.
    assert_eq!(body["tenant"], "acme");
    assert_eq!(body["details"]["records_written"], 2);
}