ignition-cli 1.7.1

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
//! Golden-file contract tests for `ign restart` + `ign wait` (02-05,
//! HLTH-09/11): the ALWAYS-guarded restart, the full restart --wait
//! lifecycle, the three wait targets, and the timeout taxonomy.
//! Harness inherited from `contract_logs.rs` (01-02): isolated
//! `IGNITION_CLI_CONFIG`, `stdout_for_golden`, `[..]` elides only
//! genuinely dynamic values (elapsed seconds, timeout durations).
//!
//! The crown pins:
//! - `ign restart` is unreachable without `--yes` in EVERY mode (exit
//!   2 `confirmation_required`, BEFORE any network work — no server
//!   mounted);
//! - `restart --yes --wait` completes the full mock lifecycle (POST →
//!   5 s floor → STARTING → RUNNING) and the golden elides the elapsed
//!   seconds;
//! - `wait restart` (RUNNING→STARTING→RUNNING) succeeds via the
//!   WITNESSED path — returns as soon as RUNNING follows non-RUNNING,
//!   no floor wait (the golden stays fast; the all-RUNNING floor path
//!   is core-tested via the injected floor in restart_wait_contract);
//! - a wait timeout exits 4 with the last observed state in the
//!   message.

use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

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

/// Isolated config dir + the config path inside it (file need not exist).
fn isolated_config() -> (tempfile::TempDir, PathBuf) {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    (dir, path)
}

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

/// Spawn `ign` with an isolated config, the mock token in the env, and args.
fn ign(config: &Path, url: &str, args: &[&str]) -> std::process::Output {
    let mut command = Command::cargo_bin("ign").expect("binary 'ign' not found");
    command
        .env("IGNITION_CLI_CONFIG", config)
        .env("IGNITION_TOKEN", "mock:name-key")
        .env("IGNITION_URL", url);
    command.args(args).output().expect("spawn ign")
}

/// Spawn `ign` WITHOUT the token env — the secret degrades to None and
/// the header-less waits must still work (the whole point of
/// `wait gateway`/`wait restart`).
fn ign_tokenless(config: &Path, url: &str, args: &[&str]) -> std::process::Output {
    let mut command = Command::cargo_bin("ign").expect("binary 'ign' not found");
    command
        .env("IGNITION_CLI_CONFIG", config)
        .env_remove("IGNITION_TOKEN")
        .env("IGNITION_URL", url);
    command.args(args).output().expect("spawn ign")
}

/// stdout minus the single trailing newline `println!` appends.
fn stdout_for_golden(out: &std::process::Output) -> &str {
    let stdout = std::str::from_utf8(&out.stdout).expect("utf-8 stdout");
    stdout.strip_suffix('\n').unwrap_or(stdout)
}

/// stderr's JSON envelope starting at the first `{` (log-tolerant parse).
fn stderr_envelope(out: &std::process::Output) -> String {
    let stderr = String::from_utf8_lossy(&out.stderr);
    let start = stderr.find('{').unwrap_or(0);
    stderr[start..].to_string()
}

/// A scripted StatusPing: serves `states` in order, the LAST entry
/// repeating forever (the same fixture shape as the core contract
/// suite).
#[derive(Clone)]
struct StatusPingScript {
    hits: Arc<Mutex<usize>>,
    states: Vec<String>,
}

impl StatusPingScript {
    fn new(states: &[&str]) -> Self {
        Self {
            hits: Arc::new(Mutex::new(0)),
            states: states.iter().map(|s| s.to_string()).collect(),
        }
    }
}

impl wiremock::Respond for StatusPingScript {
    fn respond(&self, _request: &wiremock::Request) -> wiremock::ResponseTemplate {
        let mut hits = self.hits.lock().unwrap();
        *hits += 1;
        let state = self
            .states
            .get(*hits - 1)
            .unwrap_or_else(|| self.states.last().expect("at least one state"));
        wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({ "state": state }))
    }
}

/// Mount the restart POST (200 + literal `true`, recorded).
async fn mount_restart_post(server: &wiremock::MockServer) -> wiremock::MockGuard {
    wiremock::Mock::given(wiremock::matchers::method("POST"))
        .and(wiremock::matchers::path(
            "/data/api/v1/restart-tasks/restart",
        ))
        .and(wiremock::matchers::query_param("confirm", "true"))
        .respond_with(wiremock::ResponseTemplate::new(200).set_body_string("true"))
        .expect(1..)
        .mount_as_scoped(server)
        .await
}

