diff --git a/crates/nemoclaw-sdk/tests/deployment.rs b/crates/nemoclaw-sdk/tests/deployment.rs index 395116f8012..bf2b1164a8d 100644 --- a/crates/nemoclaw-sdk/tests/deployment.rs +++ b/crates/nemoclaw-sdk/tests/deployment.rs @@ -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}, @@ -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 { @@ -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(); diff --git a/crates/nemoclaw-sdk/tests/main.rs b/crates/nemoclaw-sdk/tests/main.rs index 00b5a49534f..e9c584b040d 100644 --- a/crates/nemoclaw-sdk/tests/main.rs +++ b/crates/nemoclaw-sdk/tests/main.rs @@ -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)] diff --git a/crates/nemoclaw-sdk/tests/support/bundle.rs b/crates/nemoclaw-sdk/tests/support/bundle.rs new file mode 100644 index 00000000000..baed43972fc --- /dev/null +++ b/crates/nemoclaw-sdk/tests/support/bundle.rs @@ -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"); +} diff --git a/crates/nemoclaw-sdk/tests/support/scripted_tofu.rs b/crates/nemoclaw-sdk/tests/support/scripted_tofu.rs index c2d23df0d52..90c1ae3eaea 100644 --- a/crates/nemoclaw-sdk/tests/support/scripted_tofu.rs +++ b/crates/nemoclaw-sdk/tests/support/scripted_tofu.rs @@ -13,8 +13,7 @@ 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}, }; @@ -62,36 +61,6 @@ 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(); -} - /// 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 { @@ -121,7 +90,7 @@ impl Scripted { let script = SCRIPT .replace("@CONTROL@", "ed(scripted.control())) .replace("@STATE@", "ed(scripted.state())); - write_bundle(&scripted.bundle(), &script); + crate::bundle::write_bundle(&scripted.bundle(), Some(&script)); scripted }