|
| 1 | +use rustwide::cmd::SandboxBuilder; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn test_container_cleanup_on_success() { |
| 5 | + super::runner::run("hello-world", |run| { |
| 6 | + let container_id = run.run(SandboxBuilder::new().enable_networking(false), |build| { |
| 7 | + // Verify we are running inside a Docker container |
| 8 | + let dockerenv = build.cmd("test").args(&["-f", "/.dockerenv"]).run_capture(); |
| 9 | + assert!( |
| 10 | + dockerenv.is_ok(), |
| 11 | + "expected to run inside a Docker container" |
| 12 | + ); |
| 13 | + |
| 14 | + let output = build.cmd("cat").args(&["/etc/hostname"]).run_capture()?; |
| 15 | + Ok(output.stdout_lines()[0].trim().to_string()) |
| 16 | + })?; |
| 17 | + |
| 18 | + assert!( |
| 19 | + !container_id.is_empty(), |
| 20 | + "should have captured container ID" |
| 21 | + ); |
| 22 | + assert_container_stopped_and_removed(&container_id); |
| 23 | + Ok(()) |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn test_container_cleanup_on_command_failure() { |
| 29 | + super::runner::run("hello-world", |run| { |
| 30 | + let container_id = run.run(SandboxBuilder::new().enable_networking(false), |build| { |
| 31 | + // Verify we are running inside a Docker container |
| 32 | + let dockerenv = build.cmd("test").args(&["-f", "/.dockerenv"]).run_capture(); |
| 33 | + assert!( |
| 34 | + dockerenv.is_ok(), |
| 35 | + "expected to run inside a Docker container" |
| 36 | + ); |
| 37 | + |
| 38 | + let mut container_id = String::new(); |
| 39 | + let _err = build |
| 40 | + .cmd("sh") |
| 41 | + .args(&["-c", "cat /etc/hostname; exit 1"]) |
| 42 | + .process_lines(&mut |line, _| { |
| 43 | + if container_id.is_empty() { |
| 44 | + container_id = line.trim().to_string(); |
| 45 | + } |
| 46 | + }) |
| 47 | + .run(); |
| 48 | + Ok(container_id) |
| 49 | + })?; |
| 50 | + |
| 51 | + assert!( |
| 52 | + !container_id.is_empty(), |
| 53 | + "should have captured container ID" |
| 54 | + ); |
| 55 | + assert_container_stopped_and_removed(&container_id); |
| 56 | + Ok(()) |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +fn assert_container_stopped_and_removed(container_id: &str) { |
| 61 | + // Verify the container is not running |
| 62 | + let output = std::process::Command::new("docker") |
| 63 | + .args(["ps", "-q", "--filter", &format!("id={}", container_id)]) |
| 64 | + .output() |
| 65 | + .expect("failed to run docker ps"); |
| 66 | + let remaining = String::from_utf8_lossy(&output.stdout); |
| 67 | + assert!( |
| 68 | + remaining.trim().is_empty(), |
| 69 | + "container {} should not be running", |
| 70 | + container_id |
| 71 | + ); |
| 72 | + |
| 73 | + // Verify the container has been removed entirely |
| 74 | + let output = std::process::Command::new("docker") |
| 75 | + .args([ |
| 76 | + "ps", |
| 77 | + "-a", |
| 78 | + "-q", |
| 79 | + "--filter", |
| 80 | + &format!("id={}", container_id), |
| 81 | + ]) |
| 82 | + .output() |
| 83 | + .expect("failed to run docker ps -a"); |
| 84 | + let remaining = String::from_utf8_lossy(&output.stdout); |
| 85 | + assert!( |
| 86 | + remaining.trim().is_empty(), |
| 87 | + "container {} should have been removed", |
| 88 | + container_id |
| 89 | + ); |
| 90 | +} |
0 commit comments