Skip to content

Commit 229d9d9

Browse files
committed
refactor: avoid too specific logic on client level
1 parent 1914893 commit 229d9d9

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

testcontainers/src/compose/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod error;
1212

1313
pub use error::{ComposeError, Result};
1414

15+
const COMPOSE_PROJECT_LABEL: &str = "com.docker.compose.project";
16+
const COMPOSE_SERVICE_LABEL: &str = "com.docker.compose.service";
17+
1518
pub struct DockerCompose {
1619
project_name: String,
1720
client: Arc<client::ComposeClient>,
@@ -80,13 +83,17 @@ impl DockerCompose {
8083

8184
let docker_client = Client::lazy_client().await?;
8285

83-
let container_ids = docker_client
84-
.list_containers_by_label("com.docker.compose.project", &self.project_name)
86+
let containers = docker_client
87+
.list_containers_by_label(COMPOSE_PROJECT_LABEL, &self.project_name)
8588
.await?;
8689

87-
for (service_name, container_id) in container_ids {
88-
let raw = RawContainer::new(container_id, docker_client.clone());
89-
self.services.insert(service_name, raw);
90+
for container in containers {
91+
if let (Some(labels), Some(id)) = (container.labels, container.id) {
92+
if let Some(service_name) = labels.get(COMPOSE_SERVICE_LABEL) {
93+
let raw = RawContainer::new(id, docker_client.clone());
94+
self.services.insert(service_name.clone(), raw);
95+
}
96+
}
9097
}
9198

9299
self.docker_client = Some(docker_client.clone());

testcontainers/src/core/client.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,11 @@ impl Client {
185185
.map_err(ClientError::InspectContainer)
186186
}
187187

188-
#[cfg(feature = "docker-compose")]
189188
pub(crate) async fn list_containers_by_label(
190189
&self,
191190
label_key: &str,
192191
label_value: &str,
193-
) -> Result<HashMap<String, String>, ClientError> {
194-
use bollard::models::ContainerSummary;
195-
192+
) -> Result<Vec<bollard::models::ContainerSummary>, ClientError> {
196193
let filters = HashMap::from([(
197194
"label".to_string(),
198195
vec![format!("{}={}", label_key, label_value)],
@@ -203,25 +200,10 @@ impl Client {
203200
.filters(&filters)
204201
.build();
205202

206-
let containers: Vec<ContainerSummary> = self
207-
.bollard
203+
self.bollard
208204
.list_containers(Some(options))
209205
.await
210-
.map_err(ClientError::ListContainers)?;
211-
212-
let mut result = HashMap::new();
213-
214-
for container in containers {
215-
if let Some(labels) = container.labels {
216-
if let Some(service_name) = labels.get("com.docker.compose.service") {
217-
if let Some(id) = container.id {
218-
result.insert(service_name.clone(), id);
219-
}
220-
}
221-
}
222-
}
223-
224-
Ok(result)
206+
.map_err(ClientError::ListContainers)
225207
}
226208

227209
pub(crate) async fn rm(&self, id: &str) -> Result<(), ClientError> {

0 commit comments

Comments
 (0)