diff --git a/e2e/rust/src/harness/sandbox.rs b/e2e/rust/src/harness/sandbox.rs index 9f57a29daa..9293ce2fac 100644 --- a/e2e/rust/src/harness/sandbox.rs +++ b/e2e/rust/src/harness/sandbox.rs @@ -386,87 +386,6 @@ impl SandboxGuard { }) } - /// Create a detached scratch sandbox with pre-loaded files, then exec a - /// command in it. - /// - /// Equivalent to: - /// ```text - /// openshell sandbox create --detach --upload : [extra_args...] - /// openshell sandbox exec -- - /// ``` - /// - /// The `--no-git-ignore` flag is passed to avoid needing a git repository. - /// - /// # Errors - /// - /// Returns an error if the CLI exits with a non-zero status or the sandbox - /// name cannot be parsed. - pub async fn create_with_upload( - upload_local: &str, - upload_dest: &str, - command: &[&str], - ) -> Result { - Self::create_with_uploads(&[(upload_local, upload_dest)], command).await - } - - /// Create a sandbox with multiple `--upload` specs. - /// - /// Each element of `uploads` is a `(local_path, sandbox_dest)` pair. - /// `--no-git-ignore` is applied to all uploads. - pub async fn create_with_uploads( - uploads: &[(&str, &str)], - command: &[&str], - ) -> Result { - let mut cmd = openshell_cmd(); - cmd.arg("sandbox").arg("create").arg("--detach"); - add_unique_name_if_missing(&mut cmd, &[]); - add_test_image_if_missing(&mut cmd, &[]); - for (local, dest) in uploads { - cmd.arg("--upload").arg(format!("{local}:{dest}")); - } - cmd.arg("--no-git-ignore"); - cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); - - let output = timeout(SANDBOX_READY_TIMEOUT, cmd.output()) - .await - .map_err(|_| { - format!("sandbox create --upload timed out after {SANDBOX_READY_TIMEOUT:?}") - })? - .map_err(|e| format!("failed to spawn openshell: {e}"))?; - - let stdout = String::from_utf8_lossy(&output.stdout).to_string(); - let stderr = String::from_utf8_lossy(&output.stderr).to_string(); - let combined = format!("{stdout}{stderr}"); - - if !output.status.success() { - return Err(format!( - "sandbox create --upload failed (exit {:?}):\n{combined}", - output.status.code() - )); - } - - let name = extract_sandbox_name(&combined).ok_or_else(|| { - format!("could not parse sandbox name from create output:\n{combined}") - })?; - - let mut guard = Self { - name, - create_output: combined, - child: None, - cleaned_up: false, - }; - - match guard.exec(command).await { - Ok(exec_output) => guard.create_output.push_str(&exec_output), - Err(err) => { - guard.cleanup().await; - return Err(err); - } - } - - Ok(guard) - } - /// Upload local files to the sandbox via `openshell sandbox upload`. /// /// # Arguments diff --git a/e2e/rust/tests/upload_create.rs b/e2e/rust/tests/upload_create.rs deleted file mode 100644 index 5cc3964841..0000000000 --- a/e2e/rust/tests/upload_create.rs +++ /dev/null @@ -1,113 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -#![cfg(feature = "e2e")] - -//! E2E test: `sandbox create --upload` pre-loads files before running a command. -//! -//! Validates that the `--upload :` flag on `sandbox create` -//! transfers files into the sandbox before the user command executes, -//! so the command can read the uploaded content. -//! -//! Prerequisites: -//! - A running openshell gateway (`mise run gateway:docker`) -//! - The `openshell` binary (built automatically from the workspace) - -use std::fs; - -use openshell_e2e::harness::output::strip_ansi; -use openshell_e2e::harness::sandbox::SandboxGuard; - -/// Create a sandbox with `--upload dir:/sandbox/data` and verify directory -/// uploads preserve the source basename at `/sandbox/data//...`. -#[tokio::test] -async fn create_with_upload_directory_preserves_source_basename() { - let tmpdir = tempfile::tempdir().expect("create tmpdir"); - - // Create a directory with files to upload. - let upload_dir = tmpdir.path().join("project"); - fs::create_dir_all(upload_dir.join("src")).expect("create project/src"); - fs::write(upload_dir.join("marker.txt"), "upload-create-marker").expect("write marker.txt"); - fs::write(upload_dir.join("src/main.py"), "print('hello')").expect("write main.py"); - - let upload_str = upload_dir.to_str().expect("upload path is UTF-8"); - let remote_marker = "/sandbox/data/project/marker.txt"; - - // The command reads the marker file — if upload worked, its content - // appears in the output. - let mut guard = - SandboxGuard::create_with_upload(upload_str, "/sandbox/data", &["cat", remote_marker]) - .await - .expect("sandbox create --upload"); - - let clean = strip_ansi(&guard.create_output); - assert!( - clean.contains("upload-create-marker"), - "expected uploaded marker content in sandbox output:\n{clean}" - ); - - guard.cleanup().await; -} - -/// Two `--upload` specs in a single `sandbox create` call should both land in -/// the sandbox before the command runs. -#[tokio::test] -async fn create_with_multiple_uploads() { - let tmpdir = tempfile::tempdir().expect("create tmpdir"); - - let dir_a = tmpdir.path().join("alpha"); - let dir_b = tmpdir.path().join("beta"); - fs::create_dir_all(&dir_a).expect("create alpha"); - fs::create_dir_all(&dir_b).expect("create beta"); - fs::write(dir_a.join("a.txt"), "content-alpha").expect("write a.txt"); - fs::write(dir_b.join("b.txt"), "content-beta").expect("write b.txt"); - - let spec_a = dir_a.to_str().expect("alpha path is UTF-8"); - let spec_b = dir_b.to_str().expect("beta path is UTF-8"); - - let mut guard = SandboxGuard::create_with_uploads( - &[(spec_a, "/sandbox/alpha"), (spec_b, "/sandbox/beta")], - &[ - "sh", - "-c", - "cat /sandbox/alpha/alpha/a.txt /sandbox/beta/beta/b.txt", - ], - ) - .await - .expect("sandbox create with multiple --upload flags"); - - let clean = strip_ansi(&guard.create_output); - assert!( - clean.contains("content-alpha"), - "expected alpha content in output:\n{clean}" - ); - assert!( - clean.contains("content-beta"), - "expected beta content in output:\n{clean}" - ); - - guard.cleanup().await; -} - -/// `--upload` with a single file (not a directory) should work. -#[tokio::test] -async fn create_with_upload_single_file() { - let tmpdir = tempfile::tempdir().expect("create tmpdir"); - let file_path = tmpdir.path().join("config.txt"); - fs::write(&file_path, "single-file-upload-test").expect("write config.txt"); - - let file_str = file_path.to_str().expect("file path is UTF-8"); - - let mut guard = - SandboxGuard::create_with_upload(file_str, "/sandbox", &["cat", "/sandbox/config.txt"]) - .await - .expect("sandbox create --upload single file"); - - let clean = strip_ansi(&guard.create_output); - assert!( - clean.contains("single-file-upload-test"), - "expected single-file content in sandbox output:\n{clean}" - ); - - guard.cleanup().await; -} diff --git a/tests/suites/conformance/README.md b/tests/suites/conformance/README.md index a6edb02a7c..9937db7738 100644 --- a/tests/suites/conformance/README.md +++ b/tests/suites/conformance/README.md @@ -14,6 +14,7 @@ nextest run the tests as the library's unit tests. The crate also builds the | `persistent-workspace` | Files in `/sandbox` survive stop and start | | `policy-advisor` | `policy.local` accepts a proposed rule; a denied address or hostname yields one draft rule | | `file-transfer` | `sandbox upload` and `download`: round trips, file modes and symlinks outside the workspace, `.gitignore` filtering, workspace-escape and option-looking names, a large upload | +| `create-upload` | `sandbox create --upload`: directory basename and nested files, multiple uploads, a single file | | `credential-gating` | Provider credentials: admission of credentialed endpoints, REST body placeholders and rewriting, WebSocket binary frames, live policy binding | | `image` | A sandbox runs the default image or the one it is created from | | `labels` | Labels are stored and select sandboxes in `sandbox list` | diff --git a/tests/suites/conformance/src/create_upload.rs b/tests/suites/conformance/src/create_upload.rs new file mode 100644 index 0000000000..91112a3984 --- /dev/null +++ b/tests/suites/conformance/src/create_upload.rs @@ -0,0 +1,113 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! `sandbox create --upload`. +//! +//! Ported from `e2e/rust/tests/upload_create.rs`, reworked in +//! NVIDIA/OpenShell#4606 by politerealism. The behavior is not driver-specific. +//! +//! `--upload` cannot be combined with a command, so these tests create the +//! sandbox with the target's default image and workload and check the files +//! once `create` returns. + +use std::fs; +use std::path::Path; + +use tempfile::TempDir; +use testkit::{Context, ResultExt as _, Sandbox}; + +/// `--upload :` keeps the directory's basename under +/// the destination, preserves nested files, and does not flatten the directory. +#[testkit::test] +async fn create_with_upload_directory_preserves_source_basename( + cx: &mut Context, +) -> testkit::Result { + let local = scratch()?; + let source = local.path().join("project"); + write(&source.join("marker.txt"), "upload-create-marker")?; + write(&source.join("src/main.py"), "print('hello')")?; + + let sandbox = create_with_uploads(cx, "cu1", &[(&source, "/sandbox/data")]).await?; + sandbox + .exec(["cat", "/sandbox/data/project/marker.txt"]) + .await? + .expect_stdout("upload-create-marker")?; + sandbox + .exec(["cat", "/sandbox/data/project/src/main.py"]) + .await? + .expect_stdout("print('hello')")?; + sandbox + .exec(["sh", "-c", "test ! -e /sandbox/data/marker.txt"]) + .await? + .success()?; + sandbox.delete().await +} + +/// Several `--upload` flags on one `sandbox create` each place their files at +/// their own destination. +#[testkit::test] +async fn create_with_multiple_uploads(cx: &mut Context) -> testkit::Result { + let local = scratch()?; + let alpha = local.path().join("alpha"); + let beta = local.path().join("beta"); + write(&alpha.join("a.txt"), "content-alpha")?; + write(&beta.join("b.txt"), "content-beta")?; + + let sandbox = create_with_uploads( + cx, + "cu2", + &[(&alpha, "/sandbox/alpha"), (&beta, "/sandbox/beta")], + ) + .await?; + sandbox + .exec(["cat", "/sandbox/alpha/alpha/a.txt"]) + .await? + .expect_stdout("content-alpha")?; + sandbox + .exec(["cat", "/sandbox/beta/beta/b.txt"]) + .await? + .expect_stdout("content-beta")?; + sandbox.delete().await +} + +/// `--upload :` places a single file at the destination. +#[testkit::test] +async fn create_with_upload_single_file(cx: &mut Context) -> testkit::Result { + let local = scratch()?; + let source = local.path().join("config.txt"); + write(&source, "single-file-upload-test")?; + + let sandbox = create_with_uploads(cx, "cu3", &[(&source, "/sandbox")]).await?; + sandbox + .exec(["cat", "/sandbox/config.txt"]) + .await? + .expect_stdout("single-file-upload-test")?; + sandbox.delete().await +} + +fn scratch() -> testkit::Result { + tempfile::tempdir().context("create a local scratch directory") +} + +fn write(path: &Path, content: &str) -> testkit::Result { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent).context(format!("create {}", parent.display()))?; + } + fs::write(path, content).context(format!("write {}", path.display())) +} + +/// Create a detached sandbox with one `--upload :` flag +/// per entry and wait until it is Ready. +async fn create_with_uploads( + cx: &Context, + suffix: &str, + uploads: &[(&Path, &str)], +) -> testkit::Result { + let args = uploads.iter().flat_map(|(source, destination)| { + [ + "--upload".to_string(), + format!("{}:{destination}", source.display()), + ] + }); + cx.cli_sandbox(suffix).args(args).create_ready().await +} diff --git a/tests/suites/conformance/src/lib.rs b/tests/suites/conformance/src/lib.rs index cabd7f3a3b..b97238f84e 100644 --- a/tests/suites/conformance/src/lib.rs +++ b/tests/suites/conformance/src/lib.rs @@ -9,6 +9,7 @@ //! program in `src/main.rs` runs them by linking this library. mod cli; +mod create_upload; mod credential_gating; mod file_transfer; mod filesystem_policy;