ignition-cli 1.6.0

ign — operate Ignition 8.3+ gateways from the terminal
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
//! Binary-level contract tests for `ign api call` (09-03, EXT-01) —
//! the FULL passthrough contract pinned through the real binary over
//! a wiremock gateway:
//!
//! 1. THE verbatim pin: the envelope's `data.result.data` carries the
//!    gateway's JSON byte-verbatim (unusual key order + unknown
//!    fields preserved) — the RawValue passthrough decision as
//!    executable contract, and the README's documented contract
//!    exception.
//! 2. The exit partition table: 400+JSON → exit 2
//!    `gateway_client_error` (verbatim body in the message);
//!    401 → 5 `auth_rejected`; 404 → 6 `not_found`; 503 → 6
//!    `gateway_restarting`; 500 → 1 `internal`.
//! 3. The 4 KiB truncation cap with the explicit `... [truncated]`
//!    marker on oversized error bodies.
//! 4. The refusal matrix: auth-pattern headers (`Authorization`,
//!    case-variant `X-IGNITION-API-TOKEN`) refuse exit 2
//!    `invalid_input` with `profile: null` and ZERO gateway requests
//!    (the guard runs pre-resolve).
//! 5. Path validation: missing leading slash, absolute/foreign-host
//!    URLs, and an embedded `?` (which names `--query`) all refuse
//!    exit 2 pre-resolution.
//! 6. The ONE query mechanism: repeatable `--query k=v` pairs ride
//!    the request.
//! 7. GET body passthrough (curl parity): `--data` rides ANY method.
//! 8. A non-JSON 2xx body refuses exit 1 `internal` with the
//!    explanatory message.
//!
//! Isolation is the version_gateway_contract pattern:
//! `IGNITION_CLI_CONFIG` → tempfile whose one profile points at the
//! mock server. wiremock is an existing ignition-cli dev-dep — NO new
//! deps (plan constraint).

use std::path::{Path, PathBuf};

use assert_cmd::Command;
use serde_json::Value;

fn isolated_config() -> (tempfile::TempDir, PathBuf) {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    (dir, path)
}

/// Write a one-profile config (`active = "dev"`) whose URL points at `url`.
fn write_profile_config(config: &Path, url: &str) {
    std::fs::write(
        config,
        format!("active = \"dev\"\n\n[profiles.dev]\nurl = \"{url}\"\n"),
    )
    .expect("write config");
}

/// Spawn `ign api call --json …` with an isolated config. Ambient
/// `IGNITION_*` knobs are stripped (the stdout-purity-harness rule):
/// a developer shell's IGNITION_URL/PROFILE/YES/JSON must never
/// redirect a contract test's request. `IGNITION_TOKEN` is SET (the
/// generic EnvStore rung of the locked secret chain): `api call`
/// resolves through `Session::resolve` like every authed command, so
/// a credential must exist — the mock never validates it, only its
/// presence matters.
fn ign_api_call(config: &Path, args: &[&str]) -> std::process::Output {
    let mut command = Command::cargo_bin("ign").expect("binary 'ign' not found");
    command.env("IGNITION_CLI_CONFIG", config);
    command.env("IGNITION_TOKEN", "ign-contract-test:0123456789abcdef");
    for knob in [
        "IGNITION_URL",
        "IGNITION_PROFILE",
        "IGNITION_JSON",
        "IGNITION_YES",
    ] {
        command.env_remove(knob);
    }
    command
        .args(["api", "call", "--json"])
        .args(args)
        .output()
        .expect("spawn ign")
}

/// The error envelope is the JSON object starting at the first `{` on
/// stderr (tracing log lines share stderr by design — see
/// version_gateway_contract.rs).
fn stderr_envelope(out: &std::process::Output) -> Value {
    let stderr = String::from_utf8_lossy(&out.stderr);
    let start = stderr.find('{').unwrap_or(0);
    serde_json::from_str(&stderr[start..])
        .unwrap_or_else(|err| panic!("stderr envelope parses (full stderr {stderr:?}): {err}"))
}

