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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::collections::HashMap;

use async_trait::async_trait;
use bollard::{
    container::{Config, CreateContainerOptions},
    models::{HostConfig, PortBinding},
};

use crate::{
    core::{
        client::Client,
        mounts::{AccessMode, Mount, MountType},
        network::Network,
        ContainerState,
    },
    ContainerAsync, Image, ImageArgs, RunnableImage,
};

#[async_trait]
/// Helper trait to start containers asynchronously.
///
/// ## Example
///
/// ```rust,no_run
/// use testcontainers::{core::WaitFor, runners::AsyncRunner, GenericImage};
///
/// async fn test_redis() {
///     let container = GenericImage::new("redis", "7.2.4")
///         .with_exposed_port(6379)
///         .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections"))
///         .start()
///         .await;
/// }
/// ```
pub trait AsyncRunner<I: Image> {
    /// Starts the container and returns an instance of `ContainerAsync`.
    async fn start(self) -> ContainerAsync<I>;

    /// Pulls the image from the registry.
    /// Useful if you want to pull the image before starting the container.
    async fn pull_image(self) -> RunnableImage<I>;
}

#[async_trait]
impl<T, I> AsyncRunner<I> for T
where
    T: Into<RunnableImage<I>> + Send,
    I: Image,
{
    async fn start(self) -> ContainerAsync<I> {
        let client = Client::lazy_client().await;
        let runnable_image = self.into();
        let mut create_options: Option<CreateContainerOptions<String>> = None;

        let extra_hosts: Vec<_> = runnable_image
            .hosts()
            .map(|(key, value)| format!("{key}:{value}"))
            .collect();

        let mut config: Config<String> = Config {
            image: Some(runnable_image.descriptor()),
            host_config: Some(HostConfig {
                privileged: Some(runnable_image.privileged()),
                extra_hosts: Some(extra_hosts),
                ..Default::default()
            }),
            ..Default::default()
        };

        // shared memory
        if let Some(bytes) = runnable_image.shm_size() {
            config.host_config = config.host_config.map(|mut host_config| {
                host_config.shm_size = Some(bytes as i64);
                host_config
            });
        }

        // create network and add it to container creation
        let network = if let Some(network) = runnable_image.network() {
            config.host_config = config.host_config.map(|mut host_config| {
                host_config.network_mode = Some(network.to_string());
                host_config
            });
            Network::new(network, client.clone()).await
        } else {
            None
        };

        // name of the container
        if let Some(name) = runnable_image.container_name() {
            create_options = Some(CreateContainerOptions {
                name: name.to_owned(),
                platform: None,
            })
        }

        // handle environment variables
        let envs: Vec<String> = runnable_image
            .env_vars()
            .map(|(k, v)| format!("{k}={v}"))
            .collect();
        config.env = Some(envs);

        // mounts and volumes
        let mounts: Vec<_> = runnable_image.mounts().map(Into::into).collect();

        if !mounts.is_empty() {
            config.host_config = config.host_config.map(|mut host_config| {
                host_config.mounts = Some(mounts);
                host_config
            });
        }

        // entrypoint
        if let Some(entrypoint) = runnable_image.entrypoint() {
            config.entrypoint = Some(vec![entrypoint]);
        }

        let is_container_networked = runnable_image
            .network()
            .as_ref()
            .map(|network| network.starts_with("container:"))
            .unwrap_or(false);

        // exposed ports
        if !is_container_networked {
            config.exposed_ports = Some(
                runnable_image
                    .expose_ports()
                    .into_iter()
                    .map(|p| (format!("{p}/tcp"), HashMap::new()))
                    .collect(),
            );
        }

        // ports
        if runnable_image.ports().is_some() || !runnable_image.expose_ports().is_empty() {
            let empty: Vec<_> = Vec::new();
            let bindings = runnable_image
                .ports()
                .as_ref()
                .unwrap_or(&empty)
                .iter()
                .map(|p| {
                    (
                        format!("{}/tcp", p.internal),
                        Some(vec![PortBinding {
                            host_ip: None,
                            host_port: Some(p.local.to_string()),
                        }]),
                    )
                })
                .chain(
                    runnable_image
                        .expose_ports()
                        .into_iter()
                        .map(|p| (format!("{}/tcp", p), Some(vec![PortBinding::default()]))),
                );

            config.host_config = config.host_config.map(|mut host_config| {
                host_config.port_bindings = Some(bindings.collect());
                host_config
            });
        } else if !is_container_networked {
            config.host_config = config.host_config.map(|mut host_config| {
                host_config.publish_all_ports = Some(true);
                host_config
            });
        }

        // extra hosts

        let args = runnable_image
            .args()
            .clone()
            .into_iterator()
            .collect::<Vec<String>>();
        if !args.is_empty() {
            config.cmd = Some(args);
        }

        // create the container with options
        let create_result = client
            .create_container(create_options.clone(), config.clone())
            .await;
        let container_id = {
            match create_result {
                Ok(container) => container.id,
                Err(bollard::errors::Error::DockerResponseServerError {
                    status_code: 404, ..
                }) => {
                    client.pull_image(&runnable_image.descriptor()).await;
                    client
                        .bollard
                        .create_container(create_options, config)
                        .await
                        .unwrap()
                        .id
                }
                Err(err) => panic!("{}", err),
            }
        };

        #[cfg(feature = "watchdog")]
        if client.config.command() == crate::core::env::Command::Remove {
            crate::watchdog::register(container_id.clone());
        }

        client
            .bollard
            .start_container::<String>(&container_id, None)
            .await
            .unwrap();

        let container =
            ContainerAsync::new(container_id, client.clone(), runnable_image, network).await;

        for cmd in container
            .image()
            .exec_after_start(ContainerState::new(container.ports().await))
        {
            container.exec(cmd).await;
        }

        container
    }

    async fn pull_image(self) -> RunnableImage<I> {
        let runnable_image = self.into();
        let client = Client::lazy_client().await;
        client.pull_image(&runnable_image.descriptor()).await;

        runnable_image
    }
}

