diff --git a/testcontainers/src/core/containers/sync_container.rs b/testcontainers/src/core/containers/sync_container.rs index c6b228ec..2319d5cf 100644 --- a/testcontainers/src/core/containers/sync_container.rs +++ b/testcontainers/src/core/containers/sync_container.rs @@ -1,9 +1,7 @@ use std::{fmt, io::BufRead, net::IpAddr, sync::Arc}; use crate::{ - core::{ - copy::CopyFileFromContainer, env, error::Result, ports::Ports, ContainerPort, ExecCommand, - }, + core::{copy::CopyFileFromContainer, error::Result, ports::Ports, ContainerPort, ExecCommand}, ContainerAsync, Image, }; @@ -257,17 +255,12 @@ where impl Drop for Container { fn drop(&mut self) { - if let Some(active) = self.inner.take() { - active.runtime.block_on(async { - match active.async_impl.docker_client().config.command() { - env::Command::Remove => { - if let Err(e) = active.async_impl.rm().await { - log::error!("Failed to remove container on drop: {}", e); - } - } - env::Command::Keep => {} - } - }); + if let Some(ActiveContainer { + runtime, + async_impl, + }) = self.inner.take() + { + runtime.block_on(async { drop(async_impl) }); } } } diff --git a/testcontainers/src/core/env/config.rs b/testcontainers/src/core/env/config.rs index 655cf26e..05ffd1ee 100644 --- a/testcontainers/src/core/env/config.rs +++ b/testcontainers/src/core/env/config.rs @@ -17,6 +17,9 @@ pub enum ConfigurationError { #[cfg(feature = "properties-config")] #[error("failed to load testcontainers properties: {0}")] WrongPropertiesFormat(#[from] serde_java_properties::de::Error), + #[cfg(feature = "reusable-containers")] + #[error("container name or labels must be provided when reusing containers")] + MissingContainerNameAndLabels, } /// The default path to the Docker configuration file. diff --git a/testcontainers/src/runners/async_runner.rs b/testcontainers/src/runners/async_runner.rs index ff969d0b..22575698 100644 --- a/testcontainers/src/runners/async_runner.rs +++ b/testcontainers/src/runners/async_runner.rs @@ -117,9 +117,19 @@ where #[cfg(feature = "reusable-containers")] { - use crate::ReuseDirective::{Always, CurrentSession}; + use crate::{ + core::env::ConfigurationError, + ReuseDirective::{Always, CurrentSession}, + TestcontainersError, + }; if matches!(container_req.reuse(), Always | CurrentSession) { + if labels.is_empty() && container_req.container_name().is_none() { + return Err(TestcontainersError::Client(ClientError::Configuration( + ConfigurationError::MissingContainerNameAndLabels, + ))); + } + if let Some(container_id) = client .get_running_container_id( container_req.container_name().as_deref(),