Repository navigation
test(tmachine): prepare runner environments for the e2e/rust migration #5277
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
Merged
+341
−17
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
76953c8
test(tmachine): load the Python workload image and set the k3s host g…
matthewgrossman 283b9ff
test(testkit): add python_image() for the Python workload image
matthewgrossman 8441388
test(runner): honor family nextest config and add --test-threads
matthewgrossman 061ae1e
test(features): check host.openshell.internal from a Python sandbox
matthewgrossman 2183ead
test(tmachine): use the k3s node's IPv4 address for the host gateway
matthewgrossman 64c4a0d
test(features): run the host-gateway client with sandbox exec
matthewgrossman a5fef8b
Revert "test(runner): honor family nextest config and add --test-thre…
matthewgrossman ce41fc0
test(features): use the system Python for the host-gateway client and…
matthewgrossman b2dadcd
refactor(testkit): stop exporting environment variable names
matthewgrossman 8a38b27
Merge branch 'main' into test/5275-migration-prep/matthewgrossman
matthewgrossman 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| [package] | ||
| name = "host-gateway" | ||
| description = "Features suite: sandboxes reach a host server through host.openshell.internal" | ||
| version = "0.0.0" | ||
| edition = "2024" | ||
| rust-version = "1.94" | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/NVIDIA/OpenShell" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| openshell-testkit = { path = "../../../testkit/testkit" } | ||
| tempfile = "3" | ||
| tokio = { version = "1.43", features = ["io-util", "net", "rt"] } |
209 changes: 209 additions & 0 deletions
209
tests/suites/features/host-gateway/tests/host_gateway.rs
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,209 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! `host.openshell.internal` names the host from inside a sandbox, on every | ||
| //! compute driver. | ||
| //! | ||
| //! The test starts an HTTP server where the tests run, which is the host the | ||
| //! gateway runs its sandboxes on, and runs a client in a sandbox from the | ||
| //! Python test image ([`testkit::python_image`]) that requests a page from that | ||
| //! server through the name. | ||
|
|
||
| use testkit::{Context, Error, Result, ResultExt as _}; | ||
| use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; | ||
| use tokio::net::{TcpListener, TcpStream}; | ||
| use tokio::task::JoinHandle; | ||
|
|
||
| /// The image's system Python, which needs nothing beyond its standard library. | ||
| /// The policy names its real path, `/usr/bin/python3.*`, because a rule matches | ||
| /// the executable behind a symlink. | ||
| const PYTHON: &str = "/usr/bin/python3"; | ||
|
|
||
| /// Requests `/` from `host.openshell.internal` on the port in `argv[1]` and | ||
| /// prints the response. A failed connection is retried for 20 seconds, so a | ||
| /// slow start doesn't fail the test, and reported on stderr. | ||
| const CLIENT: &str = r#" | ||
| import socket, sys, time | ||
|
|
||
| host, port = "host.openshell.internal", int(sys.argv[1]) | ||
| deadline = time.monotonic() + 20 | ||
| while True: | ||
| try: | ||
| conn = socket.create_connection((host, port), timeout=10) | ||
| break | ||
| except OSError as error: | ||
| print(f"connect {host}:{port}: {error}", file=sys.stderr, flush=True) | ||
| if time.monotonic() > deadline: | ||
| sys.exit(1) | ||
| time.sleep(1) | ||
| with conn: | ||
| conn.sendall(f"GET / HTTP/1.1\r\nHost: {host}:{port}\r\nConnection: close\r\n\r\n".encode()) | ||
| response = b"" | ||
| while chunk := conn.recv(4096): | ||
| response += chunk | ||
| print(response.decode(errors="replace")) | ||
| "#; | ||
|
|
||
| /// A sandbox from the Python test image resolves `host.openshell.internal` | ||
| /// and gets a response from a server the test runs on the host. The default | ||
| /// policy denies all network access, so the test passes one that lets only the | ||
| /// image's Python reach the server's port. | ||
| #[testkit::test] | ||
| async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result { | ||
| let marker = format!("host-gateway-{}", cx.run_id()); | ||
| let server = HostServer::start(marker.clone()).await?; | ||
| let policy = PolicyFile::new(&policy(server.port))?; | ||
| let port = server.port.to_string(); | ||
| let image = testkit::python_image(); | ||
|
|
||
| // The client runs through `sandbox exec` in a Ready sandbox, not as the | ||
| // main process. A gateway with several replicas (k3s-ha) can refuse to | ||
| // attach to a main process that already finished with "sandbox is not | ||
| // ready" when the request reaches a replica that doesn't hold the | ||
| // sandbox's session. | ||
|
elezar marked this conversation as resolved.
Resolved
|
||
| let name = cx.name("hg"); | ||
| cx.track(&name); | ||
| cx.cli([ | ||
| "sandbox", | ||
| "create", | ||
| "--name", | ||
| &name, | ||
| "--from", | ||
| &image, | ||
| "--policy", | ||
| policy.path()?, | ||
| "--detach", | ||
| "--no-tty", | ||
| "--", | ||
| "sh", | ||
| "-c", | ||
| "exec sleep infinity", | ||
| ]) | ||
| .await? | ||
| .success() | ||
| .context(format!("create a sandbox from {image}"))?; | ||
| cx.adopt(&name).await?.wait_ready().await?; | ||
|
|
||
| let client = cx | ||
| .cli([ | ||
| "sandbox", "exec", "--name", &name, "--no-tty", "--", PYTHON, "-c", CLIENT, &port, | ||
| ]) | ||
| .await? | ||
| .success() | ||
| .context("run the client in the sandbox")?; | ||
| testkit::ensure!( | ||
| client.stdout.contains(&marker), | ||
| "the client did not get the host server's response:\n{}{}", | ||
| client.stdout, | ||
| client.stderr | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// A sandbox policy that allows the image's system Python ([`PYTHON`]) to | ||
| /// connect to `host.openshell.internal` on `port`. The endpoint names only the | ||
| /// host and port, as the policy docs describe, so the driver's address for the | ||
| /// name is what makes it reachable, not an `allowed_ips` range. | ||
| fn policy(port: u16) -> String { | ||
| format!( | ||
| r#"version: 1 | ||
|
|
||
| filesystem_policy: | ||
| include_workdir: true | ||
| read_only: [/usr, /lib, /lib64, /proc, /dev/urandom, /etc] | ||
| read_write: [/sandbox, /tmp, /dev/null] | ||
| landlock: | ||
| compatibility: best_effort | ||
|
|
||
| process: | ||
| run_as_user: sandbox | ||
| run_as_group: sandbox | ||
|
|
||
| network_policies: | ||
| host_server: | ||
| name: host_server | ||
| endpoints: | ||
| - host: host.openshell.internal | ||
| port: {port} | ||
| binaries: | ||
| - path: /usr/bin/python3.* | ||
| "# | ||
| ) | ||
| } | ||
|
|
||
| /// An HTTP server on the host that answers every request with `body`. It | ||
| /// stops when dropped. | ||
| struct HostServer { | ||
| port: u16, | ||
| task: JoinHandle<()>, | ||
| } | ||
|
|
||
| impl HostServer { | ||
| /// Listen on every host address, since drivers map | ||
| /// `host.openshell.internal` to different ones. | ||
| async fn start(body: String) -> Result<Self> { | ||
| let listener = TcpListener::bind(("0.0.0.0", 0)) | ||
| .await | ||
| .map_err(|error| Error::from_error(&error)) | ||
| .context("bind the host server")?; | ||
| let port = listener | ||
| .local_addr() | ||
| .map_err(|error| Error::from_error(&error)) | ||
| .context("read the host server's address")? | ||
| .port(); | ||
| let task = tokio::spawn(async move { | ||
| while let Ok((stream, _)) = listener.accept().await { | ||
| tokio::spawn(respond(stream, body.clone())); | ||
| } | ||
| }); | ||
| Ok(Self { port, task }) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for HostServer { | ||
| fn drop(&mut self) { | ||
| self.task.abort(); | ||
| } | ||
| } | ||
|
|
||
| /// Read one request's headers from `stream` and answer with `body`. | ||
| async fn respond(mut stream: TcpStream, body: String) { | ||
| let _ = stream.set_nodelay(true); | ||
| let mut request = Vec::new(); | ||
| let mut buf = [0_u8; 1024]; | ||
| while !request.windows(4).any(|window| window == b"\r\n\r\n") { | ||
| match stream.read(&mut buf).await { | ||
| Ok(read) if read > 0 && request.len() < 64 * 1024 => { | ||
| request.extend_from_slice(&buf[..read]); | ||
| } | ||
| _ => return, | ||
| } | ||
| } | ||
| let response = format!( | ||
| "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", | ||
| body.len() | ||
| ); | ||
| let _ = stream.write_all(response.as_bytes()).await; | ||
| let _ = stream.shutdown().await; | ||
| } | ||
|
|
||
| /// A sandbox policy written to a temporary file for `sandbox create --policy`. | ||
| struct PolicyFile(tempfile::NamedTempFile); | ||
|
|
||
| impl PolicyFile { | ||
| fn new(contents: &str) -> Result<Self> { | ||
| let file = tempfile::NamedTempFile::new() | ||
| .map_err(|error| Error::from_error(&error)) | ||
| .context("create policy file")?; | ||
| std::fs::write(file.path(), contents) | ||
| .map_err(|error| Error::from_error(&error)) | ||
| .context(format!("write {}", file.path().display()))?; | ||
| Ok(Self(file)) | ||
| } | ||
|
|
||
| fn path(&self) -> Result<&str> { | ||
| let path = self.0.path(); | ||
| path.to_str() | ||
| .ok_or_else(|| Error::msg(format!("{} is not UTF-8", path.display()))) | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -1,18 +1,35 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! The base image tests create sandboxes from or build their images on. | ||
| //! The images tests create sandboxes from or build their images on. | ||
|
|
||
| /// Overrides [`DEFAULT_BASE_IMAGE`], e.g. with a mirror. | ||
| pub const BASE_IMAGE_ENV: &str = "OPENSHELL_TEST_BASE_IMAGE"; | ||
| const BASE_IMAGE_ENV: &str = "OPENSHELL_TEST_BASE_IMAGE"; | ||
| /// OpenShell's built-in default sandbox image. Pulling it needs access to | ||
| /// nvcr.io. | ||
| pub const DEFAULT_BASE_IMAGE: &str = "nvcr.io/nvidia/base/ubuntu:24.04"; | ||
| const DEFAULT_BASE_IMAGE: &str = "nvcr.io/nvidia/base/ubuntu:24.04"; | ||
|
|
||
| /// Overrides [`DEFAULT_PYTHON_IMAGE`]. | ||
| const PYTHON_IMAGE_ENV: &str = "OPENSHELL_TEST_PYTHON_IMAGE"; | ||
| /// The workload image for tests that run `python3`, curl, git or openssl in a | ||
| /// sandbox, built from `e2e/python/Dockerfile.workload`. It isn't published: | ||
| /// the tmachine installers load it into every environment, and | ||
| /// `mise run e2e:workload:build` builds it locally. | ||
| const DEFAULT_PYTHON_IMAGE: &str = "openshell/e2e-python:dev"; | ||
|
|
||
| /// The image named by [`BASE_IMAGE_ENV`], or [`DEFAULT_BASE_IMAGE`]. | ||
| pub fn base_image() -> String { | ||
| std::env::var(BASE_IMAGE_ENV) | ||
| image_from_env(BASE_IMAGE_ENV, DEFAULT_BASE_IMAGE) | ||
| } | ||
|
|
||
| /// The image named by [`PYTHON_IMAGE_ENV`], or [`DEFAULT_PYTHON_IMAGE`]. | ||
| pub fn python_image() -> String { | ||
| image_from_env(PYTHON_IMAGE_ENV, DEFAULT_PYTHON_IMAGE) | ||
| } | ||
|
|
||
| fn image_from_env(var: &str, default: &str) -> String { | ||
| std::env::var(var) | ||
| .ok() | ||
| .filter(|image| !image.trim().is_empty()) | ||
| .unwrap_or_else(|| DEFAULT_BASE_IMAGE.to_string()) | ||
| .unwrap_or_else(|| default.to_string()) | ||
| } |
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.