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
use crate::log::LogOutput;
use crate::{container_port_mapping, util};
use crate::{log, TestContext};
use bollard::container::{
    Config, CreateContainerOptions, RemoveContainerOptions, StartContainerOptions,
};
use bollard::exec::{CreateExecOptions, StartExecResults};
use serde::Serialize;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::time::{SystemTime, UNIX_EPOCH};

pub struct PrepareContainerContext<'a> {
    test_context: &'a TestContext<'a>,
    exposed_ports: Vec<u16>,
    env: HashMap<String, String>,
}

impl<'a> PrepareContainerContext<'a> {
    pub(crate) fn new(test_context: &'a TestContext) -> Self {
        Self {
            test_context,
            exposed_ports: Vec::new(),
            env: HashMap::new(),
        }
    }

    /// Exposes a given port of the container to the host machine.
    ///
    /// The given port is mapped to a random port on the host machine. Use
    /// [`ContainerContext::address_for_port`] to obtain the local port for a mapped port.
    pub fn expose_port(&mut self, port: u16) -> &mut Self {
        self.exposed_ports.push(port);
        self
    }

    /// Inserts or updates an environment variable mapping for the container.
    ///
    /// # Example
    /// ```no_run
    /// use libcnb_test::{TestConfig, TestRunner};
    ///
    /// TestRunner::default().run_test(
    ///     TestConfig::new("heroku/builder:22", "test-fixtures/app"),
    ///     |context| {
    ///         context
    ///             .prepare_container()
    ///             .envs(vec![("FOO", "FOO_VALUE"), ("BAR", "BAR_VALUE")])
    ///             .start_with_default_process(|container| {
    ///                 // ...
    ///             })
    ///     },
    /// );
    /// ```
    pub fn env(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
        self.env.insert(key.into(), value.into());
        self
    }

    /// Adds or updates multiple environment variable mappings for the container.
    ///
    /// # Example
    /// ```no_run
    /// use libcnb_test::{TestConfig, TestRunner};
    ///
    /// TestRunner::default().run_test(
    ///     TestConfig::new("heroku/builder:22", "test-fixtures/app"),
    ///     |context| {
    ///         context
    ///             .prepare_container()
    ///             .envs(vec![("FOO", "FOO_VALUE"), ("BAR", "BAR_VALUE")])
    ///             .start_with_default_process(|container| {
    ///                 // ...
    ///             })
    ///     },
    /// );
    /// ```
    pub fn envs<K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>>(
        &mut self,
        envs: I,
    ) -> &mut Self {
        envs.into_iter().for_each(|(key, value)| {
            self.env(key.into(), value.into());
        });

        self
    }

    /// Creates and starts the container configured by this context using the image's default
    /// CNB process.
    ///
    /// See: [CNB App Developer Guide: Run a multi-process app - Default process type](https://buildpacks.io/docs/app-developer-guide/run-an-app/#default-process-type)
    ///
    /// # Panics
    /// - When the container could not be created
    /// - When the container could not be started
    pub fn start_with_default_process<F: FnOnce(ContainerContext)>(&self, f: F) {
        self.start_internal(None, None, f);
    }

    /// Creates and starts the container configured by this context using the image's default
    /// CNB process and given arguments.
    ///
    /// See: [CNB App Developer Guide: Run a multi-process app - Default process type with additional arguments](https://buildpacks.io/docs/app-developer-guide/run-an-app/#default-process-type-with-additional-arguments)
    ///
    /// # Panics
    /// - When the container could not be created
    /// - When the container could not be started
    pub fn start_with_default_process_args<
        F: FnOnce(ContainerContext),
        A: IntoIterator<Item = I>,
        I: Into<String>,
    >(
        &self,
        args: A,
        f: F,
    ) {
        self.start_internal(None, Some(args.into_iter().map(I::into).collect()), f);
    }

    /// Creates and starts the container configured by this context using the given CNB process.
    ///
    /// See: [CNB App Developer Guide: Run a multi-process app - Non-default process-type](https://buildpacks.io/docs/app-developer-guide/run-an-app/#non-default-process-type)
    ///
    /// # Panics
    /// - When the container could not be created
    /// - When the container could not be started
    pub fn start_with_process<F: FnOnce(ContainerContext), P: Into<String>>(
        &self,
        process: P,
        f: F,
    ) {
        self.start_internal(Some(vec![process.into()]), None, f);
    }

    /// Creates and starts the container configured by this context using the given CNB process
    /// and arguments.
    ///
    /// See: [CNB App Developer Guide: Run a multi-process app - Non-default process-type with additional arguments](https://buildpacks.io/docs/app-developer-guide/run-an-app/#non-default-process-type-with-additional-arguments)
    ///
    /// # Panics
    /// - When the container could not be created
    /// - When the container could not be started
    pub fn start_with_process_args<
        F: FnOnce(ContainerContext),
        A: IntoIterator<Item = I>,
        I: Into<String>,
        P: Into<String>,
    >(
        &self,
        process: P,
        args: A,
        f: F,
    ) {
        self.start_internal(
            Some(vec![process.into()]),
            Some(args.into_iter().map(I::into).collect()),
            f,
        );
    }

    /// Creates and starts the container configured by this context using the given shell command.
    ///
    /// The CNB lifecycle launcher will be implicitly used. Environment variables will be set. Uses
    /// `/bin/sh` as the shell.
    ///
    /// See: [CNB App Developer Guide: Run a multi-process app - User-provided shell process](https://buildpacks.io/docs/app-developer-guide/run-an-app/#user-provided-shell-process)
    ///
    /// # Panics
    /// - When the container could not be created
    /// - When the container could not be started
    pub fn start_with_shell_command<F: FnOnce(ContainerContext), C: Into<String>>(
        &self,
        command: C,
        f: F,
    ) {
        self.start_internal(
            Some(vec![String::from(CNB_LAUNCHER_PATH)]),
            Some(vec![
                String::from(SHELL_PATH),
                String::from("-c"),
                command.into(),
            ]),
            f,
        );
    }

