Repository navigation
Expand file tree
/
Copy pathrollout.rs
More file actions
721 lines (677 loc) · 24.4 KB
/
Copy pathrollout.rs
File metadata and controls
721 lines (677 loc) · 24.4 KB
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Staged rollout: the per-run cap on NEW patches (`scan
//! --max-new-patches`, `patches.maxNewPatches` in socket.yml).
//!
//! Pure planning only. Callers classify each selected `(project, purl)` row
//! against the recorded state ([`Recorded`]), decide eligibility with their
//! planning pass, and hand the rows to [`plan_rollout`], which admits the
//! most critical NEW packages up to the budget and defers the rest. Rows
//! that already carry a patch (ALREADY, UPGRADE) never count toward the cap.
//!
//! The order ([`rollout_cmp`]) is total and time-independent, so the same
//! inputs always give the same plan and repeated runs converge: run k lands
//! the top N, run k+1 sees them as recorded and lands the next N.
pub mod stage;
use std::cmp::{Ordering, Reverse};
use std::collections::{BTreeMap, BTreeSet};
/// What the recorded state (manifest > hosted pins > vendor ledger) says
/// about one selected row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Recorded {
/// No patch recorded for this base purl in this project: NEW.
None,
/// The recorded uuid is the selected one: ALREADY.
Same,
/// A different uuid is recorded and the selection does not supersede
/// it: ALREADY, and the writer keeps `uuid`.
Kept { uuid: String },
/// The selection supersedes the recorded uuid, or the recorded uuid is
/// no longer offered: UPGRADE.
Superseded { old_uuid: String },
}
impl Recorded {
pub fn is_new(&self) -> bool {
matches!(self, Recorded::None)
}
}
/// One selected `(project, purl)` row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Candidate {
/// Repo-relative project root; `""` is the repo root.
pub project: String,
pub purl: String,
/// [`crate::utils::purl_key::PurlKey`] of `purl`: the budget unit, so qualifier twins (a wheel
/// and its sdist, gem platforms), the API's encoded spelling and a
/// lockfile's `Newtonsoft.Json` vs a pin's `newtonsoft.json` are one
/// package.
pub base_purl: String,
/// The selected uuid.
pub uuid: String,
/// `Ecosystem::cli_name`.
pub ecosystem: &'static str,
/// `ranking::max_severity_order` of the selected patch (0 = critical).
pub severity_order: u8,
pub advisory_count: usize,
pub recorded: Recorded,
/// Every check the planning pass can decide without writing passed.
/// Only read for NEW rows.
pub eligible: bool,
/// Already proposed in an open rollout PR (in-memory engine option).
pub in_flight: bool,
}
/// Where the effective cap came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaxNewSource {
Flag,
Env,
File,
Default,
/// A server ceiling (`maxNewPatchesCap`) tightened the value.
Cap,
}
impl MaxNewSource {
pub fn as_str(self) -> &'static str {
match self {
MaxNewSource::Flag => "flag",
MaxNewSource::Env => "env",
MaxNewSource::File => "file",
MaxNewSource::Default => "default",
MaxNewSource::Cap => "cap",
}
}
}
/// The effective cap. `value: None` is unlimited.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MaxNew {
pub value: Option<u32>,
pub source: MaxNewSource,
}
impl MaxNew {
pub const UNLIMITED: MaxNew = MaxNew {
value: None,
source: MaxNewSource::Default,
};
}
/// Resolve the cap: flag > env > file > unlimited, then a server `cap`
/// tightens it (never loosens; it applies to `none` too). For `flag` and
/// `env`, `Some(None)` is an explicit `none`.
pub fn resolve_max_new(
flag: Option<Option<u32>>,
env: Option<Option<u32>>,
file: Option<u32>,
cap: Option<u32>,
) -> MaxNew {
let chosen = if let Some(value) = flag {
MaxNew {
value,
source: MaxNewSource::Flag,
}
} else if let Some(value) = env {
MaxNew {
value,
source: MaxNewSource::Env,
}
} else if let Some(value) = file {
MaxNew {
value: Some(value),
source: MaxNewSource::File,
}
} else {
MaxNew::UNLIMITED
};
match cap {
Some(cap) if chosen.value.is_none_or(|v| v > cap) => MaxNew {
value: Some(cap),
source: MaxNewSource::Cap,
},
_ => chosen,
}
}
/// Rollout order, most urgent first: in-flight, severity, advisory count
/// (descending), ecosystem, base purl, uuid. Total, and free of
/// time-dependent keys.
pub fn rollout_cmp(a: &Candidate, b: &Candidate) -> Ordering {
rollout_key(a).cmp(&rollout_key(b))
}
type RolloutKey<'a> = (bool, u8, Reverse<usize>, &'a str, &'a str, &'a str);
fn rollout_key(c: &Candidate) -> RolloutKey<'_> {
(
!c.in_flight,
c.severity_order,
Reverse(c.advisory_count),
c.ecosystem,
c.base_purl.as_str(),
c.uuid.as_str(),
)
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RolloutCounts {
/// Distinct base purls admitted as NEW.
pub new: u32,
/// Distinct base purls deferred.
pub deferred: u32,
/// UPGRADE rows.
pub upgrade: u32,
/// ALREADY rows (same or kept).
pub already: u32,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RolloutPlan {
/// Rows the writers receive: admitted NEW rows plus every ALREADY and
/// UPGRADE row, in input order. Ineligible NEW rows are in neither
/// list; they keep their own skip reasons.
pub admitted: Vec<Candidate>,
/// Eligible NEW rows over the budget, in rank order, each with the
/// 1-based rank of its base purl among eligible NEW base purls.
pub deferred: Vec<(Candidate, u32)>,
pub counts: RolloutCounts,
/// Budget left for the next project directory; `None` is unlimited.
pub remaining: Option<u32>,
/// Base purls admitted so far in this invocation (input set included).
pub admitted_base_purls: BTreeSet<String>,
}
/// Admit eligible NEW base purls in [`rollout_cmp`] order until the budget
/// is spent. A base purl already in `already_admitted` (an earlier
/// directory of the same invocation) is admitted without spending budget.
/// With a finite cap and `incomplete` set (a lookup failed for a package
/// that could have been NEW), no NEW row is admitted: a missing package
/// must not let lower-ranked ones take its slot.
pub fn plan_rollout(
candidates: Vec<Candidate>,
max_new: &MaxNew,
incomplete: bool,
already_admitted: &BTreeSet<String>,
) -> RolloutPlan {
let mut counts = RolloutCounts::default();
let mut remaining = max_new.value;
let mut admitted_base_purls = already_admitted.clone();
let mut new_rows: Vec<&Candidate> = candidates
.iter()
.filter(|c| c.recorded.is_new() && c.eligible)
.collect();
new_rows.sort_by(|a, b| rollout_cmp(a, b));
// Base purl → (rank, admitted), in rank order.
let mut decisions: BTreeMap<&str, (u32, bool)> = BTreeMap::new();
let mut rank = 0u32;
for row in &new_rows {
if decisions.contains_key(row.base_purl.as_str()) {
continue;
}
rank += 1;
let admit = if incomplete && max_new.value.is_some() {
false
} else if already_admitted.contains(&row.base_purl) {
true
} else {
match remaining.as_mut() {
None => true,
Some(0) => false,
Some(left) => {
*left -= 1;
true
}
}
};
if admit {
counts.new += 1;
admitted_base_purls.insert(row.base_purl.clone());
} else {
counts.deferred += 1;
}
decisions.insert(row.base_purl.as_str(), (rank, admit));
}
let deferred: Vec<(Candidate, u32)> = new_rows
.iter()
.filter_map(|row| match decisions[row.base_purl.as_str()] {
(rank, false) => Some(((*row).clone(), rank)),
_ => None,
})
.collect();
let mut admitted = Vec::new();
for row in &candidates {
match &row.recorded {
Recorded::None => {
if row.eligible && decisions[row.base_purl.as_str()].1 {
admitted.push(row.clone());
}
}
Recorded::Superseded { .. } => {
counts.upgrade += 1;
admitted.push(row.clone());
}
Recorded::Same | Recorded::Kept { .. } => {
counts.already += 1;
admitted.push(row.clone());
}
}
}
RolloutPlan {
admitted,
deferred,
counts,
remaining,
admitted_base_purls,
}
}
/// The severity label for a `ranking::severity_order` value.
pub fn severity_label(order: u8) -> &'static str {
match order {
0 => "critical",
1 => "high",
2 => "medium",
3 => "low",
_ => "unknown",
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::purl_key::PurlKey;
fn row(project: &str, purl: &str, uuid: &str, severity: u8, advisories: usize) -> Candidate {
Candidate {
project: project.to_string(),
purl: purl.to_string(),
base_purl: PurlKey::new(purl).into_string(),
uuid: uuid.to_string(),
ecosystem: purl
.strip_prefix("pkg:")
.and_then(|r| r.split('/').next())
.map(|e| match e {
"npm" => "npm",
"pypi" => "pypi",
"cargo" => "cargo",
"gem" => "gem",
_ => "golang",
})
.unwrap_or("npm"),
severity_order: severity,
advisory_count: advisories,
recorded: Recorded::None,
eligible: true,
in_flight: false,
}
}
fn cap(n: u32) -> MaxNew {
MaxNew {
value: Some(n),
source: MaxNewSource::Flag,
}
}
fn admitted_purls(plan: &RolloutPlan) -> Vec<&str> {
plan.admitted.iter().map(|c| c.purl.as_str()).collect()
}
fn deferred_purls(plan: &RolloutPlan) -> Vec<(&str, u32)> {
plan.deferred
.iter()
.map(|(c, r)| (c.purl.as_str(), *r))
.collect()
}
fn nine() -> Vec<Candidate> {
vec![
row("", "pkg:npm/a@1", "u1", 3, 1),
row("", "pkg:npm/b@1", "u2", 0, 1),
row("", "pkg:npm/c@1", "u3", 1, 1),
row("", "pkg:npm/d@1", "u4", 2, 1),
row("", "pkg:npm/e@1", "u5", 0, 2),
row("", "pkg:npm/f@1", "u6", 4, 1),
row("", "pkg:npm/g@1", "u7", 1, 3),
row("", "pkg:npm/h@1", "u8", 2, 1),
row("", "pkg:npm/i@1", "u9", 3, 1),
]
}
fn permutations<T: Clone>(items: &[T]) -> Vec<Vec<T>> {
if items.len() <= 1 {
return vec![items.to_vec()];
}
let mut out = Vec::new();
for i in 0..items.len() {
let mut rest = items.to_vec();
let head = rest.remove(i);
for mut tail in permutations(&rest) {
tail.insert(0, head.clone());
out.push(tail);
}
}
out
}
#[test]
fn rollout_cmp_is_a_total_order_every_permutation_sorts_the_same() {
let mut in_flight = row("", "pkg:pypi/z@1", "u9", 4, 0);
in_flight.in_flight = true;
let items = vec![
row("", "pkg:npm/b@1", "u2", 1, 1),
row("", "pkg:npm/a@1", "u3", 1, 1),
row("", "pkg:cargo/a@1", "u1", 1, 1),
row("", "pkg:npm/m@1", "u0", 1, 2),
row("", "pkg:npm/a@1", "u1", 1, 1),
in_flight,
];
let expected: Vec<(String, String)> = vec![
("pkg:pypi/z@1".into(), "u9".into()),
("pkg:npm/m@1".into(), "u0".into()),
("pkg:cargo/a@1".into(), "u1".into()),
("pkg:npm/a@1".into(), "u1".into()),
("pkg:npm/a@1".into(), "u3".into()),
("pkg:npm/b@1".into(), "u2".into()),
];
for mut perm in permutations(&items) {
perm.sort_by(rollout_cmp);
let got: Vec<(String, String)> = perm
.iter()
.map(|c| (c.purl.clone(), c.uuid.clone()))
.collect();
assert_eq!(got, expected);
}
}
#[test]
fn severity_then_advisory_count_decide_before_names() {
let mut rows = [
row("", "pkg:npm/a@1", "u", 2, 5),
row("", "pkg:npm/b@1", "u", 0, 1),
row("", "pkg:npm/c@1", "u", 0, 3),
row("", "pkg:npm/d@1", "u", 4, 9),
];
rows.sort_by(rollout_cmp);
let order: Vec<&str> = rows.iter().map(|c| c.purl.as_str()).collect();
assert_eq!(
order,
["pkg:npm/c@1", "pkg:npm/b@1", "pkg:npm/a@1", "pkg:npm/d@1"]
);
}
#[test]
fn a_cap_admits_the_most_critical_and_defers_the_rest_with_ranks() {
let plan = plan_rollout(nine(), &cap(3), false, &BTreeSet::new());
assert_eq!(
admitted_purls(&plan),
["pkg:npm/b@1", "pkg:npm/e@1", "pkg:npm/g@1"]
);
assert_eq!(
deferred_purls(&plan),
[
("pkg:npm/c@1", 4),
("pkg:npm/d@1", 5),
("pkg:npm/h@1", 6),
("pkg:npm/a@1", 7),
("pkg:npm/i@1", 8),
("pkg:npm/f@1", 9),
]
);
assert_eq!(
plan.counts,
RolloutCounts {
new: 3,
deferred: 6,
upgrade: 0,
already: 0
}
);
assert_eq!(plan.remaining, Some(0));
}
#[test]
fn three_runs_roll_nine_packages_forward_and_a_fourth_changes_nothing() {
let mut state = nine();
let mut landed: Vec<Vec<String>> = Vec::new();
for _ in 0..4 {
let plan = plan_rollout(state.clone(), &cap(3), false, &BTreeSet::new());
let new: Vec<String> = plan
.admitted
.iter()
.filter(|c| c.recorded.is_new())
.map(|c| c.purl.clone())
.collect();
for c in &mut state {
if new.contains(&c.purl) {
c.recorded = Recorded::Same;
}
}
landed.push(new);
}
assert_eq!(landed[0], ["pkg:npm/b@1", "pkg:npm/e@1", "pkg:npm/g@1"]);
assert_eq!(landed[1], ["pkg:npm/c@1", "pkg:npm/d@1", "pkg:npm/h@1"]);
assert_eq!(landed[2], ["pkg:npm/a@1", "pkg:npm/f@1", "pkg:npm/i@1"]);
assert!(landed[3].is_empty());
}
#[test]
fn zero_admits_no_new_rows_but_keeps_upgrades_and_already() {
let mut rows = nine();
rows[0].recorded = Recorded::Superseded {
old_uuid: "old".into(),
};
rows[1].recorded = Recorded::Same;
rows[2].recorded = Recorded::Kept { uuid: "k".into() };
let plan = plan_rollout(rows, &cap(0), false, &BTreeSet::new());
assert_eq!(
admitted_purls(&plan),
["pkg:npm/a@1", "pkg:npm/b@1", "pkg:npm/c@1"]
);
assert_eq!(plan.counts.new, 0);
assert_eq!(plan.counts.deferred, 6);
assert_eq!(plan.counts.upgrade, 1);
assert_eq!(plan.counts.already, 2);
assert_eq!(plan.deferred[0].1, 1, "ranks start at 1 among NEW rows");
}
#[test]
fn a_cap_of_one_admits_exactly_the_top_package() {
let plan = plan_rollout(nine(), &cap(1), false, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:npm/e@1"]);
assert_eq!(plan.counts.deferred, 8);
}
#[test]
fn unlimited_and_a_cap_above_the_supply_admit_everything() {
for max in [MaxNew::UNLIMITED, cap(50)] {
let plan = plan_rollout(nine(), &max, false, &BTreeSet::new());
assert_eq!(plan.admitted.len(), 9);
assert!(plan.deferred.is_empty());
assert_eq!(plan.counts.new, 9);
}
let plan = plan_rollout(nine(), &cap(50), false, &BTreeSet::new());
assert_eq!(plan.remaining, Some(41));
let plan = plan_rollout(nine(), &MaxNew::UNLIMITED, false, &BTreeSet::new());
assert_eq!(plan.remaining, None);
}
#[test]
fn ineligible_rows_hold_no_slot_and_are_not_reported_as_deferred() {
let mut rows = nine();
// The top two by rank cannot land.
rows[4].eligible = false; // e
rows[1].eligible = false; // b
let plan = plan_rollout(rows, &cap(2), false, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:npm/c@1", "pkg:npm/g@1"]);
assert!(plan
.deferred
.iter()
.all(|(c, _)| c.purl != "pkg:npm/b@1" && c.purl != "pkg:npm/e@1"));
assert_eq!(plan.counts.deferred, 5);
assert_eq!(plan.deferred[0].1, 3);
}
#[test]
fn incomplete_lookups_admit_nothing_new_under_a_cap() {
let mut rows = nine();
rows[0].recorded = Recorded::Superseded {
old_uuid: "old".into(),
};
let plan = plan_rollout(rows.clone(), &cap(3), true, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:npm/a@1"]);
assert_eq!(plan.counts.deferred, 8);
assert_eq!(plan.remaining, Some(3), "nothing was spent");
// Without a cap there is no slot to steal.
let plan = plan_rollout(rows, &MaxNew::UNLIMITED, true, &BTreeSet::new());
assert_eq!(plan.admitted.len(), 9);
}
#[test]
fn one_package_across_roots_costs_one_slot() {
let rows = vec![
row("services/api", "pkg:npm/qs@6.5.2", "u1", 1, 1),
row("services/web", "pkg:npm/qs@6.5.2", "u1", 1, 1),
row("", "pkg:npm/minimist@1.2.5", "u2", 0, 1),
row("", "pkg:npm/zzz@1.0.0", "u3", 3, 1),
];
let plan = plan_rollout(rows, &cap(2), false, &BTreeSet::new());
assert_eq!(
admitted_purls(&plan),
[
"pkg:npm/qs@6.5.2",
"pkg:npm/qs@6.5.2",
"pkg:npm/minimist@1.2.5"
]
);
assert_eq!(plan.counts.new, 2);
assert_eq!(deferred_purls(&plan), [("pkg:npm/zzz@1.0.0", 3)]);
}
#[test]
fn composer_version_spellings_share_one_budget_slot_across_projects() {
let rows = vec![
row("a", "pkg:composer/psr/log@3.0.2", "u1", 1, 1),
row("a", "pkg:composer/psr/log@3.0.2.0", "u1", 1, 1),
];
let plan = plan_rollout(rows, &cap(1), false, &BTreeSet::new());
assert_eq!(plan.counts.new, 1);
assert_eq!(plan.admitted.len(), 2);
assert!(plan.deferred.is_empty());
let next = plan_rollout(
vec![row("b", "pkg:composer/psr/log@v3.0.2", "u1", 1, 1)],
&cap(0),
false,
&plan.admitted_base_purls,
);
assert_eq!(next.admitted.len(), 1);
assert!(next.deferred.is_empty());
assert_eq!(
PurlKey::new("pkg:composer/psr/log@3.0.2.0").into_string(),
PurlKey::new("pkg:composer/psr/log@v3.0.2").into_string()
);
assert!(!PurlKey::new("pkg:composer/psr/log@not-a-version")
.as_str()
.contains('\u{1}'));
assert_ne!(
PurlKey::new("pkg:composer/psr/log@dev-Feature").into_string(),
PurlKey::new("pkg:composer/psr/log@dev-feature").into_string()
);
}
#[test]
fn qualifier_twins_share_a_base_purl_and_a_rank() {
assert_eq!(
PurlKey::new("pkg:pypi/foo@1.0?artifact_id=abc").into_string(),
PurlKey::new("pkg:pypi/foo@1.0?artifact_id=def").into_string()
);
assert_eq!(
PurlKey::new("pkg:npm/%40scope/x@1.0.0").into_string(),
"pkg:npm/@scope/x@1.0.0"
);
assert_eq!(
PurlKey::new("pkg:nuget/Newtonsoft.Json@13.0.3").into_string(),
PurlKey::new("pkg:nuget/newtonsoft.json@13.0.3").into_string()
);
assert_eq!(
PurlKey::new("pkg:pypi/Foo_Bar@1.0").into_string(),
PurlKey::new("pkg:pypi/foo-bar@1.0").into_string()
);
let rows = vec![
row("", "pkg:pypi/foo@1.0?artifact_id=whl", "u2", 1, 1),
row("", "pkg:pypi/foo@1.0?artifact_id=sdist", "u1", 1, 1),
row("", "pkg:pypi/bar@1.0", "u3", 0, 1),
];
let plan = plan_rollout(rows, &cap(1), false, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:pypi/bar@1.0"]);
assert_eq!(
deferred_purls(&plan),
[
("pkg:pypi/foo@1.0?artifact_id=sdist", 2),
("pkg:pypi/foo@1.0?artifact_id=whl", 2)
]
);
assert_eq!(plan.counts.deferred, 1);
}
// The ecosystem key agrees with the purl type prefix of the base purl
// key below it; the test pins the combined order.
#[test]
fn ties_across_ecosystems_break_by_ecosystem_then_name() {
let rows = vec![
row("", "pkg:npm/x@1", "u1", 1, 1),
row("", "pkg:cargo/x@1", "u2", 1, 1),
row("", "pkg:pypi/x@1", "u3", 1, 1),
row("", "pkg:gem/x@1", "u4", 1, 1),
];
let plan = plan_rollout(rows, &cap(2), false, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:cargo/x@1", "pkg:gem/x@1"]);
assert_eq!(
deferred_purls(&plan),
[("pkg:npm/x@1", 3), ("pkg:pypi/x@1", 4)]
);
}
#[test]
fn in_flight_rows_go_first_whatever_their_severity() {
let mut rows = nine();
rows[5].in_flight = true; // f, unknown severity
let plan = plan_rollout(rows, &cap(1), false, &BTreeSet::new());
assert_eq!(admitted_purls(&plan), ["pkg:npm/f@1"]);
}
#[test]
fn remaining_budget_carries_across_directories() {
let first = vec![
row("a", "pkg:npm/x@1", "u1", 0, 1),
row("a", "pkg:npm/y@1", "u2", 1, 1),
];
let plan_a = plan_rollout(first, &cap(3), false, &BTreeSet::new());
assert_eq!(plan_a.remaining, Some(1));
let carried = MaxNew {
value: plan_a.remaining,
source: MaxNewSource::Flag,
};
let second = vec![
row("b", "pkg:npm/x@1", "u1", 0, 1),
row("b", "pkg:npm/z@1", "u3", 2, 1),
row("b", "pkg:npm/w@1", "u4", 3, 1),
];
let plan_b = plan_rollout(second, &carried, false, &plan_a.admitted_base_purls);
assert_eq!(admitted_purls(&plan_b), ["pkg:npm/x@1", "pkg:npm/z@1"]);
assert_eq!(plan_b.remaining, Some(0));
assert_eq!(deferred_purls(&plan_b), [("pkg:npm/w@1", 3)]);
assert_eq!(plan_b.admitted_base_purls.len(), 3);
}
#[test]
fn an_empty_plan_is_empty() {
let plan = plan_rollout(Vec::new(), &cap(3), true, &BTreeSet::new());
assert!(plan.admitted.is_empty() && plan.deferred.is_empty());
assert_eq!(plan.counts, RolloutCounts::default());
assert_eq!(plan.remaining, Some(3));
}
#[test]
fn resolve_max_new_precedence_table() {
use MaxNewSource::*;
type Case = (
Option<Option<u32>>,
Option<Option<u32>>,
Option<u32>,
Option<u32>,
Option<u32>,
MaxNewSource,
);
let cases: [Case; 10] = [
(None, None, None, None, None, Default),
(None, None, Some(5), None, Some(5), File),
(None, Some(Some(2)), Some(5), None, Some(2), Env),
(Some(Some(1)), Some(Some(2)), Some(5), None, Some(1), Flag),
(Some(None), None, Some(5), None, None, Flag),
(None, Some(None), Some(5), None, None, Env),
(Some(None), None, None, Some(4), Some(4), Cap),
(None, None, None, Some(4), Some(4), Cap),
(Some(Some(9)), None, None, Some(4), Some(4), Cap),
(Some(Some(2)), None, None, Some(4), Some(2), Flag),
];
for (flag, env, file, cap, value, source) in cases {
assert_eq!(
resolve_max_new(flag, env, file, cap),
MaxNew { value, source },
"flag={flag:?} env={env:?} file={file:?} cap={cap:?}"
);
}
assert_eq!(resolve_max_new(None, None, Some(4), Some(4)).source, File);
}
#[test]
fn severity_labels_follow_the_ladder() {
assert_eq!(
(0..=5).map(severity_label).collect::<Vec<_>>(),
["critical", "high", "medium", "low", "unknown", "unknown"]
);
}
}