diff --git a/testcontainers/src/core/client.rs b/testcontainers/src/core/client.rs index 0b36c6a2..411332a6 100644 --- a/testcontainers/src/core/client.rs +++ b/testcontainers/src/core/client.rs @@ -77,6 +77,10 @@ pub enum ClientError { StartContainer(BollardError), #[error("failed to stop a container: {0}")] StopContainer(BollardError), + #[error("failed to pause a container: {0}")] + PauseContainer(BollardError), + #[error("failed to unpause/resume a container: {0}")] + UnpauseContainer(BollardError), #[error("failed to inspect a container: {0}")] InspectContainer(BollardError), @@ -176,6 +180,20 @@ impl Client { .map_err(ClientError::Init) } + pub(crate) async fn pause(&self, id: &str) -> Result<(), ClientError> { + self.bollard + .pause_container(id) + .await + .map_err(ClientError::PauseContainer) + } + + pub(crate) async fn unpause(&self, id: &str) -> Result<(), ClientError> { + self.bollard + .unpause_container(id) + .await + .map_err(ClientError::UnpauseContainer) + } + pub(crate) async fn exec( &self, container_id: &str, diff --git a/testcontainers/src/core/containers/async_container.rs b/testcontainers/src/core/containers/async_container.rs index f618d74a..d1333880 100644 --- a/testcontainers/src/core/containers/async_container.rs +++ b/testcontainers/src/core/containers/async_container.rs @@ -288,6 +288,20 @@ where Ok(()) } + /// Pause the container. + /// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause) + pub async fn pause(&self) -> Result<()> { + self.docker_client.pause(&self.id).await?; + Ok(()) + } + + /// Resume/Unpause the container. + /// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause) + pub async fn unpause(&self) -> Result<()> { + self.docker_client.unpause(&self.id).await?; + Ok(()) + } + /// Removes the container. pub async fn rm(mut self) -> Result<()> { log::debug!("Deleting docker container {}", self.id); diff --git a/testcontainers/src/core/containers/sync_container.rs b/testcontainers/src/core/containers/sync_container.rs index 7b26e36f..2fb96a6d 100644 --- a/testcontainers/src/core/containers/sync_container.rs +++ b/testcontainers/src/core/containers/sync_container.rs @@ -137,6 +137,18 @@ where self.rt().block_on(self.async_impl().start()) } + /// Pause the container. + /// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause) + pub async fn pause(&self) -> Result<()> { + self.rt().block_on(self.async_impl().pause()) + } + + /// Resume/Unpause the container. + /// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause) + pub async fn unpause(&self) -> Result<()> { + self.rt().block_on(self.async_impl().unpause()) + } + /// Removes the container. pub fn rm(mut self) -> Result<()> { if let Some(active) = self.inner.take() {