Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for spdx "relationshipType": "PACKAGE_OF" #1186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion modules/analysis/src/endpoints/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
let uri = format!("/api/v2/analysis/dep/{}", urlencoding::encode(purl));
let request: Request = TestRequest::get().uri(&uri).to_request();
let response: Value = app.call_and_read_body_json(request).await;
log::debug!("{response:#?}");
log::debug!("{}", serde_json::to_string_pretty(&response)?);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wish serde's impl of :#? used to_string_pretty. I can remember the former, but never the latter. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100%


let sbom = &response["items"][0];
let matches: Vec<_> = sbom["deps"]
Expand All @@ -677,5 +677,33 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {

assert_eq!(1, matches.len());

let uri = format!(
"/api/v2/analysis/root-component?q={}",
urlencoding::encode("SATELLITE-6.15-RHEL-8")
);
let request: Request = TestRequest::get().uri(&uri).to_request();
let response: Value = app.call_and_read_body_json(request).await;
log::info!("{}", serde_json::to_string_pretty(&response)?);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use log::debug! inside tests. Our default test output is noisy enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your right.. will fix.


let sbom = &response["items"][0];
let matches: Vec<_> = sbom["ancestors"]
.as_array()
.into_iter()
.flatten()
.filter(|m| {
m == &&json!({
"sbom_id": sbom["sbom_id"],
"node_id": m["node_id"],
"relationship": "PackageOf",
"purl": m["purl"], // long list assume it's correct
"cpe": m["cpe"], // long list assume it's correct
"name": "rubygem-google-cloud-compute",
"version": "0.5.0-1.el8sat"
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this is a brittle test. I'd want my expectation to be the minimum required to affirm the test. If some future change adds or takes away one of the fields that has nothing to do with this test, it's still gonna break. Is that good or annoying? I'd rather something like this, maybe even omitting name and version, too:

Suggested change
m == &&json!({
"sbom_id": sbom["sbom_id"],
"node_id": m["node_id"],
"relationship": "PackageOf",
"purl": m["purl"], // long list assume it's correct
"cpe": m["cpe"], // long list assume it's correct
"name": "rubygem-google-cloud-compute",
"version": "0.5.0-1.el8sat"
})
m["sbom_id"] == sbom["sbom_id"]
&& m["relationship"] == "PackageOf"
&& m["name"] == "rubygem-google-cloud-compute"
&& m["version"] == "0.5.0-1.el8sat"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda was thinking about similar lines. Is there an existing function that can test if an actual Value matches a partial set of fields of another expected Value? The assertion would become more concise and less brittle then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of. You can always downcast the Value to an AncestorSummary or DepSummary, but I'm not sure m["name"] is all that better or worse than m.name.

Another option is to use Value::pointer, but I haven't personally tried that.

})
.collect();

assert_eq!(1, matches.len());

Ok(())
}
Loading