| 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 { |
| 32 |
status: GateStatus::Passed { note }, |
| 33 |
log_ref: None, |
| 34 |
} |
| 35 |
} |
| 36 |
pub fn failed(failure: GateFailure) -> Self { |
| 37 |
Self { |
| 38 |
status: GateStatus::Failed { failure }, |
| 39 |
log_ref: None, |
| 40 |
} |
| 41 |
} |
| 42 |
pub fn blocked(blocker: GateBlocker) -> Self { |
| 43 |
Self { |
| 44 |
status: GateStatus::Blocked { blocker }, |
| 45 |
log_ref: None, |
| 46 |
} |
| 47 |
} |
| 48 |
#[must_use] |
| 49 |
pub fn with_log_ref(mut self, log_ref: LogRef) -> Self { |
| 50 |
self.log_ref = Some(log_ref); |
| 51 |
self |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub fn is_passed(&self) -> bool { |
| 58 |
matches!(self.status, GateStatus::Passed { .. }) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub fn status_str(&self) -> &'static str { |
| 63 |
match self.status { |
| 64 |
GateStatus::Passed { .. } => "passed", |
| 65 |
GateStatus::Failed { .. } => "failed", |
| 66 |
GateStatus::Blocked { .. } => "blocked", |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 72 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 73 |
pub enum GateStatus { |
| 74 |
|
| 75 |
|
| 76 |
Passed { note: PassNote }, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
Failed { failure: GateFailure }, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
Blocked { blocker: GateBlocker }, |
| 86 |
} |
| 87 |
|
| 88 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 89 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 90 |
pub enum PassNote { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
HealthyProbe { after_ms: u32 }, |
| 97 |
|
| 98 |
|
| 99 |
BurnInElapsed { hours: u32 }, |
| 100 |
|
| 101 |
|
| 102 |
Migrated { backup_path: String }, |
| 103 |
|
| 104 |
TestsPassed { duration_s: u32 }, |
| 105 |
|
| 106 |
OperatorConfirmed { at: DateTime<Utc> }, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
NodesHealthy { nodes: u32 }, |
| 111 |
|
| 112 |
|
| 113 |
Legacy { text: String }, |
| 114 |
} |
| 115 |
|
| 116 |
impl PassNote { |
| 117 |
pub fn summary(&self) -> String { |
| 118 |
match self { |
| 119 |
PassNote::HealthyProbe { after_ms } => format!("served /health in {after_ms}ms"), |
| 120 |
PassNote::BurnInElapsed { hours } => format!("{hours} hours elapsed"), |
| 121 |
PassNote::Migrated { backup_path } => format!("restored {backup_path} + migrated"), |
| 122 |
PassNote::TestsPassed { duration_s } => format!("tests passed in {duration_s}s"), |
| 123 |
PassNote::OperatorConfirmed { at } => format!("operator confirmed at {at}"), |
| 124 |
PassNote::NodesHealthy { nodes } => format!("{nodes} node(s) healthy"), |
| 125 |
PassNote::Legacy { text } => text.clone(), |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 131 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 132 |
pub enum GateBlocker { |
| 133 |
|
| 134 |
BurnInClockNotStarted, |
| 135 |
|
| 136 |
BurnInRemaining { |
| 137 |
hours_remaining: u32, |
| 138 |
hours_total: u32, |
| 139 |
}, |
| 140 |
|
| 141 |
|
| 142 |
AwaitingOperatorConfirmation, |
| 143 |
|
| 144 |
NoBackupAvailable, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
BackupStale { age_hours: i64, max_age_hours: u32 }, |
| 150 |
|
| 151 |
|
| 152 |
ScratchDbUrlUnset, |
| 153 |
|
| 154 |
ArtifactMissing { version: Version }, |
| 155 |
|
| 156 |
|
| 157 |
NoNodesToProbe, |
| 158 |
} |
| 159 |
|
| 160 |
impl GateBlocker { |
| 161 |
pub fn summary(&self) -> String { |
| 162 |
match self { |
| 163 |
GateBlocker::BurnInClockNotStarted => "burn-in clock not started".into(), |
| 164 |
GateBlocker::BurnInRemaining { |
| 165 |
hours_remaining, |
| 166 |
hours_total, |
| 167 |
} => format!("{hours_remaining} hours remaining of {hours_total}"), |
| 168 |
GateBlocker::AwaitingOperatorConfirmation => "waiting on operator confirmation".into(), |
| 169 |
GateBlocker::NoBackupAvailable => "no backup fetched; call /backup/fetch first".into(), |
| 170 |
GateBlocker::BackupStale { |
| 171 |
age_hours, |
| 172 |
max_age_hours, |
| 173 |
} => format!("backup is {age_hours}h old (max {max_age_hours}h); re-run /backup/fetch"), |
| 174 |
GateBlocker::ScratchDbUrlUnset => "scratch_db_url unset in daemon config".into(), |
| 175 |
GateBlocker::ArtifactMissing { version } => { |
| 176 |
format!("no artifact for version {version}") |
| 177 |
} |
| 178 |
GateBlocker::NoNodesToProbe => "node_health has no nodes to probe".into(), |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 184 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 185 |
pub enum GateFailure { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
CargoTest { |
| 193 |
failed_count: u32, |
| 194 |
first_failed: Option<String>, |
| 195 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 196 |
first_panic: Option<String>, |
| 197 |
}, |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
CompileError { |
| 205 |
error_count: u32, |
| 206 |
first_error: Option<String>, |
| 207 |
}, |
| 208 |
|
| 209 |
|
| 210 |
MigrationDrift { migration: String }, |
| 211 |
|
| 212 |
|
| 213 |
MigrationModified { migration: String }, |
| 214 |
|
| 215 |
MigrationSqlError { |
| 216 |
migration: String, |
| 217 |
sqlstate: Option<String>, |
| 218 |
}, |
| 219 |
|
| 220 |
RestoreFailed { reason: String }, |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
BootPanic { exit_code: Option<i32> }, |
| 225 |
|
| 226 |
BootExitedEarly { exit_code: Option<i32> }, |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
BootHealthProbeFailed { last_error: String }, |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
NodeUnhealthy { node: String, detail: String }, |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
CodeSmokeSetup { reason: String }, |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
CodeSmokeSeed { exit_code: Option<i32> }, |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
CodeSmokeDocs { broken: u32 }, |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
CodeSmokeNoListeningLog, |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
CodeSmokeFrontend { dir: String, exit_code: Option<i32> }, |
| 265 |
|
| 266 |
SpawnFailed { message: String }, |
| 267 |
|
| 268 |
Timeout { gate: GateKind, after_s: u32 }, |
| 269 |
|
| 270 |
|
| 271 |
Unclassified { legacy_detail: Option<String> }, |
| 272 |
} |
| 273 |
|
| 274 |
impl GateFailure { |
| 275 |
pub fn summary(&self) -> String { |
| 276 |
match self { |
| 277 |
|
| 278 |
GateFailure::CargoTest { |
| 279 |
failed_count, |
| 280 |
first_panic: Some(p), |
| 281 |
.. |
| 282 |
} => format!("{failed_count} test(s) failed; first panic: {p}"), |
| 283 |
GateFailure::CargoTest { |
| 284 |
failed_count, |
| 285 |
first_failed: Some(name), |
| 286 |
first_panic: None, |
| 287 |
} => format!("{failed_count} test(s) failed; first: {name}"), |
| 288 |
GateFailure::CargoTest { |
| 289 |
failed_count, |
| 290 |
first_failed: None, |
| 291 |
first_panic: None, |
| 292 |
} => format!("{failed_count} test(s) failed"), |
| 293 |
GateFailure::CompileError { |
| 294 |
error_count, |
| 295 |
first_error: Some(e), |
| 296 |
} => format!("compile failed ({error_count} error(s)); first: {e}"), |
| 297 |
GateFailure::CompileError { |
| 298 |
error_count, |
| 299 |
first_error: None, |
| 300 |
} => format!("compile failed ({error_count} error(s))"), |
| 301 |
GateFailure::MigrationDrift { migration } => { |
| 302 |
format!("migration {migration} previously applied but missing") |
| 303 |
} |
| 304 |
GateFailure::MigrationModified { migration } => { |
| 305 |
format!("migration {migration} previously applied but modified") |
| 306 |
} |
| 307 |
GateFailure::MigrationSqlError { |
| 308 |
migration, |
| 309 |
sqlstate: Some(s), |
| 310 |
} => format!("migration {migration} sql error ({s})"), |
| 311 |
GateFailure::MigrationSqlError { |
| 312 |
migration, |
| 313 |
sqlstate: None, |
| 314 |
} => format!("migration {migration} sql error"), |
| 315 |
GateFailure::RestoreFailed { reason } => format!("restore: {reason}"), |
| 316 |
GateFailure::BootPanic { exit_code: Some(c) } => format!("binary panicked: exit {c}"), |
| 317 |
GateFailure::BootPanic { exit_code: None } => "binary panicked".into(), |
| 318 |
GateFailure::BootExitedEarly { exit_code: Some(c) } => { |
| 319 |
format!("binary exited early: exit {c}") |
| 320 |
} |
| 321 |
GateFailure::BootExitedEarly { exit_code: None } => "binary exited early".into(), |
| 322 |
GateFailure::BootHealthProbeFailed { last_error } => { |
| 323 |
format!("started but never served /health: {last_error}") |
| 324 |
} |
| 325 |
GateFailure::NodeUnhealthy { node, detail } => { |
| 326 |
format!("node {node} unhealthy: {detail}") |
| 327 |
} |
| 328 |
GateFailure::CodeSmokeSetup { reason } => format!("code smoke db setup: {reason}"), |
| 329 |
GateFailure::CodeSmokeSeed { exit_code: Some(c) } => { |
| 330 |
format!("migrate+seed run failed: exit {c}") |
| 331 |
} |
| 332 |
GateFailure::CodeSmokeSeed { exit_code: None } => "migrate+seed run failed".into(), |
| 333 |
GateFailure::CodeSmokeDocs { broken: 0 } => "broken internal docs link(s)".into(), |
| 334 |
GateFailure::CodeSmokeDocs { broken } => { |
| 335 |
format!("{broken} broken internal docs link(s)") |
| 336 |
} |
| 337 |
GateFailure::CodeSmokeNoListeningLog => { |
| 338 |
"served /health but never logged 'listening'".into() |
| 339 |
} |
| 340 |
GateFailure::CodeSmokeFrontend { dir, exit_code } => match exit_code { |
| 341 |
Some(c) => format!("frontend build failed in {dir}: exit {c}"), |
| 342 |
None => format!("frontend build failed in {dir}"), |
| 343 |
}, |
| 344 |
GateFailure::SpawnFailed { message } => format!("spawn: {message}"), |
| 345 |
GateFailure::Timeout { gate, after_s } => format!("{gate} timed out after {after_s}s"), |
| 346 |
GateFailure::Unclassified { |
| 347 |
legacy_detail: Some(d), |
| 348 |
} => d.clone(), |
| 349 |
GateFailure::Unclassified { |
| 350 |
legacy_detail: None, |
| 351 |
} => "unclassified failure".into(), |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 365 |
pub struct DeployOutcome { |
| 366 |
pub status: DeployStatus, |
| 367 |
} |
| 368 |
|
| 369 |
impl DeployOutcome { |
| 370 |
pub fn ok() -> Self { |
| 371 |
Self { |
| 372 |
status: DeployStatus::Ok, |
| 373 |
} |
| 374 |
} |
| 375 |
pub fn failed(failure: DeployFailureKind) -> Self { |
| 376 |
Self { |
| 377 |
status: DeployStatus::Failed { failure }, |
| 378 |
} |
| 379 |
} |
| 380 |
pub fn in_progress() -> Self { |
| 381 |
Self { |
| 382 |
status: DeployStatus::InProgress, |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
pub fn status_str(&self) -> &'static str { |
| 389 |
match self.status { |
| 390 |
DeployStatus::InProgress => "in_progress", |
| 391 |
DeployStatus::Ok => "ok", |
| 392 |
DeployStatus::Failed { .. } => "failed", |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 398 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 399 |
pub enum DeployStatus { |
| 400 |
InProgress, |
| 401 |
Ok, |
| 402 |
Failed { failure: DeployFailureKind }, |
| 403 |
} |
| 404 |
|
| 405 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 406 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 407 |
pub enum DeployFailureKind { |
| 408 |
|
| 409 |
|
| 410 |
NodeUnreachable { detail: String }, |
| 411 |
|
| 412 |
|
| 413 |
RsyncFailed { detail: String }, |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
SymlinkSwapFailed { detail: String }, |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
ServiceRestartFailed { detail: String }, |
| 422 |
|
| 423 |
|
| 424 |
Unclassified { detail: String }, |
| 425 |
} |
| 426 |
|
| 427 |
impl DeployFailureKind { |
| 428 |
pub fn summary(&self) -> String { |
| 429 |
match self { |
| 430 |
DeployFailureKind::NodeUnreachable { detail } => format!("node unreachable: {detail}"), |
| 431 |
DeployFailureKind::RsyncFailed { detail } => format!("rsync: {detail}"), |
| 432 |
DeployFailureKind::SymlinkSwapFailed { detail } => format!("symlink swap: {detail}"), |
| 433 |
DeployFailureKind::ServiceRestartFailed { detail } => { |
| 434 |
format!("service restart: {detail}") |
| 435 |
} |
| 436 |
DeployFailureKind::Unclassified { detail } => detail.chars().take(200).collect(), |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 446 |
#[serde(transparent)] |
| 447 |
pub struct LogRef(pub String); |
| 448 |
|
| 449 |
impl LogRef { |
| 450 |
pub fn new(version: &Version, gate: GateKind) -> Self { |
| 451 |
Self(format!("{}/{}.log", version, gate.as_str())) |
| 452 |
} |
| 453 |
pub fn as_str(&self) -> &str { |
| 454 |
&self.0 |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
impl std::fmt::Display for LogRef { |
| 459 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 460 |
self.0.fmt(f) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
#[cfg(test)] |
| 465 |
mod tests { |
| 466 |
use super::*; |
| 467 |
|
| 468 |
#[test] |
| 469 |
fn outcome_serialization_is_two_layer_tagged() { |
| 470 |
let o = GateOutcome::failed(GateFailure::MigrationDrift { |
| 471 |
migration: "0047_widgets".into(), |
| 472 |
}); |
| 473 |
let v: serde_json::Value = serde_json::to_value(&o).unwrap(); |
| 474 |
assert_eq!(v["status"]["kind"], "failed"); |
| 475 |
assert_eq!(v["status"]["failure"]["kind"], "migration_drift"); |
| 476 |
assert_eq!(v["status"]["failure"]["migration"], "0047_widgets"); |
| 477 |
} |
| 478 |
|
| 479 |
#[test] |
| 480 |
fn outcome_round_trips_through_json() { |
| 481 |
let o = GateOutcome::passed(PassNote::TestsPassed { duration_s: 42 }); |
| 482 |
let s = serde_json::to_string(&o).unwrap(); |
| 483 |
let back: GateOutcome = serde_json::from_str(&s).unwrap(); |
| 484 |
assert!(back.is_passed()); |
| 485 |
assert_eq!(back.status_str(), "passed"); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn timeout_failure_renders_and_is_not_passed() { |
| 490 |
|
| 491 |
|
| 492 |
let o = GateOutcome::failed(GateFailure::Timeout { |
| 493 |
gate: GateKind::CargoTest, |
| 494 |
after_s: 2400, |
| 495 |
}); |
| 496 |
assert!(!o.is_passed()); |
| 497 |
let v: serde_json::Value = serde_json::to_value(&o).unwrap(); |
| 498 |
assert_eq!(v["status"]["failure"]["kind"], "timeout"); |
| 499 |
assert!( |
| 500 |
matches!(&o.status, GateStatus::Failed { failure } if failure.summary().contains("timed out")) |
| 501 |
); |
| 502 |
} |
| 503 |
|
| 504 |
#[test] |
| 505 |
fn blocked_is_not_passed() { |
| 506 |
let o = GateOutcome::blocked(GateBlocker::BurnInClockNotStarted); |
| 507 |
assert!(!o.is_passed()); |
| 508 |
assert_eq!(o.status_str(), "blocked"); |
| 509 |
} |
| 510 |
|
| 511 |
#[test] |
| 512 |
fn log_ref_construction_matches_disk_layout() { |
| 513 |
let v: Version = "0.9.6".parse().unwrap(); |
| 514 |
let lr = LogRef::new(&v, GateKind::CargoTest); |
| 515 |
assert_eq!(lr.as_str(), "0.9.6/cargo_test.log"); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn unclassified_preserves_legacy_detail() { |
| 520 |
let o = GateOutcome::failed(GateFailure::Unclassified { |
| 521 |
legacy_detail: Some( |
| 522 |
"binary exited early: exit status: 101\n==== stdout ====\n...".into(), |
| 523 |
), |
| 524 |
}); |
| 525 |
let v: serde_json::Value = serde_json::to_value(&o).unwrap(); |
| 526 |
assert_eq!(v["status"]["failure"]["kind"], "unclassified"); |
| 527 |
assert!( |
| 528 |
v["status"]["failure"]["legacy_detail"] |
| 529 |
.as_str() |
| 530 |
.unwrap() |
| 531 |
.contains("exit status: 101") |
| 532 |
); |
| 533 |
} |
| 534 |
} |
| 535 |
|