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_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());
}
}

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