From 17510c4ac2b263a8ce0bb313f77f2bb49ceeb82c Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 26 Sep 2023 15:38:55 +0530 Subject: [PATCH] feat: tests for docker-compose --- src/docker_compose.rs | 40 ++++++++++++++++++++++++++++++++-------- tests/docker-compose.yml | 14 ++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/docker-compose.yml diff --git a/src/docker_compose.rs b/src/docker_compose.rs index a1e1671..df04d94 100644 --- a/src/docker_compose.rs +++ b/src/docker_compose.rs @@ -4,6 +4,8 @@ use std::process::Command; use serde::{Deserialize, Serialize}; use serde_json::Value as JValue; +use crate::docker::Docker; + #[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] pub struct Container { pub service: String, @@ -65,13 +67,8 @@ impl DockerCompose { } pub fn logs(&self, service: &str) -> String { - let output = Command::new("docker-compose") - .current_dir(&self.base_dir) - .args(["logs", service]) - .output() - .expect("unable to get logs"); - String::from_utf8(output.stdout).unwrap() - } + Docker::get_logs(service) + } pub fn down(&self, remove_orphans: bool, volumes: bool) { let mut opts = vec!["down"]; @@ -82,10 +79,37 @@ impl DockerCompose { if volumes { opts.push("--volumes"); } - Command::new("docker-compose") + let mut child = Command::new("docker-compose") .current_dir(&self.base_dir) .args(opts) .spawn() .expect(&format!("unable to remove")); + child.wait().unwrap(); + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_docker_compose() { + DockerCompose::version(); + let cmp = DockerCompose::new("./tests/".into()); + assert!(cmp.services().is_empty()); + cmp.up(); + let services = cmp.services(); + std::thread::sleep(std::time::Duration::new(5, 0)); + assert_eq!(services.len(), 2); + for service in services.iter() { + println!("{}", service.name); + let logs = cmp.logs(&service.name); + println!("service: {} {:?}", service.name, logs); + println!("expecting: NAME={}", service.name); + assert!(logs.contains(&format!("NAME={}", service.name))); + } + cmp.down(true, true); + assert!(cmp.services().is_empty()); } } diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..b35c8cd --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,14 @@ +services: + foo: + image: "forgeflux/ftest-dev-docker-cmd" + container_name: foo + environment: + NAME: foo + + + + bar: + image: "forgeflux/ftest-dev-docker-cmd" + container_name: bar + environment: + NAME: bar