Skip to content

Commit

Permalink
Add unittest binary.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbittman committed Mar 4, 2025
1 parent 31da035 commit ceeed59
Show file tree
Hide file tree
Showing 15 changed files with 767 additions and 401 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ members = [
"src/bin/gadget",
"src/lib/devmgr",
"src/srv/devmgr-srv",
"src/bin/unittest",
]

exclude = ["toolchain/src/rust"]
Expand All @@ -67,6 +68,7 @@ initrd = [
"lib:logboi-srv",
"lib:pager-srv",
"crate:gadget",
"crate:unittest",
#"third-party:hello-world-rs"
]

Expand Down
2 changes: 1 addition & 1 deletion src/abi
Submodule abi updated 1 files
+1 −1 rt-abi/src/object.rs
45 changes: 14 additions & 31 deletions src/bin/init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::{info, warn};
use twizzler::object::RawObject;
use twizzler_abi::{
aux::KernelInitInfo,
object::{ObjID, Protections, MAX_SIZE, NULLPAGE_SIZE},
object::{ObjID, MAX_SIZE, NULLPAGE_SIZE},
pager::{CompletionToKernel, CompletionToPager, RequestFromKernel, RequestFromPager},
syscall::{sys_new_handle, NewHandleFlags},
};
Expand Down Expand Up @@ -217,39 +217,22 @@ fn find_init_name(name: &str) -> Option<ObjID> {
None
}

fn run_tests(test_list_name: &str, benches: bool) {
if let Some(id) = find_init_name(test_list_name) {
fn run_tests(test_list_name: &str, _benches: bool) {
if let Some(_id) = find_init_name(test_list_name) {
println!("=== found init test list ===");
let handle =
twizzler_rt_abi::object::twz_rt_map_object(id, Protections::READ.into()).unwrap();

let addr = unsafe { handle.start().add(NULLPAGE_SIZE) };
let bytes = unsafe {
core::slice::from_raw_parts(addr as *const u8, twizzler_abi::object::MAX_SIZE)
};
let bytes = &bytes[0..bytes.iter().position(|r| *r == 0).unwrap_or(0)];
let str = String::from_utf8(bytes.to_vec()).unwrap();
let test_failed = false;
for line in str.split("\n").filter(|l| !l.is_empty()) {
println!("STARTING TEST {}", line);
let test_comp = monitor_api::CompartmentLoader::new(
line,
line,
monitor_api::NewCompartmentFlags::empty(),
)
.args(&[line, if benches { "--bench" } else { "--test" }])

let comp = CompartmentLoader::new("unittest", "unittest", NewCompartmentFlags::empty())
.args(&["unittest"])
.load()
.expect("failed to load specified test");
let mut flags = test_comp.info().flags;
while !flags.contains(monitor_api::CompartmentFlags::EXITED) {
flags = test_comp.wait(flags);
}
}
// TODO: get exit status, and set this
if test_failed {
println!("!!! TEST MODE FAILED");
.expect("failed to start unittest");
let mut flags = comp.info().flags;
while !flags.contains(CompartmentFlags::EXITED) {
flags = comp.wait(flags);
}

println!("unittests finished");

#[allow(deprecated)]
twizzler_abi::syscall::sys_debug_shutdown(if test_failed { 1 } else { 0 });
twizzler_abi::syscall::sys_debug_shutdown(0);
}
}
9 changes: 9 additions & 0 deletions src/bin/unittest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "unittest"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "*"
serde_json = "*"
monitor-api = { path = "../../rt/monitor-api" }
113 changes: 113 additions & 0 deletions src/bin/unittest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use std::{
io::BufRead,
sync::OnceLock,
time::{Duration, Instant},
};

use serde::Serialize;

#[derive(Serialize, Debug)]
struct Report {
status: ReportStatus,
}

impl Report {
fn pending() -> Self {
Self {
status: ReportStatus::Pending,
}
}

fn ready(info: ReportInfo) -> Self {
Self {
status: ReportStatus::Ready(info),
}
}
}

#[derive(Serialize, Debug)]
enum ReportStatus {
Pending,
Ready(ReportInfo),
}

#[derive(Serialize, Debug)]
struct ReportInfo {
time: Duration,
tests: Vec<TestResult>,
}

#[derive(Serialize, Debug)]
struct TestResult {
name: String,
passed: bool,
}

static RESULT: OnceLock<Report> = OnceLock::new();

fn main() {
let file = std::fs::File::open("/initrd/test_bins").expect("no test binaries specified");

let heartbeat_thread = std::thread::spawn(|| io_heartbeat());

let mut reports = vec![];
let start = Instant::now();
for line in std::io::BufReader::new(file).lines() {
if let Ok(line) = &line {
println!("STARTING {}", line);
if let Ok(test_comp) = monitor_api::CompartmentLoader::new(
line,
line,
monitor_api::NewCompartmentFlags::empty(),
)
.args(&[line.as_str(), "--test"])
.load()
{
let mut flags = test_comp.info().flags;
while !flags.contains(monitor_api::CompartmentFlags::EXITED) {
flags = test_comp.wait(flags);
}
reports.push(TestResult {
name: line.clone(),
passed: true,
});
} else {
reports.push(TestResult {
name: line.clone(),
passed: false,
});
}
}
}
let dur = Instant::now() - start;
RESULT
.set(Report::ready(ReportInfo {
time: dur,
tests: reports,
}))
.unwrap();
heartbeat_thread.join().unwrap();
}

fn io_heartbeat() {
let mut buf = String::new();
while let Ok(_) = std::io::stdin().read_line(&mut buf) {
match buf.as_str().trim() {
"status" => {
if let Some(report) = RESULT.get() {
println!("REPORT {}", serde_json::to_string(report).unwrap());
return;
} else {
println!(
"REPORT {}",
serde_json::to_string(&Report::pending()).unwrap()
);
}
}
_ => {
println!("!! unknown command: {}", buf);
}
}
buf.clear();
}
}
5 changes: 1 addition & 4 deletions src/lib/twizzler-driver/src/dma/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,12 @@ pub enum PinError {

#[cfg(test)]
mod tests {
use twizzler_abi::syscall::{BackingType, LifetimeType};
use twizzler_object::CreateSpec;

use crate::dma::{Access, DmaOptions, DmaPool};

#[test]
fn pin_kaction() {
let pool = DmaPool::new(
CreateSpec::new(LifetimeType::Volatile, BackingType::Normal),
DmaPool::default_spec(),
Access::BiDirectional,
DmaOptions::default(),
);
Expand Down
Loading

0 comments on commit ceeed59

Please sign in to comment.