WP-10: contract and failing tests for OS and package inventory
Some checks failed
CI / backend (push) Has been cancelled
CI / frontend (push) Has been cancelled
CI / ui (push) Has been cancelled

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Dennis Nemec
2026-09-02 22:19:25 +02:00
parent b6c3ddf040
commit 7b6d242988
21 changed files with 698 additions and 0 deletions

View File

@ -0,0 +1,165 @@
//! Inspector for Debian hosts using dpkg, apt-get and snap.
use std::sync::Arc;
use async_trait::async_trait;
use domain::host::{OsInfo, Package};
use domain::ports::HostInspector;
use domain::DomainError;
use super::command::CommandRunner;
pub struct DebianInspector {
runner: Arc<dyn CommandRunner>,
}
impl DebianInspector {
pub fn new(runner: Arc<dyn CommandRunner>) -> Self {
Self { runner }
}
}
#[async_trait]
impl HostInspector for DebianInspector {
async fn os_info(&self) -> Result<OsInfo, DomainError> {
todo!()
}
async fn packages(&self) -> Result<Vec<Package>, DomainError> {
todo!()
}
}
/// `dpkg-query -W -f '${Package}\t${Version}\t${Status}\n'` → (name, version) of installed packages.
pub fn parse_dpkg(_out: &str) -> Vec<(String, String)> {
todo!()
}
/// `apt-get -s upgrade` → (name, candidate version, is_security) for lines starting with `Inst`.
pub fn parse_apt_simulation(_out: &str) -> Vec<(String, String, bool)> {
todo!()
}
/// `snap list` → (name, version) skipping the header.
pub fn parse_snap_list(_out: &str) -> Vec<(String, String)> {
todo!()
}
/// `/etc/os-release` → (PRETTY_NAME, VERSION_ID).
pub fn parse_os_release(_content: &str) -> (String, String) {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dpkg_keeps_only_installed() {
let out = "bash\t5.2.15-2+b7\tinstall ok installed\nold\t1.0\tdeinstall ok config-files\nzlib1g\t1:1.2.13.dfsg-1\tinstall ok installed\n";
assert_eq!(
parse_dpkg(out),
vec![
("bash".to_string(), "5.2.15-2+b7".to_string()),
("zlib1g".to_string(), "1:1.2.13.dfsg-1".to_string())
]
);
}
#[test]
fn apt_simulation_extracts_candidates_and_security_flag() {
let out = "Reading package lists...\nBuilding dependency tree...\n\
Inst openssl [3.0.15-1~deb12u1] (3.0.16-1~deb12u1 Debian-Security:12/stable-security [amd64])\n\
Inst curl [7.88.1-10+deb12u8] (7.88.1-10+deb12u12 Debian:12.9/stable [amd64]) []\n\
Conf openssl (3.0.16-1~deb12u1 Debian-Security:12/stable-security [amd64])\n";
assert_eq!(
parse_apt_simulation(out),
vec![
("openssl".to_string(), "3.0.16-1~deb12u1".to_string(), true),
("curl".to_string(), "7.88.1-10+deb12u12".to_string(), false)
]
);
}
#[test]
fn snap_list_skips_header() {
let out = "Name Version Rev Tracking Publisher Notes\ncore20 20260410 2866 latest/stable canonical** base\nmicrok8s v1.32.13 8702 1.32/stable canonical** classic\n";
assert_eq!(
parse_snap_list(out),
vec![
("core20".to_string(), "20260410".to_string()),
("microk8s".to_string(), "v1.32.13".to_string())
]
);
}
#[test]
fn os_release_strips_quotes() {
let c = "PRETTY_NAME=\"Debian GNU/Linux 12 (bookworm)\"\nNAME=\"Debian GNU/Linux\"\nVERSION_ID=\"12\"\n";
assert_eq!(
parse_os_release(c),
(
"Debian GNU/Linux 12 (bookworm)".to_string(),
"12".to_string()
)
);
}
struct Canned;
#[async_trait]
impl CommandRunner for Canned {
async fn run(
&self,
program: &str,
args: &[&str],
) -> Result<super::super::Output, DomainError> {
let stdout = match (program, args.first().copied()) {
("dpkg-query", _) => "bash\t5.2\tinstall ok installed\nopenssl\t3.0.15\tinstall ok installed\n",
("apt-get", Some("-s")) => "Inst openssl [3.0.15] (3.0.16 Debian-Security:12/stable-security [amd64])\n",
("apt-get", _) => "",
("snap", _) => "Name Version Rev Tracking Publisher Notes\nmicrok8s v1.32.13 8702 1.32/stable canonical** classic\n",
("uname", _) => "6.1.0-42-amd64\n",
("hostname", _) => "srv\n",
_ => "",
};
Ok(super::super::Output {
stdout: stdout.into(),
stderr: String::new(),
success: true,
})
}
async fn read_file(&self, path: &str) -> Result<Option<String>, DomainError> {
Ok(match path {
"/etc/os-release" => Some(
"PRETTY_NAME=\"Debian GNU/Linux 12 (bookworm)\"\nVERSION_ID=\"12\"\n".into(),
),
"/proc/uptime" => Some("12345.67 40000.00\n".into()),
"/var/run/reboot-required" => Some(String::new()),
_ => None,
})
}
}
#[tokio::test]
async fn inspector_combines_sources() {
let i = DebianInspector::new(Arc::new(Canned));
let os = i.os_info().await.unwrap();
assert_eq!(os.hostname, "srv");
assert_eq!(os.version, "12");
assert_eq!(os.kernel, "6.1.0-42-amd64");
assert_eq!(os.uptime_secs, 12345);
assert!(os.reboot_required);
let pkgs = i.packages().await.unwrap();
assert_eq!(pkgs.len(), 3);
let openssl = pkgs.iter().find(|p| p.name == "openssl").unwrap();
assert_eq!(openssl.candidate.as_deref(), Some("3.0.16"));
assert!(openssl.is_security);
assert_eq!(
pkgs.iter().find(|p| p.name == "bash").unwrap().candidate,
None
);
assert_eq!(
pkgs.iter().find(|p| p.name == "microk8s").unwrap().source,
domain::host::PackageSource::Snap
);
}
}