|
| 1 | +//! Running container with an installed binary |
| 2 | +
|
| 3 | +use std::path::Path; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +use testcontainers::{ContainerAsync, GenericImage}; |
| 7 | + |
| 8 | +use super::container_id::ContainerId; |
| 9 | + |
| 10 | +/// A running Ubuntu container with a binary installed and ready to execute |
| 11 | +/// |
| 12 | +/// This struct provides methods for executing commands and managing a running |
| 13 | +/// Ubuntu container that has been prepared with a binary. It handles the container |
| 14 | +/// lifecycle, ensuring the container stays alive while tests run, and provides |
| 15 | +/// convenient methods for command execution and file operations. |
| 16 | +pub struct RunningBinaryContainer { |
| 17 | + // Keep a reference to the container so it stays alive |
| 18 | + #[allow(dead_code)] |
| 19 | + container: ContainerAsync<GenericImage>, |
| 20 | + container_id: ContainerId, |
| 21 | +} |
| 22 | + |
| 23 | +impl RunningBinaryContainer { |
| 24 | + /// Create a new running binary container |
| 25 | + /// |
| 26 | + /// # Arguments |
| 27 | + /// |
| 28 | + /// * `container` - The running Docker container |
| 29 | + /// * `container_id` - The validated container ID |
| 30 | + pub(super) fn new(container: ContainerAsync<GenericImage>, container_id: ContainerId) -> Self { |
| 31 | + Self { |
| 32 | + container, |
| 33 | + container_id, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Execute a command in the container and return stdout |
| 38 | + /// |
| 39 | + /// # Arguments |
| 40 | + /// |
| 41 | + /// * `command` - Command and arguments to execute |
| 42 | + /// |
| 43 | + /// # Returns |
| 44 | + /// |
| 45 | + /// The combined stdout and stderr output as a string |
| 46 | + /// |
| 47 | + /// # Note |
| 48 | + /// |
| 49 | + /// The output combines stderr and stdout because the CLI uses tracing which writes |
| 50 | + /// logs to stderr, while user-facing messages go to stdout. We need both for |
| 51 | + /// comprehensive test assertions. Stderr is placed first to maintain chronological |
| 52 | + /// order of log messages relative to output. |
| 53 | + pub fn exec(&self, command: &[&str]) -> String { |
| 54 | + let output = Command::new("docker") |
| 55 | + .arg("exec") |
| 56 | + .arg(&self.container_id) |
| 57 | + .args(command) |
| 58 | + .output() |
| 59 | + .expect("Failed to execute docker exec command"); |
| 60 | + |
| 61 | + // Combine stderr (logs) and stdout (user messages) to capture all output |
| 62 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 63 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 64 | + format!("{stderr}{stdout}") |
| 65 | + } |
| 66 | + |
| 67 | + /// Execute a command and return the exit code |
| 68 | + /// |
| 69 | + /// # Arguments |
| 70 | + /// |
| 71 | + /// * `command` - Command and arguments to execute |
| 72 | + /// |
| 73 | + /// # Returns |
| 74 | + /// |
| 75 | + /// The exit code of the command, or 1 if the process was terminated by a signal |
| 76 | + /// |
| 77 | + /// # Note |
| 78 | + /// |
| 79 | + /// If the process was terminated by a signal (returns None from `code()`), we return 1 |
| 80 | + /// to indicate failure rather than 0, which would incorrectly suggest success. |
| 81 | + pub fn exec_with_exit_code(&self, command: &[&str]) -> i32 { |
| 82 | + let status = Command::new("docker") |
| 83 | + .arg("exec") |
| 84 | + .arg(&self.container_id) |
| 85 | + .args(command) |
| 86 | + .status() |
| 87 | + .expect("Failed to execute docker exec command"); |
| 88 | + |
| 89 | + // Return 1 (failure) if terminated by signal, otherwise use actual exit code |
| 90 | + status.code().unwrap_or(1) |
| 91 | + } |
| 92 | + |
| 93 | + /// Copy a file from the host into this running container |
| 94 | + /// |
| 95 | + /// This method uses Docker CLI to copy files into the running container. |
| 96 | + /// |
| 97 | + /// # Arguments |
| 98 | + /// |
| 99 | + /// * `source_path` - Path to the file on the host system |
| 100 | + /// * `dest_path` - Destination path inside the container |
| 101 | + /// |
| 102 | + /// # Panics |
| 103 | + /// |
| 104 | + /// Panics if the Docker copy command fails |
| 105 | + pub(super) fn copy_file_to_container(&self, source_path: &Path, dest_path: &str) { |
| 106 | + let output = Command::new("docker") |
| 107 | + .arg("cp") |
| 108 | + .arg(source_path) |
| 109 | + .arg(format!("{}:{dest_path}", self.container_id)) |
| 110 | + .output() |
| 111 | + .expect("Failed to execute docker cp command"); |
| 112 | + |
| 113 | + if !output.status.success() { |
| 114 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 115 | + panic!("Failed to copy file to container: {stderr}"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments