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
//! Support for testing using Docker containers.
//!
//! The [`Container`] type is a builder for configuring a container to run.
//! After you call `launch`, you can use the [`ContainerHandle`] to interact
//! with the running container.
//!
//! Tests using containers must use `#[cargo_test(container_test)]` to disable
//! them unless the CARGO_CONTAINER_TESTS environment variable is set.

use cargo_util::ProcessBuilder;
use std::collections::HashMap;
use std::io::Read;
use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use tar::Header;

/// A builder for configuring a container to run.
pub struct Container {
    /// The host directory that forms the basis of the Docker image.
    build_context: PathBuf,
    /// Files to copy over to the image.
    files: Vec<MkFile>,
}

/// A handle to a running container.
///
/// You can use this to interact with the container.
pub struct ContainerHandle {
    /// The name of the container.
    name: String,
    /// The IP address of the container.
    ///
    /// NOTE: This is currently unused, but may be useful so I left it in.
    /// This can only be used on Linux. macOS and Windows docker doesn't allow
    /// direct connection to the container.
    pub ip_address: String,
    /// Port mappings of container_port to host_port for ports exposed via EXPOSE.
    pub port_mappings: HashMap<u16, u16>,
}

impl Container {
    pub fn new(context_dir: &str) -> Container {
        assert!(std::env::var_os("CARGO_CONTAINER_TESTS").is_some());
        let mut build_context = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        build_context.push("containers");
        build_context.push(context_dir);
        Container {
            build_context,
            files: Vec::new(),
        }
    }

    /// Adds a file to be copied into the container.
    pub fn file(mut self, file: MkFile) -> Self {
        self.files.push(file);
        self
    }

    /// Starts the container.
    pub fn launch(mut self) -> ContainerHandle {
        static NEXT_ID: AtomicUsize = AtomicUsize::new(0);

        let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
        let name = format!("cargo_test_{id}");
        remove_if_exists(&name);
        self.create_container(&name);
        self.copy_files(&name);
        self.start_container(&name);
        let info = self.container_inspect(&name);
        let ip_address = if cfg!(target_os = "linux") {
            info[0]["NetworkSettings"]["IPAddress"]
                .as_str()
                .unwrap()
                .to_string()
        } else {
            // macOS and Windows can't make direct connections to the
            // container. It only works through exposed ports or mapped ports.
            "127.0.0.1".to_string()
        };
        let port_mappings = self.port_mappings(&info);
        self.wait_till_ready(&port_mappings);

        ContainerHandle {
            name,
            ip_address,
            port_mappings,
        }
    }

    fn create_container(&self, name: &str) {
        static BUILD_LOCK: Mutex<()> = Mutex::new(());

        let image_base = self.build_context.file_name().unwrap();
        let image_name = format!("cargo-test-{}", image_base.to_str().unwrap());
        let _lock = BUILD_LOCK
            .lock()
            .map_err(|_| panic!("previous docker build failed, unable to run test"));
        ProcessBuilder::new("docker")
            .args(&["build", "--tag", image_name.as_str()])
            .arg(&self.build_context)
            .exec_with_output()
            .unwrap();

        ProcessBuilder::new("docker")
            .args(&[
                "container",
                "create",
                "--publish-all",
                "--rm",
                "--name",
                name,
            ])
            .arg(image_name)
            .exec_with_output()
            .unwrap();
    }

    fn copy_files(&mut self, name: &str) {
        if self.files.is_empty() {
            return;
        }
        let mut ar = tar::Builder::new(Vec::new());
        let files = std::mem::replace(&mut self.files, Vec::new());
        for mut file in files {
            ar.append_data(&mut file.header, &file.path, file.contents.as_slice())
                .unwrap();
        }
        let ar = ar.into_inner().unwrap();
        ProcessBuilder::new("docker")
            .args(&["cp", "-"])
            .arg(format!("{name}:/"))
            .stdin(ar)
            .exec_with_output()
            .unwrap();
    }

    fn start_container(&self, name: &str) {
        ProcessBuilder::new("docker")
            .args(&["container", "start"])
            .arg(name)
            .exec_with_output()
            .unwrap();
    }

