|
| 1 | +//! End-to-end tests for `stellar container start|stop|logs`. |
| 2 | +//! |
| 3 | +//! These never touch a real container runtime. Instead they drop a fake `docker` |
| 4 | +//! / `container` executable into a temp dir, point `PATH` at it, and assert on |
| 5 | +//! both the CLI's behavior and the exact argv the fake was invoked with. This |
| 6 | +//! exercises the real spawn path and stderr classification that the unit tests in |
| 7 | +//! `shared.rs` can't reach. |
| 8 | +
|
| 9 | +use std::fs; |
| 10 | +use std::os::unix::fs::PermissionsExt; |
| 11 | +use std::path::PathBuf; |
| 12 | + |
| 13 | +use assert_cmd::Command; |
| 14 | +use predicates::prelude::predicate; |
| 15 | +use soroban_test::TestEnv; |
| 16 | + |
| 17 | +/// A sandbox with an isolated `PATH` that only contains the fake engines we |
| 18 | +/// explicitly install, so the real `docker`/`container` on the host is never hit. |
| 19 | +struct EngineSandbox { |
| 20 | + env: TestEnv, |
| 21 | + bin_dir: PathBuf, |
| 22 | + log: PathBuf, |
| 23 | +} |
| 24 | + |
| 25 | +impl EngineSandbox { |
| 26 | + fn new() -> Self { |
| 27 | + let env = TestEnv::default(); |
| 28 | + let bin_dir = env.dir().join("fake-bin"); |
| 29 | + fs::create_dir_all(&bin_dir).unwrap(); |
| 30 | + let log = env.dir().join("engine-invocations.log"); |
| 31 | + Self { env, bin_dir, log } |
| 32 | + } |
| 33 | + |
| 34 | + /// Installs a fake engine binary named `name` (e.g. `docker` or `container`). |
| 35 | + /// Each invocation appends its argv to the shared log; behavior is driven by |
| 36 | + /// the `FAKE_ENGINE_MODE` env var (`ok`, `already_running`, `not_found`). The |
| 37 | + /// stderr wording matches the real engine the binary is impersonating so the |
| 38 | + /// CLI's per-engine classifiers are exercised end-to-end. |
| 39 | + fn install_engine(&self, name: &str) { |
| 40 | + // Real-world stderr strings the CLI classifies, per engine. |
| 41 | + let (already_running, not_found) = if name == "container" { |
| 42 | + ( |
| 43 | + "Error: container with id stellar-local already exists", |
| 44 | + r#"Error: internalError: "failed to stop container" (cause: "notFound: "container with ID stellar-local not found"")"#, |
| 45 | + ) |
| 46 | + } else { |
| 47 | + ( |
| 48 | + r#"docker: Error response from daemon: Conflict. The container name "/stellar-local" is already in use."#, |
| 49 | + "Error response from daemon: No such container: stellar-local", |
| 50 | + ) |
| 51 | + }; |
| 52 | + let script = format!( |
| 53 | + r#"#!/bin/sh |
| 54 | +echo "{name} $@" >> "{log}" |
| 55 | +case " $* " in |
| 56 | + *" pull "*) exit 0 ;; |
| 57 | + *" run "*) |
| 58 | + case "$FAKE_ENGINE_MODE" in |
| 59 | + already_running) |
| 60 | + echo '{already_running}' >&2 |
| 61 | + exit 1 ;; |
| 62 | + *) exit 0 ;; |
| 63 | + esac ;; |
| 64 | + *" stop "*) |
| 65 | + case "$FAKE_ENGINE_MODE" in |
| 66 | + not_found) |
| 67 | + echo '{not_found}' >&2 |
| 68 | + exit 1 ;; |
| 69 | + *) exit 0 ;; |
| 70 | + esac ;; |
| 71 | + *) exit 0 ;; |
| 72 | +esac |
| 73 | +"#, |
| 74 | + name = name, |
| 75 | + log = self.log.display(), |
| 76 | + ); |
| 77 | + let path = self.bin_dir.join(name); |
| 78 | + fs::write(&path, script).unwrap(); |
| 79 | + fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); |
| 80 | + } |
| 81 | + |
| 82 | + /// Builds a `container` subcommand whose `PATH` is only the fake bin dir. |
| 83 | + fn cmd(&self, mode: &str) -> Command { |
| 84 | + let mut cmd = self.env.new_assert_cmd("container"); |
| 85 | + cmd.env("PATH", &self.bin_dir) |
| 86 | + .env("FAKE_ENGINE_MODE", mode) |
| 87 | + // Don't inherit a real DOCKER_HOST/engine from the developer's shell. |
| 88 | + .env_remove("DOCKER_HOST") |
| 89 | + .env_remove("STELLAR_CONTAINER_ENGINE"); |
| 90 | + cmd |
| 91 | + } |
| 92 | + |
| 93 | + fn invocations(&self) -> String { |
| 94 | + fs::read_to_string(&self.log).unwrap_or_default() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn line_for(log: &str, needle: &str) -> String { |
| 99 | + log.lines() |
| 100 | + .find(|l| l.contains(needle)) |
| 101 | + .unwrap_or_else(|| panic!("no invocation containing {needle:?} in:\n{log}")) |
| 102 | + .to_string() |
| 103 | +} |
| 104 | + |
| 105 | +#[test] |
| 106 | +fn start_defaults_to_docker_and_passes_expected_args() { |
| 107 | + let s = EngineSandbox::new(); |
| 108 | + s.install_engine("docker"); |
| 109 | + |
| 110 | + s.cmd("ok") |
| 111 | + .args(["start", "local"]) |
| 112 | + .assert() |
| 113 | + .success() |
| 114 | + .stderr(predicate::str::contains("Started container")); |
| 115 | + |
| 116 | + let log = s.invocations(); |
| 117 | + // Pulled and ran via docker (not the apple `image pull` form). |
| 118 | + assert!(line_for(&log, "pull").starts_with("docker pull ")); |
| 119 | + let run = line_for(&log, " run "); |
| 120 | + assert!(run.starts_with("docker "), "expected docker binary: {run}"); |
| 121 | + assert!(run.contains("--name stellar-local"), "run line: {run}"); |
| 122 | + assert!(run.contains("-p 8000:8000"), "run line: {run}"); |
| 123 | + // No host override unless requested. |
| 124 | + assert!(!run.contains("-H "), "unexpected -H: {run}"); |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn start_passes_docker_host_as_h_flag() { |
| 129 | + let s = EngineSandbox::new(); |
| 130 | + s.install_engine("docker"); |
| 131 | + |
| 132 | + s.cmd("ok") |
| 133 | + .args(["start", "local", "--docker-host", "ssh://me@host"]) |
| 134 | + .assert() |
| 135 | + .success(); |
| 136 | + |
| 137 | + let run = line_for(&s.invocations(), " run "); |
| 138 | + assert!(run.contains("-H ssh://me@host"), "run line: {run}"); |
| 139 | +} |
| 140 | + |
| 141 | +#[test] |
| 142 | +fn start_reports_already_running_from_stderr() { |
| 143 | + let s = EngineSandbox::new(); |
| 144 | + s.install_engine("docker"); |
| 145 | + |
| 146 | + s.cmd("already_running") |
| 147 | + .args(["start", "local"]) |
| 148 | + .assert() |
| 149 | + .failure() |
| 150 | + .stderr(predicate::str::contains("already running")); |
| 151 | +} |
| 152 | + |
| 153 | +#[test] |
| 154 | +fn stop_success() { |
| 155 | + let s = EngineSandbox::new(); |
| 156 | + s.install_engine("docker"); |
| 157 | + |
| 158 | + s.cmd("ok") |
| 159 | + .args(["stop", "local"]) |
| 160 | + .assert() |
| 161 | + .success() |
| 162 | + .stderr(predicate::str::contains("Container stopped")); |
| 163 | + |
| 164 | + assert!(line_for(&s.invocations(), " stop ").contains("stop stellar-local")); |
| 165 | +} |
| 166 | + |
| 167 | +#[test] |
| 168 | +fn stop_reports_not_found_from_stderr() { |
| 169 | + let s = EngineSandbox::new(); |
| 170 | + s.install_engine("docker"); |
| 171 | + |
| 172 | + s.cmd("not_found") |
| 173 | + .args(["stop", "local"]) |
| 174 | + .assert() |
| 175 | + .failure() |
| 176 | + .stderr(predicate::str::contains("container local not found")); |
| 177 | +} |
| 178 | + |
| 179 | +#[test] |
| 180 | +fn apple_engine_uses_container_binary_and_image_pull() { |
| 181 | + let s = EngineSandbox::new(); |
| 182 | + // Only the apple engine exists on PATH; if the CLI shelled out to docker it |
| 183 | + // would fail with a not-found error instead. |
| 184 | + s.install_engine("container"); |
| 185 | + |
| 186 | + s.cmd("ok") |
| 187 | + .args(["start", "local", "--engine", "apple-container"]) |
| 188 | + .assert() |
| 189 | + .success(); |
| 190 | + |
| 191 | + let log = s.invocations(); |
| 192 | + // Apple groups image ops under `image` (singular) and takes no `--tail`. |
| 193 | + assert!(line_for(&log, "pull").starts_with("container image pull ")); |
| 194 | + let run = line_for(&log, " run "); |
| 195 | + assert!( |
| 196 | + run.starts_with("container "), |
| 197 | + "expected container binary: {run}" |
| 198 | + ); |
| 199 | +} |
| 200 | + |
| 201 | +#[test] |
| 202 | +fn apple_engine_reports_already_running_from_stderr() { |
| 203 | + let s = EngineSandbox::new(); |
| 204 | + s.install_engine("container"); |
| 205 | + |
| 206 | + // The fake `container` emits Apple's `already exists` wording, so this only |
| 207 | + // passes if the CLI classifies stderr with the apple-container matcher. |
| 208 | + s.cmd("already_running") |
| 209 | + .args(["start", "local", "--engine", "apple-container"]) |
| 210 | + .assert() |
| 211 | + .failure() |
| 212 | + .stderr(predicate::str::contains("already running")); |
| 213 | +} |
| 214 | + |
| 215 | +#[test] |
| 216 | +fn apple_engine_reports_not_found_from_stderr() { |
| 217 | + let s = EngineSandbox::new(); |
| 218 | + s.install_engine("container"); |
| 219 | + |
| 220 | + // The fake `container` emits Apple's `not found` wording here. |
| 221 | + s.cmd("not_found") |
| 222 | + .args(["stop", "local", "--engine", "apple-container"]) |
| 223 | + .assert() |
| 224 | + .failure() |
| 225 | + .stderr(predicate::str::contains("container local not found")); |
| 226 | +} |
| 227 | + |
| 228 | +#[test] |
| 229 | +fn apple_engine_selectable_via_env_var() { |
| 230 | + let s = EngineSandbox::new(); |
| 231 | + s.install_engine("container"); |
| 232 | + |
| 233 | + s.cmd("ok") |
| 234 | + .env("STELLAR_CONTAINER_ENGINE", "apple-container") |
| 235 | + .args(["start", "local"]) |
| 236 | + .assert() |
| 237 | + .success(); |
| 238 | + |
| 239 | + assert!(line_for(&s.invocations(), " run ").starts_with("container ")); |
| 240 | +} |
| 241 | + |
| 242 | +#[test] |
| 243 | +fn warns_and_drops_docker_host_for_apple_engine() { |
| 244 | + let s = EngineSandbox::new(); |
| 245 | + s.install_engine("container"); |
| 246 | + |
| 247 | + s.cmd("ok") |
| 248 | + .args([ |
| 249 | + "start", |
| 250 | + "local", |
| 251 | + "--engine", |
| 252 | + "apple-container", |
| 253 | + "--docker-host", |
| 254 | + "ssh://me@host", |
| 255 | + ]) |
| 256 | + .assert() |
| 257 | + .success() |
| 258 | + .stderr(predicate::str::contains( |
| 259 | + "is ignored because the `apple-container` engine", |
| 260 | + )); |
| 261 | + |
| 262 | + // The ignored host must not reach the container binary. |
| 263 | + assert!( |
| 264 | + !s.invocations().contains("-H "), |
| 265 | + "host leaked to container CLI" |
| 266 | + ); |
| 267 | +} |
| 268 | + |
| 269 | +#[test] |
| 270 | +fn errors_when_engine_binary_is_missing() { |
| 271 | + // No engine installed on the isolated PATH. |
| 272 | + let s = EngineSandbox::new(); |
| 273 | + |
| 274 | + s.cmd("ok") |
| 275 | + .args(["stop", "local"]) |
| 276 | + .assert() |
| 277 | + .failure() |
| 278 | + .stderr(predicate::str::contains( |
| 279 | + "is docker installed and on your PATH?", |
| 280 | + )); |
| 281 | +} |