Skip to content

Commit ef09f04

Browse files
authored
feat: add Container*::is_running (#790)
Simple, but for my use-case, very useful check if a container is still live.
1 parent c5d5bf2 commit ef09f04

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

testcontainers/src/core/client.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,23 @@ impl Client {
349349
.map_err(ClientError::UploadToContainerError)
350350
}
351351

352+
pub(crate) async fn container_is_running(
353+
&self,
354+
container_id: &str,
355+
) -> Result<bool, ClientError> {
356+
let container_info = self
357+
.bollard
358+
.inspect_container(container_id, Some(InspectContainerOptions { size: false }))
359+
.await
360+
.map_err(ClientError::InspectContainer)?;
361+
362+
if let Some(state) = container_info.state {
363+
Ok(state.running.unwrap_or_default())
364+
} else {
365+
Ok(false)
366+
}
367+
}
368+
352369
pub(crate) async fn container_exit_code(
353370
&self,
354371
container_id: &str,
@@ -358,6 +375,7 @@ impl Client {
358375
.inspect_container(container_id, Some(InspectContainerOptions { size: false }))
359376
.await
360377
.map_err(ClientError::InspectContainer)?;
378+
361379
let Some(state) = container_info.state else {
362380
return Ok(None);
363381
};

testcontainers/src/core/containers/async_container.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ where
369369
Ok(stderr)
370370
}
371371

372+
/// Returns whether the container is still running.
373+
pub async fn is_running(&self) -> Result<bool> {
374+
let status = self.docker_client.container_is_running(&self.id).await?;
375+
Ok(status)
376+
}
377+
372378
/// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
373379
pub async fn exit_code(&self) -> Result<Option<i64>> {
374380
let exit_code = self.docker_client.container_exit_code(&self.id).await?;

testcontainers/src/core/containers/sync_container.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ where
209209
Ok(stderr)
210210
}
211211

212+
/// Returns whether the container is still running.
213+
pub fn is_running(&self) -> Result<bool> {
214+
self.rt().block_on(self.async_impl().is_running())
215+
}
216+
212217
/// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
213218
pub fn exit_code(&self) -> Result<Option<i64>> {
214219
self.rt().block_on(self.async_impl().exit_code())

testcontainers/tests/async_runner.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,24 @@ async fn async_copy_files_to_container() -> anyhow::Result<()> {
268268
Ok(())
269269
}
270270

271+
#[tokio::test]
272+
async fn async_container_is_running() -> anyhow::Result<()> {
273+
let _ = pretty_env_logger::try_init();
274+
275+
// Container that should run until manually quit
276+
let container = GenericImage::new("simple_web_server", "latest")
277+
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
278+
.start()
279+
.await?;
280+
281+
assert!(container.is_running().await?);
282+
283+
container.stop().await?;
284+
285+
assert!(!container.is_running().await?);
286+
Ok(())
287+
}
288+
271289
#[tokio::test]
272290
async fn async_container_exit_code() -> anyhow::Result<()> {
273291
let _ = pretty_env_logger::try_init();
@@ -283,6 +301,5 @@ async fn async_container_exit_code() -> anyhow::Result<()> {
283301
container.stop().await?;
284302

285303
assert_eq!(container.exit_code().await?, Some(0));
286-
287304
Ok(())
288305
}

testcontainers/tests/sync_runner.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,23 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
288288
Ok(())
289289
}
290290

291+
#[test]
292+
fn sync_container_is_running() -> anyhow::Result<()> {
293+
let _ = pretty_env_logger::try_init();
294+
295+
// Container that should run until manually quit
296+
let container = GenericImage::new("simple_web_server", "latest")
297+
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
298+
.start()?;
299+
300+
assert!(container.is_running()?);
301+
302+
container.stop()?;
303+
304+
assert!(!container.is_running()?);
305+
Ok(())
306+
}
307+
291308
#[test]
292309
fn sync_container_exit_code() -> anyhow::Result<()> {
293310
let _ = pretty_env_logger::try_init();
@@ -302,6 +319,5 @@ fn sync_container_exit_code() -> anyhow::Result<()> {
302319
container.stop()?;
303320

304321
assert_eq!(container.exit_code()?, Some(0));
305-
306322
Ok(())
307323
}

0 commit comments

Comments
 (0)