manager/root: Add a few more tests

This commit is contained in:
Vicki Pfau 2024-06-21 19:10:38 -07:00
parent 04d4dc74af
commit b9e23b74e2
2 changed files with 80 additions and 7 deletions

View file

@ -7,6 +7,7 @@
*/ */
use anyhow::Result; use anyhow::Result;
use std::ffi::OsStr;
use tokio::fs::File; use tokio::fs::File;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot; use tokio::sync::oneshot;
@ -66,7 +67,7 @@ const ALS_INTEGRATION_PATH: &str = "/sys/devices/platform/AMDI0010:00/i2c-0/i2c-
impl SteamOSManager { impl SteamOSManager {
async fn prepare_factory_reset(&self) -> u32 { async fn prepare_factory_reset(&self) -> u32 {
// Run steamos factory reset script and return true on success // Run steamos factory reset script and return true on success
let res = run_script("/usr/bin/steamos-factory-reset-config", &[""]).await; let res = run_script("/usr/bin/steamos-factory-reset-config", &[] as &[&OsStr]).await;
match res { match res {
Ok(_) => PrepareFactoryReset::RebootRequired as u32, Ok(_) => PrepareFactoryReset::RebootRequired as u32,
Err(_) => PrepareFactoryReset::Unknown as u32, Err(_) => PrepareFactoryReset::Unknown as u32,
@ -312,12 +313,13 @@ mod test {
use crate::daemon::root::RootContext; use crate::daemon::root::RootContext;
use crate::power::test::{format_clocks, read_clocks}; use crate::power::test::{format_clocks, read_clocks};
use crate::power::{self, get_gpu_performance_level}; use crate::power::{self, get_gpu_performance_level};
use crate::process::test::{code, exit, ok};
use crate::testing; use crate::testing;
use tokio::fs::{create_dir_all, write}; use tokio::fs::{create_dir_all, write};
use zbus::{Connection, ConnectionBuilder}; use zbus::{Connection, ConnectionBuilder};
struct TestHandle { struct TestHandle {
_handle: testing::TestHandle, h: testing::TestHandle,
connection: Connection, connection: Connection,
} }
@ -342,11 +344,82 @@ mod test {
.await?; .await?;
Ok(TestHandle { Ok(TestHandle {
_handle: handle, h: handle,
connection, connection,
}) })
} }
#[zbus::proxy(
interface = "com.steampowered.SteamOSManager1.RootManager",
default_path = "/com/steampowered/SteamOSManager1"
)]
trait PrepareFactoryReset {
fn prepare_factory_reset(&self) -> zbus::Result<u32>;
}
#[tokio::test]
async fn prepare_factory_reset() {
let test = start().await.expect("start");
let name = test.connection.unique_name().unwrap();
let proxy = PrepareFactoryResetProxy::new(&test.connection, name.clone())
.await
.unwrap();
test.h.test.process_cb.set(ok);
assert_eq!(
proxy.prepare_factory_reset().await.unwrap(),
PrepareFactoryReset::RebootRequired as u32
);
test.h.test.process_cb.set(code);
assert_eq!(
proxy.prepare_factory_reset().await.unwrap(),
PrepareFactoryReset::Unknown as u32
);
test.h.test.process_cb.set(exit);
assert_eq!(
proxy.prepare_factory_reset().await.unwrap(),
PrepareFactoryReset::Unknown as u32
);
}
#[zbus::proxy(
interface = "com.steampowered.SteamOSManager1.RootManager",
default_path = "/com/steampowered/SteamOSManager1"
)]
trait AlsCalibrationGain {
#[zbus(property(emits_changed_signal = "false"))]
fn als_calibration_gain(&self) -> zbus::Result<f64>;
}
#[tokio::test]
async fn als_calibration_gain() {
let test = start().await.expect("start");
let name = test.connection.unique_name().unwrap();
let proxy = AlsCalibrationGainProxy::new(&test.connection, name.clone())
.await
.unwrap();
test.h
.test
.process_cb
.set(|_, _| Ok((0, String::from("0.0\n"))));
assert_eq!(proxy.als_calibration_gain().await.unwrap(), 0.0);
test.h
.test
.process_cb
.set(|_, _| Ok((0, String::from("1.0\n"))));
assert_eq!(proxy.als_calibration_gain().await.unwrap(), 1.0);
test.h
.test
.process_cb
.set(|_, _| Ok((0, String::from("big\n"))));
assert_eq!(proxy.als_calibration_gain().await.unwrap(), -1.0);
}
#[zbus::proxy( #[zbus::proxy(
interface = "com.steampowered.SteamOSManager1.RootManager", interface = "com.steampowered.SteamOSManager1.RootManager",
default_path = "/com/steampowered/SteamOSManager1" default_path = "/com/steampowered/SteamOSManager1"

View file

@ -222,20 +222,20 @@ pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Resu
} }
#[cfg(test)] #[cfg(test)]
mod test { pub(crate) mod test {
use super::*; use super::*;
use crate::testing; use crate::testing;
use nix::sys::signal::Signal; use nix::sys::signal::Signal;
fn ok(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn ok(_: &str, _: &[&OsStr]) -> Result<(i32, String)> {
Ok((0, String::from("ok"))) Ok((0, String::from("ok")))
} }
fn code(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn code(_: &str, _: &[&OsStr]) -> Result<(i32, String)> {
Ok((1, String::from("code"))) Ok((1, String::from("code")))
} }
fn exit(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn exit(_: &str, _: &[&OsStr]) -> Result<(i32, String)> {
Err(anyhow!("oops!")) Err(anyhow!("oops!"))
} }