use std::collections::HashMap;
use dockertest::{waitfor::WaitFor, Composition, Image, RunningContainer, Source};
pub trait Config: Clone + Send + Sync {
fn into_composition(self) -> Composition;
fn handle(&self) -> &str;
}
pub trait Server {
type Config: Config + 'static;
fn new(config: &Self::Config, container: &RunningContainer) -> Self;
}
pub struct ContainerConfig {
pub args: Vec<String>,
pub env: HashMap<String, String>,
pub handle: String,
pub name: String,
pub source: Source,
pub version: String,
pub ports: Option<Vec<(u32, u32)>>,
pub wait: Option<Box<dyn WaitFor>>,
pub bind_mounts: HashMap<String, String>,
}
#[allow(clippy::from_over_into)] impl Into<Composition> for ContainerConfig {
fn into(self) -> Composition {
let image = Image::with_repository(self.name)
.source(self.source)
.tag(self.version);
let mut comp = Composition::with_image(image);
if let Some(p) = self.ports {
for pair in p {
comp.port_map(pair.0, pair.1);
}
};
let mut composition = match self.wait {
Some(w) => comp
.with_cmd(self.args)
.with_env(self.env)
.with_wait_for(w)
.with_container_name(self.handle),
None => comp
.with_cmd(self.args)
.with_env(self.env)
.with_container_name(self.handle),
};
for (container_path, host_path) in &self.bind_mounts {
composition.bind_mount(host_path, container_path);
}
composition
}
}
pub fn new_handle(name: &str) -> String {
format!("{}{}", name, crate::common::rand_string(10))
}
#[cfg(test)]
mod tests {
#[test]
fn test_new_handle() {
let result = super::new_handle("test");
assert_eq!(result.len(), 14);
}
}