Files
pruefwerk/compliance-agent/tests/integration/api/sbom.rs
T
sharang 9d4e46c34f
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 5m34s
fix(agent): cascade delete reads ids raw; integration tests fixed (#584)
2026-10-30 08:28:13 +00:00

86 lines
2.7 KiB
Rust

use crate::common::TestServer;
use compliance_core::models::{InImage, InImageBasis, SbomEntry, YOCTO_RECIPE_MANAGER};
/// A Yocto recipe component, in `pump-image` when `in_image`.
fn recipe(name: &str, in_image: bool) -> SbomEntry {
let mut entry = SbomEntry::new(
"yocto-target".into(),
name.into(),
"1.0".into(),
YOCTO_RECIPE_MANAGER.into(),
);
entry.purl = Some(format!("pkg:generic/{name}@1.0"));
entry.in_image = in_image.then(|| InImage {
basis: InImageBasis::Declared,
images: vec!["pump-image".into()],
});
entry
}
/// The names of the components a list request returns, in its order.
async fn names(server: &TestServer, query: &str) -> Vec<String> {
let resp = server
.get(&format!("/api/v1/sbom?repo_id=yocto-target{query}"))
.await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
body["data"]
.as_array()
.unwrap()
.iter()
.map(|e| e["name"].as_str().unwrap().to_string())
.collect()
}
#[tokio::test]
async fn the_sbom_lists_components_in_the_image_first_and_filters_by_it() {
let server = TestServer::start().await;
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
let client = mongodb::Client::with_uri_str(&mongodb_uri).await.unwrap();
// The dev tenant's database, which the API reads.
let db = client.database(&server.tenant_db_name());
let entries = [
recipe("aaa-tools", false),
recipe("busybox", true),
recipe("pump-fw", false),
recipe("zlib", true),
];
db.collection::<SbomEntry>("sbom_entries")
.insert_many(&entries)
.await
.unwrap();
assert_eq!(
names(&server, "").await,
["busybox", "zlib", "aaa-tools", "pump-fw"],
"nothing is dropped; the image's components come first"
);
assert_eq!(names(&server, "&in_image=true").await, ["busybox", "zlib"]);
assert_eq!(
names(&server, "&in_image=false").await,
["aaa-tools", "pump-fw"]
);
let resp = server
.get("/api/v1/sbom/export?repo_id=yocto-target&format=cyclonedx")
.await;
assert_eq!(resp.status(), 200);
let cdx: serde_json::Value = resp.json().await.unwrap();
let busybox = cdx["components"]
.as_array()
.unwrap()
.iter()
.find(|c| c["name"] == "busybox")
.expect("busybox");
assert!(busybox["properties"]
.as_array()
.unwrap()
.iter()
.any(
|p| p["name"] == "pruefwerk:component:in_image" && p["value"] == "declared: pump-image"
));
server.cleanup().await;
}