Extract the OSTD test runner as a kernel

This commit is contained in:
Zhang Junyang
2024-08-13 14:51:27 +00:00
committed by Tate, Hongliang Tian
parent cad36ecdab
commit be3492d7f0
24 changed files with 330 additions and 174 deletions

View File

@ -171,7 +171,7 @@ fn install_setup_with_arch(
cmd.arg("install").arg("linux-bzimage-setup");
cmd.arg("--force");
cmd.arg("--root").arg(install_dir.as_ref());
if std::env::var("AUTO_TEST").is_ok() || std::env::var("OSDK_INTEGRATION_TEST").is_ok() {
if matches!(option_env!("OSDK_LOCAL_DEV"), Some("1")) {
cmd.arg("--path")
.arg("../../../ostd/libs/linux-bzimage/setup");
}

View File

@ -72,6 +72,7 @@ pub fn create_base_and_cached_build(
&base_crate_path,
&get_current_crate_info().name,
get_current_crate_info().path,
false,
);
let original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(&base_crate_path).unwrap();

View File

@ -1,4 +1,6 @@
#![no_std]
// The feature `linkage` is required for `ostd::main` to work.
#![feature(linkage)]
#![deny(unsafe_code)]
use ostd::prelude::*;

View File

@ -7,6 +7,8 @@ use crate::{
base_crate::new_base_crate,
cli::TestArgs,
config::{scheme::ActionChoice, Config},
error::Errno,
error_msg,
util::{
get_cargo_metadata, get_current_crate_info, get_target_directory, parse_package_id_string,
},
@ -25,7 +27,26 @@ pub fn test_current_crate(config: &Config, args: &TestArgs) {
let cargo_target_directory = get_target_directory();
let osdk_output_directory = cargo_target_directory.join(DEFAULT_TARGET_RELPATH);
let target_crate_dir = osdk_output_directory.join("base");
new_base_crate(&target_crate_dir, &current_crate.name, &current_crate.path);
// A special case is that we use OSDK to test the OSDK test runner crate
// itself. We check it by name.
let runner_self_test = if current_crate.name == "osdk-test-kernel" {
if matches!(option_env!("OSDK_LOCAL_DEV"), Some("1")) {
true
} else {
error_msg!("The tested crate name collides with the OSDK test runner crate");
std::process::exit(Errno::BadCrateName as _);
}
} else {
false
};
new_base_crate(
&target_crate_dir,
&current_crate.name,
&current_crate.path,
!runner_self_test,
);
let main_rs_path = target_crate_dir.join("src").join("main.rs");
@ -39,19 +60,29 @@ pub fn test_current_crate(config: &Config, args: &TestArgs) {
ktest_crate_whitelist.push(name.clone());
}
let ktest_static_var = format!(
// Append the ktest static variable and the runner reference to the
// `main.rs` file.
let ktest_main_rs = format!(
r#"
{}
#[no_mangle]
pub static KTEST_TEST_WHITELIST: Option<&[&str]> = {};
#[no_mangle]
pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?});
"#,
ktest_test_whitelist, ktest_crate_whitelist,
);
// Append the ktest static variable to the main.rs file
"#,
if runner_self_test {
""
} else {
"extern crate osdk_test_kernel;"
},
ktest_test_whitelist,
ktest_crate_whitelist,
);
let mut main_rs_content = fs::read_to_string(&main_rs_path).unwrap();
main_rs_content.push_str(&ktest_static_var);
main_rs_content.push_str(&ktest_main_rs);
fs::write(&main_rs_path, main_rs_content).unwrap();
// Build the kernel with the given base crate