mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-13 01:41:59 -04:00
systemd: Improve test support
This commit is contained in:
parent
d1c63b6263
commit
aaeff7577b
1 changed files with 49 additions and 6 deletions
|
@ -181,7 +181,7 @@ pub mod test {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use zbus::fdo;
|
use zbus::{fdo, ObjectServer};
|
||||||
use zbus::zvariant::ObjectPath;
|
use zbus::zvariant::ObjectPath;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -232,6 +232,7 @@ pub mod test {
|
||||||
let path = ObjectPath::try_from(format!("/restart/{mode}/{}", self.job))
|
let path = ObjectPath::try_from(format!("/restart/{mode}/{}", self.job))
|
||||||
.map_err(to_zbus_fdo_error)?;
|
.map_err(to_zbus_fdo_error)?;
|
||||||
self.job += 1;
|
self.job += 1;
|
||||||
|
self.active = String::from("active");
|
||||||
Ok(path.into())
|
Ok(path.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,6 +243,7 @@ pub mod test {
|
||||||
let path = ObjectPath::try_from(format!("/start/{mode}/{}", self.job))
|
let path = ObjectPath::try_from(format!("/start/{mode}/{}", self.job))
|
||||||
.map_err(to_zbus_fdo_error)?;
|
.map_err(to_zbus_fdo_error)?;
|
||||||
self.job += 1;
|
self.job += 1;
|
||||||
|
self.active = String::from("active");
|
||||||
Ok(path.into())
|
Ok(path.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,6 +254,7 @@ pub mod test {
|
||||||
let path = ObjectPath::try_from(format!("/stop/{mode}/{}", self.job))
|
let path = ObjectPath::try_from(format!("/stop/{mode}/{}", self.job))
|
||||||
.map_err(to_zbus_fdo_error)?;
|
.map_err(to_zbus_fdo_error)?;
|
||||||
self.job += 1;
|
self.job += 1;
|
||||||
|
self.active = String::from("inactive");
|
||||||
Ok(path.into())
|
Ok(path.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -264,6 +267,7 @@ pub mod test {
|
||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
_runtime: bool,
|
_runtime: bool,
|
||||||
_force: bool,
|
_force: bool,
|
||||||
|
#[zbus(object_server)] object_server: &ObjectServer,
|
||||||
) -> fdo::Result<(bool, Vec<(String, String, String)>)> {
|
) -> fdo::Result<(bool, Vec<(String, String, String)>)> {
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for file in files {
|
for file in files {
|
||||||
|
@ -276,6 +280,14 @@ pub mod test {
|
||||||
self.states.insert(file.to_string(), EnableState::Enabled);
|
self.states.insert(file.to_string(), EnableState::Enabled);
|
||||||
res.push((String::default(), String::default(), file.to_string()));
|
res.push((String::default(), String::default(), file.to_string()));
|
||||||
}
|
}
|
||||||
|
let path = PathBuf::from("/org/freedesktop/systemd1/unit").join(escape(&file));
|
||||||
|
let mock_unit = object_server
|
||||||
|
.interface::<_, MockUnit>(path.to_string_lossy())
|
||||||
|
.await;
|
||||||
|
if let Ok(mock_unit) = mock_unit {
|
||||||
|
dbg!();
|
||||||
|
mock_unit.get_mut().await.unit_file = String::from("enabled");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok((true, res))
|
Ok((true, res))
|
||||||
}
|
}
|
||||||
|
@ -284,6 +296,7 @@ pub mod test {
|
||||||
&mut self,
|
&mut self,
|
||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
_runtime: bool,
|
_runtime: bool,
|
||||||
|
#[zbus(object_server)] object_server: &ObjectServer,
|
||||||
) -> fdo::Result<Vec<(String, String, String)>> {
|
) -> fdo::Result<Vec<(String, String, String)>> {
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for file in files {
|
for file in files {
|
||||||
|
@ -296,6 +309,13 @@ pub mod test {
|
||||||
self.states.insert(file.to_string(), EnableState::Disabled);
|
self.states.insert(file.to_string(), EnableState::Disabled);
|
||||||
res.push((String::default(), String::default(), file.to_string()));
|
res.push((String::default(), String::default(), file.to_string()));
|
||||||
}
|
}
|
||||||
|
let path = PathBuf::from("/org/freedesktop/systemd1/unit").join(escape(&file));
|
||||||
|
let mock_unit = object_server
|
||||||
|
.interface::<_, MockUnit>(path.to_string_lossy())
|
||||||
|
.await;
|
||||||
|
if let Ok(mock_unit) = mock_unit {
|
||||||
|
mock_unit.get_mut().await.unit_file = String::from("disabled");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
@ -347,8 +367,8 @@ pub mod test {
|
||||||
async fn test_unit() {
|
async fn test_unit() {
|
||||||
let mut h = testing::start();
|
let mut h = testing::start();
|
||||||
let mut unit = MockUnit::default();
|
let mut unit = MockUnit::default();
|
||||||
unit.active = String::from("active");
|
unit.active = String::from("inactive");
|
||||||
unit.unit_file = String::from("enabled");
|
unit.unit_file = String::from("disabled");
|
||||||
let connection = h.new_dbus().await.expect("dbus");
|
let connection = h.new_dbus().await.expect("dbus");
|
||||||
connection
|
connection
|
||||||
.request_name("org.freedesktop.systemd1")
|
.request_name("org.freedesktop.systemd1")
|
||||||
|
@ -363,17 +383,40 @@ pub mod test {
|
||||||
.at("/org/freedesktop/systemd1", MockManager::default())
|
.at("/org/freedesktop/systemd1", MockManager::default())
|
||||||
.await
|
.await
|
||||||
.expect("at");
|
.expect("at");
|
||||||
|
let mock_unit = object_server
|
||||||
|
.interface::<_, MockUnit>("/org/freedesktop/systemd1/unit/test_2eservice")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
sleep(Duration::from_millis(10)).await;
|
sleep(Duration::from_millis(10)).await;
|
||||||
|
|
||||||
let unit = SystemdUnit::new(connection.clone(), "test.service")
|
let unit = SystemdUnit::new(connection.clone(), "test.service")
|
||||||
.await
|
.await
|
||||||
.expect("unit");
|
.expect("unit");
|
||||||
assert!(unit.start().await.is_ok());
|
assert_eq!(unit.active().await.unwrap(), false);
|
||||||
assert!(unit.restart().await.is_ok());
|
|
||||||
assert!(unit.stop().await.is_ok());
|
|
||||||
|
|
||||||
|
assert!(unit.start().await.is_ok());
|
||||||
|
assert_eq!(mock_unit.get().await.active, "active");
|
||||||
|
assert_eq!(unit.active().await.unwrap(), true);
|
||||||
|
|
||||||
|
assert!(unit.restart().await.is_ok());
|
||||||
|
assert_eq!(mock_unit.get().await.active, "active");
|
||||||
|
assert_eq!(unit.active().await.unwrap(), true);
|
||||||
|
|
||||||
|
assert!(unit.stop().await.is_ok());
|
||||||
|
assert_eq!(mock_unit.get().await.active, "inactive");
|
||||||
|
assert_eq!(unit.active().await.unwrap(), false);
|
||||||
|
|
||||||
|
assert_eq!(mock_unit.get().await.unit_file, "disabled");
|
||||||
|
assert_eq!(unit.enabled().await.unwrap(), EnableState::Disabled);
|
||||||
|
|
||||||
|
assert!(unit.enable().await.unwrap());
|
||||||
|
assert_eq!(mock_unit.get().await.unit_file, "enabled");
|
||||||
assert_eq!(unit.enabled().await.unwrap(), EnableState::Enabled);
|
assert_eq!(unit.enabled().await.unwrap(), EnableState::Enabled);
|
||||||
|
|
||||||
|
assert!(unit.disable().await.unwrap());
|
||||||
|
assert_eq!(mock_unit.get().await.unit_file, "disabled");
|
||||||
|
assert_eq!(unit.enabled().await.unwrap(), EnableState::Disabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue