feat: tests for docker-compose

This commit is contained in:
Aravinth Manivannan 2023-09-26 15:38:55 +05:30
parent c2ac9689d8
commit 17510c4ac2
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 46 additions and 8 deletions

View file

@ -4,6 +4,8 @@ use std::process::Command;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JValue; use serde_json::Value as JValue;
use crate::docker::Docker;
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct Container { pub struct Container {
pub service: String, pub service: String,
@ -65,12 +67,7 @@ impl DockerCompose {
} }
pub fn logs(&self, service: &str) -> String { pub fn logs(&self, service: &str) -> String {
let output = Command::new("docker-compose") Docker::get_logs(service)
.current_dir(&self.base_dir)
.args(["logs", service])
.output()
.expect("unable to get logs");
String::from_utf8(output.stdout).unwrap()
} }
pub fn down(&self, remove_orphans: bool, volumes: bool) { pub fn down(&self, remove_orphans: bool, volumes: bool) {
@ -82,10 +79,37 @@ impl DockerCompose {
if volumes { if volumes {
opts.push("--volumes"); opts.push("--volumes");
} }
Command::new("docker-compose") let mut child = Command::new("docker-compose")
.current_dir(&self.base_dir) .current_dir(&self.base_dir)
.args(opts) .args(opts)
.spawn() .spawn()
.expect(&format!("unable to remove")); .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());
} }
} }

14
tests/docker-compose.yml Normal file
View file

@ -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