    fn start_internal<F: FnOnce(ContainerContext)>(
        &self,
        entrypoint: Option<Vec<String>>,
        cmd: Option<Vec<String>>,
        f: F,
    ) {
        let container_name = util::random_docker_identifier();

        self.test_context.runner.tokio_runtime.block_on(async {
            self.test_context
                .runner
                .docker
                .create_container(
                    Some(CreateContainerOptions {
                        name: container_name.clone(),
                    }),
                    Config {
                        image: Some(self.test_context.image_name.clone()),
                        env: Some(self.env.iter().map(|(k, v)| format!("{k}={v}")).collect()),
                        entrypoint,
                        cmd,
                        ..container_port_mapping::port_mapped_container_config(&self.exposed_ports)
                    },
                )
                .await
                .expect("Could not create container");

            self.test_context
                .runner
                .docker
                .start_container(&container_name, None::<StartContainerOptions<String>>)
                .await
                .expect("Could not start container");
        });

        f(ContainerContext {
            container_name,
            test_context: self.test_context,
        });
    }
}

pub struct ContainerContext<'a> {
    pub container_name: String,
    pub(crate) test_context: &'a TestContext<'a>,
}

impl<'a> ContainerContext<'a> {
    /// Gets the container's log output until the current point in time.
    ///
    /// Note: This method will only return logs until the current point in time. It will not
    /// block until the container stops. Since the output of this method depends on timing, directly
    /// asserting on its contents might result in flaky tests.
    ///
    /// See: [`logs_wait`](Self::logs_wait) for a blocking alternative.
    ///
    /// # Panics
    /// - When the log output could not be consumed/read.
    #[must_use]
    pub fn logs_now(&self) -> LogOutput {
        // Bollard forces us to cast to i64
        #[allow(clippy::cast_possible_wrap)]
        self.logs_internal(bollard::container::LogsOptions {
            stdout: true,
            stderr: true,
            since: 0,
            until: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("System time is before UNIX epoch")
                .as_secs() as i64,
            tail: "all",
            ..bollard::container::LogsOptions::default()
        })
    }

    /// Gets the container's log output until the container stops.
    ///
    /// Note: This method will block until the container stops. If the container never stops by
    /// itself, your test will hang indefinitely. This is common when the container hosts an HTTP
    /// service.
    ///
    /// See: [`logs_now`](Self::logs_now) for a non-blocking alternative.
    ///
    /// # Panics
    /// - When the log output could not be consumed/read.
    #[must_use]
    pub fn logs_wait(&self) -> LogOutput {
        self.logs_internal(bollard::container::LogsOptions {
            follow: true,
            stdout: true,
            stderr: true,
            tail: "all",
            ..bollard::container::LogsOptions::default()
        })
    }

    #[must_use]
    fn logs_internal<T: Into<String> + Serialize>(
        &self,
        logs_options: bollard::container::LogsOptions<T>,
    ) -> LogOutput {
        self.test_context
            .runner
            .tokio_runtime
            .block_on(log::consume_container_log_output(
                self.test_context
                    .runner
                    .docker
                    .logs(&self.container_name, Some(logs_options)),
            ))
            .expect("Could not consume container log output")
    }

    /// # Panics
    #[must_use]
    pub fn address_for_port(&self, port: u16) -> Option<SocketAddr> {
        self.test_context.runner.tokio_runtime.block_on(async {
            self.test_context
                .runner
                .docker
                .inspect_container(&self.container_name, None)
                .await
                .unwrap()
                .network_settings
                .and_then(|network_settings| network_settings.ports)
                .and_then(|ports| {
                    container_port_mapping::parse_port_map(&ports)
                        .unwrap()
                        .get(&port)
                        .copied()
                })
        })
    }

    /// Executes a shell command inside an already running container.
    ///
    /// # Panics
    pub fn shell_exec(&self, command: impl AsRef<str>) -> LogOutput {
        self.test_context.runner.tokio_runtime.block_on(async {
            let create_exec_result = self
                .test_context
                .runner
                .docker
                .create_exec(
                    &self.container_name,
                    CreateExecOptions {
                        cmd: Some(vec![CNB_LAUNCHER_PATH, SHELL_PATH, "-c", command.as_ref()]),
                        attach_stdout: Some(true),
                        ..CreateExecOptions::default()
                    },
                )
                .await
                .unwrap();

            let start_exec_result = self
                .test_context
                .runner
                .docker
                .start_exec(&create_exec_result.id, None)
                .await
                .unwrap();

            match start_exec_result {
                StartExecResults::Attached { output, .. } => {
                    log::consume_container_log_output(output)
                        .await
                        .expect("Could not consume container log output")
                }
                StartExecResults::Detached => LogOutput::default(),
            }
        })
    }
}

impl<'a> Drop for ContainerContext<'a> {
    fn drop(&mut self) {
        // We do not care if container removal succeeded or not. Panicking here would result in
        // SIGILL since this function might be called in a Tokio runtime.
        let _remove_container_result = self.test_context.runner.tokio_runtime.block_on(
            self.test_context.runner.docker.remove_container(
                &self.container_name,
                Some(RemoveContainerOptions {
                    force: true,
                    ..RemoveContainerOptions::default()
                }),
            ),
        );
    }
}

const CNB_LAUNCHER_PATH: &str = "/cnb/lifecycle/launcher";
const SHELL_PATH: &str = "/bin/sh";