/// A mock that answers `status` with `body` on GET /data/api/v1/x.
async fn mount_status(server: &wiremock::MockServer, status: u16, body: &str) {
    wiremock::Mock::given(wiremock::matchers::method("GET"))
        .and(wiremock::matchers::path("/data/api/v1/x"))
        .respond_with(wiremock::ResponseTemplate::new(status).set_body_string(body))
        .expect(1)
        .mount(server)
        .await;
}

/// THE verbatim pin (first contract test): unusual key order AND
/// unknown fields ride the envelope's `data.result.data` EXACTLY —
/// `zz_last` before `alpha_first`, `b` before `a` — with no new
/// top-level envelope fields. The RawValue passthrough decision is
/// now executable contract (README's documented exception).
#[tokio::test]
async fn data_is_gateway_verbatim_key_order_and_unknown_fields_preserved() {
    let server = wiremock::MockServer::start().await;
    // set_body_string (NOT set_body_json): the WIRE bytes must carry
    // the unusual order — a serde_json Value would re-sort keys and
    // neuter this pin.
    mount_status(
        &server,
        200,
        r#"{"zz_last": 1, "alpha_first": {"b": 2, "a": 1}}"#,
    )
    .await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());
    let out = ign_api_call(&config, &["--method", "GET", "--path", "/data/api/v1/x"]);
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let stdout = String::from_utf8_lossy(&out.stdout).to_string();
    let body: Value = serde_json::from_str(&stdout).expect("success envelope parses");
    let zz = stdout
        .find("\"zz_last\"")
        .expect("zz_last key present verbatim");
    let alpha = stdout
        .find("\"alpha_first\"")
        .expect("alpha_first key present verbatim");
    let b = stdout.find("\"b\"").expect("b key present verbatim");
    let a = stdout.find("\"a\"").expect("a key present verbatim");
    assert!(zz < alpha, "original key order preserved: {stdout}");
    assert!(b < a, "nested key order preserved: {stdout}");

    // The outcome shape: method/path echo + status, data inline.
    assert_eq!(body["ok"], Value::Bool(true));
    assert_eq!(body["profile"], Value::String("dev".into()));
    assert_eq!(body["data"]["method"], Value::String("GET".into()));
    assert_eq!(body["data"]["path"], Value::String("/data/api/v1/x".into()));
    assert_eq!(body["data"]["result"]["status"], Value::Number(200.into()));
    assert_eq!(
        body["data"]["result"]["data"]["zz_last"],
        Value::Number(1.into()),
        "unknown fields survive untouched"
    );

    // The LOCKED envelope never grows top-level fields.
    let mut keys: Vec<&str> = body
        .as_object()
        .expect("envelope is an object")
        .keys()
        .map(String::as_str)
        .collect();
    keys.sort_unstable();
    assert_eq!(keys, ["data", "ok", "profile"]);
}

