boxlite-cli 0.10.8

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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
use predicates::prelude::*;
use std::net::TcpListener;

mod common;

fn free_host_port() -> u16 {
    let listener = TcpListener::bind("0.0.0.0:0").expect("bind ephemeral host port");
    listener.local_addr().expect("read local addr").port()
}

// ============================================================================
// Exit Code Tests
// ============================================================================

#[test]
fn test_run_exit_code_success() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "exit 0"]);
    ctx.cmd.assert().success();
}

#[test]
fn test_run_exit_code_custom() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "exit 42"]);
    ctx.cmd.assert().code(42);
}

#[test]
fn test_run_exit_code_125() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "exit 125"]);
    ctx.cmd.assert().code(125);
}

/// Regression for #622: `boxlite run --rm <image> <cmd>` exiting non-zero
/// must tear the box down before the CLI returns. Pre-fix, the non-zero
/// path took a `std::process::exit` shortcut that skipped `BoxRunner` /
/// `BoxliteRuntime` drop and therefore `RuntimeImpl::Drop` ->
/// `shutdown_sync`, leaving the microVM's shim alive (the "orphan shims in
/// /tmp" that motivated #604).
///
/// The PerTestBoxHome drop guard catches this implicitly, but this test
/// asserts the no-leak property *explicitly* in the test body so the intent
/// is visible at the call site: scan the per-test home's `boxes/<id>/shim.pid`
/// files after `run` returns and confirm none of them point at a live
/// process.
#[test]
fn test_run_rm_non_zero_exit_does_not_leak_shim() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "exit 7"]);
    ctx.cmd.assert().code(7);

    // Read the boxes directory directly from the shared `ctx.home` so the
    // assertion is independent of `PerTestBoxHome`'s drop hook. If the
    // production cleanup path runs, every shim.pid we find should refer
    // to a dead PID (or have been removed by `--rm`).
    let boxes_dir = ctx.home.join("boxes");
    let leaks = boxlite_test_utils::home::live_shim_pids(&boxes_dir);
    assert!(
        leaks.is_empty(),
        "non-zero `boxlite run --rm` left live shim PID(s): {leaks:?}. \
         The std::process::exit fast path skipped RuntimeImpl::Drop ->\
         shutdown_sync; tearing the runtime down via a normal return is\
         what reaps the shim (see #622)."
    );
}

/// Companion to `test_run_rm_non_zero_exit_does_not_leak_shim`. The RAII
/// teardown must also hold on every *error* path through `BoxRunner::run`
/// — every `?` early-return between the box being created and the
/// streamer finishing has to drop into `RuntimeImpl::Drop` without leaking
/// a shim. Two practical CLI-reachable triggers exist:
///
/// - **`create_box()?`** (`run.rs` branch ②): the image pull fails, so no
///   shim is ever spawned. Asserts no spurious partial-box state was left
///   behind.
/// - **`litebox.exec()?`** (`run.rs` branch ③): the box *is* created and
///   running, then the exec call fails (e.g. invoking a directory). The
///   shim is alive at the failure point; only the Drop chain reaps it.
///
/// The two remaining early-exit branches in `BoxRunner::run` —
/// `streamer.start()?` (signal-handler init, effectively unreachable in a
/// normal CLI environment) and a panic mid-`run()` — are guarded by Rust's
/// language guarantee that stack unwinding always runs `Drop`, plus
/// `PerTestBoxHome::Drop`'s implicit leak panic across every test using
/// `common::boxlite()`. Not adding fragile mock-injection tests for those.
#[test]
fn test_run_image_pull_failure_does_not_leak_shim() {
    let mut ctx = common::boxlite_bare();
    ctx.cmd.timeout(std::time::Duration::from_secs(30));
    ctx.cmd.args([
        "run",
        "--rm",
        "docker.io/boxlite-test-does-not-exist/nonexistent-image:latest",
        "echo",
        "hi",
    ]);
    ctx.cmd.assert().failure();

    let boxes_dir = ctx.home.join("boxes");
    let leaks = boxlite_test_utils::home::live_shim_pids(&boxes_dir);
    assert!(
        leaks.is_empty(),
        "image-pull failure left live shim PID(s): {leaks:?}. \
         `BoxRunner::run` branch ② (create_box?) must drop the runtime \
         cleanly even when no box ever reached Running."
    );
}

