Skip to content

Commit b0a3d28

Browse files
authored
fix: reuse container requires name (#887)
... also sync `Container `should reuse `ContainerAsync`'s drop impl. This resolves #742. cc @DDtKey @the-wondersmith --------- Signed-off-by: tison <[email protected]>
1 parent 4674882 commit b0a3d28

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

testcontainers/src/core/containers/sync_container.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::{fmt, io::BufRead, net::IpAddr, sync::Arc};
22

33
use crate::{
4-
core::{
5-
copy::CopyFileFromContainer, env, error::Result, ports::Ports, ContainerPort, ExecCommand,
6-
},
4+
core::{copy::CopyFileFromContainer, error::Result, ports::Ports, ContainerPort, ExecCommand},
75
ContainerAsync, Image,
86
};
97

@@ -257,17 +255,12 @@ where
257255

258256
impl<I: Image> Drop for Container<I> {
259257
fn drop(&mut self) {
260-
if let Some(active) = self.inner.take() {
261-
active.runtime.block_on(async {
262-
match active.async_impl.docker_client().config.command() {
263-
env::Command::Remove => {
264-
if let Err(e) = active.async_impl.rm().await {
265-
log::error!("Failed to remove container on drop: {}", e);
266-
}
267-
}
268-
env::Command::Keep => {}
269-
}
270-
});
258+
if let Some(ActiveContainer {
259+
runtime,
260+
async_impl,
261+
}) = self.inner.take()
262+
{
263+
runtime.block_on(async { drop(async_impl) });
271264
}
272265
}
273266
}

testcontainers/src/core/env/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub enum ConfigurationError {
1717
#[cfg(feature = "properties-config")]
1818
#[error("failed to load testcontainers properties: {0}")]
1919
WrongPropertiesFormat(#[from] serde_java_properties::de::Error),
20+
#[cfg(feature = "reusable-containers")]
21+
#[error("container name or labels must be provided when reusing containers")]
22+
MissingContainerNameAndLabels,
2023
}
2124

2225
/// The default path to the Docker configuration file.

testcontainers/src/runners/async_runner.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,19 @@ where
117117

118118
#[cfg(feature = "reusable-containers")]
119119
{
120-
use crate::ReuseDirective::{Always, CurrentSession};
120+
use crate::{
121+
core::env::ConfigurationError,
122+
ReuseDirective::{Always, CurrentSession},
123+
TestcontainersError,
124+
};
121125

122126
if matches!(container_req.reuse(), Always | CurrentSession) {
127+
if labels.is_empty() && container_req.container_name().is_none() {
128+
return Err(TestcontainersError::Client(ClientError::Configuration(
129+
ConfigurationError::MissingContainerNameAndLabels,
130+
)));
131+
}
132+
123133
if let Some(container_id) = client
124134
.get_running_container_id(
125135
container_req.container_name().as_deref(),

0 commit comments

Comments
 (0)