-
Notifications
You must be signed in to change notification settings - Fork 136
Support selecting the container engine (docker or Apple Container) #2643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fnando
wants to merge
5
commits into
main
Choose a base branch
from
container-engine-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ada53fc
Add --engine flag to pick docker or Apple Container.
fnando 2d81c85
Cover Apple container error messages in tests.
fnando 82c5cd8
Merge branch 'main' into container-engine-selection
fnando 4bed983
Merge branch 'main' into container-engine-selection
fnando 3658ab1
Merge branch 'main' into container-engine-selection
fnando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| //! End-to-end tests for `stellar container start|stop|logs`. | ||
| //! | ||
| //! These never touch a real container runtime. Instead they drop a fake `docker` | ||
| //! / `container` executable into a temp dir, point `PATH` at it, and assert on | ||
| //! both the CLI's behavior and the exact argv the fake was invoked with. This | ||
| //! exercises the real spawn path and stderr classification that the unit tests in | ||
| //! `shared.rs` can't reach. | ||
|
|
||
| use std::fs; | ||
| use std::os::unix::fs::PermissionsExt; | ||
| use std::path::PathBuf; | ||
|
|
||
| use assert_cmd::Command; | ||
| use predicates::prelude::predicate; | ||
| use soroban_test::TestEnv; | ||
|
|
||
| /// A sandbox with an isolated `PATH` that only contains the fake engines we | ||
| /// explicitly install, so the real `docker`/`container` on the host is never hit. | ||
| struct EngineSandbox { | ||
| env: TestEnv, | ||
| bin_dir: PathBuf, | ||
| log: PathBuf, | ||
| } | ||
|
|
||
| impl EngineSandbox { | ||
| fn new() -> Self { | ||
| let env = TestEnv::default(); | ||
| let bin_dir = env.dir().join("fake-bin"); | ||
| fs::create_dir_all(&bin_dir).unwrap(); | ||
| let log = env.dir().join("engine-invocations.log"); | ||
| Self { env, bin_dir, log } | ||
| } | ||
|
|
||
| /// Installs a fake engine binary named `name` (e.g. `docker` or `container`). | ||
| /// Each invocation appends its argv to the shared log; behavior is driven by | ||
| /// the `FAKE_ENGINE_MODE` env var (`ok`, `already_running`, `not_found`). The | ||
| /// stderr wording matches the real engine the binary is impersonating so the | ||
| /// CLI's per-engine classifiers are exercised end-to-end. | ||
| fn install_engine(&self, name: &str) { | ||
| // Real-world stderr strings the CLI classifies, per engine. | ||
| let (already_running, not_found) = if name == "container" { | ||
| ( | ||
| "Error: container with id stellar-local already exists", | ||
| r#"Error: internalError: "failed to stop container" (cause: "notFound: "container with ID stellar-local not found"")"#, | ||
| ) | ||
| } else { | ||
| ( | ||
| r#"docker: Error response from daemon: Conflict. The container name "/stellar-local" is already in use."#, | ||
| "Error response from daemon: No such container: stellar-local", | ||
| ) | ||
| }; | ||
| let script = format!( | ||
| r#"#!/bin/sh | ||
| echo "{name} $@" >> "{log}" | ||
| case " $* " in | ||
| *" pull "*) exit 0 ;; | ||
| *" run "*) | ||
| case "$FAKE_ENGINE_MODE" in | ||
| already_running) | ||
| echo '{already_running}' >&2 | ||
| exit 1 ;; | ||
| *) exit 0 ;; | ||
| esac ;; | ||
| *" stop "*) | ||
| case "$FAKE_ENGINE_MODE" in | ||
| not_found) | ||
| echo '{not_found}' >&2 | ||
| exit 1 ;; | ||
| *) exit 0 ;; | ||
| esac ;; | ||
| *) exit 0 ;; | ||
| esac | ||
| "#, | ||
| name = name, | ||
| log = self.log.display(), | ||
| ); | ||
| let path = self.bin_dir.join(name); | ||
| fs::write(&path, script).unwrap(); | ||
| fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); | ||
| } | ||
|
|
||
| /// Builds a `container` subcommand whose `PATH` is only the fake bin dir. | ||
| fn cmd(&self, mode: &str) -> Command { | ||
| let mut cmd = self.env.new_assert_cmd("container"); | ||
| cmd.env("PATH", &self.bin_dir) | ||
| .env("FAKE_ENGINE_MODE", mode) | ||
| // Don't inherit a real DOCKER_HOST/engine from the developer's shell. | ||
| .env_remove("DOCKER_HOST") | ||
| .env_remove("STELLAR_CONTAINER_ENGINE"); | ||
| cmd | ||
| } | ||
|
|
||
| fn invocations(&self) -> String { | ||
| fs::read_to_string(&self.log).unwrap_or_default() | ||
| } | ||
| } | ||
|
|
||
| fn line_for(log: &str, needle: &str) -> String { | ||
| log.lines() | ||
| .find(|l| l.contains(needle)) | ||
| .unwrap_or_else(|| panic!("no invocation containing {needle:?} in:\n{log}")) | ||
| .to_string() | ||
| } | ||
|
|
||
| #[test] | ||
| fn start_defaults_to_docker_and_passes_expected_args() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("docker"); | ||
|
|
||
| s.cmd("ok") | ||
| .args(["start", "local"]) | ||
| .assert() | ||
| .success() | ||
| .stderr(predicate::str::contains("Started container")); | ||
|
|
||
| let log = s.invocations(); | ||
| // Pulled and ran via docker (not the apple `image pull` form). | ||
| assert!(line_for(&log, "pull").starts_with("docker pull ")); | ||
| let run = line_for(&log, " run "); | ||
| assert!(run.starts_with("docker "), "expected docker binary: {run}"); | ||
| assert!(run.contains("--name stellar-local"), "run line: {run}"); | ||
| assert!(run.contains("-p 8000:8000"), "run line: {run}"); | ||
| // No host override unless requested. | ||
| assert!(!run.contains("-H "), "unexpected -H: {run}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn start_passes_docker_host_as_h_flag() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("docker"); | ||
|
|
||
| s.cmd("ok") | ||
| .args(["start", "local", "--docker-host", "ssh://me@host"]) | ||
| .assert() | ||
| .success(); | ||
|
|
||
| let run = line_for(&s.invocations(), " run "); | ||
| assert!(run.contains("-H ssh://me@host"), "run line: {run}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn start_reports_already_running_from_stderr() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("docker"); | ||
|
|
||
| s.cmd("already_running") | ||
| .args(["start", "local"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicate::str::contains("already running")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn stop_success() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("docker"); | ||
|
|
||
| s.cmd("ok") | ||
| .args(["stop", "local"]) | ||
| .assert() | ||
| .success() | ||
| .stderr(predicate::str::contains("Container stopped")); | ||
|
|
||
| assert!(line_for(&s.invocations(), " stop ").contains("stop stellar-local")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn stop_reports_not_found_from_stderr() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("docker"); | ||
|
|
||
| s.cmd("not_found") | ||
| .args(["stop", "local"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicate::str::contains("container local not found")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn apple_engine_uses_container_binary_and_image_pull() { | ||
| let s = EngineSandbox::new(); | ||
| // Only the apple engine exists on PATH; if the CLI shelled out to docker it | ||
| // would fail with a not-found error instead. | ||
| s.install_engine("container"); | ||
|
|
||
| s.cmd("ok") | ||
| .args(["start", "local", "--engine", "apple-container"]) | ||
| .assert() | ||
| .success(); | ||
|
|
||
| let log = s.invocations(); | ||
| // Apple groups image ops under `image` (singular) and takes no `--tail`. | ||
| assert!(line_for(&log, "pull").starts_with("container image pull ")); | ||
| let run = line_for(&log, " run "); | ||
| assert!( | ||
| run.starts_with("container "), | ||
| "expected container binary: {run}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn apple_engine_reports_already_running_from_stderr() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("container"); | ||
|
|
||
| // The fake `container` emits Apple's `already exists` wording, so this only | ||
| // passes if the CLI classifies stderr with the apple-container matcher. | ||
| s.cmd("already_running") | ||
| .args(["start", "local", "--engine", "apple-container"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicate::str::contains("already running")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn apple_engine_reports_not_found_from_stderr() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("container"); | ||
|
|
||
| // The fake `container` emits Apple's `not found` wording here. | ||
| s.cmd("not_found") | ||
| .args(["stop", "local", "--engine", "apple-container"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicate::str::contains("container local not found")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn apple_engine_selectable_via_env_var() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("container"); | ||
|
|
||
| s.cmd("ok") | ||
| .env("STELLAR_CONTAINER_ENGINE", "apple-container") | ||
| .args(["start", "local"]) | ||
| .assert() | ||
| .success(); | ||
|
|
||
| assert!(line_for(&s.invocations(), " run ").starts_with("container ")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn warns_and_drops_docker_host_for_apple_engine() { | ||
| let s = EngineSandbox::new(); | ||
| s.install_engine("container"); | ||
|
|
||
| s.cmd("ok") | ||
| .args([ | ||
| "start", | ||
| "local", | ||
| "--engine", | ||
| "apple-container", | ||
| "--docker-host", | ||
| "ssh://me@host", | ||
| ]) | ||
| .assert() | ||
| .success() | ||
| .stderr(predicate::str::contains( | ||
| "is ignored because the `apple-container` engine", | ||
| )); | ||
|
|
||
| // The ignored host must not reach the container binary. | ||
| assert!( | ||
| !s.invocations().contains("-H "), | ||
| "host leaked to container CLI" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn errors_when_engine_binary_is_missing() { | ||
| // No engine installed on the isolated PATH. | ||
| let s = EngineSandbox::new(); | ||
|
|
||
| s.cmd("ok") | ||
| .args(["stop", "local"]) | ||
| .assert() | ||
| .failure() | ||
| .stderr(predicate::str::contains( | ||
| "is docker installed and on your PATH?", | ||
| )); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| mod build; | ||
| mod config; | ||
| #[cfg(unix)] | ||
| mod container; | ||
| #[cfg(feature = "emulator-tests")] | ||
| mod emulator; | ||
| mod help; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.