impemented mock testing

This commit is contained in:
2026-02-26 17:11:42 +01:00
parent f76acd6256
commit 667d94af7a
21 changed files with 480 additions and 110 deletions

55
tests/common/fakesys.rs Normal file
View File

@@ -0,0 +1,55 @@
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
pub struct FakeSysBuilder {
temp_dir: TempDir,
}
impl FakeSysBuilder {
pub fn new() -> Self {
Self {
temp_dir: TempDir::new().expect("Failed to create temporary directory"),
}
}
pub fn base_path(&self) -> PathBuf {
self.temp_dir.path().to_path_buf()
}
pub fn add_dmi(&self, vendor: &str, product: &str) -> &Self {
let dmi_path = self.base_path().join("sys/class/dmi/id");
fs::create_dir_all(&dmi_path).expect("Failed to create DMI directory");
fs::write(dmi_path.join("sys_vendor"), vendor).expect("Failed to write sys_vendor");
fs::write(dmi_path.join("product_name"), product).expect("Failed to write product_name");
self
}
pub fn add_hwmon(&self, name: &str, temp_label: &str, temp_input: &str) -> &Self {
let hwmon_path = self.base_path().join("sys/class/hwmon/hwmon0");
fs::create_dir_all(&hwmon_path).expect("Failed to create hwmon directory");
fs::write(hwmon_path.join("name"), name).expect("Failed to write hwmon name");
fs::write(hwmon_path.join("temp1_label"), temp_label).expect("Failed to write temp label");
fs::write(hwmon_path.join("temp1_input"), temp_input).expect("Failed to write temp input");
self
}
pub fn add_rapl(&self, name: &str, energy_uj: &str, pl1_uw: &str) -> &Self {
let rapl_path = self.base_path().join("sys/class/powercap/intel-rapl:0");
fs::create_dir_all(&rapl_path).expect("Failed to create RAPL directory");
fs::write(rapl_path.join("name"), name).expect("Failed to write RAPL name");
fs::write(rapl_path.join("energy_uj"), energy_uj).expect("Failed to write energy_uj");
fs::write(rapl_path.join("constraint_0_power_limit_uw"), pl1_uw).expect("Failed to write pl1_uw");
self
}
pub fn add_proc_cmdline(&self, cmdline: &str) -> &Self {
let proc_path = self.base_path().join("proc");
fs::create_dir_all(&proc_path).expect("Failed to create proc directory");
fs::write(proc_path.join("cmdline"), cmdline).expect("Failed to write cmdline");
self
}
}

1
tests/common/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod fakesys;