#[test]
fn test_run_exec_setup_failure_does_not_leak_shim() {
    let mut ctx = common::boxlite();
    // Invoking `/etc` (a directory) makes the guest's exec setup fail and
    // boxlite returns Err from `litebox.exec(cmd)` — exercising the third
    // `?` in `BoxRunner::run` while the box is fully running.
    ctx.cmd.args(["run", "--rm", "alpine:latest", "/etc"]);
    ctx.cmd.assert().failure();

    let boxes_dir = ctx.home.join("boxes");
    let leaks = boxlite_test_utils::home::live_shim_pids(&boxes_dir);
    assert!(
        leaks.is_empty(),
        "exec-setup failure left live shim PID(s): {leaks:?}. \
         `BoxRunner::run` branch ③ (litebox.exec?) leaves the box \
         Running at the failure point; Drop must still reap the shim."
    );
}

// ============================================================================
// Command Execution Error Tests
// ============================================================================

#[test]
fn test_run_command_not_found() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "nonexistent_command"]);
    ctx.cmd.assert()
        .failure() // Currently exits with 1, should be 127?
        .stderr(
            predicate::str::contains("not found")
                .or(predicate::str::contains("No such file"))
                .or(predicate::str::contains("executable"))
        );
}

#[test]
fn test_run_invalid_executable() {
    let mut ctx = common::boxlite();
    ctx.cmd.args(["run", "--rm", "alpine:latest", "/etc"]);
    ctx.cmd.assert()
        .failure() // Currently exits with 1, should be 126
        .stderr(
            predicate::str::contains("is a directory")
                .or(predicate::str::contains("permission denied"))
                .or(predicate::str::contains("cannot invoke"))
                .or(predicate::str::contains("not a regular file"))
                .or(predicate::str::contains("does not have correct permissions"))
        );
}

// ============================================================================
// Environment Variable Tests
// ============================================================================

#[test]
fn test_run_single_env_var() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-e",
        "BOX=lite",
        "alpine:latest",
        "sh",
        "-c",
        "echo $BOX",
    ]);
    ctx.cmd.assert().success().stdout("lite\n");
}

#[test]
fn test_run_multiple_env_vars() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-e",
        "BOX=lite",
        "-e",
        "HELLO=world",
        "alpine:latest",
        "sh",
        "-c",
        "echo $BOX-$HELLO",
    ]);
    ctx.cmd.assert().success().stdout("lite-world\n");
}

#[test]
fn test_run_env_var_from_host() {
    let mut ctx = common::boxlite();
    ctx.cmd.env("BOXLITE_TEST_VAR", "from_host");
    ctx.cmd.args([
        "run",
        "--rm",
        "-e",
        "BOXLITE_TEST_VAR",
        "alpine:latest",
        "sh",
        "-c",
        "echo $BOXLITE_TEST_VAR",
    ]);
    ctx.cmd.assert().success().stdout("from_host\n");
}

#[test]
fn test_run_env_var_empty_value() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-e",
        "EMPTY=",
        "alpine:latest",
        "sh",
        "-c",
        "echo \"x${EMPTY}x\"",
    ]);

    ctx.cmd.assert().success().stdout("xx\n");
}

// ============================================================================
// Working Directory Tests
// ============================================================================

#[test]
fn test_run_working_dir_default() {
    let mut ctx = common::boxlite();
    ctx.cmd.args(["run", "--rm", "alpine:latest", "pwd"]);
    ctx.cmd.assert().success().stdout("/\n");
}

#[test]
fn test_run_working_dir_custom() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "-w", "/tmp", "alpine:latest", "pwd"]);
    ctx.cmd.assert().success().stdout("/tmp\n");
}

#[test]
fn test_run_working_dir_absolute_path() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "-w", "/etc", "alpine:latest", "pwd"]);
    ctx.cmd.assert().success().stdout("/etc\n");
}

// ============================================================================
// IO Stream Tests
// ============================================================================

#[test]
fn test_run_stdout_capture() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "echo", "hello boxlite"]);
    ctx.cmd.assert().success().stdout("hello boxlite\n");
}

#[test]
fn test_run_stderr_capture() {
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "echo error >&2"]);
    ctx.cmd
        .assert()
        .success()
        .stderr(predicate::str::contains("error"));
}

