Skip to content

[worker:coverage] test(sandbox2): add behavior tests for Sandbox::new - #259

Merged
norrietaylor merged 1 commit into
mainfrom
chore/sandbox2-new-coverage-behavior-d3d42287700c95da
Jun 1, 2026
Merged

norrietaylor merged 1 commit into
mainfrom
chore/sandbox2-new-coverage-behavior-d3d42287700c95da

Conversation

@gominimal-aw-bot

@gominimal-aw-bot gominimal-aw-bot Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Exercise four observable contracts of sandbox2::Sandbox::new (issue #243, regions=214, whole-function coverage = 0).

What was changed

Added four #[test] cases to the existing #[cfg(test)] mod tests in crates/sandbox2/src/lib.rs, plus a make_base_with_synth() test-setup helper:

Test Contract exercised
sandbox_new_rejects_mapped_file_in_rootfs SandboxMapped::File in config.rootfs → Err(Error::MappedFile(_))
sandbox_new_derives_state_dir_from_base no state_dir configured → sandbox.state_dir == base_dir/state
sandbox_new_honours_explicit_state_dir with_state_dir(p) → sandbox.state_dir == p
sandbox_new_minenv_socket_is_connectable after construction, run/minenv_sock accepts a UnixStream::connect

Why these tests, not directory-existence tests

PR #250 was closed because it asserted that build/, state/, and state/home/ exist — change-detector tests that restate the constructor body. Per the test authoring standards, these are banned.

These four tests assert the function's observable contract:

  • The error variant returned when a file is in rootfs (caller-visible error type).
  • The state_dir field value that new_container uses to bind-mount /state (caller-visible field).
  • That the channel listener thread is running and connectable (caller-visible async side-effect).

No directory-existence assertions are made.

Setup helper

make_base_with_synth() creates base_dir/synth/usr/ — the minimum structure needed for hardlink_dir_contents and the usr/lib64 → lib symlink step to succeed. This is test setup, not assertion.

Closes #243

Verification

cargo test -p sandbox2 — 6 tests: 6 ok.

cargo fmt --all --check — no diff.

cargo clippy --workspace --all-targets — no warnings (exit 0).

Generated by Chore-issue worker agent (side-repo-ops operator) · ● 50M · ◷

Summary by CodeRabbit

  • Tests
    • Expanded sandbox initialization test suite with additional validation tests covering configuration handling and system behavior verification.

Review Change Stack

Test three observable contracts of Sandbox::new, not directory layout:

- sandbox_new_rejects_mapped_file_in_rootfs: SandboxMapped::File in
  config.rootfs must return Err(Error::MappedFile(_)). Files cannot be
  hardlinked at the rootfs level; only directories are valid.

- sandbox_new_derives_state_dir_from_base: when no state_dir is
  configured, sandbox.state_dir must equal base_dir/state. Callers
  rely on this to bind-mount /state at the correct host path.

- sandbox_new_honours_explicit_state_dir: when with_state_dir() is
  used, sandbox.state_dir must equal the caller-supplied path verbatim.

- sandbox_new_minenv_socket_is_connectable: after construction, the
  Unix socket at run/minenv_sock must accept connections, confirming
  the channel listener thread is running.

No directory-existence assertions are made; all four tests assert the
function's observable contract per the test authoring standards.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR extends test coverage for Sandbox::new by adding a test helper and multiple validation cases within the existing test module. The changes verify rootfs configuration constraints, default and explicit state directory handling, and successful minenv Unix socket initialization without modifying public APIs or production code.

Changes

Sandbox::new test coverage

Layer / File(s) Summary
Test infrastructure and Sandbox::new validation
crates/sandbox2/src/lib.rs
Test module imports Config and SandboxMapped, adds make_base_with_synth helper to create temporary base directories with required synth/usr structure, and implements new tests verifying: rejection of SandboxMapped::File entries in config.rootfs, default state_dir assignment to base_dir/state, explicit state_dir override, and minenv Unix socket connectability after sandbox initialization.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • gominimal/minimal#188: Related through the minenv Unix socket listener setup being tested here, which was modified in PR #188.

Suggested reviewers

  • msample
  • twitchyliquid64
  • mitodrummer

Poem

🐰 A sandbox springs to life with tests so fine,
Sockets singing, state dirs aligned,
Coverage gap now stitched with care,
Filesystem paths checked with flair! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the primary change: adding behavior tests for Sandbox::new in the sandbox2 crate.
Linked Issues check ✅ Passed The PR implements most of issue #243's objectives but with reduced scope: tests verify MappedFile rejection and state_dir behavior, plus socket connectivity, but do not cover WdSetup::BoundDir or MINIMAL_INTERNAL_CS_BUILD branches as originally proposed.
Out of Scope Changes check ✅ Passed All changes are scoped to test code within #[cfg(test)] mod tests in lib.rs, directly supporting issue #243's coverage objectives with no unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (5)
crates/sandbox2/src/lib.rs (5)

1029-1034: ⚡ Quick win

Use expect() instead of unwrap() for consistency.

Line 1034 uses .unwrap() while line 1036 correctly uses .expect(). Apply expect() consistently per coding guidelines.

🔧 Suggested improvement
-    let sandbox = Sandbox::new(base, config, ()).unwrap();
+    let sandbox = Sandbox::new(base, config, ())
+        .expect("Sandbox::new should succeed for socket test");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 1029 - 1034, In the test function
sandbox_new_minenv_socket_is_connectable replace the use of .unwrap() on the
Sandbox::new(base, config, ()) call with .expect(...) to match the existing
style; update the call in sandbox_new_minenv_socket_is_connectable to use
Sandbox::new(...).expect("failed to create Sandbox in
sandbox_new_minenv_socket_is_connectable") (or a similar descriptive message) so
it is consistent with the other expect() usage.

967-976: ⚡ Quick win

Use expect() instead of unwrap() for clearer test failures.

The helper uses .unwrap() at lines 972 and 974. As per coding guidelines, prefer expect("reason") to provide context when panics occur during test setup.

🔧 Suggested improvement
 fn make_base_with_synth() -> (tempfile::TempDir, PathBuf) {
-    let tmp = tempfile::TempDir::new().unwrap();
+    let tmp = tempfile::TempDir::new().expect("failed to create temp directory for test");
     let base = tmp.path().to_path_buf();
-    fs::create_dir_all(base.join("synth").join("usr")).unwrap();
+    fs::create_dir_all(base.join("synth").join("usr"))
+        .expect("failed to create synth/usr test structure");
     (tmp, base)
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 967 - 976, In
make_base_with_synth(), replace the two .unwrap() calls with .expect(...) so
test failures show context: change tempfile::TempDir::new().unwrap() to
.expect("create tempdir in make_base_with_synth") and change
fs::create_dir_all(...).unwrap() to .expect("create synth/usr directory in
make_base_with_synth"); reference tmp, base, tempfile::TempDir::new, and
fs::create_dir_all when making the edits.

1014-1019: ⚡ Quick win

Use expect() instead of unwrap().

Lines 1016 and 1019 use .unwrap(). As per coding guidelines, use expect("reason") for clearer failure context.

🔧 Suggested improvement
-    let state_tmp = tempfile::TempDir::new().unwrap();
+    let state_tmp = tempfile::TempDir::new().expect("failed to create state temp directory");
     let state_path = state_tmp.path().to_path_buf();
     let config = Config::new("test-state-explicit").with_state_dir(state_path.clone());
-    let sandbox = Sandbox::new(base, config, ()).unwrap();
+    let sandbox = Sandbox::new(base, config, ())
+        .expect("Sandbox::new should succeed with explicit state_dir");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 1014 - 1019, In
sandbox_new_honours_explicit_state_dir replace the two uses of .unwrap() with
.expect(...) to provide clear failure context: use state_tmp creation like
tempfile::TempDir::new().expect("failed to create temporary state dir for
sandbox_new_honours_explicit_state_dir") and replace Sandbox::new(base, config,
()).unwrap() with Sandbox::new(base, config, ()).expect("failed to create
Sandbox in sandbox_new_honours_explicit_state_dir"); this keeps the same
behavior but gives actionable error messages and references the functions
make_base_with_synth, tempfile::TempDir::new, and Sandbox::new for locating the
calls.

999-1002: ⚡ Quick win

Use expect() instead of unwrap().

Line 1002 uses .unwrap(). As per coding guidelines, use expect("reason") for clearer failure messages.

🔧 Suggested improvement
-    let sandbox = Sandbox::new(base.clone(), config, ()).unwrap();
+    let sandbox = Sandbox::new(base.clone(), config, ())
+        .expect("Sandbox::new should succeed with minimal config");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 999 - 1002, In the test function
sandbox_new_derives_state_dir_from_base replace the unsafe .unwrap() call on
Sandbox::new(base.clone(), config, ()) with .expect("failed to create Sandbox in
sandbox_new_derives_state_dir_from_base") so test failures print a clear
message; locate the call to Sandbox::new in that function (and any similar uses
in the same test) and swap unwrap() for expect(...) with a short descriptive
reason referencing Sandbox creation.

982-988: ⚡ Quick win

Use expect() instead of unwrap().

Line 983 uses .unwrap(). As per coding guidelines, use expect("reason") to clarify intent and provide better panic messages.

🔧 Suggested improvement
-    let tmp = tempfile::TempDir::new().unwrap();
+    let tmp = tempfile::TempDir::new().expect("failed to create temp directory");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/sandbox2/src/lib.rs` around lines 982 - 988, In the test function
sandbox_new_rejects_mapped_file_in_rootfs the call to
tempfile::TempDir::new().unwrap() should use expect() with a clear message
instead of unwrap(); replace the unwrap() on the TempDir creation with
expect("failed to create temp dir for
sandbox_new_rejects_mapped_file_in_rootfs") (or similar) so failures produce a
helpful panic message when creating base in Sandbox::new.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/sandbox2/src/lib.rs`:
- Around line 1029-1034: In the test function
sandbox_new_minenv_socket_is_connectable replace the use of .unwrap() on the
Sandbox::new(base, config, ()) call with .expect(...) to match the existing
style; update the call in sandbox_new_minenv_socket_is_connectable to use
Sandbox::new(...).expect("failed to create Sandbox in
sandbox_new_minenv_socket_is_connectable") (or a similar descriptive message) so
it is consistent with the other expect() usage.
- Around line 967-976: In make_base_with_synth(), replace the two .unwrap()
calls with .expect(...) so test failures show context: change
tempfile::TempDir::new().unwrap() to .expect("create tempdir in
make_base_with_synth") and change fs::create_dir_all(...).unwrap() to
.expect("create synth/usr directory in make_base_with_synth"); reference tmp,
base, tempfile::TempDir::new, and fs::create_dir_all when making the edits.
- Around line 1014-1019: In sandbox_new_honours_explicit_state_dir replace the
two uses of .unwrap() with .expect(...) to provide clear failure context: use
state_tmp creation like tempfile::TempDir::new().expect("failed to create
temporary state dir for sandbox_new_honours_explicit_state_dir") and replace
Sandbox::new(base, config, ()).unwrap() with Sandbox::new(base, config,
()).expect("failed to create Sandbox in
sandbox_new_honours_explicit_state_dir"); this keeps the same behavior but gives
actionable error messages and references the functions make_base_with_synth,
tempfile::TempDir::new, and Sandbox::new for locating the calls.
- Around line 999-1002: In the test function
sandbox_new_derives_state_dir_from_base replace the unsafe .unwrap() call on
Sandbox::new(base.clone(), config, ()) with .expect("failed to create Sandbox in
sandbox_new_derives_state_dir_from_base") so test failures print a clear
message; locate the call to Sandbox::new in that function (and any similar uses
in the same test) and swap unwrap() for expect(...) with a short descriptive
reason referencing Sandbox creation.
- Around line 982-988: In the test function
sandbox_new_rejects_mapped_file_in_rootfs the call to
tempfile::TempDir::new().unwrap() should use expect() with a clear message
instead of unwrap(); replace the unwrap() on the TempDir creation with
expect("failed to create temp dir for
sandbox_new_rejects_mapped_file_in_rootfs") (or similar) so failures produce a
helpful panic message when creating base in Sandbox::new.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 82551142-94ac-4a94-be98-a1b448794ecd

📥 Commits

Reviewing files that changed from the base of the PR and between 422f245 and 2a21789.

📒 Files selected for processing (1)
  • crates/sandbox2/src/lib.rs

@norrietaylor
norrietaylor enabled auto-merge (squash) June 1, 2026 18:06
@norrietaylor
norrietaylor merged commit 30c1618 into main Jun 1, 2026
14 of 15 checks passed
@norrietaylor
norrietaylor deleted the chore/sandbox2-new-coverage-behavior-d3d42287700c95da branch June 1, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant