boxlite-cli 0.9.2

Command-line interface for BoxLite container runtime
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
use std::time::Duration;

use assert_cmd::Command;
use predicates::prelude::*;

mod common;

/// No args and no --latest
#[test]
fn test_inspect_no_args() {
    let ctx = common::boxlite();
    ctx.new_cmd()
        .args(["inspect"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("no names or ids specified"));
}

#[test]
fn test_inspect_nonexistent() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["inspect", "no-such-box-123"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("no such"))
        .stdout(predicate::str::contains("[]"));
}

#[test]
fn test_inspect_by_name() {
    let mut ctx = common::boxlite();
    let name = "inspect-by-name";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();
    let output = ctx.new_cmd().args(["inspect", name]).output().unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("inspect output should be valid JSON");
    let arr = v.as_array().expect("inspect output should be a JSON array");
    assert_eq!(arr.len(), 1, "single box => array of one");
    let obj = arr[0].as_object().expect("first element should be object");
    assert!(obj.contains_key("Id"), "JSON should contain Id");
    assert!(obj.contains_key("Image"), "JSON should contain Image");
    assert!(obj.contains_key("Status"), "JSON should contain Status");
    assert_eq!(
        obj.get("Image").and_then(|s| s.as_str()),
        Some("alpine:latest")
    );

    ctx.cleanup_box(name);
}

#[test]
fn test_inspect_by_id() {
    let mut ctx = common::boxlite();
    let create_out = ctx.cmd.args(["create", "alpine:latest"]).output().unwrap();
    assert!(create_out.status.success());
    let id = String::from_utf8(create_out.stdout)
        .unwrap()
        .trim()
        .to_string();
    assert!(id.len() >= 12, "id should be at least 12 chars");

    let output = ctx.new_cmd().args(["inspect", &id]).output().unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("inspect output should be valid JSON");
    let arr = v.as_array().expect("inspect output should be a JSON array");
    assert_eq!(arr.len(), 1);
    let obj = arr[0].as_object().expect("first element should be object");
    assert_eq!(obj.get("Id").and_then(|s| s.as_str()), Some(id.as_str()));

    ctx.cleanup_box(&id);
}

#[test]
fn test_inspect_format_json() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-json";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();
    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "json", name])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let _: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("--format json should produce valid JSON");

    ctx.cleanup_box(name);
}

#[test]
fn test_inspect_format_yaml() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-yaml";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();
    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "yaml", name])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let _: serde_yaml::Value =
        serde_yaml::from_str(stdout.trim()).expect("--format yaml should produce valid YAML");

    ctx.cleanup_box(name);
}

#[test]
fn test_inspect_format_template_state() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-template";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();

    // boxlite inspect --format '{{.State}}' hellobox
    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "{{.State}}", name])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    // State is an object with status, running, pid; output may be JSON or single line
    assert!(
        stdout.contains("running") || stdout.contains("configured") || stdout.contains("stopped"),
        "template {{.State}} output should contain status value; got: {}",
        stdout.trim()
    );

    ctx.cleanup_box(name);
}

/// {{json .State}} outputs JSON (Podman/Docker aligned).
#[test]
fn test_inspect_format_template_json_state() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-json-state";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();

    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "{{json .State}}", name])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap().trim().to_string();
    let v: serde_json::Value =
        serde_json::from_str(&stdout).expect("{{json .State}} should produce valid JSON");
    let obj = v.as_object().expect("should be object");
    assert!(obj.contains_key("Status"));
    assert!(obj.contains_key("Running"));
    assert!(obj.contains_key("Pid"));

    ctx.cleanup_box(name);
}

/// Invalid format (e.g. xml, foo)
#[test]
fn test_inspect_format_invalid() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-invalid";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();

    for bad_format in ["xml", "foo", "XML"] {
        let output = ctx
            .new_cmd()
            .args(["inspect", "--format", bad_format, name])
            .output()
            .unwrap();
        assert!(
            !output.status.success(),
            "format {:?} should fail",
            bad_format
        );
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("Unknown format") || stderr.contains("Valid formats"),
            "stderr should mention unknown/valid format; got: {}",
            stderr
        );
    }

    ctx.cleanup_box(name);
}

#[test]
fn test_inspect_format_table_rejected() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-table";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();

    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "table", name])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("inspect does not support table format"),
        "stderr should say table not supported; got: {}",
        stderr
    );

    ctx.cleanup_box(name);
}

/// Template '{{"\n"}}' must not choke
#[test]
fn test_inspect_format_template_newline() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-newline";
    let _ = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output();

    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", r#"{{"\n"}}"#, name])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.trim().is_empty(),
        "template newline should produce empty output; got: {:?}",
        stdout
    );

    ctx.cleanup_box(name);
}