#[test]
fn test_run_stdout_stderr_separate() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "alpine:latest",
        "sh",
        "-c",
        "echo out; echo err >&2",
    ]);
    ctx.cmd
        .assert()
        .success()
        .stdout("out\n")
        .stderr(predicate::str::contains("err"));
}

#[test]
fn test_run_multiline_output() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "alpine:latest",
        "sh",
        "-c",
        "echo line1; echo line2; echo line3",
    ]);
    ctx.cmd.assert().success().stdout("line1\nline2\nline3\n");
}

// ============================================================================
// Naming Tests
// ============================================================================

#[test]
fn test_run_with_name() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "--name",
        "test-boxlite",
        "alpine:latest",
        "echo",
        "helloboxlite",
    ]);

    ctx.cmd.assert().success().stdout("helloboxlite\n");
}

// ============================================================================
// Resource Limit Tests
// ============================================================================

#[test]
fn test_run_cpus_limit() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "--cpus",
        "2",
        "alpine:latest",
        "echo",
        "boxlite-cpu-limited",
    ]);

    ctx.cmd.assert().success().stdout("boxlite-cpu-limited\n");
}

#[test]
fn test_run_memory_limit() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "--memory",
        "512",
        "alpine:latest",
        "echo",
        "boxlite-memory-limited",
    ]);

    ctx.cmd
        .assert()
        .success()
        .stdout("boxlite-memory-limited\n");
}

#[test]
fn test_run_combined_resource_limits() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "--cpus",
        "1",
        "--memory",
        "256",
        "alpine:latest",
        "echo",
        "boxlite-resource-limited",
    ]);

    ctx.cmd
        .assert()
        .success()
        .stdout("boxlite-resource-limited\n");
}

// ============================================================================
// Interactive, Detach, Cleanup
// ============================================================================

#[test]
fn test_run_interactive_pipe() {
    // e.g. echo "hello" | boxlite run -i ... cat
    let mut ctx = common::boxlite();
    ctx.cmd.args(["run", "--rm", "-i", "alpine:latest", "cat"]);
    //simulate pipe
    ctx.cmd.write_stdin("hello from boxlite pipe\n");
    ctx.cmd
        .assert()
        .success()
        .stdout("hello from boxlite pipe\n");
}

#[test]
fn test_run_detach() {
    let mut ctx = common::boxlite();
    ctx.cmd.args(["run", "-d", "alpine:latest", "sleep", "300"]);
    let output = ctx.cmd.assert().success().get_output().clone();
    let stdout = String::from_utf8_lossy(&output.stdout);
    let box_id = stdout.trim();

    assert!(!box_id.is_empty());
    assert!(box_id.chars().all(|c| c.is_alphanumeric()));

    // Cleanup: Detached boxes must be manually removed since they don't support --rm
    let mut rm_cmd = ctx.new_cmd();
    rm_cmd.args(["rm", "--force", box_id]);
    rm_cmd.assert().success();
}

#[test]
fn test_run_rm_cleanup() {
    let mut ctx = common::boxlite();
    let name = "test-auto-remove-boxlite";

    ctx.cmd.args([
        "run",
        "--rm",
        "--name",
        name,
        "alpine:latest",
        "echo",
        "done",
    ]);

    ctx.cmd.assert().success();

    // run another box with the SAME name
    let mut cmd2 = ctx.new_cmd();

    cmd2.args([
        "run",
        "--rm",
        "--name",
        name,
        "alpine:latest",
        "echo",
        "reused",
    ]);
    cmd2.assert().success().stdout("reused\n");
}

// ============================================================================
// Port Publish (-p / --publish) Tests
// ============================================================================

#[test]
fn test_run_with_publish_success() {
    let mut ctx = common::boxlite();
    let host_port = free_host_port();
    ctx.cmd.args([
        "run",
        "--rm",
        "-p",
        &format!("{host_port}:18789"),
        "alpine:latest",
        "echo",
        "ok",
    ]);
    ctx.cmd.assert().success().stdout("ok\n");
}

#[test]
fn test_run_with_publish_short_flag() {
    let mut ctx = common::boxlite();
    let host_port = free_host_port();
    ctx.cmd.args([
        "run",
        "--rm",
        "-p",
        &format!("{host_port}:80"),
        "alpine:latest",
        "sh",
        "-c",
        "echo done",
    ]);
    ctx.cmd.assert().success().stdout("done\n");
}

