From c2ac9689d8a4e2d34bff0953e3178c119a5c9cc6 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 20 Sep 2023 21:54:23 +0530 Subject: [PATCH] feat: tests for docker cmd and env vars --- src/docker.rs | 48 ++++++++++++++++++++++++++++++--- tests/docker-cmd/Dockerfile | 10 +++++++ tests/docker-cmd/Makefile | 4 +++ tests/docker-cmd/scripts/run.sh | 8 ++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/docker-cmd/Dockerfile create mode 100644 tests/docker-cmd/Makefile create mode 100755 tests/docker-cmd/scripts/run.sh diff --git a/src/docker.rs b/src/docker.rs index 8d7049c..40cd134 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::process::Command; pub struct Docker; @@ -13,11 +14,22 @@ impl Docker { x.get(1).unwrap().trim().to_string() } - pub fn run_container(name: &str, img: &str) { - Command::new("docker") - .args(["run", "--name", name, img]) + pub fn run_container(name: &str, img: &str, env: &HashMap) { + let mut env_args = Vec::with_capacity(env.len() * 2 + 6); + env_args.push("run".to_string()); + env_args.push("-d".to_string()); + env_args.push("--name".to_string()); + env_args.push(name.to_string()); + for (k, v) in env.iter() { + env_args.push("-e".to_string()); + env_args.push(format!("{k}={v}")); + } + env_args.push(img.to_string()); + let mut child = Command::new("docker") + .args(&env_args) .spawn() .expect("unable to obtain Docker version"); + child.wait().unwrap(); } pub fn get_logs(name: &str) -> String { @@ -40,3 +52,33 @@ impl Docker { .expect(&format!("unable to remove docker container {name}")); } } + +#[cfg(test)] +mod tests { + use crate::utils::get_random; + + use super::*; + + #[test] + fn test_docker() { + Docker::version(); + let name = format!("test_sleep__{}", get_random(4)); + let mut env = HashMap::new(); + env.insert("FOO".to_string(), "BAR".to_string()); + env.insert("BAZ".to_string(), "BOO".to_string()); + Docker::run_container(&name, "forgeflux/ftest-dev-docker-cmd", &env); + let out = Command::new("docker") + .args(["container", "inspect", "-f", "'{{.State.Running}}'", &name]) + .output() + .unwrap(); + let out = String::from_utf8(out.stdout).unwrap(); + assert!(out.contains("true")); + std::thread::sleep(std::time::Duration::new(10, 0)); + let logs = Docker::get_logs(&name); + println!("{logs}"); + assert!(logs.contains("running")); + assert!(logs.contains("FOO=BAR")); + assert!(logs.contains("BAZ=BOO")); + Docker::rm_container(&name, true); + } +} diff --git a/tests/docker-cmd/Dockerfile b/tests/docker-cmd/Dockerfile new file mode 100644 index 0000000..772bac3 --- /dev/null +++ b/tests/docker-cmd/Dockerfile @@ -0,0 +1,10 @@ +FROM debian:latest + +LABEL org.opencontainers.image.source https://git.batsense.net/ForgeFlux/ftest + +RUN mkdir /sr/ +WORKDIR /src/ +COPY . . +RUN find . +RUN ls . +CMD ["/src/scripts/run.sh"] diff --git a/tests/docker-cmd/Makefile b/tests/docker-cmd/Makefile new file mode 100644 index 0000000..7b560c0 --- /dev/null +++ b/tests/docker-cmd/Makefile @@ -0,0 +1,4 @@ +default: + docker buildx build -t forgeflux/ftest-dev-docker-cmd --load . +run: + docker run forgeflux/ftest-dev-test-docker-cmd diff --git a/tests/docker-cmd/scripts/run.sh b/tests/docker-cmd/scripts/run.sh new file mode 100755 index 0000000..710ea1c --- /dev/null +++ b/tests/docker-cmd/scripts/run.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "running" +echo "env:" +printenv $@ +sleep 10 +echo "exiting" +exit 0