use rightsize::{Container, ContainerGuard, Result, Wait};
const AWS_PORT: u16 = 4566;
const AZURE_PORT: u16 = 4577;
const GCP_PORT: u16 = 4588;
pub struct FlociContainer {
container: Container,
port: u16,
}
impl FlociContainer {
fn new(image: &str, port: u16) -> Self {
Self {
container: Container::new(image)
.with_exposed_ports(&[port])
.waiting_for(Wait::for_http("/health").for_port(port)),
port,
}
}
pub fn aws() -> Self {
Self::aws_with_image("floci/floci:1.5.30")
}
pub fn aws_with_image(image: &str) -> Self {
Self::new(image, AWS_PORT)
}
pub fn azure() -> Self {
Self::azure_with_image("floci/floci-az:0.8.0")
}
pub fn azure_with_image(image: &str) -> Self {
Self::new(image, AZURE_PORT)
}
pub fn gcp() -> Self {
Self::gcp_with_image("floci/floci-gcp:0.4.0")
}
pub fn gcp_with_image(image: &str) -> Self {
Self::new(image, GCP_PORT)
}
pub async fn start(self) -> Result<FlociGuard> {
Ok(FlociGuard {
guard: self.container.start().await?,
port: self.port,
})
}
}
pub struct FlociGuard {
guard: ContainerGuard,
port: u16,
}
impl FlociGuard {
pub fn endpoint_url(&self) -> String {
format!(
"http://{}:{}",
self.guard.host(),
self.guard.get_mapped_port(self.port).unwrap()
)
}
pub async fn stop(self) -> Result<()> {
self.guard.stop().await
}
}
impl std::ops::Deref for FlociGuard {
type Target = ContainerGuard;
fn deref(&self) -> &ContainerGuard {
&self.guard
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn aws_picks_port_4566() {
let c = FlociContainer::aws();
assert_eq!(c.port, AWS_PORT);
}
#[test]
fn azure_picks_port_4577() {
let c = FlociContainer::azure();
assert_eq!(c.port, AZURE_PORT);
}
#[test]
fn gcp_picks_port_4588() {
let c = FlociContainer::gcp();
assert_eq!(c.port, GCP_PORT);
}
}