//! Append-only release status transition tests (`src/release/status.rs`). //! //! The module is included by path so its `crate::` imports resolve inside this //! integration crate; `tests/common` supplies the scripted PDS fake. use std::sync::Arc; use serde::Deserialize; use serde_json::json; use vuln_notif::constants::STATUS_COLLECTION; use vuln_notif::db::DbHandle; use vuln_notif::db::model::{ReleaseStatusEventRow, ReleaseStatusRow}; use vuln_notif::pds::{Publisher, PublisherConfig}; use vuln_notif::release::source::StatusInfo; pub use vuln_notif::{constants, db, links, pds, records, release, time}; #[path = "../src/release/status.rs"] mod status; mod common; use common::{FakePds, FakeTime, response}; use status::{StatusCounters, StatusTransition, apply_transitions, plan_transitions}; const REPO: &str = "did:plc:test"; const SUBJECT: &str = "at://did:plc:test/software.release.version/acme-widget:1.2.3"; const OBSERVED: &str = include_str!("fixtures/status/observed.json"); const EXPECTED_EVENTS: &str = include_str!("fixtures/status/expected_events.json"); const OBSERVED_SECURITY: &str = include_str!("fixtures/status/observed_security.json"); const OBSERVED_DEPRECATED: &str = include_str!("fixtures/status/observed_deprecated.json"); const OBSERVED_DEPRECATED_CHANGED: &str = include_str!("fixtures/status/observed_deprecated_changed.json"); const OBSERVED_DEPRECATED_PLAIN: &str = include_str!("fixtures/status/observed_deprecated_plain.json"); const CURRENT_DEPRECATED: &str = include_str!("fixtures/status/current_deprecated.json"); #[derive(Deserialize)] struct SignalFixture { status: String, #[serde(default)] kind: Option, #[serde(default)] reason: String, } impl SignalFixture { fn info(&self) -> StatusInfo { StatusInfo { status: self.status.clone(), kind: self.kind.clone(), reason: self.reason.clone(), } } } #[derive(Deserialize)] struct CurrentFixture { subject_uri: String, status: String, } impl CurrentFixture { fn row(&self) -> ReleaseStatusRow { ReleaseStatusRow { repo_did: REPO.to_owned(), subject_uri: self.subject_uri.clone(), status: self.status.clone(), ..ReleaseStatusRow::default() } } } #[derive(Deserialize)] struct EventFixture { status: String, #[serde(default)] kind: Option, #[serde(default)] reason: String, source: String, } fn signals(raw: &str) -> Vec { let fixtures: Vec = serde_json::from_str(raw).expect("signal fixture"); fixtures.iter().map(SignalFixture::info).collect() } fn expected_events(raw: &str) -> Vec { serde_json::from_str(raw).expect("event fixture") } struct Fixture { _dir: tempfile::TempDir, db: DbHandle, pds: Arc, publisher: Publisher, } async fn fixture() -> Fixture { let dir = tempfile::tempdir().expect("temp dir"); let db = DbHandle::open(dir.path().join("release-status.db")) .await .expect("db"); let pds = FakePds::new(); let time = FakeTime::new(1_700_000_000.0); let governor = common::governor(&db, time.clone()).await; let (logger, _sink) = common::logger(); let publisher = Publisher::with_api( PublisherConfig { pds_url: "http://127.0.0.1:1".to_owned(), handle: "alice.test".to_owned(), app_password: "app-pw".to_owned(), collection: STATUS_COLLECTION.to_owned(), timeout_secs: 5.0, governor, }, pds.clone(), logger, time, ) .expect("publisher"); Fixture { _dir: dir, db, pds, publisher, } } async fn plan( fixture: &Fixture, subject: &str, observed: &[StatusInfo], current: &[ReleaseStatusRow], ) -> Vec { plan_transitions(&fixture.db, REPO, subject, observed, current) .await .expect("plan transitions") } async fn apply( fixture: &Fixture, subject: &str, transitions: &[StatusTransition], ) -> (StatusCounters, Vec) { let events = fixture .db .list_release_status_events(REPO, subject, 0) .await .expect("events"); let counters = apply_transitions(&fixture.publisher, &fixture.db, REPO, transitions, &events).await; let events = fixture .db .list_release_status_events(REPO, subject, 0) .await .expect("events"); (counters, events) } async fn current_rows(fixture: &Fixture, statuses: &[&str]) -> Vec { let mut rows = Vec::new(); for status in statuses { if let Some(row) = fixture .db .get_release_status(REPO, SUBJECT, status) .await .expect("current status") { rows.push(row); } } rows } #[tokio::test] async fn transitions_create_append_only_records_and_rerun_is_a_noop() { let fixture = fixture().await; let observed = signals(OBSERVED); let expected = expected_events(EXPECTED_EVENTS); let transitions = plan(&fixture, SUBJECT, &observed, &[]).await; assert_eq!(transitions.len(), expected.len()); for (transition, expected) in transitions.iter().zip(&expected) { assert_eq!(transition.subject_uri, SUBJECT); assert_eq!(transition.status, expected.status); assert_eq!(transition.kind, expected.kind); assert_eq!(transition.reason, expected.reason); assert_eq!(transition.source, expected.source); } let (counters, events) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!( counters, StatusCounters { planned: expected.len() as u64, created: expected.len() as u64, unchanged: 0, failures: 0, } ); assert_eq!(events.len(), expected.len()); for expected in &expected { let event = events .iter() .find(|event| event.status == expected.status) .expect("event row"); assert_eq!(event.kind, expected.kind); assert_eq!(event.reason.as_deref().unwrap_or(""), expected.reason); assert_eq!(event.cid.as_deref(), Some(common::PUT_CID)); } let puts = fixture.pds.puts(); assert_eq!(puts.len(), expected.len()); for put in &puts { assert_eq!(put.collection, STATUS_COLLECTION); let record = put.record.as_ref().expect("status record"); let expected = expected .iter() .find(|expected| record["status"].as_str() == Some(expected.status.as_str())) .expect("expected status"); assert_eq!(record["subject"].as_str(), Some(SUBJECT)); match &expected.kind { Some(kind) => assert_eq!(record["kind"].as_str(), Some(kind.as_str())), None => assert!(record.get("kind").is_none()), } if expected.reason.is_empty() { assert!(record.get("reason").is_none()); } else { assert_eq!(record["reason"].as_str(), Some(expected.reason.as_str())); } assert!( fixture.pds.record(STATUS_COLLECTION, &put.rkey).is_some(), "record must be stored on the PDS" ); } // Every status record indexes its subject link. let backlinks = fixture .db .backlinks(REPO, SUBJECT, Some(STATUS_COLLECTION), Some("subject"), 10) .await .expect("backlinks"); assert_eq!(backlinks.len(), expected.len()); for link in &backlinks { assert_eq!(link.path, "subject"); assert_eq!(link.subject_uri, SUBJECT); } // Identical re-run: no new records, no new events. let statuses: Vec<&str> = expected.iter().map(|event| event.status.as_str()).collect(); let current = current_rows(&fixture, &statuses).await; assert_eq!(current.len(), expected.len()); let replanned = plan(&fixture, SUBJECT, &observed, ¤t).await; assert!( replanned.is_empty(), "identical signals must be a no-op: {replanned:?}" ); let (counters, events) = apply(&fixture, SUBJECT, &replanned).await; assert_eq!(counters, StatusCounters::default()); assert_eq!(events.len(), expected.len()); assert_eq!(fixture.pds.puts().len(), expected.len()); } #[tokio::test] async fn reason_change_appends_without_rewriting_history() { let fixture = fixture().await; let observed = signals(OBSERVED_DEPRECATED); let transitions = plan(&fixture, SUBJECT, &observed, &[]).await; assert_eq!(transitions.len(), 1); let (counters, events) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!(counters.created, 1); assert_eq!(events.len(), 1); let first = events[0].clone(); assert_eq!(first.reason.as_deref(), Some("superseded by 2.0")); let current = current_rows(&fixture, &["deprecated"]).await; assert_eq!(current.len(), 1); let changed = signals(OBSERVED_DEPRECATED_CHANGED); let transitions = plan(&fixture, SUBJECT, &changed, ¤t).await; assert_eq!(transitions.len(), 1); assert_eq!(transitions[0].reason, "superseded by 3.0"); let (counters, events) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!(counters.planned, 1); assert_eq!(counters.created, 1); assert_eq!(counters.failures, 0); assert_eq!(events.len(), 2); let reasons: Vec<&str> = events .iter() .filter_map(|event| event.reason.as_deref()) .collect(); assert!(reasons.contains(&"superseded by 2.0")); assert!(reasons.contains(&"superseded by 3.0")); assert!( events.iter().any(|event| event.rkey == first.rkey), "the old event must remain" ); assert!(fixture.pds.record(STATUS_COLLECTION, &first.rkey).is_some()); let row = fixture .db .get_release_status(REPO, SUBJECT, "deprecated") .await .expect("status row") .expect("current row"); assert_ne!(row.rkey, first.rkey); assert!(events.iter().any(|event| event.rkey == row.rkey)); assert_eq!(fixture.pds.puts().len(), 2); } #[tokio::test] async fn security_kind_comes_only_from_the_signal() { let fixture = fixture().await; let observed = signals(OBSERVED_SECURITY); let transitions = plan(&fixture, SUBJECT, &observed, &[]).await; assert_eq!(transitions.len(), 2); let yanked = transitions .iter() .find(|transition| transition.status == "yanked") .expect("yanked transition"); assert_eq!(yanked.kind.as_deref(), Some("security")); let archived = transitions .iter() .find(|transition| transition.status == "archived") .expect("archived transition"); assert_eq!( archived.kind, None, "a CVE ID in the reason must not promote kind" ); let (counters, _) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!(counters.created, 2); let records: Vec<_> = fixture .pds .puts() .into_iter() .filter_map(|put| put.record) .collect(); let yanked = records .iter() .find(|record| record["status"].as_str() == Some("yanked")) .expect("yanked record"); assert_eq!(yanked["kind"].as_str(), Some("security")); let archived = records .iter() .find(|record| record["status"].as_str() == Some("archived")) .expect("archived record"); assert!(archived.get("kind").is_none()); } #[tokio::test] async fn cve_signal_alone_never_creates_a_status() { let fixture = fixture().await; let transitions = plan(&fixture, SUBJECT, &[], &[]).await; assert!(transitions.is_empty()); let (counters, events) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!(counters, StatusCounters::default()); assert!(events.is_empty()); assert_eq!(fixture.pds.count("putRecord"), 0); for status in status::STATUSES { assert!( fixture .db .get_release_status(REPO, SUBJECT, status) .await .expect("status row") .is_none() ); } } #[tokio::test] async fn pds_failure_does_not_append_the_local_event() { let fixture = fixture().await; let observed = vec![StatusInfo { status: "archived".to_owned(), kind: None, reason: String::new(), }]; let transitions = plan(&fixture, SUBJECT, &observed, &[]).await; assert_eq!(transitions.len(), 1); fixture.pds.push_put(response( 400, json!({"error": "InvalidRequest", "message": "rejected"}), )); let (counters, events) = apply(&fixture, SUBJECT, &transitions).await; assert_eq!(counters.planned, 1); assert_eq!(counters.created, 0); assert_eq!(counters.failures, 1); assert!(events.is_empty(), "the local event waits for the PDS write"); assert!( fixture .db .get_release_status(REPO, SUBJECT, "archived") .await .expect("status row") .is_none() ); let put = &fixture.pds.puts()[0]; assert!(fixture.pds.record(STATUS_COLLECTION, &put.rkey).is_none()); } #[tokio::test] async fn legacy_current_row_without_events_is_not_duplicated() { let fixture = fixture().await; let current: Vec = serde_json::from_str(CURRENT_DEPRECATED).expect("current fixture"); let current: Vec = current.iter().map(CurrentFixture::row).collect(); let observed = signals(OBSERVED_DEPRECATED_PLAIN); let transitions = plan(&fixture, SUBJECT, &observed, ¤t).await; assert!(transitions.is_empty()); }