1
use super::*;
2

            
3
2
fn report_with(migrated: usize, quarantined: Vec<(Uuid, String)>) -> FleetReport {
4
2
    FleetReport {
5
2
        migrated,
6
2
        quarantined,
7
2
        ..FleetReport::default()
8
2
    }
9
2
}
10

            
11
#[test]
12
1
fn a_run_that_verified_everything_is_healthy() {
13
1
    assert!(report_with(3, Vec::new()).is_healthy());
14
1
    assert!(FleetReport::default().is_healthy());
15
1
}
16

            
17
#[test]
18
1
fn a_single_quarantine_makes_the_run_degraded() {
19
1
    let report = report_with(9, vec![(Uuid::nil(), "unreachable".to_owned())]);
20

            
21
1
    assert!(
22
1
        !report.is_healthy(),
23
        "one tenant left behind must be visible in the exit code, not averaged away"
24
    );
25
1
}
26

            
27
#[test]
28
1
fn the_summary_reports_every_category() {
29
1
    let report = FleetReport {
30
1
        migrated: 2,
31
1
        quarantined: vec![(Uuid::nil(), "boom".to_owned())],
32
1
        job_failed: vec![(Uuid::nil(), "lock".to_owned())],
33
1
        unresolved: vec![Uuid::nil()],
34
1
        unpublished: vec![(Uuid::nil(), "write failed".to_owned())],
35
1
    };
36

            
37
1
    assert_eq!(
38
1
        report.summary(),
39
1
        "migrated 2, quarantined 1, job failed 1, unresolved 1, unpublished 1"
40
    );
41
1
}
42

            
43
#[test]
44
1
fn a_tenant_this_run_never_verified_is_not_a_success() {
45
    // Another worker held the claim to the end. It may since have died, and
46
    // nothing would say so — exiting 0 here would gate a rollout on a database
47
    // no one has looked at.
48
1
    let report = FleetReport {
49
1
        unresolved: vec![Uuid::nil()],
50
1
        ..FleetReport::default()
51
1
    };
52

            
53
1
    assert!(!report.is_healthy());
54
1
}
55

            
56
#[test]
57
1
fn a_verdict_that_could_not_be_written_fails_the_run() {
58
    // The dangerous case: the tenant failed, but its quarantine did not stick,
59
    // so it is still being served on the wrong schema.
60
1
    let report = FleetReport {
61
1
        migrated: 5,
62
1
        unpublished: vec![(Uuid::nil(), "directory unreachable".to_owned())],
63
1
        ..FleetReport::default()
64
1
    };
65

            
66
1
    assert!(!report.is_healthy());
67
1
}
68

            
69
#[test]
70
1
fn outcomes_carry_their_reason() {
71
1
    assert_eq!(Outcome::Migrated, Outcome::Migrated);
72
1
    assert_ne!(Outcome::Migrated, Outcome::Unresolved);
73
1
    assert_eq!(
74
1
        Outcome::Quarantined("no such database".to_owned()),
75
1
        Outcome::Quarantined("no such database".to_owned())
76
    );
77
1
}
78

            
79
#[test]
80
1
fn a_directory_at_the_current_schema_is_aligned() {
81
1
    let drift = schema_drift(&[1, 2, 3, 5, 6], &[1, 2, 3, 5, 6]);
82

            
83
1
    assert!(drift.is_aligned());
84
1
}
85

            
86
#[test]
87
1
fn a_directory_behind_this_binary_is_reported_as_missing() {
88
    // The Job reconciled before its own release migrated the global database.
89
1
    let drift = schema_drift(&[1, 2, 3, 5, 6], &[1, 2, 3]);
90

            
91
1
    assert_eq!(drift.missing, vec![5, 6]);
92
1
    assert!(drift.unknown.is_empty());
93
1
    assert!(!drift.is_aligned());
94
1
}
95

            
96
#[test]
97
1
fn a_directory_ahead_of_this_binary_is_reported_as_unknown() {
98
    // A rolled-back image, or one whose migration files were edited in place
99
    // after being applied. Passing this would fail EVERY tenant on a
100
    // disagreement that is not the tenant's, and those quarantines would outrank
101
    // the correct run's because they are written later.
102
1
    let drift = schema_drift(&[1, 2, 3], &[1, 2, 3, 5, 6]);
103

            
104
1
    assert_eq!(drift.unknown, vec![5, 6]);
105
1
    assert!(drift.missing.is_empty());
106
1
    assert!(!drift.is_aligned());
107
1
}
108

            
109
#[test]
110
1
fn drift_is_judged_by_version_not_by_count() {
111
    // Five applied, but the wrong five. Counting would call this aligned.
112
1
    let drift = schema_drift(&[1, 2, 3, 5, 6], &[1, 2, 3, 4, 7]);
113

            
114
1
    assert_eq!(drift.missing, vec![5, 6]);
115
1
    assert_eq!(drift.unknown, vec![4, 7]);
116
1
}
117

            
118
#[test]
119
1
fn a_failed_data_job_degrades_the_run_without_a_quarantine() {
120
1
    let report = FleetReport {
121
1
        migrated: 1,
122
1
        job_failed: vec![(Uuid::nil(), "lock timeout".to_owned())],
123
1
        ..FleetReport::default()
124
1
    };
125

            
126
1
    assert!(!report.is_healthy());
127
1
    assert!(report.quarantined.is_empty());
128
1
}
129

            
130
#[tokio::test(start_paused = true)]
131
1
async fn a_silent_server_hits_the_connect_deadline() -> anyhow::Result<()> {
132
1
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
133
1
    let dsn = format!("postgres://u@{}/db", listener.local_addr()?);
134

            
135
1
    let err = connect(&dsn)
136
1
        .await
137
1
        .expect_err("nothing answers the handshake");
138

            
139
1
    assert!(matches!(err, DBError::ConnectTimedOut), "{err:?}");
140
1
    assert!(err.is_unavailable());
141
2
    Ok(())
142
1
}