camel-integration-test 0.43.0

Scenario document model, parser, and integration-tier test harness for rust-camel
Documentation
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! boot_scenario delegation tests (scenario-shared-boot task 3.1).
//!
//! Unit-test module of the lib target, declared in `src/lib.rs` under
//! `#[cfg(test)]`. Every test is project-based: it writes a temporary
//! project (`Camel.toml`, an optional route file, a `.test.yaml`
//! document parsed through [`crate::parse_scenario_document`]) and
//! boots it through [`crate::boot_scenario`] with an empty
//! [`LayeredEnv`]. Every rejection path here fires before
//! `ctx.start()` (tier security gating, route-file pre-checks,
//! discovery), so no listener teardown is needed.

use std::collections::BTreeMap;

use camel_api::CamelError;

use crate::boot_scenario::boot_scenario;
use crate::env_layers::{LayeredEnv, ambient_std};
use crate::parse_scenario_document;
use crate::{RouteSource, ScenarioAction, ScenarioDocument};

/// A minimal route file: one `direct:` → `log:` route.
const ROUTE: &str = r#"
routes:
  - id: boot-route
    from: direct:start
    steps:
      - to: log:info
"#;

/// A minimal scenario document declaring the route file.
const DOC: &str = r#"
routeFiles: [routes.yaml]
scenario:
  - sleep:
      duration: 1s
"#;

/// Writes a temporary project: `Camel.toml` from `camel_toml`, an
/// optional route file `(name, text)`, and the scenario document
/// `doc`. Returns the project dir and the parsed document.
fn project(
    camel_toml: &str,
    route: Option<(&str, &str)>,
    doc: &str,
) -> (tempfile::TempDir, ScenarioDocument) {
    let dir = tempfile::tempdir().expect("temp dir");
    std::fs::write(dir.path().join("Camel.toml"), camel_toml).expect("write Camel.toml");
    if let Some((name, text)) = route {
        std::fs::write(dir.path().join(name), text).expect("write route file");
    }
    let doc_path = dir.path().join("case.test.yaml");
    std::fs::write(&doc_path, doc).expect("write document");
    let document = parse_scenario_document(&doc_path).expect("document parses");
    (dir, document)
}

/// An empty layered environment: no doc, harness, or passthrough
/// layer, so ambient values can never resolve through it.
fn empty_env() -> LayeredEnv {
    LayeredEnv::new(BTreeMap::new(), BTreeMap::new(), Vec::new(), ambient_std())
}

/// Extracts the error from a rejected boot; panics with `what` when
/// the boot unexpectedly succeeded.
fn expect_boot_error(
    result: Result<crate::boot_scenario::ScenarioRun, CamelError>,
    what: &str,
) -> CamelError {
    match result {
        Ok(_) => panic!("{what}"),
        Err(err) => err,
    }
}

#[tokio::test]
async fn boot_rejects_keycloak_offline() {
    // The keycloak fixture field set from camel-bundles'
    // security_boot tests; the fixture's `{{env:KC}}` secret marker is
    // rejected by the sealed config loader's legacy-brace gate before
    // the tier gate, so the secret is literal here — the gate keys on
    // the section's presence, never on the secret.
    let (dir, doc) = project(
        r#"
[security.keycloak]
server_url = "https://kc.example.com"
realm = "camel"
client_id = "camel-api"
client_secret = "kc-secret"
"#,
        Some(("routes.yaml", ROUTE)),
        DOC,
    );
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "keycloak security must be rejected in the offline tier",
    );
    assert!(
        matches!(err, CamelError::AuthProviderUnavailable(_)),
        "expected AuthProviderUnavailable, got: {err:?}"
    );
}

#[tokio::test]
async fn boot_rejects_oidc_offline() {
    // Minimal `[security.oidc]`: `issuer` is OidcSecurityConfig's only
    // required field.
    let (dir, doc) = project(
        r#"
[security.oidc]
issuer = "https://oidc.example.test"
"#,
        Some(("routes.yaml", ROUTE)),
        DOC,
    );
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "oidc security must be rejected in the offline tier",
    );
    assert!(
        matches!(err, CamelError::AuthProviderUnavailable(_)),
        "expected AuthProviderUnavailable, got: {err:?}"
    );
}

