Skip to content

Commit 040b59f

Browse files
authored
feat: support pause and unpause container (#785)
This adds the pause/unpause calls for ContainerAsync/Container because I wanted to have an integration test keeping the same port for a mosquitto container. fixes #715, #433
1 parent 6d55d23 commit 040b59f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

testcontainers/src/core/client.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ pub enum ClientError {
7777
StartContainer(BollardError),
7878
#[error("failed to stop a container: {0}")]
7979
StopContainer(BollardError),
80+
#[error("failed to pause a container: {0}")]
81+
PauseContainer(BollardError),
82+
#[error("failed to unpause/resume a container: {0}")]
83+
UnpauseContainer(BollardError),
8084
#[error("failed to inspect a container: {0}")]
8185
InspectContainer(BollardError),
8286

@@ -183,6 +187,20 @@ impl Client {
183187
.map_err(ClientError::Init)
184188
}
185189

190+
pub(crate) async fn pause(&self, id: &str) -> Result<(), ClientError> {
191+
self.bollard
192+
.pause_container(id)
193+
.await
194+
.map_err(ClientError::PauseContainer)
195+
}
196+
197+
pub(crate) async fn unpause(&self, id: &str) -> Result<(), ClientError> {
198+
self.bollard
199+
.unpause_container(id)
200+
.await
201+
.map_err(ClientError::UnpauseContainer)
202+
}
203+
186204
pub(crate) async fn exec(
187205
&self,
188206
container_id: &str,

testcontainers/src/core/containers/async_container.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,20 @@ where
304304
Ok(())
305305
}
306306

307+
/// Pause the container.
308+
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause)
309+
pub async fn pause(&self) -> Result<()> {
310+
self.docker_client.pause(&self.id).await?;
311+
Ok(())
312+
}
313+
314+
/// Resume/Unpause the container.
315+
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause)
316+
pub async fn unpause(&self) -> Result<()> {
317+
self.docker_client.unpause(&self.id).await?;
318+
Ok(())
319+
}
320+
307321
/// Removes the container.
308322
pub async fn rm(mut self) -> Result<()> {
309323
log::debug!("Deleting docker container {}", self.id);

testcontainers/src/core/containers/sync_container.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ where
147147
self.rt().block_on(self.async_impl().start())
148148
}
149149

150+
/// Pause the container.
151+
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause)
152+
pub async fn pause(&self) -> Result<()> {
153+
self.rt().block_on(self.async_impl().pause())
154+
}
155+
156+
/// Resume/Unpause the container.
157+
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause)
158+
pub async fn unpause(&self) -> Result<()> {
159+
self.rt().block_on(self.async_impl().unpause())
160+
}
161+
150162
/// Removes the container.
151163
pub fn rm(mut self) -> Result<()> {
152164
if let Some(active) = self.inner.take() {

0 commit comments

Comments
 (0)