feat: tests for docker-compose
This commit is contained in:
parent
c2ac9689d8
commit
17510c4ac2
2 changed files with 46 additions and 8 deletions
|
@ -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,12 +67,7 @@ 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) {
|
||||
|
@ -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
14
tests/docker-compose.yml
Normal 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
|
Loading…
Reference in a new issue