use std::time::Duration;
use crate::{
Container, ExposedPort, HealthCheck, ImageName, Port, PortError, RunnableContainer,
RunnableContainerBuilder, ToRunnableContainer,
};
const REDIS_IMAGE: &ImageName = &ImageName::new("docker.io/redis");
const PORT: Port = Port(6379);
#[derive(Debug)]
pub struct Redis {
image: ImageName,
port: ExposedPort,
}
impl Redis {
#[must_use]
pub fn with_tag(self, tag: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_tag(tag);
Self { image, ..self }
}
#[must_use]
pub fn with_digest(self, digest: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_digest(digest);
Self { image, ..self }
}
#[must_use]
pub fn with_port(mut self, port: ExposedPort) -> Self {
self.port = port;
self
}
}
impl Redis {}
impl Default for Redis {
fn default() -> Self {
Self {
image: REDIS_IMAGE.clone(),
port: ExposedPort::new(PORT),
}
}
}
impl Container<Redis> {
pub async fn endpoint(&self) -> Result<String, PortError> {
let port = self.port.host_port().await?;
let host_ip = self.runner.container_host_ip().await?;
let url = format!("redis://{host_ip}:{port}");
Ok(url)
}
}
impl ToRunnableContainer for Redis {
fn to_runnable(&self, builder: RunnableContainerBuilder) -> RunnableContainer {
builder
.with_image(self.image.clone())
.with_wait_strategy(
HealthCheck::builder()
.with_command("redis-cli --raw incr ping")
.with_start_period(Duration::from_millis(96))
.with_interval(Duration::from_millis(96))
.build(),
)
.with_port_mappings([self.port.clone()])
.build()
}
}