Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions crates/nemoclaw-sdk/tests/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use nemoclaw_sdk::{
CancellationToken, Deployment, Error, ObservationError, Progress, Secrets,
bundle::{Bundle, Manifest, hash_file, required_files},
compile::OPENTOFU_VERSION,
CancellationToken, Deployment, Error, ObservationError, Progress, Secrets, bundle::Bundle,
config::Document,
};
use std::{
fs,
path::Path,
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
Expand All @@ -31,28 +28,6 @@ async fn invalid_programmatic_configuration_cannot_create_deployment_state() {
assert!(!state.exists());
}

/// A bundle whose hashes verify, so opening it succeeds. Its files cannot
/// execute, so no operation that reaches OpenTofu can do anything.
fn write_bundle(directory: &Path) {
let mut manifest = Manifest {
version: "0.1.0".into(),
rust: "1.98.1".into(),
opentofu: OPENTOFU_VERSION.into(),
files: Default::default(),
};
for name in required_files(&manifest.version).unwrap() {
let path = directory.join(&name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, b"non-executable fixture").unwrap();
manifest.files.insert(name, hash_file(&path).unwrap());
}
fs::write(
directory.join("manifest.json"),
serde_json::to_vec(&manifest).unwrap(),
)
.unwrap();
}

#[derive(Default)]
struct CountingSecrets(AtomicUsize);
impl Secrets for CountingSecrets {
Expand All @@ -67,7 +42,7 @@ async fn a_document_that_fails_validation_is_rejected_before_the_bundle_opens_or
{
let directory = tempfile::tempdir().unwrap();
let bundle = directory.path().join("bundle");
write_bundle(&bundle);
crate::bundle::write_bundle(&bundle, None);
assert!(Bundle::open(&bundle).is_ok(), "the bundle must be valid");
let document = Document::default();
let mut defaulted = document.clone();
Expand Down
2 changes: 2 additions & 0 deletions crates/nemoclaw-sdk/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! SDK integration tests, linked into one binary so the SDK links once.
//! Each file is a module; helpers under support/ are shared by all of them.

#[path = "support/bundle.rs"]
mod bundle;
#[path = "support/examples.rs"]
mod examples;
#[cfg(unix)]
Expand Down
48 changes: 48 additions & 0 deletions crates/nemoclaw-sdk/tests/support/bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! Bundles that verify without a build, for tests that open one.

use nemoclaw_sdk::{
bundle::{Manifest, executable, hash_file, required_files},
compile::OPENTOFU_VERSION,
};
use std::{fs, path::Path};

/// Write a bundle whose hashes verify, so opening it succeeds. With `tofu`,
/// `libexec/tofu` is that shell script; every other file cannot execute, so
/// without it no operation that reaches OpenTofu can do anything. The SDK
/// requires `rust` to be non-empty and `version` to be a semantic version, so
/// these values need not match the toolchain or the workspace version.
pub fn write_bundle(directory: &Path, tofu: Option<&str>) {
let mut manifest = Manifest {
version: "0.1.0".into(),
rust: "1.98.1".into(),
opentofu: OPENTOFU_VERSION.into(),
files: Default::default(),
};
let tofu_file = format!("libexec/{}", executable("tofu"));
for name in required_files(&manifest.version).unwrap() {
let path = directory.join(&name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
match tofu.filter(|_| name == tofu_file) {
Some(script) => write_script(&path, script),
None => fs::write(&path, b"non-executable fixture").unwrap(),
}
manifest.files.insert(name, hash_file(&path).unwrap());
}
fs::write(
directory.join("manifest.json"),
serde_json::to_vec(&manifest).unwrap(),
)
.unwrap();
}

#[cfg(unix)]
fn write_script(path: &Path, script: &str) {
crate::executable::write_executable(path, script);
}

#[cfg(not(unix))]
fn write_script(_: &Path, _: &str) {
panic!("a scripted tofu needs a Unix shell");
}
47 changes: 3 additions & 44 deletions crates/nemoclaw-sdk/tests/support/scripted_tofu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

use nemoclaw_sdk::{
CancellationToken, Deployment, ObservationError, Progress, Secrets, StepOutcome,
bundle::{Manifest, executable, hash_file, required_files},
compile::{Generations, OPENTOFU_VERSION, Target},
compile::{Generations, Target},
config::Document,
kubernetes::{GATEWAY_KIND, STORAGE_KIND, gateway::ADDRESS as HELM_RELEASE},
};
use serde_json::{Value, json};
use std::{
fs,
path::{Path, PathBuf},
sync::{Arc, Mutex, MutexGuard, PoisonError},
sync::{Arc, Mutex},
};

/// `@CONTROL@` and `@STATE@` become the control directory and the state
Expand Down Expand Up @@ -62,60 +61,20 @@ case "$1" in
esac
"##;

/// A bundle whose files verify; only `libexec/tofu` can execute. Adapted from
/// `deployment.rs::write_bundle`, which keeps its own copy because it also runs
/// on Windows. The SDK requires `rust` to be non-empty and `version` to be a
/// semantic version, so these values need not match the toolchain or the
/// workspace version.
fn write_bundle(directory: &Path, script: &str) {
let mut manifest = Manifest {
version: "0.1.0".into(),
rust: "1.98.1".into(),
opentofu: OPENTOFU_VERSION.into(),
files: Default::default(),
};
let tofu = format!("libexec/{}", executable("tofu"));
for name in required_files(&manifest.version).unwrap() {
let path = directory.join(&name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
if name == tofu {
crate::executable::write_executable(&path, script);
} else {
fs::write(&path, b"non-executable fixture").unwrap();
}
manifest.files.insert(name, hash_file(&path).unwrap());
}
fs::write(
directory.join("manifest.json"),
serde_json::to_vec(&manifest).unwrap(),
)
.unwrap();
}

/// Scripted tests of one process run one at a time. `Deployment` holds a lock
/// file for each operation, and a child that another thread forks while the lock
/// is held keeps the lock until it executes, so a test's next operation can find
/// the lock held. Nextest runs each test in its own process, where nothing
/// waits here; `cargo test` runs them as threads of one process.
static ONE_AT_A_TIME: Mutex<()> = Mutex::new(());

/// A temporary bundle with a scripted `tofu`, a control directory, and the path
/// of a state directory that `Deployment` creates. A test creates one.
pub struct Scripted {
_turn: MutexGuard<'static, ()>,
_directory: tempfile::TempDir,
root: PathBuf,
}

impl Scripted {
pub fn new() -> Self {
let turn = ONE_AT_A_TIME.lock().unwrap_or_else(PoisonError::into_inner);
let directory = tempfile::tempdir().unwrap();
// The script compares its physical working directory with the state
// directory, and a macOS temporary path traverses a symbolic link.
let root = directory.path().canonicalize().unwrap();
let scripted = Self {
_turn: turn,
_directory: directory,
root,
};
Expand All @@ -131,7 +90,7 @@ impl Scripted {
let script = SCRIPT
.replace("@CONTROL@", &quoted(scripted.control()))
.replace("@STATE@", &quoted(scripted.state()));
write_bundle(&scripted.bundle(), &script);
crate::bundle::write_bundle(&scripted.bundle(), Some(&script));
scripted
}

Expand Down