#applications #podman #docker-build #helper #file-path #testing #run

docker-test

A small helper library to build and run Rust applications in Docker (podman)

7 unstable releases (3 breaking)

0.5.0 May 17, 2023
0.4.0 Apr 30, 2023
0.2.0 Oct 29, 2022
0.1.3 Jan 18, 2022

#535 in Unix APIs

38 downloads per month

MPL-2.0 license

11KB
193 lines

Docker Test Helper

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.

Feature flags

  • docker: Uses docker instead of podman.

Example

use docker_test::{build::build_image_sync, util::build_and_deploy};

fn build_test_image() -> String {
    let dir = "tests/custom-container";
    let name = "docker-test-self-test-image";
    build_image_sync(dir, name).unwrap()
}

#[test]
fn is_root() {
    let image_name = build_test_image();
    let (container, bin) = build_and_deploy(
        "testproj",
        Some("tests/testproj"),
        None,
        image_name.as_str(),
    )
    .unwrap();
    let out = container.exec(vec![bin.as_str(), "--help"]).unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(out.status.success());
    assert!(stdout.contains("Hello, docker! My UID is [0]"));
}

#[test]
fn not_root() {
    let image_name = build_test_image();
    let (container, bin) = build_and_deploy(
        "testproj",
        Some("tests/testproj"),
        None,
        image_name.as_str(),
    )
    .unwrap();
    let out = container
        .exec_as("nobody", vec![bin.as_str(), "--help"])
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(out.status.success());
    assert!(stdout.contains("Hello, docker! My UID is [65534]"));
}

#[test]
fn stock_image() {
    let image_name = "docker.io/rust:1.64.0-slim-bullseye";
    let (container, bin) = build_and_deploy(
        "testproj",
        Some("tests/testproj"),
        None,
        image_name,
    )
    .unwrap();
    let out = container
        .exec_as("nobody", vec![bin.as_str(), "--help"])
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(out.status.success());
    assert!(stdout.contains("Hello, docker! My UID is [65534]"));
}

Dependencies

~2.1–7MB
~41K SLoC