aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-test / src / testutil.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Test helpers: unique temp dirs and throwaway git repositories. |
| 17 | |
| 18 | use std::fs; |
| 19 | use std::os::unix::fs::PermissionsExt; |
| 20 | use std::path::{Path, PathBuf}; |
| 21 | use std::process::Command; |
| 22 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 23 | |
| 24 | static COUNTER: AtomicU64 = AtomicU64::new(0); |
| 25 | |
| 26 | pub struct TempDir { |
| 27 | path: PathBuf, |
| 28 | } |
| 29 | |
| 30 | impl TempDir { |
| 31 | pub fn new(tag: &str) -> TempDir { |
| 32 | let n = COUNTER.fetch_add(1, Ordering::SeqCst); |
| 33 | let nanos = std::time::SystemTime::now() |
| 34 | .duration_since(std::time::UNIX_EPOCH) |
| 35 | .map(|d| d.subsec_nanos()) |
| 36 | .unwrap_or(0); |
| 37 | let path = std::env::temp_dir().join(format!( |
| 38 | "aien-test-{tag}-{}-{n}-{nanos}", |
| 39 | std::process::id() |
| 40 | )); |
| 41 | fs::create_dir_all(&path).expect("create temp dir"); |
| 42 | let path = fs::canonicalize(&path).expect("canonicalize temp dir"); |
| 43 | TempDir { path } |
| 44 | } |
| 45 | |
| 46 | pub fn path(&self) -> &Path { |
| 47 | &self.path |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl Drop for TempDir { |
| 52 | fn drop(&mut self) { |
| 53 | let _ = fs::remove_dir_all(&self.path); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub fn git(dir: &Path, args: &[&str]) { |
| 58 | let mut cmd = Command::new("git"); |
| 59 | cmd.arg("-C") |
| 60 | .arg(dir) |
| 61 | .args([ |
| 62 | "-c", |
| 63 | "user.name=test", |
| 64 | "-c", |
| 65 | "user.email=test@example.invalid", |
| 66 | "-c", |
| 67 | "commit.gpgsign=false", |
| 68 | ]) |
| 69 | .args(args); |
| 70 | // Gated like every spawn: see `process::SPAWN_GATE`. |
| 71 | let out = crate::process::output_gated(&mut cmd).expect("run git"); |
| 72 | assert!( |
| 73 | out.status.success(), |
| 74 | "git {:?} failed: {}", |
| 75 | args, |
| 76 | String::from_utf8_lossy(&out.stderr) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | pub fn write_file(dir: &Path, rel: &str, content: &str, executable: bool) { |
| 81 | let p = dir.join(rel); |
| 82 | if let Some(parent) = p.parent() { |
| 83 | fs::create_dir_all(parent).expect("create parent"); |
| 84 | } |
| 85 | fs::write(&p, content).expect("write file"); |
| 86 | if executable { |
| 87 | fs::set_permissions(&p, fs::Permissions::from_mode(0o755)).expect("chmod"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// A fresh git repository with one commit containing whatever `files` lists |
| 92 | /// as (relative path, content, executable). |
| 93 | pub fn init_repo(tag: &str, files: &[(&str, &str, bool)]) -> TempDir { |
| 94 | let t = TempDir::new(tag); |
| 95 | git(t.path(), &["init", "-q"]); |
| 96 | for (rel, content, exec) in files { |
| 97 | write_file(t.path(), rel, content, *exec); |
| 98 | } |
| 99 | git(t.path(), &["add", "-A"]); |
| 100 | git(t.path(), &["commit", "-q", "-m", "init"]); |
| 101 | t |
| 102 | } |
| 103 | |
| 104 | /// Whether `/proc/locks` shows a blocked `flock` waiter on `path` (a line |
| 105 | /// `N: -> FLOCK ... maj:min:inode ...`), so a test holder can let go only |
| 106 | /// once the code under test is really queued behind it. |
| 107 | pub fn queued_on(path: &Path) -> bool { |
| 108 | use std::os::unix::fs::MetadataExt; |
| 109 | let ino = match fs::metadata(path) { |
| 110 | Ok(m) => m.ino(), |
| 111 | Err(_) => return false, |
| 112 | }; |
| 113 | let suffix = format!(":{ino}"); |
| 114 | fs::read_to_string("/proc/locks") |
| 115 | .unwrap_or_default() |
| 116 | .lines() |
| 117 | .filter(|l| l.contains("-> FLOCK")) |
| 118 | .any(|l| l.split_whitespace().any(|w| w.ends_with(&suffix))) |
| 119 | } |