#[tokio::test]
async fn boot_rejects_wasm_security_policies() {
    // Minimal `[security.policies]` entry: the wrapper's only field is
    // the `wasm` map; one policy with its required `path`. The gate
    // fires before any builder call, so the .wasm file need not exist.
    let (dir, doc) = project(
        r#"
[security.policies.wasm.demo]
path = "policies/demo.wasm"
"#,
        Some(("routes.yaml", ROUTE)),
        DOC,
    );
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "wasm security policies must be rejected in the scenario tier",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected Config, got: {err:?}"
    );
    assert!(
        err.to_string()
            .contains("not supported in the scenario tier"),
        "error must name the v1 tier limitation: {err}"
    );
}

#[tokio::test]
async fn boot_hermetic_env_not_resolved_from_process() {
    // The variable exists only in the process environment; the empty
    // layered environment must not resolve it (edition-2024 unsafe
    // env APIs, same wrapping as the camel-config properties tests).
    unsafe { std::env::set_var("RC_6BSF_HERMETIC", "leak") };
    let route = r#"
routes:
  - id: hermetic
    from: direct:${env:RC_6BSF_HERMETIC}
    steps:
      - to: log:info
"#;
    let (dir, doc) = project("# minimal\n", Some(("routes.yaml", route)), DOC);
    let result = boot_scenario(&doc, dir.path(), &empty_env()).await;
    unsafe { std::env::remove_var("RC_6BSF_HERMETIC") };
    let err = expect_boot_error(
        result,
        "the process environment must not resolve scenario placeholders",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected the unresolved-placeholder Config error, got: {err:?}"
    );
    let display = err.to_string();
    assert!(
        display.contains("RC_6BSF_HERMETIC"),
        "error must name the variable: {display}"
    );
    assert!(
        display.contains("no layer of the scenario environment defines it"),
        "error must name the hermetic resolution failure: {display}"
    );
}

#[tokio::test]
async fn boot_missing_route_file_names_the_file() {
    let doc = r#"
routeFiles: [nope.yaml]
scenario:
  - sleep:
      duration: 1s
"#;
    let (dir, doc) = project("# minimal\n", None, doc);
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "a missing declared route file must fail the boot",
    );
    assert!(
        matches!(err, CamelError::Io(_)),
        "expected Io, got: {err:?}"
    );
    assert!(
        err.to_string().contains("nope.yaml"),
        "error must name the missing file: {err}"
    );
}

/// A nested scenario document declaring its route file relative to
/// itself: the file lives next to the document, not at the root.
const NESTED_DOC: &str = r#"
routeFiles: [local.yaml]
scenario:
  - sleep:
      duration: 1s
"#;

/// A nested scenario document declaring its route file against the
/// project root's route space (`rr/` under the `Camel.toml` dir).
const NESTED_FROM_ROOT_DOC: &str = r#"
routeFilesFromRoot: [rr/root-route.yaml]
scenario:
  - sleep:
      duration: 1s
"#;

#[tokio::test]
async fn boot_root_walks_to_ancestor() {
    // rc-jjzy5, nested tree: the sealed `Camel.toml` and the
    // `routeFilesFromRoot` file live at the ANCESTOR root; the
    // relative-routeFiles file lives only next to the document. One
    // document declares exactly one route source
    // (`RouteSourceConflict`), so the pair boots twice: each `Ok`
    // proves its declared file was found and parsed at its own anchor
    // — the document directory for `routeFiles`, the passed root for
    // `routeFilesFromRoot`. Anchoring `routeFiles` at the root (the
    // pre-fix behavior) misses `local.yaml` and fails the first boot.
    let root = tempfile::tempdir().expect("temp dir");
    let sub = root.path().join("sub");
    std::fs::create_dir(&sub).expect("mkdir sub");
    std::fs::create_dir_all(root.path().join("rr")).expect("mkdir rr");
    std::fs::write(root.path().join("Camel.toml"), "# minimal\n").expect("write Camel.toml");
    std::fs::write(root.path().join("rr/root-route.yaml"), ROUTE).expect("write root route");
    std::fs::write(sub.join("local.yaml"), ROUTE).expect("write local route");

    let files_path = sub.join("doc.test.yaml");
    std::fs::write(&files_path, NESTED_DOC).expect("write document");
    let files_doc = parse_scenario_document(&files_path).expect("document parses");

    let from_root_path = sub.join("from-root.test.yaml");
    std::fs::write(&from_root_path, NESTED_FROM_ROOT_DOC).expect("write document");
    let from_root_doc = parse_scenario_document(&from_root_path).expect("document parses");

    boot_scenario(&files_doc, root.path(), &empty_env())
        .await
        .expect("relative routeFiles must anchor at the document directory");
    boot_scenario(&from_root_doc, root.path(), &empty_env())
        .await
        .expect("routeFilesFromRoot must follow the resolved root");
}

