microsandbox-cli 0.7.8

CLI binary for managing microsandbox environments.
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
//! Restore a snapshot into a detached sandbox with explicit host resource bindings.

use clap::Args;
use microsandbox::{
    MicrosandboxError, RestoreKind,
    sandbox::{
        ForkBuilder, ForkManyBuilder, GuestClockPolicy, RestoreBuilder, Sandbox, SecurityProfile,
    },
};

#[cfg(feature = "net")]
use super::common::parse_port_mapping;
use super::common::{
    display_restore_warnings, guest_clock_parser, parse_explicit_disk_mount, parse_restore_volume,
    parse_vsock_route,
};
use crate::ui::{self, ErrorLine};

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Restore an installed snapshot or archive into a new sandbox.
#[derive(Debug, Args)]
pub struct RestoreArgs {
    /// Snapshot group/member, ID, or archive path.
    pub snapshot: String,
    /// Unique name of the destination sandbox.
    #[arg(short, long)]
    pub name: String,
    /// Restore captured RAM using private copy-on-write mappings.
    #[arg(long, conflicts_with = "disk_only")]
    pub cow_mem: bool,
    /// Deprecated alias for --cow-mem.
    #[arg(long, hide = true, conflicts_with = "disk_only")]
    pub forked: bool,
    /// Cold-boot only the captured disk, without restoring processes or RAM.
    #[arg(long)]
    pub disk_only: bool,
    /// Exact base snapshot or archive for a dependent export.
    #[arg(long)]
    pub snapshot_base: Option<String>,
    /// Allow missing full-restore resources with warnings instead of refusing activation.
    #[arg(long)]
    pub allow_missing_resources: bool,
    /// Destination resource bindings.
    #[command(flatten)]
    pub resources: RestoreResourceArgs,
    /// Destination controls applied before the restored workload can run.
    #[command(flatten)]
    pub controls: RestoreControlArgs,
    /// Suppress progress output, but not unavailable-resource warnings.
    #[arg(short, long)]
    pub quiet: bool,
}

/// Resource choices shared by restore and branch, separate from fresh-boot configuration.
#[derive(Debug, Args)]
pub struct RestoreResourceArgs {
    /// Bind a host socket/named pipe to a guest-to-host vsock port: PATH:PORT[/stream|/dgram].
    #[arg(long)]
    pub vsock: Vec<String>,
    /// Map `SOURCE:GUEST[:OPTIONS]`, or select a captured private disk with GUEST alone.
    #[arg(short, long, value_name = "SOURCE:GUEST|GUEST")]
    pub volume: Vec<String>,
    /// Attach a host disk image (`SOURCE:DEST[:OPTIONS]`), like `create --mount-disk`.
    #[arg(long = "mount-disk", value_name = "SOURCE:DEST[:OPTIONS]")]
    pub mount_disk: Vec<String>,
    /// Publish child listeners: `[BIND:]HOST:GUEST[/tcp|udp]`, with equal-length port ranges.
    #[cfg(feature = "net")]
    #[arg(short, long)]
    pub port: Vec<String>,
    /// Accept-queue depth for the child's published TCP ports (default: 1024; clamped to somaxconn).
    #[cfg(feature = "net")]
    #[arg(long, value_name = "DEPTH", value_parser = clap::value_parser!(u32).range(1..=i64::from(i32::MAX)))]
    pub tcp_accept_queue_size: Option<u32>,
    /// Default user for new exec commands; captured processes keep their credentials.
    #[arg(short, long)]
    pub user: Option<String>,
    /// Validate explicitly mapped filesystems strictly or allow supported stale resources.
    #[arg(long, value_parser = ["strict", "relaxed"])]
    pub external_mount_policy: Option<String>,
    /// Fill unspecified bindings from validated source-local records; may share host resources.
    #[arg(long)]
    pub dangerously_inherit_resources: bool,
}