#[test]
fn test_run_with_publish_tcp_suffix() {
    let mut ctx = common::boxlite();
    let host_port = free_host_port();
    ctx.cmd.args([
        "run",
        "--rm",
        "--publish",
        &format!("{host_port}:9000/tcp"),
        "alpine:latest",
        "echo",
        "tcp",
    ]);
    ctx.cmd.assert().success().stdout("tcp\n");
}

#[test]
fn test_run_with_publish_invalid_format() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-p",
        "not-a-port",
        "alpine:latest",
        "echo",
        "ok",
    ]);
    ctx.cmd
        .assert()
        .failure()
        .stderr(predicate::str::contains("invalid"));
}

// ============================================================================
// Volume (-v / --volume) Tests
// ============================================================================

#[test]
fn test_run_with_volume_success() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path();
    std::fs::write(path.join("hello.txt"), "hello-boxlite\n").unwrap();

    let mut ctx = common::boxlite();
    let host_path = path.to_str().unwrap();
    ctx.cmd.args([
        "run",
        "--rm",
        "-v",
        &format!("{}:/data", host_path),
        "alpine:latest",
        "cat",
        "/data/hello.txt",
    ]);
    ctx.cmd.assert().success().stdout("hello-boxlite\n");
}

#[test]
fn test_run_with_volume_short_flag() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path();
    std::fs::write(path.join("x"), "ok").unwrap();

    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-v",
        &format!("{}:/mnt", path.to_str().unwrap()),
        "alpine:latest",
        "cat",
        "/mnt/x",
    ]);
    ctx.cmd.assert().success().stdout("ok");
}

#[test]
fn test_run_with_volume_read_only() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path();
    std::fs::write(path.join("boxlite.txt"), "readonly").unwrap();

    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-v",
        &format!("{}:/data:ro", path.to_str().unwrap()),
        "alpine:latest",
        "cat",
        "/data/boxlite.txt",
    ]);
    ctx.cmd.assert().success().stdout("readonly");
}

#[test]
fn test_run_with_volume_invalid_format() {
    // Relative box path is invalid for anonymous volume
    let mut ctx = common::boxlite();
    ctx.cmd
        .args(["run", "--rm", "-v", "data", "alpine:latest", "echo", "ok"]);
    ctx.cmd
        .assert()
        .failure()
        .stderr(predicate::str::contains("absolute"));
}

#[test]
fn test_run_with_volume_anonymous() {
    // -v /data (anonymous volume): CLI creates a host dir and mounts it at /data
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-v",
        "/data",
        "alpine:latest",
        "sh",
        "-c",
        "echo anon > /data/x && cat /data/x",
    ]);
    ctx.cmd.assert().success().stdout("anon\n");
}

// ============================================================================
// Timeout Protection Tests
// ============================================================================

#[test]
fn test_run_basic_command_with_timeout() {
    let mut ctx = common::boxlite();
    ctx.cmd.timeout(std::time::Duration::from_secs(30));
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "echo", "hello boxlite"]);
    ctx.cmd.assert().success().stdout("hello boxlite\n");
}

#[test]
fn test_run_invalid_command_no_hang() {
    let mut ctx = common::boxlite();
    ctx.cmd.timeout(std::time::Duration::from_secs(5));
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "invalidcommand"]);
    ctx.cmd.assert().failure();
}

// ============================================================================
// Python Code Execution Tests
// ============================================================================

#[test]
fn test_run_python_simple_print() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "print('Hello BoxLite')",
    ]);

    ctx.cmd.assert().success().stdout("Hello BoxLite\n");
}

#[test]
fn test_run_python_json_processing() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "import json; data = [{'id': i, 'value': i*2} for i in range(3)]; print(json.dumps(data))",
    ]);

    let output = ctx.cmd.assert().success().get_output().clone();
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(stdout.contains("\"id\""), "JSON should contain 'id' field");
    assert!(
        stdout.contains("\"value\""),
        "JSON should contain 'value' field"
    );
    assert!(stdout.contains("0"), "JSON should contain data");
}

#[test]
fn test_run_python_computation() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "import json; data = [{'id': i, 'value': i*2} for i in range(5)]; \
         total = sum(item['value'] for item in data); \
         print(f'Total: {total}')",
    ]);
    ctx.cmd
        .assert()
        .success()
        .stdout(predicate::str::contains("Total: 20"));
}