#[tokio::test]
async fn route_files_anchor_to_doc_dir() {
    // rc-jjzy5: the route file exists ONLY next to the nested
    // document; resolving relative `routeFiles` against the boot root
    // would miss it and fail the boot.
    let root = tempfile::tempdir().expect("temp dir");
    let sub = root.path().join("sub");
    std::fs::create_dir(&sub).expect("mkdir sub");
    std::fs::write(root.path().join("Camel.toml"), "# minimal\n").expect("write Camel.toml");
    std::fs::write(sub.join("local.yaml"), ROUTE).expect("write local route");
    let doc_path = sub.join("case.test.yaml");
    std::fs::write(&doc_path, NESTED_DOC).expect("write document");
    let doc = parse_scenario_document(&doc_path).expect("document parses");

    boot_scenario(&doc, root.path(), &empty_env())
        .await
        .expect("the colocated route file must load from the document directory");
}

#[tokio::test]
async fn boot_inline_routes_still_rejected() {
    // The load-time doc gate (rc-9dpx) rejects inline route sources
    // inside `parse_scenario_document`, so the boot-level rejection is
    // reachable only through a directly-constructed document — exactly
    // the defense-in-depth path this test keeps covered.
    let routes = camel_dsl::parse_yaml(
        "routes:\n  - id: inline-route\n    from: direct:start\n    steps:\n      - to: log:info\n",
    )
    .expect("inline routes parse");
    let doc = ScenarioDocument {
        source_path: std::path::PathBuf::new(),
        route_source: RouteSource::Inline(routes),
        scenario: vec![ScenarioAction::Sleep {
            duration: std::time::Duration::from_secs(1),
        }],
        partners: None,
        env: None,
        env_passthrough: None,
        profile: None,
        send_deadline: None,
        inbound: None,
        logs: None,
    };
    let dir = tempfile::tempdir().expect("temp dir");
    std::fs::write(dir.path().join("Camel.toml"), "# minimal\n").expect("write Camel.toml");
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "inline route sources must not boot in v1",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected Config, got: {err:?}"
    );
    assert!(
        err.to_string()
            .contains("inline route sources cannot boot in v1"),
        "error must keep the inline-routes message: {err}"
    );
}

/// The `inbound:` rejection without the `http` feature, mirroring the
/// inline-routes defense-in-depth path.
#[cfg(not(feature = "http"))]
#[tokio::test]
async fn boot_rejects_inbound_without_http_feature() {
    // The load-time doc gate rejects `inbound:` without the `http`
    // feature inside `parse_scenario_document`, so the boot-level
    // rejection is reachable only through a directly-constructed
    // document — the same defense-in-depth path
    // `boot_inline_routes_still_rejected` keeps covered.
    let doc = ScenarioDocument {
        source_path: std::path::PathBuf::new(),
        route_source: RouteSource::RouteFiles(vec!["routes.yaml".into()]),
        scenario: vec![ScenarioAction::Sleep {
            duration: std::time::Duration::from_secs(1),
        }],
        partners: None,
        env: None,
        env_passthrough: None,
        profile: None,
        send_deadline: None,
        inbound: Some(crate::InboundListener {
            bind_var: "INBOUND".to_string(),
        }),
        logs: None,
    };
    let dir = tempfile::tempdir().expect("temp dir");
    std::fs::write(dir.path().join("Camel.toml"), "# minimal\n").expect("write Camel.toml");
    std::fs::write(dir.path().join("routes.yaml"), ROUTE).expect("write route file");
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "an inbound document must not boot without the http feature",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected Config, got: {err:?}"
    );
    assert!(
        err.to_string().contains("http` feature"),
        "error must name the missing feature: {err}"
    );
}

