use getset::{CopyGetters, Getters};
use testcontainers::core::{Mount, WaitFor};
use typed_builder::TypedBuilder;
use crate::environment::Variable;
#[derive(Clone, Debug, CopyGetters, Getters, TypedBuilder)]
pub struct Server {
#[builder(setter(into))]
#[getset(get = "pub")]
image: String,
#[builder(setter(into))]
#[getset(get = "pub")]
tag: String,
#[getset(get_copy = "pub")]
port: u16,
#[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 server'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 server = Server::builder()
.image("doco")
.tag("latest")
.port(8080)
.cmd_arg("--config")
.cmd_arg("/etc/app.toml")
.build();
assert_eq!(2, server.cmd.len());
}
#[test]
fn env_collects_variables() {
let server = Server::builder()
.image("doco")
.tag("latest")
.port(8080)
.env("LOG_LEVEL", "debug")
.env("PORT", "8080")
.build();
assert_eq!(2, server.envs.len());
}
#[test]
fn mount_collects_mounts() {
let server = Server::builder()
.image("doco")
.tag("latest")
.port(8080)
.mount(Mount::bind_mount("/host/path", "/container/path"))
.mount(Mount::bind_mount("/host/other", "/container/other"))
.build();
assert_eq!(2, server.mounts.len());
}
#[test]
fn trait_send() {
assert_send::<Server>();
}
#[test]
fn trait_sync() {
assert_sync::<Server>();
}
#[test]
fn trait_unpin() {
assert_unpin::<Server>();
}
}