Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds NATS image #31

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rustainers/src/images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub use self::alpine::*;
mod mosquitto;
pub use self::mosquitto::*;

mod nats;
pub use self::nats::*;

/// A Generic Image
#[derive(Debug)]
pub struct GenericImage(RunnableContainer);
Expand Down
145 changes: 145 additions & 0 deletions rustainers/src/images/nats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use crate::{
Container, ExposedPort, ImageName, Port, PortError, RunnableContainer,
RunnableContainerBuilder, ToRunnableContainer, WaitStrategy,
};

const NATS_IMAGE: &ImageName = &ImageName::new("docker.io/nats");

const CLIENT_PORT: Port = Port(4222);
const CLUSTER_PORT: Port = Port(6222);
const MONITORING_PORT: Port = Port(8222);

/// A `Nats` image
///
/// # Example
///
/// ```rust, no_run
/// # async fn run() -> anyhow::Result<()> {
/// use rustainers::images::Nats;
///
/// let default_image = Nats::default();
///
/// let custom_image = Nats::default()
/// .with_tag("2.7.4");
///
/// # let runner = rustainers::runner::Runner::auto()?;
/// // ...
/// let container = runner.start(default_image).await?;
/// let endpoint = container.client_endpoint().await?;
/// // ...
/// # Ok(())
/// # }
#[derive(Debug)]
pub struct Nats {
image: ImageName,
client_port: ExposedPort,
cluster_port: ExposedPort,
monitoring_port: ExposedPort,
}

impl Nats {
/// Set the image tag
#[must_use]
pub fn with_tag(self, tag: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_tag(tag);
Self { image, ..self }
}

/// Set the image digest
#[must_use]
pub fn with_digest(self, digest: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_digest(digest);
Self { image, ..self }
}

/// Set the client port
#[must_use]
pub fn with_client_port(mut self, port: ExposedPort) -> Self {
self.client_port = port;
self
}

/// Set the cluster port
#[must_use]
pub fn with_cluster_port(mut self, port: ExposedPort) -> Self {
self.cluster_port = port;
self
}

/// Set the monitoring port
#[must_use]
pub fn with_monitoring_port(mut self, port: ExposedPort) -> Self {
self.monitoring_port = port;
self
}
}

impl Default for Nats {
fn default() -> Self {
Self {
image: NATS_IMAGE.clone(),
client_port: ExposedPort::new(CLIENT_PORT),
cluster_port: ExposedPort::new(CLUSTER_PORT),
monitoring_port: ExposedPort::new(MONITORING_PORT),
}
}
}

impl Container<Nats> {
/// Get endpoint URL for the client port
///
/// # Errors
///
/// Could fail if the port is not bind
pub async fn client_endpoint(&self) -> Result<String, PortError> {
let port = self.client_port.host_port().await?;
let host_ip = self.runner.container_host_ip().await?;
let url = format!("nats://{host_ip}:{port}");

Ok(url)
}

/// Get endpoint URL for the monitoring port
///
/// # Errors
///
/// Could fail if the port is not bind
pub async fn monitoring_endpoint(&self) -> Result<String, PortError> {
let port = self.monitoring_port.host_port().await?;
let host_ip = self.runner.container_host_ip().await?;
let url = format!("http://{host_ip}:{port}");

Ok(url)
}

/// Get endpoint URL for the cluster port
///
/// # Errors
///
/// Could fail if the port is not bind
pub async fn cluster_endpoint(&self) -> Result<String, PortError> {
let port = self.cluster_port.host_port().await?;
let host_ip = self.runner.container_host_ip().await?;
let url = format!("nats-route://{host_ip}:{port}");

Ok(url)
}
}

impl ToRunnableContainer for Nats {
fn to_runnable(&self, builder: RunnableContainerBuilder) -> RunnableContainer {
builder
.with_image(self.image.clone())
.with_wait_strategy(WaitStrategy::stderr_contains(
"Listening for client connections",
))
.with_port_mappings([
self.client_port.clone(),
self.cluster_port.clone(),
self.monitoring_port.clone(),
])
.build()
}
}
59 changes: 58 additions & 1 deletion rustainers/tests/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rstest::rstest;
use tokio::task::JoinSet;
use tracing::{debug, info};

use rustainers::images::{Minio, Mongo, Mosquitto, Postgres, Redis};
use rustainers::images::{Minio, Mongo, Mosquitto, Nats, Postgres, Redis};
use rustainers::runner::{RunOption, Runner};
use rustainers::{ExposedPort, Port};

Expand Down Expand Up @@ -95,6 +95,63 @@ async fn test_redis_endpoint(runner: &Runner) -> anyhow::Result<()> {
Ok(())
}

#[rstest]
#[tokio::test]
async fn test_image_nats(runner: &Runner) -> anyhow::Result<()> {
let options = RunOption::builder().with_remove(true).build();
let image = Nats::default();
let container = runner.start_with_options(image, options).await?;
debug!("Started {container}");

container.client_endpoint().await?;
Ok(())
}

#[rstest]
#[tokio::test]
async fn test_nats_client_endpoint(runner: &Runner) -> anyhow::Result<()> {
let options = RunOption::builder().with_remove(true).build();
let image =
Nats::default().with_client_port(ExposedPort::fixed(Port::new(8333), Port::new(8333)));
let container = runner.start_with_options(image, options).await?;
debug!("Started {container}");

let result = container.client_endpoint().await;
let_assert!(Ok(endpoint) = result);
check!(endpoint == "nats://127.0.0.1:8333");
Ok(())
}

#[rstest]
#[tokio::test]
async fn test_nats_monitoring_endpoint(runner: &Runner) -> anyhow::Result<()> {
let options = RunOption::builder().with_remove(true).build();
let image =
Nats::default().with_monitoring_port(ExposedPort::fixed(Port::new(8666), Port::new(8666)));
let container = runner.start_with_options(image, options).await?;
debug!("Started {container}");

let result = container.monitoring_endpoint().await;
let_assert!(Ok(endpoint) = result);
check!(endpoint == "http://127.0.0.1:8666");
Ok(())
}

#[rstest]
#[tokio::test]
async fn test_nats_cluster_endpoint(runner: &Runner) -> anyhow::Result<()> {
let options = RunOption::builder().with_remove(true).build();
let image =
Nats::default().with_cluster_port(ExposedPort::fixed(Port::new(8777), Port::new(8777)));
let container = runner.start_with_options(image, options).await?;
debug!("Started {container}");

let result = container.cluster_endpoint().await;
let_assert!(Ok(endpoint) = result);
check!(endpoint == "nats-route://127.0.0.1:8777");
Ok(())
}

#[rstest]
#[tokio::test]
async fn test_image_mongo(runner: &Runner) -> anyhow::Result<()> {
Expand Down
Loading