impl From<&Mount> for bollard::models::Mount {
    fn from(mount: &Mount) -> Self {
        let mount_type = match mount.mount_type() {
            MountType::Bind => bollard::models::MountTypeEnum::BIND,
            MountType::Volume => bollard::models::MountTypeEnum::VOLUME,
            MountType::Tmpfs => bollard::models::MountTypeEnum::TMPFS,
        };

        let is_read_only = matches!(mount.access_mode(), AccessMode::ReadOnly);

        Self {
            target: mount.target().map(str::to_string),
            source: mount.source().map(str::to_string),
            typ: Some(mount_type),
            read_only: Some(is_read_only),
            ..Default::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{core::WaitFor, images::generic::GenericImage};

    #[tokio::test]
    async fn async_run_command_should_expose_all_ports_if_no_explicit_mapping_requested() {
        let client = Client::lazy_client().await;
        let container = RunnableImage::from(GenericImage::new("hello-world", "latest"))
            .start()
            .await;

        let container_details = client.inspect(container.id()).await;
        let publish_ports = container_details
            .host_config
            .unwrap()
            .publish_all_ports
            .unwrap();
        assert!(publish_ports, "publish_all_ports must be `true`");
    }

    #[tokio::test]
    async fn async_run_command_should_map_exposed_port() {
        let image = GenericImage::new("simple_web_server", "latest")
            .with_exposed_port(5000)
            .with_wait_for(WaitFor::message_on_stdout("server is ready"))
            .with_wait_for(WaitFor::seconds(1));
        let container = image.start().await;
        container.get_host_port_ipv4(5000).await;
    }

    #[tokio::test]
    async fn async_run_command_should_expose_only_requested_ports() {
        let client = Client::lazy_client().await;
        let image = GenericImage::new("hello-world", "latest");
        let container = RunnableImage::from(image)
            .with_mapped_port((123, 456))
            .with_mapped_port((555, 888))
            .start()
            .await;

        let container_details = client.inspect(container.id()).await;

        let port_bindings = container_details
            .host_config
            .unwrap()
            .port_bindings
            .unwrap();
        assert!(port_bindings.contains_key("456/tcp"));
        assert!(port_bindings.contains_key("888/tcp"));
    }

    #[tokio::test]
    async fn async_run_command_should_include_network() {
        let client = Client::lazy_client().await;
        let image = GenericImage::new("hello-world", "latest");
        let container = RunnableImage::from(image)
            .with_network("awesome-net-1")
            .start()
            .await;

        let container_details = client.inspect(container.id()).await;
        let networks = container_details
            .network_settings
            .unwrap()
            .networks
            .unwrap();

        assert!(
            networks.contains_key("awesome-net-1"),
            "Networks is {networks:?}"
        );
    }

    #[tokio::test]
    async fn async_run_command_should_include_name() {
        let client = Client::lazy_client().await;
        let image = GenericImage::new("hello-world", "latest");
        let container = RunnableImage::from(image)
            .with_container_name("async_hello_container")
            .start()
            .await;

        let container_details = client.inspect(container.id()).await;
        let container_name = container_details.name.unwrap();
        assert!(container_name.ends_with("async_hello_container"));
    }

    #[tokio::test]
    async fn async_should_create_network_if_image_needs_it_and_drop_it_in_the_end() {
        let hello_world = GenericImage::new("hello-world", "latest");

        {
            let client = Client::lazy_client().await;
            assert!(!client.network_exists("awesome-net-2").await);

            // creating the first container creates the network
            let _container1 = RunnableImage::from(hello_world.clone())
                .with_network("awesome-net-2")
                .start()
                .await;

            // creating a 2nd container doesn't fail because check if the network exists already
            let _container2 = RunnableImage::from(hello_world)
                .with_network("awesome-net-2")
                .start()
                .await;

            assert!(client.network_exists("awesome-net-2").await);
        }

        // containers have been dropped, should clean up networks
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
        let client = Client::lazy_client().await;
        assert!(!client.network_exists("awesome-net-2").await)
    }

    #[tokio::test]
    async fn async_run_command_should_set_shared_memory_size() {
        let client = Client::lazy_client().await;
        let image = GenericImage::new("hello-world", "latest");
        let container = RunnableImage::from(image)
            .with_shm_size(1_000_000)
            .start()
            .await;

        let container_details = client.inspect(container.id()).await;
        let shm_size = container_details.host_config.unwrap().shm_size.unwrap();

        assert_eq!(shm_size, 1_000_000);
    }

    #[tokio::test]
    async fn async_run_command_should_include_privileged() {
        let image = GenericImage::new("hello-world", "latest");
        let container = RunnableImage::from(image)
            .with_privileged(true)
            .start()
            .await;

        let client = Client::lazy_client().await;
        let container_details = client.inspect(container.id()).await;

        let privileged = container_details.host_config.unwrap().privileged.unwrap();
        assert!(privileged, "privileged must be `true`");
    }
}