Files
sharang a22b4497a5
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Canceled after 0s
CI / Deploy Agent (push) Canceled after 0s
CI / Deploy Dashboard (push) Successful in 5m32s
fix(scan): uploads carry their sha256, so change detection compares the bytes (#531)
2026-10-08 21:45:32 +00:00

94 lines
3.2 KiB
Rust

//! Change detection over uploads (#465): a scheduled scan skips a target only
//! while the files uploaded to it are byte for byte the ones its last
//! completed scan read. Drives the real upload API against a live Mongo
//! (`TEST_MONGODB_URI`).
#![allow(clippy::expect_used, clippy::unwrap_used)]
mod common;
use common::TestServer;
use compliance_agent::pipeline::change::{build_id, upload_inputs, upload_unchanged};
use compliance_core::models::{Artifact, LastScan, OnboardedTarget, TargetType};
use sha2::{Digest, Sha256};
async fn create_target(server: &TestServer) -> String {
let created: serde_json::Value = server
.post(
"/api/v1/targets",
&serde_json::json!({ "name": "line", "target_type": "backend_service" }),
)
.await
.json()
.await
.unwrap();
created["data"]["_id"]["$oid"].as_str().unwrap().to_string()
}
/// Upload `bytes` as a source archive and return the target's artifacts.
async fn upload(server: &TestServer, id: &str, bytes: &[u8]) -> Vec<Artifact> {
let form = reqwest::multipart::Form::new()
.text("kind", "source_archive")
.part(
"file",
reqwest::multipart::Part::bytes(bytes.to_vec()).file_name("release.zip"),
);
let target: serde_json::Value = server
.client
.post(format!(
"{}/api/v1/targets/{id}/artifacts/upload",
server.base_url
))
.multipart(form)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
serde_json::from_value(target["data"]["artifacts"].clone()).unwrap()
}
/// The target as the next scan reads it, after a completed scan of `scanned`.
fn after_scan_of(scanned: &[Artifact], now: Vec<Artifact>) -> OnboardedTarget {
let mut read = OnboardedTarget::new("line".into(), TargetType::BackendService);
read.artifacts = scanned.to_vec();
let mut target = OnboardedTarget::new("line".into(), TargetType::BackendService);
target.artifacts = now;
target.last_scan = Some(LastScan {
build: build_id(),
inputs: upload_inputs(&read),
});
target
}
#[tokio::test]
async fn a_new_upload_to_a_scanned_target_is_scanned_again() {
let server = TestServer::start().await;
let id = create_target(&server).await;
// Arrange: upload A, and a completed scan of it.
let first = upload(&server, &id, b"release 1.0").await;
assert_eq!(
first[0].content_hash.as_deref(),
Some(hex::encode(Sha256::digest(b"release 1.0")).as_str()),
"the upload records the sha256 of its bytes"
);
assert!(
upload_unchanged(&after_scan_of(&first, first.clone())),
"nothing new uploaded: a scheduled scan may skip"
);
// Act: upload B to the same target.
let second = upload(&server, &id, b"release 1.1").await;
// Assert: the next scheduled scan runs in full.
assert_eq!(second.len(), 2);
assert!(!upload_unchanged(&after_scan_of(&first, second.clone())));
// A is replaced by B (A removed): still a change.
let only_b: Vec<Artifact> = second[1..].to_vec();
assert!(!upload_unchanged(&after_scan_of(&first, only_b)));
server.cleanup().await;
}