/// Single-field template {{.Id}}
#[test]
fn test_inspect_format_template_single_field_id() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-id";
    let create_out = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output()
        .unwrap();
    assert!(create_out.status.success());
    let expected_id = String::from_utf8(create_out.stdout)
        .unwrap()
        .trim()
        .to_string();

    let output = ctx
        .new_cmd()
        .args(["inspect", "--format", "{{.Id}}", name])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let got = String::from_utf8(output.stdout).unwrap().trim().to_string();
    assert_eq!(got, expected_id, "{{.Id}} should match create output id");

    ctx.cleanup_box(name);
}

/// Template alias {{.ID}} and {{.ImageID}} work like {{.Id}} / {{.Image}}.
#[test]
fn test_inspect_format_template_alias_id_and_image() {
    let mut ctx = common::boxlite();
    let name = "inspect-format-alias";
    let create_out = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output()
        .unwrap();
    assert!(create_out.status.success());
    let expected_id = String::from_utf8(create_out.stdout)
        .unwrap()
        .trim()
        .to_string();

    let out_id = ctx
        .new_cmd()
        .args(["inspect", "--format", "{{.ID}}", name])
        .output()
        .unwrap();
    assert!(
        out_id.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out_id.stderr)
    );
    assert_eq!(
        String::from_utf8(out_id.stdout).unwrap().trim(),
        expected_id,
        "{{.ID}} should match create output id"
    );

    let out_image = ctx
        .new_cmd()
        .args(["inspect", "--format", "{{.ImageID}}", name])
        .output()
        .unwrap();
    assert!(
        out_image.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out_image.stderr)
    );
    assert_eq!(
        String::from_utf8(out_image.stdout).unwrap().trim(),
        "alpine:latest",
        "{{.ImageID}} should output image"
    );

    ctx.cleanup_box(name);
}

/// Multiple IDs
#[test]
fn test_inspect_multiple_ids() {
    let ctx = common::boxlite();
    let name1 = "inspect-multi-1";
    let name2 = "inspect-multi-2";
    let c1 = ctx
        .new_cmd()
        .args(["create", "--name", name1, "alpine:latest"])
        .output()
        .unwrap();
    assert!(
        c1.status.success(),
        "create {}: {}",
        name1,
        String::from_utf8_lossy(&c1.stderr)
    );
    let c2 = ctx
        .new_cmd()
        .args(["create", "--name", name2, "alpine:latest"])
        .output()
        .unwrap();
    assert!(
        c2.status.success(),
        "create {}: {}",
        name2,
        String::from_utf8_lossy(&c2.stderr)
    );

    let output = ctx
        .new_cmd()
        .args(["inspect", name1, name2])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("inspect output should be valid JSON");
    let arr = v.as_array().expect("inspect output should be a JSON array");
    assert_eq!(arr.len(), 2, "inspect id1 id2 => array of two");
    let ids: Vec<&str> = arr
        .iter()
        .filter_map(|o| o.get("Id").and_then(|s| s.as_str()))
        .collect();
    assert_eq!(ids.len(), 2);
    let names: Vec<&str> = arr
        .iter()
        .filter_map(|o| o.get("Name").and_then(|s| s.as_str()))
        .collect();
    assert!(names.contains(&name1));
    assert!(names.contains(&name2));

    ctx.cleanup_box(name1);
    ctx.cleanup_box(name2);
}

/// --latest: inspect the most recently created box.
#[test]
fn test_inspect_latest() {
    let mut ctx = common::boxlite();
    let name = "inspect-latest-box";
    let create_out = ctx
        .cmd
        .args(["create", "--name", name, "alpine:latest"])
        .output()
        .unwrap();
    assert!(create_out.status.success());
    let expected_id = String::from_utf8(create_out.stdout)
        .unwrap()
        .trim()
        .to_string();

    let output = ctx
        .new_cmd()
        .args(["inspect", "--latest"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let v: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("inspect --latest output should be valid JSON");
    let arr = v.as_array().expect("inspect output should be a JSON array");
    assert_eq!(arr.len(), 1);
    let obj = arr[0].as_object().expect("first element should be object");
    assert_eq!(
        obj.get("Id").and_then(|s| s.as_str()),
        Some(expected_id.as_str())
    );
    assert_eq!(obj.get("Name").and_then(|s| s.as_str()), Some(name));

    ctx.cleanup_box(name);
}

/// --latest with no boxes: use isolated empty home so there are no boxes
#[test]
fn test_inspect_latest_no_boxes() {
    let temp = tempfile::tempdir().unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_boxlite"))
        .timeout(Duration::from_secs(60))
        .arg("--home")
        .arg(temp.path())
        .args(["inspect", "--latest"])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "inspect --latest with no boxes must fail; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("no boxes to inspect"),
        "stderr should mention no boxes to inspect; got: {}",
        stderr
    );
}

/// --latest and BOX arguments are mutually exclusive
#[test]
fn test_inspect_latest_and_ref_fail() {
    let ctx = common::boxlite();
    let output = ctx
        .new_cmd()
        .args(["inspect", "--latest", "some-id"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used together"),
        "stderr should say --latest and arguments cannot be used together; got: {}",
        stderr
    );
}