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
use super::Docker;
use crate::{
    composition::Composition,
    container::{CleanupContainer, HostPortMappings},
    static_container::STATIC_CONTAINERS,
    waitfor::MessageSource,
    DockerTestError, OperationalContainer, PendingContainer,
};
use bollard::{
    container::{
        InspectContainerOptions, KillContainerOptions, LogOutput, RemoveContainerOptions,
        StartContainerOptions, StopContainerOptions,
    },
    errors::Error,
    network::DisconnectNetworkOptions,
    secret::ContainerInspectResponse,
};
use bytes::Bytes;
use futures::StreamExt;
use futures::{future::join_all, Stream};
use std::{
    convert::TryFrom,
    sync::{Arc, Mutex},
};
use tracing::{event, Level};

pub struct ContainerInfo {
    pub ip: std::net::Ipv4Addr,
    pub ports: HostPortMappings,
}

/// All possible states a container can be in
#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display)]
#[allow(missing_docs)]
pub enum ContainerState {
    Empty,
    Created,
    Running,
    Paused,
    Restarting,
    Removing,
    Exited,
    Dead,
}

impl From<bollard::secret::ContainerStateStatusEnum> for ContainerState {
    fn from(value: bollard::secret::ContainerStateStatusEnum) -> Self {
        match value {
            bollard::secret::ContainerStateStatusEnum::EMPTY => ContainerState::Empty,
            bollard::secret::ContainerStateStatusEnum::CREATED => ContainerState::Created,
            bollard::secret::ContainerStateStatusEnum::RUNNING => ContainerState::Running,
            bollard::secret::ContainerStateStatusEnum::PAUSED => ContainerState::Paused,
            bollard::secret::ContainerStateStatusEnum::RESTARTING => ContainerState::Restarting,
            bollard::secret::ContainerStateStatusEnum::REMOVING => ContainerState::Removing,
            bollard::secret::ContainerStateStatusEnum::EXITED => ContainerState::Exited,
            bollard::secret::ContainerStateStatusEnum::DEAD => ContainerState::Dead,
        }
    }
}

#[derive(Default)]
pub struct ContainerLogSource {
    pub log_stderr: bool,
    pub log_stdout: bool,
    pub follow: bool,
}

impl From<MessageSource> for ContainerLogSource {
    fn from(value: MessageSource) -> Self {
        match value {
            MessageSource::Stdout => ContainerLogSource {
                log_stdout: true,
                ..Default::default()
            },
            MessageSource::Stderr => ContainerLogSource {
                log_stderr: true,
                ..Default::default()
            },
        }
    }
}

pub struct LogEntry {
    pub message: Bytes,
    pub source: MessageSource,
}

impl Docker {
    pub async fn unpause(&self, container_name: &str) -> Result<(), DockerTestError> {
        self.client
            .unpause_container(container_name)
            .await
            .map_err(|e| DockerTestError::Daemon(format!("failed to unpause container: {e:?}")))
    }
    pub async fn kill(&self, container_name: &str) -> Result<(), DockerTestError> {
        self.client
            .kill_container(
                container_name,
                Some(KillContainerOptions { signal: "SIGKILL" }),
            )
            .await
            .map_err(|e| DockerTestError::Daemon(format!("failed to kill container: {e:?}")))
    }
    pub async fn pause(&self, container_name: &str) -> Result<(), DockerTestError> {
        self.client
            .pause_container(container_name)
            .await
            .map_err(|e| DockerTestError::Daemon(format!("failed to pause container: {e:?}")))
    }
    pub async fn container_state(
        &self,
        container_name: &str,
    ) -> Result<ContainerState, DockerTestError> {
        self.client
            .inspect_container(container_name, None::<InspectContainerOptions>)
            .await
            .map_err(|e| {
                DockerTestError::Daemon(format!("failed to check container state: {e:?}"))
            })?
            .state
            .ok_or(DockerTestError::Daemon(
                "container state was 'None'".to_string(),
            ))?
            .status
            .ok_or(DockerTestError::Daemon(
                "container status was 'None'".to_string(),
            ))
            .map(|v| v.into())
    }

    pub fn container_logs(
        &self,
        container_id: &str,
        source: ContainerLogSource,
    ) -> impl Stream<Item = Result<LogEntry, DockerTestError>> {
        let mut log_options = bollard::container::LogsOptions::<String> {
            follow: true,
            ..Default::default()
        };

        if source.log_stderr {
            log_options.stderr = true;
        }

        if source.log_stdout {
            log_options.stdout = true;
        }

        self.client
            .logs(container_id, Some(log_options))
            .filter_map(move |chunk| {
                let out = match chunk {
                    Ok(chunk) => match chunk {
                        LogOutput::StdErr { message } if source.log_stderr => Some(Ok(LogEntry {
                            message,
                            source: MessageSource::Stderr,
                        })),
                        LogOutput::StdOut { message } if source.log_stdout => Some(Ok(LogEntry {
                            message,
                            source: MessageSource::Stdout,
                        })),
                        _ => None,
                    },
                    Err(e) => Some(Err(DockerTestError::ContainerLogStream(e.to_string()))),
                };

                futures::future::ready(out)
            })
    }

