Skip to content
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
18 changes: 18 additions & 0 deletions testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ impl Client {
.map_err(ClientError::UploadToContainerError)
}

pub(crate) async fn container_is_running(
&self,
container_id: &str,
) -> Result<bool, ClientError> {
let container_info = self
.bollard
.inspect_container(container_id, Some(InspectContainerOptions { size: false }))
.await
.map_err(ClientError::InspectContainer)?;

if let Some(state) = container_info.state {
Ok(state.running.unwrap_or_default())
} else {
Ok(false)
}
}

pub(crate) async fn container_exit_code(
&self,
container_id: &str,
Expand All @@ -358,6 +375,7 @@ impl Client {
.inspect_container(container_id, Some(InspectContainerOptions { size: false }))
.await
.map_err(ClientError::InspectContainer)?;

let Some(state) = container_info.state else {
return Ok(None);
};
Expand Down
6 changes: 6 additions & 0 deletions testcontainers/src/core/containers/async_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ where
Ok(stderr)
}

/// Returns whether the container is still running.
pub async fn is_running(&self) -> Result<bool> {
let status = self.docker_client.container_is_running(&self.id).await?;
Ok(status)
}

/// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
pub async fn exit_code(&self) -> Result<Option<i64>> {
let exit_code = self.docker_client.container_exit_code(&self.id).await?;
Expand Down
5 changes: 5 additions & 0 deletions testcontainers/src/core/containers/sync_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ where
Ok(stderr)
}

/// Returns whether the container is still running.
pub fn is_running(&self) -> Result<bool> {
self.rt().block_on(self.async_impl().is_running())
}

/// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
pub fn exit_code(&self) -> Result<Option<i64>> {
self.rt().block_on(self.async_impl().exit_code())
Expand Down
19 changes: 18 additions & 1 deletion testcontainers/tests/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ async fn async_copy_files_to_container() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test]
async fn async_container_is_running() -> anyhow::Result<()> {
let _ = pretty_env_logger::try_init();

// Container that should run until manually quit
let container = GenericImage::new("simple_web_server", "latest")
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
.start()
.await?;

assert!(container.is_running().await?);

container.stop().await?;

assert!(!container.is_running().await?);
Ok(())
}

#[tokio::test]
async fn async_container_exit_code() -> anyhow::Result<()> {
let _ = pretty_env_logger::try_init();
Expand All @@ -283,6 +301,5 @@ async fn async_container_exit_code() -> anyhow::Result<()> {
container.stop().await?;

assert_eq!(container.exit_code().await?, Some(0));

Ok(())
}
18 changes: 17 additions & 1 deletion testcontainers/tests/sync_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,23 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn sync_container_is_running() -> anyhow::Result<()> {
let _ = pretty_env_logger::try_init();

// Container that should run until manually quit
let container = GenericImage::new("simple_web_server", "latest")
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
.start()?;

assert!(container.is_running()?);

container.stop()?;

assert!(!container.is_running()?);
Ok(())
}

#[test]
fn sync_container_exit_code() -> anyhow::Result<()> {
let _ = pretty_env_logger::try_init();
Expand All @@ -302,6 +319,5 @@ fn sync_container_exit_code() -> anyhow::Result<()> {
container.stop()?;

assert_eq!(container.exit_code()?, Some(0));

Ok(())
}
Loading