/// The exit partition table (research OQ7) — each row: (status, exit
/// code, slug). The 400 row additionally pins the verbatim body in
/// the message; every error row keeps stdout empty.
#[tokio::test]
async fn exit_partition_table() {
    let cases: [(u16, i32, &str, bool); 5] = [
        (400, 2, "gateway_client_error", true),
        (401, 5, "auth_rejected", false),
        (404, 6, "not_found", false),
        (503, 6, "gateway_restarting", false),
        (500, 1, "internal", false),
    ];
    for (status, exit, slug, body_verbatim) in cases {
        let server = wiremock::MockServer::start().await;
        let marker = format!("case-{status}-gateway-body");
        let body = format!(r#"{{"detail":"{marker}"}}"#);
        mount_status(&server, status, &body).await;

        let (_dir, config) = isolated_config();
        write_profile_config(&config, &server.uri());
        let out = ign_api_call(&config, &["--method", "GET", "--path", "/data/api/v1/x"]);
        assert_eq!(
            out.status.code(),
            Some(exit),
            "status {status} must exit {exit} ({slug}); stderr: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert!(out.stdout.is_empty(), "errors never touch stdout");

        let envelope = stderr_envelope(&out);
        assert_eq!(envelope["ok"], Value::Bool(false), "status {status}");
        assert_eq!(
            envelope["error"]["code"],
            Value::String(slug.to_string()),
            "status {status}"
        );
        let message = envelope["error"]["message"].as_str().expect("message");
        if body_verbatim {
            assert!(
                message.contains(&marker),
                "the 4xx body rides the message verbatim: {message}"
            );
        }
    }
}

/// Pitfall 6's cap: a >4 KiB error body truncates at 4 KiB with the
/// explicit `... [truncated]` marker in the envelope message.
#[tokio::test]
async fn oversized_error_body_truncates_at_cap_with_marker() {
    let server = wiremock::MockServer::start().await;
    let big = "A".repeat(5000);
    mount_status(&server, 400, &format!(r#"{{"pad":"{big}"}}"#)).await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());
    let out = ign_api_call(&config, &["--method", "GET", "--path", "/data/api/v1/x"]);
    assert_eq!(out.status.code(), Some(2), "gateway_client_error class");

    let envelope = stderr_envelope(&out);
    assert_eq!(
        envelope["error"]["code"],
        Value::String("gateway_client_error".into())
    );
    let message = envelope["error"]["message"].as_str().expect("message");
    assert!(
        message.contains("... [truncated]"),
        "the cap carries an explicit marker: {message}"
    );
    assert!(
        message.len() < 5000 + 200,
        "the message is capped near the 4 KiB body cap, not the full body: {}",
        message.len()
    );
}

/// THE refusal matrix (binary): auth-pattern headers refuse exit 2
/// `invalid_input` with `profile: null` — and the mock server
/// received ZERO requests (the guard runs pre-resolve; nothing is
/// ever constructed or sent).
#[tokio::test]
async fn auth_pattern_headers_refused_with_zero_requests() {
    for header in ["Authorization: Bearer x", "X-IGNITION-API-TOKEN: zzz"] {
        let server = wiremock::MockServer::start().await; // bare — no mounts
        let (_dir, config) = isolated_config();
        write_profile_config(&config, &server.uri());
        let out = ign_api_call(
            &config,
            &[
                "--method",
                "GET",
                "--path",
                "/data/api/v1/x",
                "--header",
                header,
            ],
        );
        assert_eq!(
            out.status.code(),
            Some(2),
            "{header} must refuse exit 2; stderr: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert!(out.stdout.is_empty(), "errors never touch stdout");

        let envelope = stderr_envelope(&out);
        assert_eq!(
            envelope["error"]["code"],
            Value::String("invalid_input".into())
        );
        assert_eq!(
            envelope["profile"],
            Value::Null,
            "the refusal fired pre-resolve — the envelope profile stays null"
        );
        let received = server.received_requests().await.unwrap_or_default();
        assert!(
            received.is_empty(),
            "ZERO requests may hit the gateway on a refusal ({header})"
        );
    }
}

/// Path validation (binary): missing leading slash, an absolute
/// foreign-host URL, and an embedded `?` all refuse exit 2
/// pre-resolution — the `?` case names `--query` (the ONE query
/// mechanism).
#[tokio::test]
async fn path_validation_refuses_bad_shapes_with_specific_reasons() {
    let server = wiremock::MockServer::start().await;
    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());

    let cases = [
        ("data/api/v1/x", None),
        ("http://other-host/x", None),
        ("/data/api/v1/x?embed=1", Some("--query")),
    ];
    for (path, expected_hint) in cases {
        let out = ign_api_call(&config, &["--method", "GET", "--path", path]);
        assert_eq!(
            out.status.code(),
            Some(2),
            "{path} must refuse exit 2; stderr: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        let envelope = stderr_envelope(&out);
        assert_eq!(
            envelope["error"]["code"],
            Value::String("invalid_input".into()),
            "{path}"
        );
        assert_eq!(envelope["profile"], Value::Null, "{path}: pre-resolve");
        if let Some(expected_hint) = expected_hint {
            let message = envelope["error"]["message"].as_str().expect("message");
            assert!(
                message.contains(expected_hint),
                "the ? refusal names the ONE query mechanism: {message}"
            );
        }
    }
    // Zero requests: every case refused pre-resolution.
    let received = server.received_requests().await.unwrap_or_default();
    assert!(received.is_empty(), "no case may hit the gateway");
}

/// The ONE query mechanism: repeatable `--query k=v` — both pairs
/// arrive on the request (the mock only answers when BOTH match).
#[tokio::test]
async fn query_pairs_ride_the_request() {
    let server = wiremock::MockServer::start().await;
    wiremock::Mock::given(wiremock::matchers::method("GET"))
        .and(wiremock::matchers::path("/data/api/v1/x"))
        .and(wiremock::matchers::query_param("a", "1"))
        .and(wiremock::matchers::query_param("b", "2"))
        .respond_with(wiremock::ResponseTemplate::new(200).set_body_string("{}"))
        .expect(1)
        .mount(&server)
        .await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());
    let out = ign_api_call(
        &config,
        &[
            "--method",
            "GET",
            "--path",
            "/data/api/v1/x",
            "--query",
            "a=1",
            "--query",
            "b=2",
        ],
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let requests = server.received_requests().await.expect("requests recorded");
    assert_eq!(requests.len(), 1);
    let url = requests[0].url.clone();
    assert!(url.query().unwrap_or_default().contains("a=1"), "{url}");
    assert!(url.query().unwrap_or_default().contains("b=2"), "{url}");
}

/// GET body passthrough (curl parity — decision 6): `--data` rides
/// ANY method, byte-verbatim.
#[tokio::test]
async fn get_method_carries_the_body() {
    let server = wiremock::MockServer::start().await;
    wiremock::Mock::given(wiremock::matchers::method("GET"))
        .and(wiremock::matchers::path("/data/api/v1/x"))
        .and(wiremock::matchers::body_bytes(br#"{"x":1}"#.to_vec()))
        .respond_with(wiremock::ResponseTemplate::new(200).set_body_string("{}"))
        .expect(1)
        .mount(&server)
        .await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());
    let out = ign_api_call(
        &config,
        &[
            "--method",
            "GET",
            "--path",
            "/data/api/v1/x",
            "--data",
            r#"{"x":1}"#,
        ],
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let requests = server.received_requests().await.expect("requests recorded");
    assert_eq!(requests.len(), 1);
    assert_eq!(
        String::from_utf8_lossy(&requests[0].body),
        r#"{"x":1}"#,
        "the GET carries the raw body verbatim"
    );
}

/// A non-JSON 2xx body refuses exit 1 `internal` with the
/// explanatory message (the README's documented honesty refusal —
/// binary endpoints belong to the download pipelines).
#[tokio::test]
async fn non_json_2xx_refuses_internal_with_explanation() {
    let server = wiremock::MockServer::start().await;
    mount_status(&server, 200, "this is not json").await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, &server.uri());
    let out = ign_api_call(&config, &["--method", "GET", "--path", "/data/api/v1/x"]);
    assert_eq!(out.status.code(), Some(1), "internal class");
    assert!(out.stdout.is_empty(), "errors never touch stdout");

    let envelope = stderr_envelope(&out);
    assert_eq!(envelope["error"]["code"], Value::String("internal".into()));
    let message = envelope["error"]["message"].as_str().expect("message");
    assert!(
        message.contains("non-JSON"),
        "the message explains the contract: {message}"
    );
}