    pub async fn get_container_ip_and_ports(
        &self,
        container_id: &str,
        network_name: &str,
    ) -> Result<ContainerInfo, DockerTestError> {
        let details = self
            .client
            .inspect_container(container_id, None::<InspectContainerOptions>)
            .await
            .map_err(|e| DockerTestError::Daemon(format!("failed to inspect container: {}", e)))?;

        // Get the ip address from the network
        let container_ip = if let Some(inspected_network) = details
            .network_settings
            .as_ref()
            .unwrap()
            .networks
            .as_ref()
            .unwrap()
            .get(network_name)
        {
            event!(
                Level::DEBUG,
                "container ip from inspect: {}",
                inspected_network.ip_address.as_ref().unwrap()
            );
            inspected_network
                .ip_address
                .as_ref()
                .unwrap()
                .parse::<std::net::Ipv4Addr>()
                // Exited containers will not have an IP address
                .unwrap_or_else(|e| {
                    event!(Level::TRACE, "container ip address failed to parse: {}", e);
                    std::net::Ipv4Addr::UNSPECIFIED
                })
        } else {
            std::net::Ipv4Addr::UNSPECIFIED
        };

        let host_ports = if let Some(ports) = details.network_settings.unwrap().ports {
            event!(
                Level::DEBUG,
                "container ports from inspect: {:?}",
                ports.clone()
            );
            HostPortMappings::try_from(ports)
                .map_err(|e| DockerTestError::HostPort(e.to_string()))?
        } else {
            HostPortMappings::default()
        };

        Ok(ContainerInfo {
            ip: container_ip,
            ports: host_ports,
        })
    }

    pub async fn start_container(
        &self,
        pending: PendingContainer,
    ) -> Result<OperationalContainer, DockerTestError> {
        if pending.is_static {
            STATIC_CONTAINERS.start(&pending).await
        } else {
            self.start_container_inner(pending).await
        }
    }

    // Internal start method should only be invoked from the static mod.
    // TODO: isolate to static mod only
    pub async fn start_container_inner(
        &self,
        mut pending: PendingContainer,
    ) -> Result<OperationalContainer, DockerTestError> {
        self.client
            .start_container(&pending.name, None::<StartContainerOptions<String>>)
            .await
            .map_err(|e| match e {
                Error::DockerResponseServerError {
                    message,
                    status_code,
                } => {
                    if status_code == 404 {
                        let json: Result<serde_json::Value, serde_json::error::Error> =
                            serde_json::from_str(message.as_str());
                        match json {
                            Ok(json) => DockerTestError::Startup(format!(
                                "failed to start container due to `{}`",
                                json["message"].as_str().unwrap()
                            )),
                            Err(e) => DockerTestError::Daemon(format!(
                                "daemon json response decode failure: {}",
                                e
                            )),
                        }
                    } else {
                        DockerTestError::Daemon(format!("failed to start container: {}", message))
                    }
                }
                _ => DockerTestError::Daemon(format!("failed to start container: {}", e)),
            })?;

        pending.wait.take().unwrap().wait_for_ready(pending).await
    }

    pub async fn stop_containers(&self, containers: &[CleanupContainer]) {
        join_all(
            containers
                .iter()
                .filter(|c| !c.is_static())
                .map(|c| {
                    self.client
                        .stop_container(&c.id, None::<StopContainerOptions>)
                })
                .collect::<Vec<_>>(),
        )
        .await;
    }

    pub async fn remove_containers(&self, containers: &[CleanupContainer]) {
        let futures = containers
            .iter()
            .filter(|c| !c.is_static())
            .map(|c| {
                // It's unlikely that anonymous volumes will be used by several containers.
                // In this case there will be remove errors that it's possible just to ignore
                // See:
                // https://github.com/moby/moby/blob/7b9275c0da707b030e62c96b679a976f31f929d3/daemon/mounts.go#L34).
                let options = Some(RemoveContainerOptions {
                    force: true,
                    v: true,
                    ..Default::default()
                });

                self.client.remove_container(&c.id, options)
            })
            .collect::<Vec<_>>();
        join_all(futures).await;
    }

    // TODO: isolate to static mod only
    pub async fn remove_container(&self, id: &str) {
        let remove_opts = Some(RemoveContainerOptions {
            force: true,
            ..Default::default()
        });

        if let Err(e) = self.client.remove_container(id, remove_opts).await {
            event!(Level::ERROR, "failed to remove static container: {}", e);
        }
    }

    // TODO: isolate to static mod only
    pub async fn disconnect_container(&self, container_id: &str, network: &str) {
        let opts = DisconnectNetworkOptions::<&str> {
            container: container_id,
            force: true,
        };
        if let Err(e) = self.client.disconnect_network(network, opts).await {
            event!(
                Level::ERROR,
                "unable to remove dockertest-container from network: {}",
                e
            );
        }
    }

    // TODO: isolate to static mod only
    pub async fn running_container_from_composition(
        &self,
        composition: Composition,
        container_details: ContainerInspectResponse,
    ) -> Result<OperationalContainer, DockerTestError> {
        if let Some(id) = container_details.id {
            Ok(OperationalContainer {
                client: self.clone(),
                id,
                name: composition.container_name.clone(),
                handle: composition.container_name,
                ip: std::net::Ipv4Addr::UNSPECIFIED,
                ports: HostPortMappings::default(),
                is_static: true,
                log_options: composition.log_options,
                assumed_state: Arc::new(Mutex::new(composition.wait.expected_state())),
            })
        } else {
            Err(DockerTestError::Daemon(
                "failed to retrieve container id for external container".to_string(),
            ))
        }
    }
}