    fn container_inspect(&self, name: &str) -> serde_json::Value {
        let output = ProcessBuilder::new("docker")
            .args(&["inspect", name])
            .exec_with_output()
            .unwrap();
        serde_json::from_slice(&output.stdout).unwrap()
    }

    /// Returns the mapping of container_port->host_port for ports that were
    /// exposed with EXPOSE.
    fn port_mappings(&self, info: &serde_json::Value) -> HashMap<u16, u16> {
        info[0]["NetworkSettings"]["Ports"]
            .as_object()
            .unwrap()
            .iter()
            .map(|(key, value)| {
                let key = key
                    .strip_suffix("/tcp")
                    .expect("expected TCP only ports")
                    .parse()
                    .unwrap();
                let values = value.as_array().unwrap();
                let value = values
                    .iter()
                    .find(|value| value["HostIp"].as_str().unwrap() == "0.0.0.0")
                    .expect("expected localhost IP");
                let host_port = value["HostPort"].as_str().unwrap().parse().unwrap();
                (key, host_port)
            })
            .collect()
    }

    fn wait_till_ready(&self, port_mappings: &HashMap<u16, u16>) {
        for port in port_mappings.values() {
            let mut ok = false;
            for _ in 0..30 {
                match std::net::TcpStream::connect(format!("127.0.0.1:{port}")) {
                    Ok(_) => {
                        ok = true;
                        break;
                    }
                    Err(e) => {
                        if e.kind() != std::io::ErrorKind::ConnectionRefused {
                            panic!("unexpected localhost connection error: {e:?}");
                        }
                        std::thread::sleep(std::time::Duration::new(1, 0));
                    }
                }
            }
            if !ok {
                panic!("no listener on localhost port {port}");
            }
        }
    }
}

impl ContainerHandle {
    /// Executes a program inside a running container.
    pub fn exec(&self, args: &[&str]) -> std::process::Output {
        ProcessBuilder::new("docker")
            .args(&["container", "exec", &self.name])
            .args(args)
            .exec_with_output()
            .unwrap()
    }

    /// Returns the contents of a file inside the container.
    pub fn read_file(&self, path: &str) -> String {
        let output = ProcessBuilder::new("docker")
            .args(&["cp", &format!("{}:{}", self.name, path), "-"])
            .exec_with_output()
            .unwrap();
        let mut ar = tar::Archive::new(output.stdout.as_slice());
        let mut entry = ar.entries().unwrap().next().unwrap().unwrap();
        let mut contents = String::new();
        entry.read_to_string(&mut contents).unwrap();
        contents
    }
}

impl Drop for ContainerHandle {
    fn drop(&mut self) {
        // To help with debugging, this will keep the container alive.
        if std::env::var_os("CARGO_CONTAINER_TEST_KEEP").is_some() {
            return;
        }
        remove_if_exists(&self.name);
    }
}

fn remove_if_exists(name: &str) {
    if let Err(e) = Command::new("docker")
        .args(&["container", "rm", "--force", name])
        .output()
    {
        panic!("failed to run docker: {e}");
    }
}

/// Builder for configuring a file to copy into a container.
pub struct MkFile {
    path: String,
    contents: Vec<u8>,
    header: Header,
}

impl MkFile {
    /// Defines a file to add to the container.
    ///
    /// This should be passed to `Container::file`.
    ///
    /// The path is the path inside the container to create the file.
    pub fn path(path: &str) -> MkFile {
        MkFile {
            path: path.to_string(),
            contents: Vec::new(),
            header: Header::new_gnu(),
        }
    }

    pub fn contents(mut self, contents: impl Into<Vec<u8>>) -> Self {
        self.contents = contents.into();
        self.header.set_size(self.contents.len() as u64);
        self
    }

    pub fn mode(mut self, mode: u32) -> Self {
        self.header.set_mode(mode);
        self
    }

    pub fn uid(mut self, uid: u64) -> Self {
        self.header.set_uid(uid);
        self
    }

    pub fn gid(mut self, gid: u64) -> Self {
        self.header.set_gid(gid);
        self
    }
}