Skip to content

Commit

Permalink
refactor!: return PortNotExposed error from `ContainerState::host_p…
Browse files Browse the repository at this point in the history
…ort_*` (#644)

That's much easier for users to use `?` in `exec_after_start` instead of
mapping manually to appropriate error.
  • Loading branch information
DDtKey authored May 26, 2024
1 parent 54e80e4 commit 966221c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
6 changes: 2 additions & 4 deletions testcontainers/src/core/containers/async_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,8 @@ where
/// Starts the container.
pub async fn start(&self) -> Result<()> {
self.docker_client.start(&self.id).await?;
for cmd in self
.image
.exec_after_start(ContainerState::new(self.ports().await?))?
{
let state = ContainerState::new(self.id(), self.ports().await?);
for cmd in self.image.exec_after_start(state)? {
self.exec(cmd).await?;
}
Ok(())
Expand Down
32 changes: 26 additions & 6 deletions testcontainers/src/core/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,40 @@ where

#[derive(Debug)]
pub struct ContainerState {
id: String,
ports: Ports,
}

impl ContainerState {
pub fn new(ports: Ports) -> Self {
Self { ports }
pub fn new(id: impl Into<String>, ports: Ports) -> Self {
Self {
id: id.into(),
ports,
}
}

pub fn host_port_ipv4(&self, internal_port: u16) -> Option<u16> {
self.ports.map_to_host_port_ipv4(internal_port)
/// Returns the host port for the given internal port (`IPv4`).
///
/// Results in an error ([`TestcontainersError::PortNotExposed`]) if the port is not exposed.
pub fn host_port_ipv4(&self, internal_port: u16) -> Result<u16, TestcontainersError> {
self.ports
.map_to_host_port_ipv4(internal_port)
.ok_or_else(|| TestcontainersError::PortNotExposed {
id: self.id.clone(),
port: internal_port,
})
}

pub fn host_port_ipv6(&self, internal_port: u16) -> Option<u16> {
self.ports.map_to_host_port_ipv6(internal_port)
/// Returns the host port for the given internal port (`IPv6`).
///
/// Results in an error ([`TestcontainersError::PortNotExposed`]) if the port is not exposed.
pub fn host_port_ipv6(&self, internal_port: u16) -> Result<u16, TestcontainersError> {
self.ports
.map_to_host_port_ipv6(internal_port)
.ok_or_else(|| TestcontainersError::PortNotExposed {
id: self.id.clone(),
port: internal_port,
})
}
}

Expand Down
6 changes: 2 additions & 4 deletions testcontainers/src/runners/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ where
let container =
ContainerAsync::new(container_id, client.clone(), runnable_image, network).await?;

for cmd in container
.image()
.exec_after_start(ContainerState::new(container.ports().await?))?
{
let state = ContainerState::new(container.id(), container.ports().await?);
for cmd in container.image().exec_after_start(state)? {
container.exec(cmd).await?;
}

Expand Down

0 comments on commit 966221c

Please sign in to comment.