crates/run/container/tests/sweep.rs 2.5 KiB 76 lines Raw
1 //! A sweep belongs to one control, not to every control on the daemon.2 3 use std::process::{Command, Stdio};4 use std::time::{Duration, Instant};5 6 use inf3r_container::{ContainerSpec, sweep};7 8 fn docker_image() -> Option<String> {9     for image in ["inf3r:local", "inf3r/mock/x64-cpu:local", "inf3r:compose"] {10         let ok = Command::new("docker")11             .args(["image", "inspect", image])12             .stdout(Stdio::null())13             .stderr(Stdio::null())14             .status()15             .is_ok_and(|status| status.success());16         if ok {17             return Some(image.to_string());18         }19     }20     None21 }22 23 fn running(name: &str) -> bool {24     Command::new("docker")25         .args(["inspect", "-f", "{{.State.Running}}", name])26         .output()27         .ok()28         .filter(|output| output.status.success())29         .map(|output| String::from_utf8_lossy(&output.stdout).trim() == "true")30         .unwrap_or(false)31 }32 33 fn wait_running(name: &str) -> bool {34     let deadline = Instant::now() + Duration::from_secs(10);35     while Instant::now() < deadline {36         if running(name) {37             return true;38         }39         std::thread::sleep(Duration::from_millis(50));40     }41     false42 }43 44 #[test]45 fn a_sweep_removes_only_its_own_control() {46     let Some(image) = docker_image() else {47         eprintln!("container sweep test skipped: no local proof image");48         return;49     };50     let left = format!("inf3r-sweep-a-{}", std::process::id());51     let right = format!("inf3r-sweep-b-{}", std::process::id());52     let mut a = ContainerSpec::new(image.clone(), "sweep").with_control("scope-a");53     a.command = vec!["sleep".to_string(), "30".to_string()];54     let mut b = ContainerSpec::new(image, "sweep").with_control("scope-b");55     b.command = vec!["sleep".to_string(), "30".to_string()];56     let mut child_a = a.spawn(&left, &[]).expect("start scope a");57     let mut child_b = b.spawn(&right, &[]).expect("start scope b");58     assert!(wait_running(&left), "scope a did not start");59     assert!(wait_running(&right), "scope b did not start");60 61     sweep("scope-a");62     let deadline = Instant::now() + Duration::from_secs(10);63     while running(&left) && Instant::now() < deadline {64         std::thread::sleep(Duration::from_millis(50));65     }66     assert!(!running(&left), "the swept control survived");67     assert!(running(&right), "the sibling control was swept");68 69     let _ = child_a.wait();70     let _ = Command::new("docker")71         .args(["rm", "-f", &right])72         .stdout(Stdio::null())73         .stderr(Stdio::null())74         .status();75     let _ = child_b.wait();76 }