1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#![crate_name = "testcontainers_ext"]
use bollard::{
models::ContainerSummaryStateEnum, query_parameters::ListContainersOptions,
query_parameters::StopContainerOptions,
};
use std::future::Future;
use testcontainers::{ContainerRequest, Image, ImageExt, TestcontainersError};
pub trait ImagePruneExistedLabelExt<I>: Sized + ImageExt<I> + Send
where
I: Image,
{
/// Given a scope, a container label, a prune flag, and a force flag,
/// this method will prune the container if the prune flag is true.
///
/// Example:
///
/// ```
/// use tokio::runtime::Runtime;
/// use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::AsyncRunner, GenericImage, ImageExt};
/// use testcontainers_ext::ImagePruneExistedLabelExt;
/// use anyhow::Result;
///
/// async fn test () -> Result<()> {
/// let container = GenericImage::new("redis", "7.2.4")
/// .with_exposed_port(6379.tcp())
/// .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
/// .with_prune_existed_label("my-project-scope", "redis", true, true).await?
/// .start()
/// .await?;
/// Ok(())
/// }
///
/// Runtime::new().unwrap().block_on(test()).unwrap();
/// ```
///
fn with_prune_existed_label(
self,
scope: &str,
container_label: &str,
prune: bool,
force: bool,
) -> impl Future<Output = Result<ContainerRequest<I>, TestcontainersError>> + Send {
use std::collections::HashMap;
use bollard::query_parameters::PruneContainersOptions;
use testcontainers::core::client::docker_client_instance;
let testcontainers_project_key = format!("{scope}.testcontainers.scope");
let testcontainers_container_key = format!("{scope}.testcontainers.container");
let testcontainers_prune_key = format!("{scope}.testcontainers.prune");
async move {
if prune {
let client = docker_client_instance().await?;
let mut filters = HashMap::<String, Vec<String>>::new();
filters.insert(
String::from("label"),
vec![
format!("{testcontainers_prune_key}=true"),
format!("{}={}", testcontainers_project_key, scope),
format!("{}={}", testcontainers_container_key, container_label),
],
);
if force {
let result = client
.list_containers(Some(ListContainersOptions {
all: false,
filters: Some(filters.clone()),
..Default::default()
}))
.await
.map_err(|err| TestcontainersError::Other(Box::new(err)))?;
let remove_containers = result
.iter()
.filter(|c| {
c.state
.is_some_and(|s| matches!(s, ContainerSummaryStateEnum::RUNNING))
})
.flat_map(|c| c.id.as_deref())
.collect::<Vec<_>>();
futures::future::try_join_all(
remove_containers
.iter()
.map(|c| client.stop_container(c, None::<StopContainerOptions>)),
)
.await
.map_err(|error| TestcontainersError::Other(Box::new(error)))?;
#[cfg(feature = "tracing")]
if !remove_containers.is_empty() {
tracing::warn!(name = "stop running containers", result = ?remove_containers);
}
}
let prune_result = client
.prune_containers(Some(PruneContainersOptions {
filters: Some(filters),
}))
.await;
match prune_result {
Ok(_result) => {
#[cfg(feature = "tracing")]
if _result
.containers_deleted
.as_ref()
.is_some_and(|c| !c.is_empty())
{
tracing::warn!(name = "prune existed containers", result = ?_result);
}
}
Err(bollard::errors::Error::DockerResponseServerError { status_code: 409, .. }) => {
#[cfg(feature = "tracing")]
tracing::debug!("Concurrent prune currently in progress, safely ignoring HTTP 409.");
}
Err(err) => return Err(TestcontainersError::Other(Box::new(err))),
}
}
let result = self.with_labels([
(testcontainers_prune_key, "true"),
(testcontainers_project_key, scope),
(testcontainers_container_key, container_label),
]);
Ok(result)
}
}
}
impl<R, I> ImagePruneExistedLabelExt<I> for R
where
R: Sized + ImageExt<I> + Send,
I: Image,
{
}
pub trait ImageDefaultLogConsumerExt<I>: Sized + ImageExt<I>
where
I: Image,
{
/// Given a container, this method will return a container request with a default log consumer.
///
/// Example:
///
/// ```
/// use tokio::runtime::Runtime;
/// use testcontainers::{core::{IntoContainerPort, WaitFor}, runners::AsyncRunner, GenericImage, ImageExt};
/// use testcontainers_ext::ImageDefaultLogConsumerExt;
/// use anyhow::Result;
///
/// async fn test () -> Result<()> {
/// let container = GenericImage::new("redis", "7.2.4")
/// .with_exposed_port(6379.tcp())
/// .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
/// .with_default_log_consumer()
/// .start()
/// .await?;
/// Ok(())
/// }
///
/// Runtime::new().unwrap().block_on(test()).unwrap();
/// ```
///
fn with_default_log_consumer(self) -> ContainerRequest<I> {
use testcontainers::core::logs::consumer::logging_consumer::LoggingConsumer;
self.with_log_consumer(
LoggingConsumer::new()
.with_stdout_level(log::Level::Info)
.with_stderr_level(log::Level::Error),
)
}
}
impl<R, I> ImageDefaultLogConsumerExt<I> for R
where
R: Sized + ImageExt<I>,
I: Image,
{
}