| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use crate::domain::{GateKind, Version}; |
| 14 |
use chrono::{DateTime, Utc}; |
| 15 |
use serde::{Deserialize, Serialize}; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 20 |
pub struct GateOutcome { |
| 21 |
pub status: GateStatus, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[serde(skip_serializing_if = "Option::is_none", default)] |
| 26 |
pub log_ref: Option<LogRef>, |
| 27 |
} |
| 28 |
|
| 29 |
impl GateOutcome { |
| 30 |
pub fn passed(note: PassNote) -> Self { |
| 31 |
Self { status: GateStatus::Passed { note }, log_ref: None } |
| 32 |
} |
| 33 |
pub fn failed(failure: GateFailure) -> Self { |
| 34 |
Self { status: GateStatus::Failed { failure }, log_ref: None } |
| 35 |
} |
| 36 |
pub fn blocked(blocker: GateBlocker) -> Self { |
| 37 |
Self { status: GateStatus::Blocked { blocker }, log_ref: None } |
| 38 |
} |
| 39 |
pub fn with_log_ref(mut self, log_ref: LogRef) -> Self { |
| 40 |
self.log_ref = Some(log_ref); |
| 41 |
self |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub fn is_passed(&self) -> bool { |
| 48 |
matches!(self.status, GateStatus::Passed { .. }) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
pub fn status_str(&self) -> &'static str { |
| 53 |
match self.status { |
| 54 |
GateStatus::Passed { .. } => "passed", |
| 55 |
GateStatus::Failed { .. } => "failed", |
| 56 |
GateStatus::Blocked { .. } => "blocked", |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 62 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 63 |
pub enum GateStatus { |
| 64 |
|
| 65 |
|
| 66 |
Passed { note: PassNote }, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
Failed { failure: GateFailure }, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
Blocked { blocker: GateBlocker }, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 79 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 80 |
pub enum PassNote { |
| 81 |
|
| 82 |
StayedUp { duration_s: u32 }, |
| 83 |
|
| 84 |
|
| 85 |
BurnInElapsed { hours: u32 }, |
| 86 |
|
| 87 |
|
| 88 |
Migrated { backup_path: String }, |
| 89 |
|
| 90 |
TestsPassed { duration_s: u32 }, |
| 91 |
|
| 92 |
OperatorConfirmed { at: DateTime<Utc> }, |
| 93 |
|
| 94 |
|
| 95 |
Legacy { text: String }, |
| 96 |
} |
| 97 |
|
| 98 |
impl PassNote { |
| 99 |
pub fn summary(&self) -> String { |
| 100 |
match self { |
| 101 |
PassNote::StayedUp { duration_s } => format!("stayed up for {duration_s}s"), |
| 102 |
PassNote::BurnInElapsed { hours } => format!("{hours} hours elapsed"), |
| 103 |
PassNote::Migrated { backup_path } => format!("restored {backup_path} + migrated"), |
| 104 |
PassNote::TestsPassed { duration_s } => format!("tests passed in {duration_s}s"), |
| 105 |
PassNote::OperatorConfirmed { at } => format!("operator confirmed at {at}"), |
| 106 |
PassNote::Legacy { text } => text.clone(), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 112 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 113 |
pub enum GateBlocker { |
| 114 |
|
| 115 |
BurnInClockNotStarted, |
| 116 |
|
| 117 |
BurnInRemaining { hours_remaining: u32, hours_total: u32 }, |
| 118 |
|
| 119 |
|
| 120 |
AwaitingOperatorConfirmation, |
| 121 |
|
| 122 |
NoBackupAvailable, |
| 123 |
|
| 124 |
|
| 125 |
ScratchDbUrlUnset, |
| 126 |
|
| 127 |
ArtifactMissing { version: Version }, |
| 128 |
} |
| 129 |
|
| 130 |
impl GateBlocker { |
| 131 |
pub fn summary(&self) -> String { |
| 132 |
match self { |
| 133 |
GateBlocker::BurnInClockNotStarted => "burn-in clock not started".into(), |
| 134 |
GateBlocker::BurnInRemaining { hours_remaining, hours_total } => |
| 135 |
format!("{hours_remaining} hours remaining of {hours_total}"), |
| 136 |
GateBlocker::AwaitingOperatorConfirmation => "waiting on operator confirmation".into(), |
| 137 |
GateBlocker::NoBackupAvailable => "no backup fetched; call /backup/fetch first".into(), |
| 138 |
GateBlocker::ScratchDbUrlUnset => "scratch_db_url unset in daemon config".into(), |
| 139 |
GateBlocker::ArtifactMissing { version } => format!("no artifact for version {version}"), |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 145 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 146 |
pub enum GateFailure { |
| 147 |
|
| 148 |
|
| 149 |
CargoTest { failed_count: u32, first_failed: Option<String> }, |
| 150 |
|
| 151 |
|
| 152 |
MigrationDrift { migration: String }, |
| 153 |
|
| 154 |
|
| 155 |
MigrationModified { migration: String }, |
| 156 |
|
| 157 |
MigrationSqlError { migration: String, sqlstate: Option<String> }, |
| 158 |
|
| 159 |
RestoreFailed { reason: String }, |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
BootPanic { exit_code: Option<i32> }, |
| 164 |
|
| 165 |
BootExitedEarly { exit_code: Option<i32> }, |
| 166 |
|
| 167 |
SpawnFailed { message: String }, |
| 168 |
|
| 169 |
Timeout { gate: GateKind, after_s: u32 }, |
| 170 |
|
| 171 |
|
| 172 |
Unclassified { legacy_detail: Option<String> }, |
| 173 |
} |
| 174 |
|
| 175 |
impl GateFailure { |
| 176 |
pub fn summary(&self) -> String { |
| 177 |
match self { |
| 178 |
GateFailure::CargoTest { failed_count, first_failed: Some(name) } => |
| 179 |
format!("{failed_count} test(s) failed; first: {name}"), |
| 180 |
GateFailure::CargoTest { failed_count, first_failed: None } => |
| 181 |
format!("{failed_count} test(s) failed"), |
| 182 |
GateFailure::MigrationDrift { migration } => |
| 183 |
format!("migration {migration} previously applied but missing"), |
| 184 |
GateFailure::MigrationModified { migration } => |
| 185 |
format!("migration {migration} previously applied but modified"), |
| 186 |
GateFailure::MigrationSqlError { migration, sqlstate: Some(s) } => |
| 187 |
format!("migration {migration} sql error ({s})"), |
| 188 |
GateFailure::MigrationSqlError { migration, sqlstate: None } => |
| 189 |
format!("migration {migration} sql error"), |
| 190 |
GateFailure::RestoreFailed { reason } => format!("restore: {reason}"), |
| 191 |
GateFailure::BootPanic { exit_code: Some(c) } => format!("binary panicked: exit {c}"), |
| 192 |
GateFailure::BootPanic { exit_code: None } => "binary panicked".into(), |
| 193 |
GateFailure::BootExitedEarly { exit_code: Some(c) } => format!("binary exited early: exit {c}"), |
| 194 |
GateFailure::BootExitedEarly { exit_code: None } => "binary exited early".into(), |
| 195 |
GateFailure::SpawnFailed { message } => format!("spawn: {message}"), |
| 196 |
GateFailure::Timeout { gate, after_s } => format!("{gate} timed out after {after_s}s"), |
| 197 |
GateFailure::Unclassified { legacy_detail: Some(d) } => d.clone(), |
| 198 |
GateFailure::Unclassified { legacy_detail: None } => "unclassified failure".into(), |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 212 |
pub struct DeployOutcome { |
| 213 |
pub status: DeployStatus, |
| 214 |
} |
| 215 |
|
| 216 |
impl DeployOutcome { |
| 217 |
pub fn ok() -> Self { Self { status: DeployStatus::Ok } } |
| 218 |
pub fn failed(failure: DeployFailureKind) -> Self { |
| 219 |
Self { status: DeployStatus::Failed { failure } } |
| 220 |
} |
| 221 |
pub fn in_progress() -> Self { Self { status: DeployStatus::InProgress } } |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
pub fn status_str(&self) -> &'static str { |
| 226 |
match self.status { |
| 227 |
DeployStatus::InProgress => "in_progress", |
| 228 |
DeployStatus::Ok => "ok", |
| 229 |
DeployStatus::Failed { .. } => "failed", |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 235 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 236 |
pub enum DeployStatus { |
| 237 |
InProgress, |
| 238 |
Ok, |
| 239 |
Failed { failure: DeployFailureKind }, |
| 240 |
} |
| 241 |
|
| 242 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 243 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 244 |
pub enum DeployFailureKind { |
| 245 |
|
| 246 |
|
| 247 |
NodeUnreachable { detail: String }, |
| 248 |
|
| 249 |
|
| 250 |
RsyncFailed { detail: String }, |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
SymlinkSwapFailed { detail: String }, |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
ServiceRestartFailed { detail: String }, |
| 259 |
|
| 260 |
|
| 261 |
Unclassified { detail: String }, |
| 262 |
} |
| 263 |
|
| 264 |
impl DeployFailureKind { |
| 265 |
pub fn summary(&self) -> String { |
| 266 |
match self { |
| 267 |
DeployFailureKind::NodeUnreachable { detail } => format!("node unreachable: {detail}"), |
| 268 |
DeployFailureKind::RsyncFailed { detail } => format!("rsync: {detail}"), |
| 269 |
DeployFailureKind::SymlinkSwapFailed { detail } => format!("symlink swap: {detail}"), |
| 270 |
DeployFailureKind::ServiceRestartFailed { detail } => format!("service restart: {detail}"), |
| 271 |
DeployFailureKind::Unclassified { detail } => detail.chars().take(200).collect(), |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 281 |
#[serde(transparent)] |
| 282 |
pub struct LogRef(pub String); |
| 283 |
|
| 284 |
impl LogRef { |
| 285 |
pub fn new(version: &Version, gate: GateKind) -> Self { |
| 286 |
Self(format!("{}/{}.log", version, gate.as_str())) |
| 287 |
} |
| 288 |
pub fn as_str(&self) -> &str { &self.0 } |
| 289 |
} |
| 290 |
|
| 291 |
impl std::fmt::Display for LogRef { |
| 292 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } |
| 293 |
} |
| 294 |
|
| 295 |
#[cfg(test)] |
| 296 |
mod tests { |
| 297 |
use super::*; |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn outcome_serialization_is_two_layer_tagged() { |
| 301 |
let o = GateOutcome::failed(GateFailure::MigrationDrift { |
| 302 |
migration: "0047_widgets".into(), |
| 303 |
}); |
| 304 |
let v: serde_json::Value = serde_json::to_value(&o).unwrap(); |
| 305 |
assert_eq!(v["status"]["kind"], "failed"); |
| 306 |
assert_eq!(v["status"]["failure"]["kind"], "migration_drift"); |
| 307 |
assert_eq!(v["status"]["failure"]["migration"], "0047_widgets"); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn outcome_round_trips_through_json() { |
| 312 |
let o = GateOutcome::passed(PassNote::TestsPassed { duration_s: 42 }); |
| 313 |
let s = serde_json::to_string(&o).unwrap(); |
| 314 |
let back: GateOutcome = serde_json::from_str(&s).unwrap(); |
| 315 |
assert!(back.is_passed()); |
| 316 |
assert_eq!(back.status_str(), "passed"); |
| 317 |
} |
| 318 |
|
| 319 |
#[test] |
| 320 |
fn blocked_is_not_passed() { |
| 321 |
let o = GateOutcome::blocked(GateBlocker::BurnInClockNotStarted); |
| 322 |
assert!(!o.is_passed()); |
| 323 |
assert_eq!(o.status_str(), "blocked"); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn log_ref_construction_matches_disk_layout() { |
| 328 |
let v: Version = "0.9.6".parse().unwrap(); |
| 329 |
let lr = LogRef::new(&v, GateKind::CargoTest); |
| 330 |
assert_eq!(lr.as_str(), "0.9.6/cargo_test.log"); |
| 331 |
} |
| 332 |
|
| 333 |
#[test] |
| 334 |
fn unclassified_preserves_legacy_detail() { |
| 335 |
let o = GateOutcome::failed(GateFailure::Unclassified { |
| 336 |
legacy_detail: Some("binary exited early: exit status: 101\n==== stdout ====\n...".into()), |
| 337 |
}); |
| 338 |
let v: serde_json::Value = serde_json::to_value(&o).unwrap(); |
| 339 |
assert_eq!(v["status"]["failure"]["kind"], "unclassified"); |
| 340 |
assert!(v["status"]["failure"]["legacy_detail"] |
| 341 |
.as_str().unwrap().contains("exit status: 101")); |
| 342 |
} |
| 343 |
} |
| 344 |
|