/// Mount a scripted StatusPing responder (any number of hits).
async fn mount_status_ping(server: &wiremock::MockServer, script: StatusPingScript) {
    wiremock::Mock::given(wiremock::matchers::method("GET"))
        .and(wiremock::matchers::path("/StatusPing"))
        .respond_with(script)
        .expect(1..)
        .mount(server)
        .await
}

/// THE guard pin: `ign restart` without `--yes` exits 2 with the
/// `confirmation_required` envelope BEFORE any network work — no
/// server is mounted; the unreachable URL proves the refusal never
/// fired a request.
#[tokio::test]
async fn restart_without_yes_exits_2_before_any_network() {
    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://127.0.0.1:1");

    let out = ign(&config, "http://127.0.0.1:1", &["restart", "--compact"]);
    assert_eq!(out.status.code(), Some(2), "usage class");
    assert!(out.stdout.is_empty(), "errors never touch stdout");
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stderr_envelope(&out),
        snapbox::str![[r#"
{"ok":false,"profile":null,"error":{"code":"confirmation_required","message":"restart is destructive; rerun with --yes to confirm","endpoint":null,"hint":"this operation is destructive; re-run with --yes or set IGNITION_YES=1"}}

"#]],
    );

    // The flag is wait-independent: `restart --wait` guards too.
    let out = ign(&config, "http://127.0.0.1:1", &["restart", "--wait"]);
    assert_eq!(out.status.code(), Some(2));
}

/// `ign restart --yes` (no --wait): the POST fires exactly once and
/// the human line carries the READY-in-~1-min advisory + the --wait
/// suggestion; JSON reports `{restarted: true}`.
#[tokio::test]
async fn restart_with_yes_fires_post_and_advises() {
    let server = wiremock::MockServer::start().await;
    let guard = mount_restart_post(&server).await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign(&config, &server.uri(), &["restart", "--yes"]);
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stdout_for_golden(&out),
        snapbox::str![[r#"
[profile: dev]
restarting; gateway READY in ~1 min; consider `ign restart --wait`
"#]],
    );

    let out = ign(&config, &server.uri(), &["restart", "--yes", "--compact"]);
    assert!(out.status.success());
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stdout_for_golden(&out),
        snapbox::str![[r#"{"ok":true,"profile":"dev","data":{"restarted":true}}"#]],
    );

    let requests = guard.received_requests().await;
    assert_eq!(requests.len(), 2, "one POST per invocation");
    for request in &requests {
        assert!(request.body.is_empty(), "empty body");
        assert!(
            request
                .url
                .query()
                .unwrap_or_default()
                .contains("confirm=true"),
            "confirm=true on the query string"
        );
    }
}

/// `ign restart --yes --wait` against the full mock lifecycle (POST →
/// 5 s floor → STARTING → STARTING → RUNNING): success golden with the
/// elapsed seconds elided (the floor makes them dynamic ≥ 5).
#[tokio::test]
async fn restart_yes_wait_completes_lifecycle_golden() {
    let server = wiremock::MockServer::start().await;
    // Bind the guard: a dropped scoped guard UNMOUNTS the fixture
    // (the 02-01 wiremock gotcha) and the verify would fail at 0 hits.
    let _post_guard = mount_restart_post(&server).await;
    mount_status_ping(
        &server,
        StatusPingScript::new(&["STARTING", "STARTING", "RUNNING"]),
    )
    .await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign(
        &config,
        &server.uri(),
        &["restart", "--yes", "--wait", "--interval", "1"],
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stdout_for_golden(&out),
        snapbox::str![[r#"
[profile: dev]
gateway RUNNING after [..]s
"#]],
    );

    let out = ign(
        &config,
        &server.uri(),
        &["restart", "--yes", "--wait", "--interval", "1", "--compact"],
    );
    assert!(out.status.success());
    let body: Value = serde_json::from_slice(&out.stdout).expect("envelope parses");
    assert_eq!(body["ok"], Value::Bool(true));
    assert_eq!(body["data"]["restarted"], Value::Bool(true));
    assert_eq!(body["data"]["state"], Value::String("RUNNING".into()));
    assert!(
        body["data"]["elapsed_secs"].as_u64().expect("elapsed") >= 5,
        "the 5 s floor rode the wait: {}",
        body["data"]["elapsed_secs"]
    );
}

/// `ign wait gateway` against STARTING→RUNNING: the unauth StatusPing
/// poll succeeds — proven header-less by running WITHOUT any token in
/// the env (the secret degrades to None).
#[tokio::test]
async fn wait_gateway_polls_starting_to_running_headerless() {
    let server = wiremock::MockServer::start().await;
    mount_status_ping(&server, StatusPingScript::new(&["STARTING", "RUNNING"])).await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign_tokenless(
        &config,
        &server.uri(),
        &["wait", "gateway", "--interval", "1", "--compact"],
    );
    assert!(
        out.status.success(),
        "no token needed — stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let body: Value = serde_json::from_slice(&out.stdout).expect("envelope parses");
    assert_eq!(body["data"]["target"], Value::String("gateway".into()));
    assert_eq!(body["data"]["state"], Value::String("RUNNING".into()));

    // Human mode: final state + elapsed.
    let out = ign_tokenless(
        &config,
        &server.uri(),
        &["wait", "gateway", "--interval", "1"],
    );
    assert!(out.status.success());
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stdout_for_golden(&out),
        snapbox::str![[r#"
[profile: dev]
gateway RUNNING after [..]s
"#]],
    );
}

/// `ign wait restart` against RUNNING→STARTING→RUNNING: the WITNESSED
/// path returns as soon as RUNNING follows non-RUNNING — no floor wait
/// (fast golden); the all-RUNNING floor path is core-tested with an
/// injected floor.
#[tokio::test]
async fn wait_restart_witnessed_path_golden() {
    let server = wiremock::MockServer::start().await;
    let script = StatusPingScript::new(&["RUNNING", "STARTING", "RUNNING"]);
    let hits = Arc::clone(&script.hits);
    mount_status_ping(&server, script).await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign(
        &config,
        &server.uri(),
        &["wait", "restart", "--interval", "1", "--compact"],
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    // Witnessed restart returns on the RUNNING poll that follows
    // STARTING, without waiting out the 5 s floor. Counted in polls, not
    // wall-clock time: the floor path at `--interval 1` would poll about
    // six times, and a wall-clock bound flaked under parallel test load.
    assert_eq!(
        *hits.lock().unwrap(),
        3,
        "witnessed path is floor-free: it stops at the third poll"
    );
    let body: Value = serde_json::from_slice(&out.stdout).expect("envelope parses");
    assert_eq!(body["data"]["target"], Value::String("restart".into()));
    assert_eq!(body["data"]["state"], Value::String("RUNNING".into()));
}

/// `ign wait module <id>`: the modules poll until ACTIVE.
#[tokio::test]
async fn wait_module_polls_to_active() {
    let server = wiremock::MockServer::start().await;
    wiremock::Mock::given(wiremock::matchers::method("GET"))
        .and(wiremock::matchers::path("/data/api/v1/modules/healthy"))
        .and(wiremock::matchers::query_param(
            "search",
            "com.inductiveautomation.perspective",
        ))
        .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "items": [
                {"id": "com.inductiveautomation.perspective", "name": "Perspective", "version": "8.3.6", "state": "ACTIVE"}
            ],
            "metadata": {"total": 1, "matching": 1, "limit": -1, "offset": 0}
        })))
        .expect(1..)
        .mount(&server)
        .await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign(
        &config,
        &server.uri(),
        &[
            "wait",
            "module",
            "com.inductiveautomation.perspective",
            "--interval",
            "1",
        ],
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    snapbox::Assert::new().action_env("SNAPSHOTS").eq(
        stdout_for_golden(&out),
        snapbox::str![[r#"
[profile: dev]
module com.inductiveautomation.perspective ACTIVE after [..]s
"#]],
    );
}

/// Wait timeout taxonomy: `wait gateway` against an always-STARTING
/// mock with a short deadline exits 4 (`network_error`) and the
/// message carries the last observed state.
#[tokio::test]
async fn wait_timeout_exits_4_with_last_state() {
    let server = wiremock::MockServer::start().await;
    mount_status_ping(&server, StatusPingScript::new(&["STARTING"])).await;

    let (_dir, config) = isolated_config();
    write_profile_config(&config, "http://ignored.example.com");

    let out = ign_tokenless(
        &config,
        &server.uri(),
        &[
            "wait",
            "gateway",
            "--interval",
            "1",
            "--timeout",
            "2",
            "--compact",
        ],
    );
    assert_eq!(out.status.code(), Some(4), "network class");
    let body: Value = serde_json::from_str(&stderr_envelope(&out)).expect("error envelope parses");
    assert_eq!(body["error"]["code"], Value::String("network_error".into()));
    let message = body["error"]["message"]
        .as_str()
        .expect("message present")
        .to_string();
    assert!(
        message.contains("STARTING"),
        "last observed state named: {message}"
    );
    assert!(message.contains("timed out"), "timeout named: {message}");
}