#[tokio::test]
async fn boot_rejects_test_suffix_route_file() {
    // Discovery's reserved-suffix gate silently skips `.test.yaml`/
    // `.test.yml` files (they name `camel test` documents, not
    // routes); a document that explicitly declares one must fail the
    // boot instead of starting zero routes.
    let doc = r#"
routeFiles: [routes.test.yaml]
scenario:
  - sleep:
      duration: 1s
"#;
    let (dir, doc) = project("# minimal\n", Some(("routes.test.yaml", ROUTE)), doc);
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "a reserved-suffix route file must fail the boot",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected Config, got: {err:?}"
    );
    let display = err.to_string();
    assert!(
        display.contains("routes.test.yaml"),
        "error must name the declared file: {display}"
    );
    assert!(
        display.contains("`camel test`"),
        "error must carry the `camel test` guidance: {display}"
    );
}

/// A scenario document with no `sql:` action: the boot-time sqlite
/// memory lint fires on the datasource config alone, before any action
/// or route is considered. The declared route file need not exist — the
/// lint runs before route discovery.
const NO_SQL_DOC: &str = r#"
routeFiles: [routes.yaml]
scenario:
  - sleep:
      duration: 1s
"#;

/// Asserts the boot error is NOT the sql-memory-not-shared lint,
/// panicking with `what` when it is.
fn assert_not_memory_lint(
    result: Result<crate::boot_scenario::ScenarioRun, CamelError>,
    what: &str,
) {
    if let Err(err) = result {
        let display = err.to_string();
        assert!(
            !display.contains(crate::SQL_MEMORY_NOT_SHARED),
            "{what}: unexpected sql-memory-not-shared lint: {display}"
        );
    }
}

#[tokio::test]
async fn bare_memory_sqlite_rejected() {
    // A bare `sqlite::memory:` URL gives every pooled connection its
    // own private in-memory database; the boot must reject it before
    // any context preparation, naming the datasource (never the URL).
    let (dir, doc) = project(
        r#"
[datasources.appdb]
db_url = "sqlite::memory:"
"#,
        None,
        NO_SQL_DOC,
    );
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "a bare sqlite :memory: datasource must fail the boot",
    );
    assert!(
        matches!(err, CamelError::Config(_)),
        "expected Config, got: {err:?}"
    );
    let display = err.to_string();
    assert!(
        display.contains(crate::SQL_MEMORY_NOT_SHARED),
        "error must carry the sql-memory-not-shared key: {display}"
    );
    assert!(
        display.contains("appdb"),
        "error must name the datasource: {display}"
    );
}

#[tokio::test]
async fn shared_cache_memory_sqlite_passes_lint() {
    // `?cache=shared` makes all connections in the process share one
    // in-memory database, so the lint must not fire. The boot may still
    // fail later for unrelated reasons, so only the absence of the lint
    // is asserted.
    let (dir, doc) = project(
        r#"
[datasources.appdb]
db_url = "sqlite::memory:?cache=shared"
"#,
        None,
        NO_SQL_DOC,
    );
    assert_not_memory_lint(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "a shared-cache sqlite :memory: datasource must pass the lint",
    );
}

#[tokio::test]
async fn file_backed_sqlite_unaffected() {
    // A file-backed sqlite URL is not an in-memory database at all; the
    // lint must not fire.
    let (dir, doc) = project(
        r#"
[datasources.appdb]
db_url = "sqlite:file:/tmp/x.db"
"#,
        None,
        NO_SQL_DOC,
    );
    assert_not_memory_lint(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "a file-backed sqlite datasource must pass the lint",
    );
}

/// The lint is a config-shape check, so it runs even in a build without
/// the `sql` feature.
#[cfg(not(feature = "sql"))]
#[tokio::test]
async fn lint_is_ungated() {
    let (dir, doc) = project(
        r#"
[datasources.appdb]
db_url = "sqlite::memory:"
"#,
        None,
        NO_SQL_DOC,
    );
    let err = expect_boot_error(
        boot_scenario(&doc, dir.path(), &empty_env()).await,
        "the sqlite memory lint must fire without the sql feature",
    );
    assert!(
        err.to_string().contains(crate::SQL_MEMORY_NOT_SHARED),
        "error must carry the sql-memory-not-shared key: {err}"
    );
}