/// Destination policy and geometry controls, separate from captured host-resource bindings.
#[derive(Debug, Default, Args)]
pub struct RestoreControlArgs {
    /// Guest CPU count for disk boot; full restore requires the captured count.
    #[arg(short = 'c', long)]
    pub cpus: Option<u8>,
    /// Guest memory for disk boot; full restore requires the captured size.
    #[arg(short = 'm', long, value_name = "SIZE")]
    pub memory: Option<String>,
    /// Guest security profile for disk boot; rejected for full execution restore.
    #[arg(long, value_parser = ["default", "restricted"])]
    pub security: Option<String>,
    /// Guest wall-clock policy (`sync` or `off`); defaults to the policy recorded in the snapshot.
    #[arg(long = "guest-clock", value_name = "POLICY", value_parser = guest_clock_parser())]
    pub guest_clock: Option<GuestClockPolicy>,
    /// Maximum lifetime of the destination sandbox (e.g. 30s, 5m, 1h).
    #[arg(long, value_name = "DURATION")]
    pub max_duration: Option<String>,
    /// Stop after this much inactivity in the destination sandbox.
    #[arg(long, value_name = "DURATION")]
    pub idle_timeout: Option<String>,
    /// Deny traffic by default; combine with --net-rule to allow selected destinations.
    #[cfg(feature = "net")]
    #[arg(long, conflicts_with = "net_default")]
    pub no_net: bool,
    /// Default action for both traffic directions in the destination host policy.
    #[cfg(feature = "net")]
    #[arg(long, value_parser = ["allow", "deny"])]
    pub net_default: Option<String>,
    /// Destination host policy rules, using the same syntax as create/run (repeatable).
    /// Requires --net-default or --no-net so the complete replacement policy is explicit.
    #[cfg(feature = "net")]
    #[arg(long, value_name = "TOKENS")]
    pub net_rule: Vec<String>,
    /// Deprecated alias for --max-tcp-connections.
    #[cfg(feature = "net")]
    #[arg(long, conflicts_with = "max_tcp_connections")]
    pub max_connections: Option<usize>,
    /// Concurrent TCP limit; zero explicitly selects unlimited.
    #[cfg(feature = "net")]
    #[arg(long)]
    pub max_tcp_connections: Option<usize>,
    /// Concurrent UDP session limit; zero explicitly selects unlimited.
    #[cfg(feature = "net")]
    #[arg(long)]
    pub max_udp_connections: Option<usize>,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl RestoreControlArgs {
    /// Parse controls before any asynchronous source resolution or destination mutation.
    fn apply(&self, mut builder: RestoreBuilder) -> anyhow::Result<RestoreBuilder> {
        if let Some(cpus) = self.cpus {
            builder = builder.cpus(cpus);
        }
        if let Some(memory) = &self.memory {
            builder = builder.memory(ui::parse_size_mib(memory).map_err(anyhow::Error::msg)?);
        }
        if let Some(security) = &self.security {
            let profile = match security.as_str() {
                "default" => SecurityProfile::Default,
                "restricted" => SecurityProfile::Restricted,
                other => anyhow::bail!("invalid security profile {other:?}"),
            };
            builder = builder.security(profile);
        }
        if let Some(policy) = self.guest_clock {
            builder = builder.guest_clock(policy);
        }
        if let Some(duration) = &self.max_duration {
            builder = builder.max_duration(super::common::parse_duration_secs(duration)?);
        }
        if let Some(duration) = &self.idle_timeout {
            builder = builder.idle_timeout(super::common::parse_duration_secs(duration)?);
        }
        #[cfg(feature = "net")]
        {
            // Reuse the established CLI policy grammar and defaults. Do not reinterpret
            // --no-net as device removal: it is deny-both policy and supports an allowlist.
            if let Some(policy) = self.network_policy()? {
                builder = builder.network_policy(policy);
            }
            if let Some(limit) = self.max_tcp_connections.or(self.max_connections) {
                builder = builder.max_tcp_connections(limit);
            }
            if let Some(limit) = self.max_udp_connections {
                builder = builder.max_udp_connections(limit);
            }
        }
        Ok(builder)
    }

    #[cfg(feature = "net")]
    fn network_policy(
        &self,
    ) -> anyhow::Result<Option<microsandbox_network::policy::NetworkPolicy>> {
        if !self.net_rule.is_empty() && !self.no_net && self.net_default.is_none() {
            anyhow::bail!(
                "restore --net-rule requires --net-default or --no-net to select an explicit destination policy"
            );
        }
        super::common::SandboxOpts {
            no_net: self.no_net,
            net_default: self.net_default.clone(),
            net_rule: self.net_rule.clone(),
            ..Default::default()
        }
        .build_network_policy()
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Restore captured state; use exec separately to start a new command.
pub async fn run(
    args: RestoreArgs,
    log_level: Option<microsandbox::LogLevel>,
) -> anyhow::Result<()> {
    let mut builder = Sandbox::restore(&args.snapshot).name(&args.name);
    if let Some(level) = log_level {
        builder = builder.log_level(level);
    }
    if args.forked {
        ui::warn("--forked is deprecated; use --cow-mem instead");
    }
    if args.cow_mem || args.forked {
        builder = builder.cow_memory();
    }
    if args.disk_only {
        builder = builder.disk_only();
    }
    if args.allow_missing_resources {
        builder = builder.allow_missing_resources();
    }
    if let Some(base) = args.snapshot_base {
        builder = builder.snapshot_base(base);
    }
    builder = args.resources.apply_restore(builder)?;
    builder = args.controls.apply(builder)?;
    let (mut progress, task) = builder.restore_with_progress()?;
    let mut display = if args.quiet {
        ui::PullProgressDisplay::quiet(&args.snapshot)
    } else {
        ui::PullProgressDisplay::new(&args.snapshot)
    };
    while let Some(event) = progress.recv().await {
        display.handle_creation_event(event);
    }
    let result = task.await;
    display.finish();
    let sandbox = match result.map_err(|error| anyhow::anyhow!("restore task failed: {error}"))? {
        Ok(sandbox) => sandbox,
        Err(error) => {
            if let Some(hints) = missing_bindings_hints(&error) {
                let lines: Vec<_> = hints.into_iter().map(ErrorLine::Hint).collect();
                ui::error_with_lines(&error.to_string(), &lines);
                return Err(ui::AlreadyRenderedError.into());
            }

            return Err(error.into());
        }
    };
    display_restore_warnings(&sandbox).await;
    sandbox.detach().await;
    Ok(())
}

/// CLI guidance for a restore refused because guest paths lack destination bindings.
fn missing_bindings_hints(error: &MicrosandboxError) -> Option<Vec<&'static str>> {
    let MicrosandboxError::MissingRestoreBindings { restore, .. } = error else {
        return None;
    };

    let mut hints = vec!["map each path with -v SOURCE:GUEST or --mount-disk SOURCE:GUEST"];
    // Only a full restore can select disks it captured; a disk restore never offers that.
    if *restore == RestoreKind::Full {
        hints.push("select a captured disk with -v GUEST");
    }
    hints.push("or pass --allow-missing-resources to start without them");

    Some(hints)
}

//--------------------------------------------------------------------------------------------------
// Macros
//--------------------------------------------------------------------------------------------------

macro_rules! apply_resources {
    ($method:ident, $builder:ty) => {
        impl RestoreResourceArgs {
            pub(crate) fn $method(&self, mut builder: $builder) -> anyhow::Result<$builder> {
                if let Some(user) = &self.user {
                    builder = builder.user(user);
                }
                if self.dangerously_inherit_resources {
                    builder = builder.dangerously_inherit_resources();
                }
                if let Some(policy) = &self.external_mount_policy {
                    builder = builder.external_mount_policy(match policy.as_str() {
                        "relaxed" => microsandbox::ExternalMountRestorePolicy::Relaxed,
                        _ => microsandbox::ExternalMountRestorePolicy::Strict,
                    });
                }
                for volume in &self.volume {
                    let (guest, mount) = parse_restore_volume(volume)?;
                    builder = builder.volume(guest, |_| mount);
                }
                for spec in &self.mount_disk {
                    let (guest, mount) = parse_explicit_disk_mount(spec)?;
                    builder = builder.volume(guest, |_| mount);
                }
                for route in &self.vsock {
                    let (host, port, kind) = parse_vsock_route(route)?;
                    builder = match kind {
                        microsandbox::sandbox::VsockSocketType::Stream => builder.vsock(host, port),
                        microsandbox::sandbox::VsockSocketType::Dgram => {
                            builder.vsock_dgram(host, port)
                        }
                    };
                }
                #[cfg(feature = "net")]
                for port in &self.port {
                    for port in parse_port_mapping(port)? {
                        builder = match port.protocol {
                            microsandbox_network::config::PortProtocol::Udp => builder
                                .port_udp_bind(port.host_bind, port.host_port, port.guest_port),
                            microsandbox_network::config::PortProtocol::Tcp => {
                                builder.port_bind(port.host_bind, port.host_port, port.guest_port)
                            }
                        };
                    }
                }
                #[cfg(feature = "net")]
                if let Some(size) = self.tcp_accept_queue_size {
                    builder = builder.tcp_accept_queue_size(size);
                }
                Ok(builder)
            }
        }
    };
}

apply_resources!(apply_restore, RestoreBuilder);
apply_resources!(apply_branch, ForkBuilder);
apply_resources!(apply_branch_many, ForkManyBuilder);

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use clap::Parser;

    use super::*;

    #[derive(Debug, Parser)]
    struct TestCli {
        #[command(flatten)]
        args: RestoreArgs,
    }

    #[test]
    fn missing_bindings_refusal_gets_flag_hints() {
        let disk_refusal = MicrosandboxError::MissingRestoreBindings {
            missing: vec!["mount /data".into()],
            restore: RestoreKind::Disk,
        };
        let full_refusal = MicrosandboxError::MissingRestoreBindings {
            missing: vec!["disk /data".into(), "filesystem /work".into()],
            restore: RestoreKind::Full,
        };
        let other = MicrosandboxError::InvalidConfig("invalid volume".into());

        let disk_hints = missing_bindings_hints(&disk_refusal).unwrap().join("\n");
        let full_hints = missing_bindings_hints(&full_refusal).unwrap().join("\n");

        assert!(disk_hints.contains("-v SOURCE:GUEST"));
        assert!(disk_hints.contains("--mount-disk SOURCE:GUEST"));
        assert!(disk_hints.contains("--allow-missing-resources"));
        assert!(!disk_hints.contains("-v GUEST"));
        assert!(full_hints.contains("-v SOURCE:GUEST"));
        assert!(full_hints.contains("-v GUEST"));
        assert!(full_hints.contains("--allow-missing-resources"));
        assert!(missing_bindings_hints(&other).is_none());
    }

    #[test]
    fn missing_bindings_hint_ignores_guest_paths() {
        let disk_refusal = MicrosandboxError::MissingRestoreBindings {
            missing: vec!["mount /captured disk".into()],
            restore: RestoreKind::Disk,
        };

        let hints = missing_bindings_hints(&disk_refusal).unwrap().join("\n");

        assert!(hints.contains("-v SOURCE:GUEST"));
        assert!(!hints.contains("-v GUEST"));
    }

    #[test]
    fn missing_resource_opt_out_is_independent_of_mapping_policy_and_inheritance() {
        let defaults = TestCli::try_parse_from(["restore", "saved", "--name", "child"]).unwrap();
        assert!(!defaults.args.allow_missing_resources);
        let explicit = TestCli::try_parse_from([
            "restore",
            "saved",
            "--name",
            "child",
            "--allow-missing-resources",
            "--external-mount-policy",
            "strict",
            "--dangerously-inherit-resources",
        ])
        .unwrap();
        assert!(explicit.args.allow_missing_resources);
        assert!(explicit.args.resources.dangerously_inherit_resources);
        assert_eq!(
            explicit.args.resources.external_mount_policy.as_deref(),
            Some("strict")
        );
    }

    #[test]
    fn restore_attaches_mount_disk_like_create() {
        let spec = "/images/seed.img:/data2:ro,fstype=ext4";
        let cli =
            TestCli::try_parse_from(["restore", "group:snap", "--name", "x", "--mount-disk", spec])
                .unwrap();
        assert_eq!(cli.args.resources.mount_disk, [spec]);
        let (guest, mount) = parse_explicit_disk_mount(&cli.args.resources.mount_disk[0]).unwrap();
        assert_eq!(guest, "/data2");
        match mount.build().unwrap() {
            microsandbox::sandbox::VolumeMount::DiskImage {
                host,
                guest,
                fstype,
                options,
                ..
            } => {
                assert_eq!(host, std::path::Path::new("/images/seed.img"));
                assert_eq!(guest, "/data2");
                assert_eq!(fstype.as_deref(), Some("ext4"));
                assert!(options.readonly);
            }
            other => panic!("expected DiskImage, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mount_disk_reaches_run_builder_validation() {
        let directory = tempfile::tempdir().unwrap();
        let missing = directory.path().join("missing");
        let cli = TestCli::try_parse_from([
            "restore",
            missing.to_str().unwrap(),
            "--name",
            "restore-disk-binding",
            "--mount-disk",
            "/images/seed.img:/:ro,fstype=ext4",
        ])
        .unwrap();
        // A root guest path is rejected by mount validation before the snapshot is
        // opened, so this fails only if `run` hands the disk to the restore builder.
        let error = run(cli.args, None).await.unwrap_err();
        assert!(
            error
                .to_string()
                .contains("cannot mount a volume at guest root /"),
            "{error:#}"
        );
    }

    #[test]
    fn mount_disk_specs_are_applied_to_the_restore_builder() {
        let args = |spec: &str| {
            TestCli::try_parse_from(["restore", "group:snap", "--name", "x", "--mount-disk", spec])
                .unwrap()
                .args
                .resources
        };
        let good = args("/images/seed.img:/data2:ro,fstype=ext4");
        assert!(good.apply_restore(Sandbox::restore("group:snap")).is_ok());
        let bad = args("/images/seed.img");
        assert!(bad.apply_restore(Sandbox::restore("group:snap")).is_err());
    }

    #[test]
    fn restore_parses_destination_controls_without_creation_aliases() {
        let cli = TestCli::try_parse_from([
            "restore",
            "toolchain:ready",
            "--name",
            "worker",
            "--cpus",
            "2",
            "--memory",
            "2G",
            "--security",
            "restricted",
            "--guest-clock",
            "off",
            "--max-duration",
            "10m",
            "--idle-timeout",
            "2m",
        ])
        .unwrap();
        assert_eq!(cli.args.controls.cpus, Some(2));
        assert_eq!(cli.args.controls.memory.as_deref(), Some("2G"));
        assert_eq!(cli.args.controls.security.as_deref(), Some("restricted"));
        assert_eq!(cli.args.controls.guest_clock, Some(GuestClockPolicy::Off));
        assert!(
            cli.args
                .controls
                .apply(Sandbox::restore("toolchain:ready"))
                .is_ok()
        );
        for flag in ["--replace", "--from-snapshot", "--entrypoint"] {
            assert!(
                TestCli::try_parse_from(["restore", "ready", "--name", "worker", flag]).is_err()
            );
        }
    }

    #[test]
    fn invalid_destination_controls_fail_before_source_resolution() {
        assert!(
            TestCli::try_parse_from([
                "restore",
                "source-not-opened",
                "--name",
                "worker",
                "--guest-clock",
                "host",
            ])
            .is_err()
        );

        for controls in [
            RestoreControlArgs {
                memory: Some("not-a-size".into()),
                ..Default::default()
            },
            RestoreControlArgs {
                max_duration: Some("not-a-duration".into()),
                ..Default::default()
            },
            RestoreControlArgs {
                security: Some("unknown".into()),
                ..Default::default()
            },
        ] {
            assert!(
                controls
                    .apply(Sandbox::restore("source-not-opened"))
                    .is_err()
            );
        }
    }

    #[cfg(feature = "net")]
    #[test]
    fn restore_parses_independent_tcp_udp_limits_and_rejects_duplicate_tcp_aliases() {
        for tcp_flag in ["--max-connections", "--max-tcp-connections"] {
            let cli = TestCli::try_parse_from([
                "restore",
                "ready",
                "--name",
                "child",
                tcp_flag,
                "0",
                "--max-udp-connections",
                "7",
            ])
            .unwrap();
            let controls = &cli.args.controls;
            assert_eq!(
                controls.max_tcp_connections.or(controls.max_connections),
                Some(0)
            );
            assert_eq!(controls.max_udp_connections, Some(7));
            assert!(controls.apply(Sandbox::restore("ready")).is_ok());
        }
        let defaults = TestCli::try_parse_from(["restore", "ready", "--name", "child"]).unwrap();
        assert_eq!(defaults.args.controls.max_tcp_connections, None);
        assert_eq!(defaults.args.controls.max_udp_connections, None);
        assert!(
            TestCli::try_parse_from([
                "restore",
                "ready",
                "--name",
                "child",
                "--max-connections",
                "1",
                "--max-tcp-connections",
                "2",
            ])
            .is_err()
        );
    }

    #[cfg(feature = "net")]
    #[test]
    fn restore_parses_accept_queue_size_for_child_listeners() {
        let cli = TestCli::try_parse_from([
            "restore",
            "ready",
            "--name",
            "child",
            "-p",
            "8080:80",
            "--tcp-accept-queue-size",
            "4096",
        ])
        .unwrap();
        assert_eq!(cli.args.resources.tcp_accept_queue_size, Some(4096));
        let defaults = TestCli::try_parse_from(["restore", "ready", "--name", "child"]).unwrap();
        assert_eq!(defaults.args.resources.tcp_accept_queue_size, None);
        for invalid in ["0", "2147483648"] {
            assert!(
                TestCli::try_parse_from([
                    "restore",
                    "ready",
                    "--name",
                    "child",
                    "--tcp-accept-queue-size",
                    invalid,
                ])
                .is_err(),
                "{invalid}"
            );
        }
    }

    #[cfg(feature = "net")]
    #[test]
    fn restore_policy_requires_explicit_defaults_and_matches_create_grammar() {
        use microsandbox_network::policy::Action;

        let offline = TestCli::try_parse_from([
            "restore",
            "ready",
            "--name",
            "worker",
            "--no-net",
            "--max-connections",
            "0",
        ])
        .unwrap();
        assert_eq!(offline.args.controls.max_connections, Some(0));
        let offline_policy = offline.args.controls.network_policy().unwrap().unwrap();
        assert_eq!(offline_policy.default_egress, Action::Deny);
        assert_eq!(offline_policy.default_ingress, Action::Deny);
        assert!(offline_policy.rules.is_empty());
        assert!(
            offline
                .args
                .controls
                .apply(Sandbox::restore("ready"))
                .is_ok()
        );

        let omitted = RestoreControlArgs {
            max_connections: Some(8),
            ..Default::default()
        };
        assert!(omitted.network_policy().unwrap().is_none());
        let rule_only = RestoreControlArgs {
            net_rule: vec!["allow@books.toscrape.com:tcp:443".into()],
            ..Default::default()
        };
        assert!(
            rule_only
                .network_policy()
                .unwrap_err()
                .to_string()
                .contains("--net-default")
        );
        let allowlist = RestoreControlArgs {
            no_net: true,
            ..rule_only
        };
        let policy = allowlist.network_policy().unwrap().unwrap();
        assert_eq!(policy.default_egress, Action::Deny);
        assert_eq!(policy.default_ingress, Action::Deny);
        assert_eq!(policy.rules.len(), 1);
        let create_policy = super::super::common::SandboxOpts {
            no_net: true,
            net_rule: allowlist.net_rule,
            ..Default::default()
        }
        .build_network_policy()
        .unwrap()
        .unwrap();
        assert_eq!(
            serde_json::to_value(policy).unwrap(),
            serde_json::to_value(create_policy).unwrap()
        );
        assert!(
            TestCli::try_parse_from([
                "restore",
                "ready",
                "--name",
                "worker",
                "--no-net",
                "--net-default",
                "allow"
            ])
            .is_err()
        );
    }
}