use getset::{CopyGetters, Getters};
use testcontainers::core::{Mount, WaitFor};
use typed_builder::TypedBuilder;
use crate::environment::Variable;
#[derive(Clone, Debug, Default, CopyGetters, Getters, TypedBuilder)]
pub struct Service {
#[builder(setter(into))]
#[getset(get = "pub")]
image: String,
#[builder(setter(into))]
#[getset(get = "pub")]
tag: String,
#[builder(via_mutators(init = Vec::new()), mutators(
pub fn env(mut self, name: impl Into<String>, value: impl Into<String>) {
self.envs.push(Variable::new(name, value));
}
))]
#[getset(get = "pub")]
envs: Vec<Variable>,
#[builder(via_mutators(init = Vec::new()), mutators(
/// Adds a filesystem mount to the service's container
pub fn mount(mut self, mount: Mount) {
self.mounts.push(mount);
}
))]
#[getset(get = "pub")]
mounts: Vec<Mount>,
#[builder(via_mutators(init = Vec::new()), mutators(
/// Adds an argument to the container's command
pub fn cmd_arg(mut self, arg: impl Into<String>) {
self.cmd.push(arg.into());
}
))]
#[getset(get = "pub")]
cmd: Vec<String>,
#[builder(default, setter(into))]
#[getset(get = "pub")]
wait: Option<WaitFor>,
}
#[cfg(test)]
mod tests {
use crate::test_utils::*;
use super::*;
#[test]
fn cmd_arg_collects_args() {
let service = Service::builder()
.image("postgres")
.tag("latest")
.cmd_arg("--config")
.cmd_arg("/etc/postgres.conf")
.build();
assert_eq!(2, service.cmd.len());
}
#[test]
fn env_collects_variables() {
let service = Service::builder()
.image("postgres")
.tag("latest")
.env("POSTGRES_PASSWORD", "password")
.env("POSTGRES_USER", "postgres")
.env("POSTGRES_PASSWORD", "postgres")
.build();
assert_eq!(3, service.envs.len());
}
#[test]
fn mount_collects_mounts() {
let service = Service::builder()
.image("postgres")
.tag("latest")
.mount(Mount::bind_mount("/host/data", "/var/lib/postgresql/data"))
.mount(Mount::bind_mount("/host/config", "/etc/postgresql"))
.build();
assert_eq!(2, service.mounts.len());
}
#[test]
fn trait_send() {
assert_send::<Service>();
}
#[test]
fn trait_sync() {
assert_sync::<Service>();
}
#[test]
fn trait_unpin() {
assert_unpin::<Service>();
}
}