#[test]
fn test_run_python_with_env_vars() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "-e",
        "DATA_SIZE=5",
        "-e",
        "MULTIPLIER=3",
        "python:alpine",
        "python",
        "-c",
        "import os; size = int(os.getenv('DATA_SIZE', 0)); \
         mult = int(os.getenv('MULTIPLIER', 1)); \
         result = size * mult; \
         print(f'Result: {result}')",
    ]);

    ctx.cmd
        .assert()
        .success()
        .stdout(predicate::str::contains("Result: 15"));
}

#[test]
fn test_run_python_multi_step_pipeline() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "sh",
        "-c",
        "python -c \"print('Step 1: Generate')\" && \
         python -c \"print('Step 2: Process')\" && \
         python -c \"print('Step 3: Complete')\"",
    ]);
    ctx.cmd
        .assert()
        .success()
        .stdout(predicate::str::contains("Step 1: Generate"))
        .stdout(predicate::str::contains("Step 2: Process"))
        .stdout(predicate::str::contains("Step 3: Complete"));
}

#[test]
fn test_run_python_list_comprehension() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "squares = [x**2 for x in range(5)]; print(squares)",
    ]);
    ctx.cmd
        .assert()
        .success()
        .stdout(predicate::str::contains("[0, 1, 4, 9, 16]"));
}

#[test]
fn test_run_python_error_handling() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "import sys; sys.exit(42)",
    ]);
    ctx.cmd.assert().code(42);
}

#[test]
fn test_run_python_import_stdlib() {
    let mut ctx = common::boxlite();
    ctx.cmd.args([
        "run",
        "--rm",
        "python:alpine",
        "python",
        "-c",
        "import sys, os, json, math; \
         print(f'Python {sys.version_info.major}.{sys.version_info.minor}')",
    ]);

    ctx.cmd
        .assert()
        .success()
        .stdout(predicate::str::contains("Python 3."));
}

// ============================================================================
// Signal Handling Tests
// ============================================================================

#[test]
fn test_run_signal_exit_code_sigterm() {
    let mut ctx = common::boxlite();

    // The command is the container's init (PID 1 of the pid namespace).
    // The kernel drops signals sent to a pid-ns init from inside its own
    // namespace unless a handler is installed — including self-signals —
    // so `kill -TERM $$` is a no-op and the shell exits 0. Verified
    // docker-parity: `podman run --rm alpine sh -c 'kill -TERM $$'` → 0.
    // (The old 143 assertion reflected the pre-init-semantics tenant
    // behavior, where the command was an ordinary process.)
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "kill -TERM $$"]);

    ctx.cmd.assert().code(0);
}

#[test]
fn test_run_signal_exit_code_sigkill() {
    let mut ctx = common::boxlite();

    // Same pid-ns-init signal protection as the SIGTERM case above:
    // self-SIGKILL of PID 1 is dropped by the kernel; podman-verified → 0.
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "kill -KILL $$"]);

    ctx.cmd.assert().code(0);
}

#[test]
fn test_run_signal_exit_code_sigint() {
    let mut ctx = common::boxlite();

    // SIGINT (Ctrl+C) = 2
    ctx.cmd
        .args(["run", "--rm", "alpine:latest", "sh", "-c", "kill -INT $$"]);

    ctx.cmd.assert().code(130);
}

// ============================================================================
// Other Tests
// ============================================================================

#[test]
fn test_run_invalid_image() {
    let mut ctx = common::boxlite_bare();
    ctx.cmd.timeout(std::time::Duration::from_secs(30));
    ctx.cmd.args([
        "run",
        "docker.io/boxlite-test-does-not-exist/nonexistent-image:latest",
        "echo",
        "hi",
    ]);
    ctx.cmd.assert().failure().stderr(
        predicate::str::contains("failed to pull")
            .or(predicate::str::contains("not found"))
            .or(predicate::str::contains("Not authorized"))
            .or(predicate::str::contains("manifest unknown"))
            .or(predicate::str::contains("pull access denied")),
    );
}

#[test]
fn test_run_tty_error_in_pipe() {
    let mut ctx = common::boxlite();
    ctx.cmd.args(["run", "--tty", "alpine:latest"]);
    // Simulate non-TTY input by writing to stdin
    ctx.cmd.write_stdin("ls\n");
    ctx.cmd
        .assert()
        .failure()
        .stderr(predicate::str::contains("input device is not a TTY"));
}