This is a small helper library to build and run Rust applications inside a
Docker container (actually using podman). Its main use is to test applications
that require specific file paths or permissions that would be inappropriate to
perform locally on a development machine (e.g. chmod
to root or enabling
setuid
).
Right now it is somewhat specific to my use-cases, but may be of use to others.
docker
: Uses docker
instead of podman
.```rust use dockertest::{build::buildimagesync, util::buildand_deploy};
fn buildtestimage() -> String { let dir = "tests/custom-container"; let name = "docker-test-self-test-image"; buildimagesync(dir, name).unwrap() }
fn isroot() { let imagename = buildtestimage(); let (container, bin) = buildanddeploy( "testproj", Some("tests/testproj"), None, imagename.asstr(), ) .unwrap(); let out = container.exec(vec![bin.asstr(), "--help"]).unwrap(); let stdout = String::fromutf8(out.stdout).unwrap(); assert!(out.status.success()); assert!(stdout.contains("Hello, docker! My UID is [0]")); }
fn notroot() { let imagename = buildtestimage(); let (container, bin) = buildanddeploy( "testproj", Some("tests/testproj"), None, imagename.asstr(), ) .unwrap(); let out = container .execas("nobody", vec![bin.asstr(), "--help"]) .unwrap(); let stdout = String::from_utf8(out.stdout).unwrap(); assert!(out.status.success()); assert!(stdout.contains("Hello, docker! My UID is [65534]")); }
fn stockimage() { let imagename = "docker.io/rust:1.64.0-slim-bullseye"; let (container, bin) = buildanddeploy( "testproj", Some("tests/testproj"), None, imagename, ) .unwrap(); let out = container .execas("nobody", vec![bin.asstr(), "--help"]) .unwrap(); let stdout = String::fromutf8(out.stdout).unwrap(); assert!(out.status.success()); assert!(stdout.contains("Hello, docker! My UID is [65534]")); } ```