1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Identity migration admin tool: plan grouping, dependency-ordered apply,
//! cursor resume, idempotence, verification, snapshots, and purl slug
//! normalization.
mod common;
use std::path::PathBuf;
use std::sync::Arc;
use serde_json::{Value, json};
use tempfile::TempDir;
use common::*;
use vuln_notif::constants::{
ATPROTO_COLLECTION, CHANNEL_COLLECTION, MANIFEST_COLLECTION, PROJECT_COLLECTION,
PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION,
};
use vuln_notif::db::DbHandle;
use vuln_notif::db::model::SoftwareMark;
use vuln_notif::migrate::apply::{ApplyOptions, apply_plan};
use vuln_notif::migrate::plan::build_plan;
use vuln_notif::migrate::snapshot::write_snapshot;
use vuln_notif::migrate::verify::verify_plan;
use vuln_notif::migrate::{MigrationAction, normalize_purl_slugs};
use vuln_notif::pds::Publisher;
const LEGACY_SLUG: &str = "red-hat-red-hat-enterprise-linux-8";
const CANONICAL_SLUG: &str = "red-hat-enterprise-linux";
const CVE_ID: &str = "CVE-2024-0001";
const PURL: &str = "pkg:rpm/redhat/enterprise-linux@8";
struct Fixture {
_dir: TempDir,
cache: PathBuf,
db: DbHandle,
pds: Arc<FakePds>,
publisher: Publisher,
}
impl Fixture {
fn legacy_uri(&self, collection: &str) -> String {
record_uri(REPO, collection, LEGACY_SLUG)
}
fn project_uri(&self, slug: &str) -> String {
record_uri(REPO, PROJECT_COLLECTION, slug)
}
}
async fn seed_local(db: &DbHandle) {
let cve = json!({
"id": CVE_ID,
"configurations": [{"nodes": [{"cpeMatch": [
{"criteria": "cpe:2.3:o:redhat:enterprise_linux:9.0:*:*:*:*:*:*:*"}
]}]}],
"affected": [
{
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 8",
"version": "8.0",
"cpes": ["cpe:2.3:o:redhat:enterprise_linux:8.0:*:*:*:*:*:*:*"],
},
{
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 9",
"version": "9.4",
"cpes": ["cpe:2.3:o:redhat:enterprise_linux:9.0:*:*:*:*:*:*:*"],
},
],
});
db.upsert_cves(REPO, &[cve]).await.expect("seed cves");
for (slug, part, vendor, product) in [
(LEGACY_SLUG, "o", "Red Hat", "Red Hat Enterprise Linux 8"),
("redhat-enterprise-linux", "o", "redhat", "enterprise_linux"),
] {
db.mark_software(SoftwareMark {
repo_did: REPO.to_owned(),
slug: slug.to_owned(),
part: part.to_owned(),
vendor: vendor.to_owned(),
product: product.to_owned(),
project_uri: String::new(),
project_cid: String::new(),
software_uri: String::new(),
software_cid: String::new(),
})
.await
.expect("seed software");
}
// Legacy rows stored the full AT-URI instead of the slug.
db.mark_purl(
REPO,
PURL,
&record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG),
"at://seed/purl",
"bafyseed",
)
.await
.expect("seed purl");
// The db actor keeps writes in its transaction until an explicit commit.
db.commit().await.expect("commit seed");
}
fn seed_remote(pds: &FakePds) {
let project = record_uri(REPO, PROJECT_COLLECTION, LEGACY_SLUG);
let version_uri = record_uri(REPO, VERSION_COLLECTION, &format!("{LEGACY_SLUG}:8.0"));
let manifest_uri = record_uri(REPO, MANIFEST_COLLECTION, "3manifest");
let software_uri = record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG);
let purl_rkey = "purlhash";
pds.seed_record(
PROJECT_COLLECTION,
LEGACY_SLUG,
json!({"$type": PROJECT_COLLECTION, "name": LEGACY_SLUG}),
);
pds.seed_record(
CHANNEL_COLLECTION,
&format!("{LEGACY_SLUG}:8"),
json!({
"$type": CHANNEL_COLLECTION,
"project": project,
"name": "8",
"version": strong_ref(&version_uri, PUT_CID),
}),
);
pds.seed_record(
VERSION_COLLECTION,
&format!("{LEGACY_SLUG}:8.0"),
json!({
"$type": VERSION_COLLECTION,
"project": project,
"version": "8.0",
"manifest": strong_ref(&manifest_uri, PUT_CID),
}),
);
pds.seed_record(
MANIFEST_COLLECTION,
"3manifest",
json!({"$type": MANIFEST_COLLECTION, "project": project, "artifacts": []}),
);
pds.seed_record(
STATUS_COLLECTION,
"3status",
json!({"$type": STATUS_COLLECTION, "subject": version_uri, "status": "active"}),
);
pds.seed_record(
SOFTWARE_COLLECTION,
LEGACY_SLUG,
json!({
"$type": SOFTWARE_COLLECTION,
"project": strong_ref(&project, PUT_CID),
"part": "o",
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 8",
}),
);
pds.seed_record(
PURL_COLLECTION,
purl_rkey,
json!({
"$type": PURL_COLLECTION,
"purl": PURL,
"software": [strong_ref(&software_uri, "bafyold")],
}),
);
pds.seed_record(
ATPROTO_COLLECTION,
CVE_ID,
json!({
"$type": ATPROTO_COLLECTION,
"createdAt": "2026-01-01T00:00:00.000Z",
"cve": CVE_ID,
"nvd": strong_ref(&record_uri(REPO, COLLECTION, CVE_ID), PUT_CID),
"software": [{
"project": strong_ref(&project, PUT_CID),
"part": "o",
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 8",
"versions": [strong_ref(&version_uri, PUT_CID)],
}],
}),
);
}
async fn setup() -> Fixture {
let dir = tempfile::tempdir().expect("tempdir");
let cache = dir.path().join("cache.db");
let db = DbHandle::open(&cache).await.expect("cache db");
seed_local(&db).await;
let pds = FakePds::new();
seed_remote(&pds);
let publisher = make_publisher(pds.clone(), &db);
publisher.login().await.expect("login");
Fixture {
_dir: dir,
cache,
db,
pds,
publisher,
}
}
async fn plan_for(fixture: &Fixture) -> vuln_notif::migrate::MigrationPlan {
build_plan(&fixture.publisher, REPO, &fixture.cache, None)
.await
.expect("plan")
}
fn write_calls(pds: &FakePds) -> Vec<String> {
pds.calls()
.into_iter()
.filter(|call| {
matches!(
call.method,
"putRecord" | "createRecord" | "deleteRecord" | "applyWrites" | "uploadBlob"
)
})
.map(|call| call.method.to_owned())
.collect()
}
#[tokio::test]
async fn plan_groups_rhel_variants_and_writes_nothing() {
let fixture = setup().await;
let baseline = fixture.pds.calls().len();
let plan = plan_for(&fixture).await;
let create = plan
.entries
.iter()
.find(|entry| entry.action == MigrationAction::CreateProject)
.expect("create canonical project");
assert_eq!(create.canonical_slug.as_deref(), Some(CANONICAL_SLUG));
let software = plan
.entries
.iter()
.find(|entry| {
entry.action == MigrationAction::RepointSoftware
&& entry.legacy_slug.as_deref() == Some(LEGACY_SLUG)
})
.expect("repoint legacy software");
assert_eq!(software.canonical_slug.as_deref(), Some(CANONICAL_SLUG));
for collection in [
PROJECT_COLLECTION,
CHANNEL_COLLECTION,
VERSION_COLLECTION,
MANIFEST_COLLECTION,
STATUS_COLLECTION,
] {
assert!(
plan.entries.iter().any(|entry| {
entry.action == MigrationAction::DeleteReleaseRecord
&& entry
.legacy_uri
.as_deref()
.is_some_and(|uri| uri.contains(&format!("/{collection}/")))
}),
"missing delete action for {collection}"
);
}
assert!(
plan.entries
.iter()
.any(|entry| entry.action == MigrationAction::RepointCve)
);
assert!(
plan.entries
.iter()
.any(|entry| entry.action == MigrationAction::RepointPurl)
);
assert!(
plan.entries
.iter()
.any(|entry| entry.action == MigrationAction::NormalizePurlSlug)
);
// Dry-run plan performs reads only.
assert!(write_calls(&fixture.pds).is_empty());
assert!(
fixture.pds.calls()[baseline..]
.iter()
.all(|call| call.method == "listRecords")
);
// The local cache is untouched: no cursor rows, purl stays raw.
assert!(
fixture
.db
.list_identity_migration(REPO, None, 0)
.await
.expect("cursor rows")
.is_empty()
);
let purl = fixture
.db
.get_purl(REPO, PURL)
.await
.expect("purl")
.expect("purl row");
assert!(purl.software_slug.starts_with("at://"));
}
#[tokio::test]
async fn apply_is_ordered_batched_resumable_and_idempotent() {
let fixture = setup().await;
let plan = plan_for(&fixture).await;
// Create the canonical project, then fail the first software repoint.
fixture.pds.push_put(response(
200,
json!({
"uri": fixture.project_uri(CANONICAL_SLUG),
"cid": PUT_CID,
}),
));
fixture.pds.push_put(response(
400,
json!({"error": "InvalidRequest", "message": "injected failure"}),
));
let options = ApplyOptions {
batch: 2,
limit: 0,
resume: false,
};
let error = apply_plan(&fixture.publisher, &fixture.db, &plan, &options)
.await
.expect_err("injected failure");
assert!(error.to_string().contains("injected failure"), "{error}");
let rows = fixture
.db
.list_identity_migration(REPO, None, 0)
.await
.expect("cursor rows");
assert!(
rows.iter()
.any(|row| row.action.as_deref() == Some("create_project") && row.status == "done")
);
assert!(
rows.iter()
.any(|row| row.action.as_deref() == Some("repoint_software") && row.status == "error")
);
// Resume skips completed entries, retries the failed one, and batches the
// five legacy deletes two at a time.
let resumed = apply_plan(
&fixture.publisher,
&fixture.db,
&plan,
&ApplyOptions {
batch: 2,
limit: 0,
resume: true,
},
)
.await
.expect("resume apply");
assert_eq!(resumed.deleted, 5);
assert_eq!(
fixture
.db
.list_identity_migration(REPO, Some("done"), 0)
.await
.expect("done rows")
.len(),
plan.entries.len()
);
let calls = fixture.pds.calls();
let writes: Vec<&ApiCall> = calls
.iter()
.filter(|call| {
matches!(
call.method,
"putRecord" | "createRecord" | "deleteRecord" | "applyWrites"
)
})
.collect();
let first = |collection: &str| {
writes
.iter()
.position(|call| call.method == "putRecord" && call.collection == collection)
.expect("put for collection")
};
assert!(first(PROJECT_COLLECTION) < first(SOFTWARE_COLLECTION));
assert!(first(SOFTWARE_COLLECTION) < first(PURL_COLLECTION));
assert!(first(PURL_COLLECTION) < first(ATPROTO_COLLECTION));
let apply_calls: Vec<&ApiCall> = writes
.iter()
.copied()
.filter(|call| call.method == "applyWrites")
.collect();
assert_eq!(apply_calls.len(), 3);
assert!(
apply_calls
.iter()
.all(|call| call.writes.as_ref().is_some_and(|writes| writes.len() <= 2))
);
let deleted: usize = apply_calls
.iter()
.map(|call| call.writes.as_ref().map_or(0, Vec::len))
.sum();
assert_eq!(deleted, 5);
let last_put = writes
.iter()
.rposition(|call| call.method == "putRecord")
.expect("put");
let first_apply = writes
.iter()
.position(|call| call.method == "applyWrites")
.expect("apply");
assert!(last_put < first_apply);
// A second apply is a no-op.
let before = write_calls(&fixture.pds).len();
let second = apply_plan(
&fixture.publisher,
&fixture.db,
&plan,
&ApplyOptions {
batch: 2,
limit: 0,
resume: false,
},
)
.await
.expect("second apply");
assert_eq!(second.applied, 0);
assert_eq!(second.deleted, 0);
assert_eq!(write_calls(&fixture.pds).len(), before);
}
#[tokio::test]
async fn verify_catches_stale_cid_and_legacy_cve_ref() {
let fixture = setup().await;
let plan = plan_for(&fixture).await;
apply_plan(
&fixture.publisher,
&fixture.db,
&plan,
&ApplyOptions::default(),
)
.await
.expect("apply");
// Software record keeps a stale project CID.
fixture.pds.seed_record(
SOFTWARE_COLLECTION,
LEGACY_SLUG,
json!({
"$type": SOFTWARE_COLLECTION,
"project": {
"uri": fixture.project_uri(CANONICAL_SLUG),
"cid": "bafystale",
},
"part": "o",
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 8",
}),
);
// CVE record still points at the deleted legacy project.
fixture.pds.seed_record(
ATPROTO_COLLECTION,
CVE_ID,
json!({
"$type": ATPROTO_COLLECTION,
"createdAt": "2026-01-01T00:00:00.000Z",
"cve": CVE_ID,
"nvd": strong_ref(&record_uri(REPO, COLLECTION, CVE_ID), PUT_CID),
"software": [{
"project": strong_ref(&fixture.legacy_uri(PROJECT_COLLECTION), PUT_CID),
"part": "o",
"vendor": "Red Hat",
"product": "Red Hat Enterprise Linux 8",
}],
}),
);
let report = verify_plan(&fixture.publisher, &fixture.db, &plan)
.await
.expect("verify");
assert!(!report.ok());
assert!(
report
.errors
.iter()
.any(|error| error.contains("stale cid")),
"{:?}",
report.errors
);
assert!(
report
.errors
.iter()
.any(|error| error.contains("legacy project")),
"{:?}",
report.errors
);
assert!(report.stale_refs >= 1);
}
#[tokio::test]
async fn normalize_purl_slugs_converts_at_uris() {
let fixture = setup().await;
let raw = record_uri(REPO, SOFTWARE_COLLECTION, LEGACY_SLUG);
let purl = fixture
.db
.get_purl(REPO, PURL)
.await
.expect("purl")
.expect("purl row");
assert_eq!(purl.software_slug, raw);
let updated = normalize_purl_slugs(&fixture.db, REPO)
.await
.expect("normalize");
assert_eq!(updated, 1);
let purl = fixture
.db
.get_purl(REPO, PURL)
.await
.expect("purl")
.expect("purl row");
assert_eq!(purl.software_slug, LEGACY_SLUG);
}
#[tokio::test]
async fn snapshot_writes_uri_cid_and_value_jsonl() {
let fixture = setup().await;
let plan = plan_for(&fixture).await;
let dir = fixture._dir.path().join("snapshot");
let count = write_snapshot(&fixture.publisher, &plan, &dir)
.await
.expect("snapshot");
assert!(count >= 8);
let path = dir.join(format!("{PROJECT_COLLECTION}.jsonl"));
let raw = std::fs::read_to_string(&path).expect("project snapshot");
let project_uri = fixture.legacy_uri(PROJECT_COLLECTION);
let line = raw
.lines()
.map(|line| serde_json::from_str::<Value>(line).expect("json line"))
.find(|line| line.get("uri").and_then(Value::as_str) == Some(project_uri.as_str()))
.expect("legacy project line");
assert_eq!(line.get("cid").and_then(Value::as_str), Some(PUT_CID));
assert_eq!(
line.get("value")
.and_then(|value| value.get("name"))
.and_then(Value::as_str),
Some(LEGACY_SLUG)
);
}