| 1 |
use crate::domain::{GateKind, NodeId, TierId}; |
| 2 |
use anyhow::{Context, Result}; |
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
use std::path::Path; |
| 5 |
|
| 6 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 7 |
pub struct Topology { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[serde(default)] |
| 17 |
pub repo: Option<RepoConfig>, |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#[serde(deserialize_with = "one_or_many_backup")] |
| 28 |
pub backup: Vec<BackupConfig>, |
| 29 |
#[serde(rename = "tier")] |
| 30 |
pub tiers: Vec<Tier>, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#[serde(default, rename = "aux_repo")] |
| 36 |
pub aux_repos: Vec<AuxRepo>, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 56 |
pub struct AuxRepo { |
| 57 |
|
| 58 |
pub name: String, |
| 59 |
|
| 60 |
|
| 61 |
pub bare_path: String, |
| 62 |
|
| 63 |
|
| 64 |
pub upstream: String, |
| 65 |
|
| 66 |
pub branch: String, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub checkout_dir: String, |
| 73 |
} |
| 74 |
|
| 75 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 76 |
pub struct RepoConfig { |
| 77 |
pub bare_path: String, |
| 78 |
pub branch: String, |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[serde(default)] |
| 85 |
pub upstream: Option<String>, |
| 86 |
} |
| 87 |
|
| 88 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 89 |
pub struct BackupConfig { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
#[serde(default = "default_backup_name")] |
| 96 |
pub name: String, |
| 97 |
pub source: String, |
| 98 |
pub local_path: String, |
| 99 |
} |
| 100 |
|
| 101 |
fn default_backup_name() -> String { |
| 102 |
"server".into() |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
fn one_or_many_backup<'de, D>(de: D) -> std::result::Result<Vec<BackupConfig>, D::Error> |
| 110 |
where |
| 111 |
D: serde::Deserializer<'de>, |
| 112 |
{ |
| 113 |
#[derive(Deserialize)] |
| 114 |
#[serde(untagged)] |
| 115 |
enum OneOrMany { |
| 116 |
One(BackupConfig), |
| 117 |
Many(Vec<BackupConfig>), |
| 118 |
} |
| 119 |
Ok(match OneOrMany::deserialize(de)? { |
| 120 |
OneOrMany::One(b) => vec![b], |
| 121 |
OneOrMany::Many(v) => v, |
| 122 |
}) |
| 123 |
} |
| 124 |
|
| 125 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 126 |
pub struct Tier { |
| 127 |
pub name: TierId, |
| 128 |
#[serde(default)] |
| 129 |
pub provisioned: bool, |
| 130 |
pub gates: Vec<Gate>, |
| 131 |
#[serde(default)] |
| 132 |
pub canary: CanaryPolicy, |
| 133 |
#[serde(default, rename = "node")] |
| 134 |
pub nodes: Vec<Node>, |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#[serde(default)] |
| 144 |
pub public_url: Option<String>, |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 148 |
pub struct Node { |
| 149 |
pub name: NodeId, |
| 150 |
pub ssh_target: String, |
| 151 |
pub release_root: String, |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
#[serde(default)] |
| 161 |
pub platform: Option<crate::domain::Platform>, |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#[serde(default)] |
| 172 |
pub base_image: Option<ops_core::base_image::BaseImage>, |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#[serde(default)] |
| 182 |
pub libc: Option<String>, |
| 183 |
|
| 184 |
|
| 185 |
#[serde(default = "default_service_name")] |
| 186 |
pub service_name: String, |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#[serde(default)] |
| 197 |
pub config_check_env_file: Option<String>, |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#[serde(default = "default_actuate")] |
| 202 |
pub actuate: Vec<String>, |
| 203 |
#[serde(default = "default_observe")] |
| 204 |
pub observe: Vec<String>, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#[serde(default)] |
| 212 |
pub health_url: Option<String>, |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
#[serde(default, rename = "companion")] |
| 220 |
pub companions: Vec<NodeCompanion>, |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 225 |
pub struct NodeCompanion { |
| 226 |
|
| 227 |
pub name: String, |
| 228 |
|
| 229 |
|
| 230 |
pub install_path: String, |
| 231 |
|
| 232 |
|
| 233 |
pub service_name: String, |
| 234 |
} |
| 235 |
|
| 236 |
fn default_service_name() -> String { |
| 237 |
"makenotwork.service".into() |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
pub fn default_actuate() -> Vec<String> { |
| 244 |
vec!["deploy".into(), "restart".into()] |
| 245 |
} |
| 246 |
pub fn default_observe() -> Vec<String> { |
| 247 |
vec!["health".into()] |
| 248 |
} |
| 249 |
|
| 250 |
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)] |
| 251 |
#[serde(rename_all = "snake_case")] |
| 252 |
pub enum CanaryPolicy { |
| 253 |
#[default] |
| 254 |
Sequential, |
| 255 |
Parallel, |
| 256 |
} |
| 257 |
|
| 258 |
impl CanaryPolicy { |
| 259 |
pub fn as_str(self) -> &'static str { |
| 260 |
match self { |
| 261 |
CanaryPolicy::Sequential => "sequential", |
| 262 |
CanaryPolicy::Parallel => "parallel", |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 268 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 269 |
pub enum Gate { |
| 270 |
CargoTest, |
| 271 |
HardeningTest, |
| 272 |
Clippy, |
| 273 |
Fmt, |
| 274 |
CargoAudit, |
| 275 |
CargoDeny, |
| 276 |
MigrationDryRun, |
| 277 |
CodeSmoke, |
| 278 |
BootSmoke, |
| 279 |
NodeHealth, |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
PageSmoke, |
| 291 |
BurnIn { |
| 292 |
hours: u32, |
| 293 |
}, |
| 294 |
ManualConfirm, |
| 295 |
} |
| 296 |
|
| 297 |
impl Gate { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
pub fn kind(&self) -> GateKind { |
| 302 |
match self { |
| 303 |
Gate::CargoTest => GateKind::CargoTest, |
| 304 |
Gate::HardeningTest => GateKind::HardeningTest, |
| 305 |
Gate::Clippy => GateKind::Clippy, |
| 306 |
Gate::Fmt => GateKind::Fmt, |
| 307 |
Gate::CargoAudit => GateKind::CargoAudit, |
| 308 |
Gate::CargoDeny => GateKind::CargoDeny, |
| 309 |
Gate::MigrationDryRun => GateKind::MigrationDryRun, |
| 310 |
Gate::CodeSmoke => GateKind::CodeSmoke, |
| 311 |
Gate::BootSmoke => GateKind::BootSmoke, |
| 312 |
Gate::NodeHealth => GateKind::NodeHealth, |
| 313 |
Gate::PageSmoke => GateKind::PageSmoke, |
| 314 |
Gate::BurnIn { .. } => GateKind::BurnIn, |
| 315 |
Gate::ManualConfirm => GateKind::ManualConfirm, |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
pub fn runs_post_deploy(&self) -> bool { |
| 328 |
matches!(self, Gate::NodeHealth | Gate::PageSmoke) |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
pub fn guards_promotion(&self) -> bool { |
| 340 |
matches!( |
| 341 |
self, |
| 342 |
Gate::NodeHealth | Gate::PageSmoke | Gate::BurnIn { .. } | Gate::ManualConfirm |
| 343 |
) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
impl Topology { |
| 348 |
pub fn load(path: &Path) -> Result<Self> { |
| 349 |
let raw = std::fs::read_to_string(path) |
| 350 |
.with_context(|| format!("reading topology at {}", path.display()))?; |
| 351 |
let topo: Topology = toml::from_str(&raw)?; |
| 352 |
topo.validate()?; |
| 353 |
Ok(topo) |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
pub fn ensure_build_host_not_serving(&self, build_host: &str) -> Result<()> { |
| 363 |
for t in &self.tiers { |
| 364 |
if !t.provisioned || t.name.as_str() == "host" { |
| 365 |
continue; |
| 366 |
} |
| 367 |
for n in &t.nodes { |
| 368 |
if n.name.as_str() == build_host || n.ssh_target == build_host { |
| 369 |
anyhow::bail!( |
| 370 |
"build_host {build_host:?} is also node {} (ssh {}) in serving tier {} — \ |
| 371 |
the builder must not be a prod/serving node", |
| 372 |
n.name, |
| 373 |
n.ssh_target, |
| 374 |
t.name |
| 375 |
); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
Ok(()) |
| 380 |
} |
| 381 |
|
| 382 |
#[cfg(test)] |
| 383 |
pub(crate) fn validate_for_test(&self) -> Result<()> { |
| 384 |
self.validate() |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
pub fn ensure_migration_checks_have_backups( |
| 395 |
&self, |
| 396 |
checks: &[crate::config::MigrationCheck], |
| 397 |
) -> Result<()> { |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
if !self |
| 403 |
.tiers |
| 404 |
.iter() |
| 405 |
.flat_map(|t| &t.gates) |
| 406 |
.any(|g| g.kind() == GateKind::MigrationDryRun) |
| 407 |
{ |
| 408 |
return Ok(()); |
| 409 |
} |
| 410 |
for c in checks { |
| 411 |
anyhow::ensure!( |
| 412 |
self.backup_named(&c.backup).is_some(), |
| 413 |
"migration_check {} restores backup {:?}, which no [[backup]] in {} declares \ |
| 414 |
(have: {})", |
| 415 |
c.dir.display(), |
| 416 |
c.backup, |
| 417 |
"the topology", |
| 418 |
self.backup |
| 419 |
.iter() |
| 420 |
.map(|b| b.name.as_str()) |
| 421 |
.collect::<Vec<_>>() |
| 422 |
.join(", "), |
| 423 |
); |
| 424 |
} |
| 425 |
Ok(()) |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
pub fn ensure_test_target_aux_repos_exist( |
| 435 |
&self, |
| 436 |
targets: &[crate::config::TestTarget], |
| 437 |
) -> Result<()> { |
| 438 |
for t in targets { |
| 439 |
let Some(name) = t.aux_repo.as_deref() else { |
| 440 |
continue; |
| 441 |
}; |
| 442 |
anyhow::ensure!( |
| 443 |
self.aux_repos.iter().any(|a| a.name == name), |
| 444 |
"test_target {} names aux_repo {:?}, which no [[aux_repo]] in the topology \ |
| 445 |
checks out, so the gate would skip it as absent (have: {})", |
| 446 |
t.label(), |
| 447 |
name, |
| 448 |
if self.aux_repos.is_empty() { |
| 449 |
"none".to_string() |
| 450 |
} else { |
| 451 |
self.aux_repos |
| 452 |
.iter() |
| 453 |
.map(|a| a.name.as_str()) |
| 454 |
.collect::<Vec<_>>() |
| 455 |
.join(", ") |
| 456 |
}, |
| 457 |
); |
| 458 |
} |
| 459 |
Ok(()) |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
pub fn ensure_node_companions_are_built( |
| 470 |
&self, |
| 471 |
built: &[crate::config::Companion], |
| 472 |
) -> Result<()> { |
| 473 |
for t in &self.tiers { |
| 474 |
for n in &t.nodes { |
| 475 |
for c in &n.companions { |
| 476 |
anyhow::ensure!( |
| 477 |
built.iter().any(|b| b.name == c.name), |
| 478 |
"tier {} node {} installs companion {:?}, which no [[companion]] in the \ |
| 479 |
daemon config builds, so nothing would be staged under \ |
| 480 |
companions/{} (have: {})", |
| 481 |
t.name, |
| 482 |
n.name, |
| 483 |
c.name, |
| 484 |
c.name, |
| 485 |
if built.is_empty() { |
| 486 |
"none".to_string() |
| 487 |
} else { |
| 488 |
built |
| 489 |
.iter() |
| 490 |
.map(|b| b.name.as_str()) |
| 491 |
.collect::<Vec<_>>() |
| 492 |
.join(", ") |
| 493 |
}, |
| 494 |
); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
Ok(()) |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
pub fn backup_named(&self, name: &str) -> Option<&BackupConfig> { |
| 503 |
self.backup.iter().find(|b| b.name == name) |
| 504 |
} |
| 505 |
|
| 506 |
fn validate(&self) -> Result<()> { |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
let dry_runs_migrations = self |
| 514 |
.tiers |
| 515 |
.iter() |
| 516 |
.flat_map(|t| &t.gates) |
| 517 |
.any(|g| g.kind() == GateKind::MigrationDryRun); |
| 518 |
anyhow::ensure!( |
| 519 |
!dry_runs_migrations || !self.backup.is_empty(), |
| 520 |
"a tier configures migration_dry_run but the topology declares no [backup]; \ |
| 521 |
the gate would have nothing to restore" |
| 522 |
); |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
for t in &self.tiers { |
| 528 |
let smokes = t.gates.iter().any(|g| g.kind() == GateKind::PageSmoke); |
| 529 |
anyhow::ensure!( |
| 530 |
!smokes || t.public_url.is_some(), |
| 531 |
"tier {} configures page_smoke but declares no public_url; the gate has to \ |
| 532 |
request the site the way a visitor does, and a URL derived from a node would \ |
| 533 |
reach the origin and miss the CDN it exists to watch", |
| 534 |
t.name, |
| 535 |
); |
| 536 |
} |
| 537 |
for (i, b) in self.backup.iter().enumerate() { |
| 538 |
anyhow::ensure!( |
| 539 |
!b.name.is_empty() |
| 540 |
&& b.name |
| 541 |
.bytes() |
| 542 |
.all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'-'), |
| 543 |
"backup name {:?} must be non-empty and match [A-Za-z0-9_-]+; it keys the \ |
| 544 |
`backups` table and a daemon-config migration_check", |
| 545 |
b.name, |
| 546 |
); |
| 547 |
anyhow::ensure!( |
| 548 |
!b.source.is_empty() && !b.local_path.is_empty(), |
| 549 |
"backup {} has an empty source/local_path", |
| 550 |
b.name, |
| 551 |
); |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
for prior in &self.backup[..i] { |
| 558 |
anyhow::ensure!( |
| 559 |
prior.name != b.name, |
| 560 |
"two backup entries share the name {:?}", |
| 561 |
b.name, |
| 562 |
); |
| 563 |
anyhow::ensure!( |
| 564 |
prior.local_path != b.local_path, |
| 565 |
"backups {:?} and {:?} share local_path {:?}; they would overwrite each other", |
| 566 |
prior.name, |
| 567 |
b.name, |
| 568 |
b.local_path, |
| 569 |
); |
| 570 |
} |
| 571 |
} |
| 572 |
anyhow::ensure!( |
| 573 |
!self.tiers.is_empty(), |
| 574 |
"topology must declare at least one tier" |
| 575 |
); |
| 576 |
for t in &self.tiers { |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
let is_build_tier = t.name.as_str() == "host"; |
| 582 |
if t.provisioned && t.nodes.is_empty() && !is_build_tier { |
| 583 |
anyhow::bail!("tier {} is provisioned but has no nodes", t.name); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
if t.provisioned && !is_build_tier && !t.gates.iter().any(Gate::guards_promotion) { |
| 590 |
anyhow::bail!( |
| 591 |
"tier {} is provisioned to serve but declares no promotion gate \ |
| 592 |
(need at least one of node_health / burn_in / manual_confirm)", |
| 593 |
t.name |
| 594 |
); |
| 595 |
} |
| 596 |
} |
| 597 |
let mut seen_dirs: Vec<Vec<&str>> = Vec::new(); |
| 598 |
for aux in &self.aux_repos { |
| 599 |
anyhow::ensure!( |
| 600 |
!aux.name.is_empty() && !aux.bare_path.is_empty() && !aux.branch.is_empty(), |
| 601 |
"aux_repo entry has an empty name/bare_path/branch" |
| 602 |
); |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
let dir = &aux.checkout_dir; |
| 608 |
let parts: Vec<&str> = dir.split('/').collect(); |
| 609 |
anyhow::ensure!( |
| 610 |
!dir.is_empty() |
| 611 |
&& !dir.contains('\\') |
| 612 |
&& parts |
| 613 |
.iter() |
| 614 |
.all(|c| !c.is_empty() && *c != "." && *c != ".."), |
| 615 |
"aux_repo {} has an unsafe checkout_dir {dir:?} (must be a relative path of \ |
| 616 |
plain components: no leading slash, empty segments, or dot-dot)", |
| 617 |
aux.name, |
| 618 |
); |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
for prior in &seen_dirs { |
| 623 |
let common = prior.len().min(parts.len()); |
| 624 |
anyhow::ensure!( |
| 625 |
prior[..common] != parts[..common], |
| 626 |
"two aux_repo entries share or nest checkout_dir {dir:?}; \ |
| 627 |
they would clobber each other" |
| 628 |
); |
| 629 |
} |
| 630 |
seen_dirs.push(parts); |
| 631 |
} |
| 632 |
Ok(()) |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
#[cfg(test)] |
| 637 |
mod tests { |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
#[test] |
| 644 |
fn the_shipped_topology_declares_what_each_node_is() { |
| 645 |
let raw = std::fs::read_to_string( |
| 646 |
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../sando.toml"), |
| 647 |
) |
| 648 |
.expect("the repo's sando.toml must be readable from the daemon crate"); |
| 649 |
let topo: Topology = toml::from_str(&raw).expect("sando.toml must parse"); |
| 650 |
|
| 651 |
let nodes: Vec<&Node> = topo.tiers.iter().flat_map(|t| t.nodes.iter()).collect(); |
| 652 |
let by = |name: &str| { |
| 653 |
*nodes |
| 654 |
.iter() |
| 655 |
.find(|n| n.name.as_str() == name) |
| 656 |
.unwrap_or_else(|| panic!("`{name}` must be in the topology")) |
| 657 |
}; |
| 658 |
let image = |n: &Node| n.base_image.as_ref().map(ToString::to_string); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
let testnot = by("testnot-1"); |
| 664 |
assert_eq!(image(testnot).as_deref(), Some("ubuntu/26.04")); |
| 665 |
assert_eq!(testnot.libc.as_deref(), Some("2.43")); |
| 666 |
|
| 667 |
let prod = by("prod-1"); |
| 668 |
assert_eq!(image(prod).as_deref(), Some("ubuntu/24.04")); |
| 669 |
assert_eq!(prod.libc.as_deref(), Some("2.39")); |
| 670 |
|
| 671 |
assert_ne!( |
| 672 |
testnot.base_image, prod.base_image, |
| 673 |
"if these ever match, delete this assertion and the comment in \ |
| 674 |
sando.toml that explains why they do not" |
| 675 |
); |
| 676 |
} |
| 677 |
|
| 678 |
use super::*; |
| 679 |
|
| 680 |
|
| 681 |
fn topo_with_serving_gates(provisioned: bool, gates: &str) -> Topology { |
| 682 |
let raw = format!( |
| 683 |
r#" |
| 684 |
[repo] |
| 685 |
bare_path = "/tmp/repo.git" |
| 686 |
branch = "main" |
| 687 |
|
| 688 |
[backup] |
| 689 |
source = "ssh://prod/dump.sql.gz" |
| 690 |
local_path = "/tmp/dump.sql.gz" |
| 691 |
|
| 692 |
[[tier]] |
| 693 |
name = "b" |
| 694 |
provisioned = {provisioned} |
| 695 |
gates = [{gates}] |
| 696 |
[[tier.node]] |
| 697 |
name = "prod-1" |
| 698 |
ssh_target = "prod-1" |
| 699 |
release_root = "/srv/mnw" |
| 700 |
"# |
| 701 |
); |
| 702 |
toml::from_str(&raw).expect("parse test topology") |
| 703 |
} |
| 704 |
|
| 705 |
#[test] |
| 706 |
fn page_smoke_without_a_public_url_is_rejected_at_load() { |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
let topo = topo_with_serving_gates(true, r#"{ kind = "page_smoke" }"#); |
| 711 |
let err = topo.validate_for_test().expect_err("must refuse"); |
| 712 |
assert!( |
| 713 |
err.to_string().contains("public_url"), |
| 714 |
"error should name the missing field: {err}" |
| 715 |
); |
| 716 |
} |
| 717 |
|
| 718 |
#[test] |
| 719 |
fn page_smoke_with_a_public_url_loads() { |
| 720 |
let raw = r#" |
| 721 |
[repo] |
| 722 |
bare_path = "/tmp/repo.git" |
| 723 |
branch = "main" |
| 724 |
|
| 725 |
[backup] |
| 726 |
source = "ssh://prod/dump.sql.gz" |
| 727 |
local_path = "/tmp/dump.sql.gz" |
| 728 |
|
| 729 |
[[tier]] |
| 730 |
name = "a" |
| 731 |
provisioned = true |
| 732 |
public_url = "https://testnot.work" |
| 733 |
gates = [{ kind = "page_smoke" }] |
| 734 |
[[tier.node]] |
| 735 |
name = "testnot-1" |
| 736 |
ssh_target = "testnot-1" |
| 737 |
release_root = "/srv/mnw" |
| 738 |
"#; |
| 739 |
let topo: Topology = toml::from_str(raw).expect("parse"); |
| 740 |
topo.validate_for_test().expect("valid"); |
| 741 |
assert_eq!( |
| 742 |
topo.tiers[0].public_url.as_deref(), |
| 743 |
Some("https://testnot.work") |
| 744 |
); |
| 745 |
|
| 746 |
|
| 747 |
assert!(topo.tiers[0].gates[0].guards_promotion()); |
| 748 |
assert!(topo.tiers[0].gates[0].runs_post_deploy()); |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn provisioned_serving_tier_with_no_gates_is_rejected() { |
| 753 |
let topo = topo_with_serving_gates(true, ""); |
| 754 |
let err = topo.validate_for_test().unwrap_err().to_string(); |
| 755 |
assert!(err.contains("no promotion gate"), "{err}"); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn provisioned_serving_tier_with_only_build_gates_is_rejected() { |
| 760 |
|
| 761 |
|
| 762 |
let topo = topo_with_serving_gates( |
| 763 |
true, |
| 764 |
r#"{ kind = "cargo_test" }, { kind = "migration_dry_run" }"#, |
| 765 |
); |
| 766 |
let err = topo.validate_for_test().unwrap_err().to_string(); |
| 767 |
assert!(err.contains("no promotion gate"), "{err}"); |
| 768 |
} |
| 769 |
|
| 770 |
#[test] |
| 771 |
fn provisioned_serving_tier_with_a_promotion_gate_is_accepted() { |
| 772 |
let topo = topo_with_serving_gates(true, r#"{ kind = "node_health" }"#); |
| 773 |
assert!(topo.validate_for_test().is_ok()); |
| 774 |
} |
| 775 |
|
| 776 |
#[test] |
| 777 |
fn provisioned_serving_tier_with_only_boot_smoke_is_rejected() { |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
let topo = topo_with_serving_gates(true, r#"{ kind = "boot_smoke" }"#); |
| 782 |
let err = topo.validate_for_test().unwrap_err().to_string(); |
| 783 |
assert!(err.contains("no promotion gate"), "{err}"); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn unprovisioned_tier_with_empty_gates_is_skipped() { |
| 788 |
|
| 789 |
|
| 790 |
let topo = topo_with_serving_gates(false, ""); |
| 791 |
assert!(topo.validate_for_test().is_ok()); |
| 792 |
} |
| 793 |
|
| 794 |
#[test] |
| 795 |
fn build_host_matching_a_serving_node_is_rejected() { |
| 796 |
|
| 797 |
|
| 798 |
let topo = topo_with_serving_gates(true, r#"{ kind = "node_health" }"#); |
| 799 |
let err = topo |
| 800 |
.ensure_build_host_not_serving("prod-1") |
| 801 |
.unwrap_err() |
| 802 |
.to_string(); |
| 803 |
assert!(err.contains("must not be a prod/serving node"), "{err}"); |
| 804 |
} |
| 805 |
|
| 806 |
#[test] |
| 807 |
fn build_host_distinct_from_serving_nodes_is_accepted() { |
| 808 |
let topo = topo_with_serving_gates(true, r#"{ kind = "node_health" }"#); |
| 809 |
assert!(topo.ensure_build_host_not_serving("fw13").is_ok()); |
| 810 |
} |
| 811 |
|
| 812 |
#[test] |
| 813 |
fn node_companions_default_empty_and_parse_when_present() { |
| 814 |
|
| 815 |
let plain = topo_with_serving_gates(true, r#"{ kind = "node_health" }"#); |
| 816 |
assert!(plain.tiers[0].nodes[0].companions.is_empty()); |
| 817 |
|
| 818 |
|
| 819 |
let raw = r#" |
| 820 |
[repo] |
| 821 |
bare_path = "/tmp/repo.git" |
| 822 |
branch = "main" |
| 823 |
[backup] |
| 824 |
source = "s" |
| 825 |
local_path = "/tmp/d" |
| 826 |
[[tier]] |
| 827 |
name = "b" |
| 828 |
provisioned = true |
| 829 |
gates = [{ kind = "node_health" }] |
| 830 |
[[tier.node]] |
| 831 |
name = "prod-1" |
| 832 |
ssh_target = "makenotwork@alpha-west-1" |
| 833 |
release_root = "/opt/mnw" |
| 834 |
[[tier.node.companion]] |
| 835 |
name = "mnw-cli" |
| 836 |
install_path = "/opt/mnw-cli/mnw-cli" |
| 837 |
service_name = "mnw-cli.service" |
| 838 |
"#; |
| 839 |
let topo: Topology = toml::from_str(raw).expect("parse companion topology"); |
| 840 |
let c = &topo.tiers[0].nodes[0].companions; |
| 841 |
assert_eq!(c.len(), 1); |
| 842 |
assert_eq!(c[0].name, "mnw-cli"); |
| 843 |
assert_eq!(c[0].install_path, "/opt/mnw-cli/mnw-cli"); |
| 844 |
assert_eq!(c[0].service_name, "mnw-cli.service"); |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
fn topo_installing(names: &[&str]) -> Topology { |
| 849 |
let mut blocks = String::new(); |
| 850 |
for n in names { |
| 851 |
use std::fmt::Write; |
| 852 |
let _ = write!( |
| 853 |
blocks, |
| 854 |
"[[tier.node.companion]]\nname = \"{n}\"\n\ |
| 855 |
install_path = \"/opt/{n}/{n}\"\nservice_name = \"{n}.service\"\n" |
| 856 |
); |
| 857 |
} |
| 858 |
let raw = format!( |
| 859 |
r#" |
| 860 |
[repo] |
| 861 |
bare_path = "/tmp/repo.git" |
| 862 |
branch = "main" |
| 863 |
[backup] |
| 864 |
source = "s" |
| 865 |
local_path = "/tmp/d" |
| 866 |
[[tier]] |
| 867 |
name = "b" |
| 868 |
provisioned = true |
| 869 |
gates = [{{ kind = "node_health" }}] |
| 870 |
[[tier.node]] |
| 871 |
name = "prod-1" |
| 872 |
ssh_target = "makenotwork@alpha-west-1" |
| 873 |
release_root = "/opt/mnw" |
| 874 |
{blocks}"# |
| 875 |
); |
| 876 |
toml::from_str(&raw).expect("parse topology") |
| 877 |
} |
| 878 |
|
| 879 |
fn built(names: &[&str]) -> Vec<crate::config::Companion> { |
| 880 |
names |
| 881 |
.iter() |
| 882 |
.map(|n| crate::config::Companion { |
| 883 |
name: (*n).to_string(), |
| 884 |
manifest_dir: (*n).into(), |
| 885 |
bin: (*n).to_string(), |
| 886 |
}) |
| 887 |
.collect() |
| 888 |
} |
| 889 |
|
| 890 |
fn test_target(dir: &str, aux_repo: Option<&str>) -> crate::config::TestTarget { |
| 891 |
crate::config::TestTarget { |
| 892 |
dir: dir.into(), |
| 893 |
aux_repo: aux_repo.map(str::to_string), |
| 894 |
features: Vec::new(), |
| 895 |
all_features: false, |
| 896 |
scratch_db: false, |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn a_test_target_naming_a_checked_out_aux_repo_is_accepted() { |
| 902 |
let topo = topo_with_aux( |
| 903 |
"[[aux_repo]]\nname = \"docengine\"\nbare_path = \"/tmp/d.git\"\n\ |
| 904 |
upstream = \"git@h:max/d.git\"\nbranch = \"main\"\ncheckout_dir = \"Libraries/docengine\"\n", |
| 905 |
) |
| 906 |
.expect("parse"); |
| 907 |
assert!( |
| 908 |
topo.ensure_test_target_aux_repos_exist(&[ |
| 909 |
test_target("server", None), |
| 910 |
test_target("", Some("docengine")), |
| 911 |
]) |
| 912 |
.is_ok() |
| 913 |
); |
| 914 |
} |
| 915 |
|
| 916 |
#[test] |
| 917 |
fn a_test_target_naming_an_unknown_aux_repo_is_rejected_at_load() { |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
let topo = topo_with_aux( |
| 922 |
"[[aux_repo]]\nname = \"synckit\"\nbare_path = \"/tmp/s.git\"\n\ |
| 923 |
upstream = \"git@h:max/s.git\"\nbranch = \"main\"\ncheckout_dir = \"synckit\"\n", |
| 924 |
) |
| 925 |
.expect("parse"); |
| 926 |
let err = topo |
| 927 |
.ensure_test_target_aux_repos_exist(&[test_target("", Some("docengine"))]) |
| 928 |
.unwrap_err() |
| 929 |
.to_string(); |
| 930 |
assert!(err.contains("docengine"), "{err}"); |
| 931 |
assert!(err.contains("have: synckit"), "{err}"); |
| 932 |
} |
| 933 |
|
| 934 |
#[test] |
| 935 |
fn test_targets_without_an_aux_repo_need_no_aux_repos_declared() { |
| 936 |
let topo = topo_with_serving_gates(true, r#"{ kind = "node_health" }"#); |
| 937 |
assert!( |
| 938 |
topo.ensure_test_target_aux_repos_exist(&[test_target("server", None)]) |
| 939 |
.is_ok() |
| 940 |
); |
| 941 |
} |
| 942 |
|
| 943 |
#[test] |
| 944 |
fn a_node_companion_the_daemon_builds_is_accepted() { |
| 945 |
let topo = topo_installing(&["mnw-cli", "multithreaded"]); |
| 946 |
assert!( |
| 947 |
topo.ensure_node_companions_are_built(&built(&["mnw-cli", "multithreaded"])) |
| 948 |
.is_ok() |
| 949 |
); |
| 950 |
} |
| 951 |
|
| 952 |
#[test] |
| 953 |
fn a_node_companion_nothing_builds_is_rejected_at_load() { |
| 954 |
|
| 955 |
|
| 956 |
let topo = topo_installing(&["mnw-cli", "multithreadd"]); |
| 957 |
let err = topo |
| 958 |
.ensure_node_companions_are_built(&built(&["mnw-cli", "multithreaded"])) |
| 959 |
.unwrap_err() |
| 960 |
.to_string(); |
| 961 |
assert!(err.contains("multithreadd"), "{err}"); |
| 962 |
assert!(err.contains("no [[companion]]"), "{err}"); |
| 963 |
|
| 964 |
assert!(err.contains("mnw-cli, multithreaded"), "{err}"); |
| 965 |
} |
| 966 |
|
| 967 |
#[test] |
| 968 |
fn a_node_companion_with_no_companions_configured_at_all_is_rejected() { |
| 969 |
let topo = topo_installing(&["multithreaded"]); |
| 970 |
let err = topo |
| 971 |
.ensure_node_companions_are_built(&[]) |
| 972 |
.unwrap_err() |
| 973 |
.to_string(); |
| 974 |
assert!(err.contains("have: none"), "{err}"); |
| 975 |
} |
| 976 |
|
| 977 |
#[test] |
| 978 |
fn a_topology_installing_no_companions_is_fine_with_none_built() { |
| 979 |
let topo = topo_installing(&[]); |
| 980 |
assert!(topo.ensure_node_companions_are_built(&[]).is_ok()); |
| 981 |
} |
| 982 |
|
| 983 |
fn topo_with_aux(aux_block: &str) -> Result<Topology> { |
| 984 |
let raw = format!( |
| 985 |
r#" |
| 986 |
[repo] |
| 987 |
bare_path = "/tmp/repo.git" |
| 988 |
branch = "main" |
| 989 |
[backup] |
| 990 |
source = "s" |
| 991 |
local_path = "/tmp/d" |
| 992 |
[[tier]] |
| 993 |
name = "b" |
| 994 |
provisioned = true |
| 995 |
gates = [{{ kind = "node_health" }}] |
| 996 |
[[tier.node]] |
| 997 |
name = "prod-1" |
| 998 |
ssh_target = "prod-1" |
| 999 |
release_root = "/srv/mnw" |
| 1000 |
{aux_block} |
| 1001 |
"# |
| 1002 |
); |
| 1003 |
let topo: Topology = toml::from_str(&raw)?; |
| 1004 |
topo.validate_for_test()?; |
| 1005 |
Ok(topo) |
| 1006 |
} |
| 1007 |
|
| 1008 |
#[test] |
| 1009 |
fn aux_repos_default_empty() { |
| 1010 |
let topo = topo_with_aux("").expect("no aux_repo block is fine"); |
| 1011 |
assert!(topo.aux_repos.is_empty()); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[test] |
| 1015 |
fn aux_repo_parses_all_fields() { |
| 1016 |
let topo = topo_with_aux( |
| 1017 |
r#" |
| 1018 |
[[aux_repo]] |
| 1019 |
name = "synckit" |
| 1020 |
bare_path = "/srv/sando/synckit.git" |
| 1021 |
upstream = "git@ssh.makenot.work:max/synckit.git" |
| 1022 |
branch = "main" |
| 1023 |
checkout_dir = "synckit""#, |
| 1024 |
) |
| 1025 |
.expect("valid aux_repo parses"); |
| 1026 |
assert_eq!(topo.aux_repos.len(), 1); |
| 1027 |
let a = &topo.aux_repos[0]; |
| 1028 |
assert_eq!(a.name, "synckit"); |
| 1029 |
assert_eq!(a.bare_path, "/srv/sando/synckit.git"); |
| 1030 |
assert_eq!(a.upstream, "git@ssh.makenot.work:max/synckit.git"); |
| 1031 |
assert_eq!(a.branch, "main"); |
| 1032 |
assert_eq!(a.checkout_dir, "synckit"); |
| 1033 |
} |
| 1034 |
|
| 1035 |
#[test] |
| 1036 |
fn aux_repo_with_nested_checkout_dir_is_accepted() { |
| 1037 |
let topo = topo_with_aux( |
| 1038 |
r#" |
| 1039 |
[[aux_repo]] |
| 1040 |
name = "docengine" |
| 1041 |
bare_path = "/srv/sando/docengine.git" |
| 1042 |
upstream = "git@ssh.makenot.work:max/docengine.git" |
| 1043 |
branch = "main" |
| 1044 |
checkout_dir = "Libraries/docengine""#, |
| 1045 |
) |
| 1046 |
.expect("a nested checkout_dir is a valid location"); |
| 1047 |
assert_eq!(topo.aux_repos[0].checkout_dir, "Libraries/docengine"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
#[test] |
| 1051 |
fn aux_repo_with_traversing_checkout_dir_is_rejected() { |
| 1052 |
for bad in [ |
| 1053 |
"../escape", |
| 1054 |
"a/../../escape", |
| 1055 |
"a/./b", |
| 1056 |
"a//b", |
| 1057 |
"/abs", |
| 1058 |
"a/", |
| 1059 |
"..", |
| 1060 |
".", |
| 1061 |
] { |
| 1062 |
let err = topo_with_aux(&format!( |
| 1063 |
r#" |
| 1064 |
[[aux_repo]] |
| 1065 |
name = "x" |
| 1066 |
bare_path = "/srv/sando/x.git" |
| 1067 |
upstream = "u" |
| 1068 |
branch = "main" |
| 1069 |
checkout_dir = "{bad}""#, |
| 1070 |
)) |
| 1071 |
.unwrap_err() |
| 1072 |
.to_string(); |
| 1073 |
assert!(err.contains("unsafe checkout_dir"), "for {bad:?}: {err}"); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
#[test] |
| 1078 |
fn aux_repos_sharing_a_checkout_dir_are_rejected() { |
| 1079 |
let err = topo_with_aux( |
| 1080 |
r#" |
| 1081 |
[[aux_repo]] |
| 1082 |
name = "one" |
| 1083 |
bare_path = "/srv/sando/one.git" |
| 1084 |
upstream = "u" |
| 1085 |
branch = "main" |
| 1086 |
checkout_dir = "shared" |
| 1087 |
[[aux_repo]] |
| 1088 |
name = "two" |
| 1089 |
bare_path = "/srv/sando/two.git" |
| 1090 |
upstream = "u" |
| 1091 |
branch = "main" |
| 1092 |
checkout_dir = "shared""#, |
| 1093 |
) |
| 1094 |
.unwrap_err() |
| 1095 |
.to_string(); |
| 1096 |
assert!(err.contains("share or nest checkout_dir"), "{err}"); |
| 1097 |
} |
| 1098 |
|
| 1099 |
#[test] |
| 1100 |
fn aux_repo_nested_inside_another_checkout_dir_is_rejected() { |
| 1101 |
let err = topo_with_aux( |
| 1102 |
r#" |
| 1103 |
[[aux_repo]] |
| 1104 |
name = "outer" |
| 1105 |
bare_path = "/srv/sando/outer.git" |
| 1106 |
upstream = "u" |
| 1107 |
branch = "main" |
| 1108 |
checkout_dir = "Libraries" |
| 1109 |
[[aux_repo]] |
| 1110 |
name = "inner" |
| 1111 |
bare_path = "/srv/sando/inner.git" |
| 1112 |
upstream = "u" |
| 1113 |
branch = "main" |
| 1114 |
checkout_dir = "Libraries/docengine""#, |
| 1115 |
) |
| 1116 |
.unwrap_err() |
| 1117 |
.to_string(); |
| 1118 |
assert!(err.contains("share or nest checkout_dir"), "{err}"); |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
fn topo_with_backup_block(backup_block: &str) -> Result<Topology> { |
| 1123 |
let raw = format!( |
| 1124 |
r#" |
| 1125 |
[repo] |
| 1126 |
bare_path = "/tmp/repo.git" |
| 1127 |
branch = "main" |
| 1128 |
{backup_block} |
| 1129 |
[[tier]] |
| 1130 |
name = "b" |
| 1131 |
provisioned = true |
| 1132 |
# migration_dry_run is what makes the backup rules apply at all: a product no |
| 1133 |
# tier dry-runs migrations for owes no dumps, so a fixture exercising those rules |
| 1134 |
# has to configure the gate. |
| 1135 |
gates = [{{ kind = "node_health" }}, {{ kind = "migration_dry_run" }}] |
| 1136 |
[[tier.node]] |
| 1137 |
name = "prod-1" |
| 1138 |
ssh_target = "prod-1" |
| 1139 |
release_root = "/srv/mnw" |
| 1140 |
"# |
| 1141 |
); |
| 1142 |
let topo: Topology = toml::from_str(&raw)?; |
| 1143 |
topo.validate_for_test()?; |
| 1144 |
Ok(topo) |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
fn topo_without_backup(gates: &str) -> Result<Topology> { |
| 1150 |
let raw = format!( |
| 1151 |
r#" |
| 1152 |
backup = [] |
| 1153 |
|
| 1154 |
[repo] |
| 1155 |
bare_path = "/tmp/repo.git" |
| 1156 |
branch = "main" |
| 1157 |
|
| 1158 |
[[tier]] |
| 1159 |
name = "b" |
| 1160 |
provisioned = true |
| 1161 |
gates = [{gates}] |
| 1162 |
[[tier.node]] |
| 1163 |
name = "prod-1" |
| 1164 |
ssh_target = "prod-1" |
| 1165 |
release_root = "/srv/mnw" |
| 1166 |
"# |
| 1167 |
); |
| 1168 |
let topo: Topology = toml::from_str(&raw)?; |
| 1169 |
topo.validate_for_test()?; |
| 1170 |
Ok(topo) |
| 1171 |
} |
| 1172 |
|
| 1173 |
#[test] |
| 1174 |
fn a_product_that_never_dry_runs_migrations_owes_no_dump() { |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
let topo = topo_without_backup(r#"{ kind = "node_health" }"#) |
| 1179 |
.expect("a topology with no migration gate loads without a [backup]"); |
| 1180 |
assert!(topo.backup.is_empty()); |
| 1181 |
} |
| 1182 |
|
| 1183 |
#[test] |
| 1184 |
fn a_migration_dry_run_with_no_backup_is_rejected_at_load() { |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
let err = |
| 1189 |
topo_without_backup(r#"{ kind = "node_health" }, { kind = "migration_dry_run" }"#) |
| 1190 |
.expect_err("a dry-run gate with no dump declared must not load") |
| 1191 |
.to_string(); |
| 1192 |
assert!(err.contains("declares no [backup]"), "{err}"); |
| 1193 |
} |
| 1194 |
|
| 1195 |
#[test] |
| 1196 |
fn a_single_backup_table_still_parses_as_one_named_server() { |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
let topo = topo_with_backup_block( |
| 1201 |
r#" |
| 1202 |
[backup] |
| 1203 |
source = "ssh://prod/dump.sql.gz" |
| 1204 |
local_path = "/tmp/dump.sql.gz""#, |
| 1205 |
) |
| 1206 |
.expect("the single-table form must still load"); |
| 1207 |
assert_eq!(topo.backup.len(), 1); |
| 1208 |
assert_eq!(topo.backup[0].name, "server"); |
| 1209 |
assert!(topo.backup_named("server").is_some()); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn a_backup_list_parses_and_keeps_its_names() { |
| 1214 |
let topo = topo_with_backup_block( |
| 1215 |
r#" |
| 1216 |
[[backup]] |
| 1217 |
name = "server" |
| 1218 |
source = "ssh://prod/makenotwork/latest.sql.gz" |
| 1219 |
local_path = "/tmp/server.sql.gz" |
| 1220 |
[[backup]] |
| 1221 |
name = "multithreaded" |
| 1222 |
source = "ssh://prod/multithreaded/latest.sql.gz" |
| 1223 |
local_path = "/tmp/mt.sql.gz""#, |
| 1224 |
) |
| 1225 |
.expect("the list form must load"); |
| 1226 |
assert_eq!(topo.backup.len(), 2); |
| 1227 |
assert_eq!( |
| 1228 |
topo.backup_named("multithreaded").unwrap().local_path, |
| 1229 |
"/tmp/mt.sql.gz" |
| 1230 |
); |
| 1231 |
assert!(topo.backup_named("nope").is_none()); |
| 1232 |
} |
| 1233 |
|
| 1234 |
#[test] |
| 1235 |
fn two_backups_sharing_a_name_are_rejected() { |
| 1236 |
|
| 1237 |
|
| 1238 |
let err = topo_with_backup_block( |
| 1239 |
r#" |
| 1240 |
[[backup]] |
| 1241 |
name = "server" |
| 1242 |
source = "a" |
| 1243 |
local_path = "/tmp/a.sql.gz" |
| 1244 |
[[backup]] |
| 1245 |
name = "server" |
| 1246 |
source = "b" |
| 1247 |
local_path = "/tmp/b.sql.gz""#, |
| 1248 |
) |
| 1249 |
.unwrap_err() |
| 1250 |
.to_string(); |
| 1251 |
assert!(err.contains("share the name"), "{err}"); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#[test] |
| 1255 |
fn two_backups_sharing_a_local_path_are_rejected() { |
| 1256 |
|
| 1257 |
|
| 1258 |
let err = topo_with_backup_block( |
| 1259 |
r#" |
| 1260 |
[[backup]] |
| 1261 |
name = "server" |
| 1262 |
source = "a" |
| 1263 |
local_path = "/tmp/same.sql.gz" |
| 1264 |
[[backup]] |
| 1265 |
name = "multithreaded" |
| 1266 |
source = "b" |
| 1267 |
local_path = "/tmp/same.sql.gz""#, |
| 1268 |
) |
| 1269 |
.unwrap_err() |
| 1270 |
.to_string(); |
| 1271 |
assert!(err.contains("share local_path"), "{err}"); |
| 1272 |
} |
| 1273 |
|
| 1274 |
#[test] |
| 1275 |
fn a_migration_check_naming_an_undeclared_backup_is_rejected_at_startup() { |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
let topo = topo_with_backup_block( |
| 1280 |
r#" |
| 1281 |
[backup] |
| 1282 |
source = "s" |
| 1283 |
local_path = "/tmp/d""#, |
| 1284 |
) |
| 1285 |
.unwrap(); |
| 1286 |
let checks = vec![crate::config::MigrationCheck { |
| 1287 |
dir: std::path::PathBuf::from("multithreaded/migrations"), |
| 1288 |
backup: "multithreaded".into(), |
| 1289 |
scratch_db: Some("sando_scratch_mt".into()), |
| 1290 |
owner_role: Some("multithreaded".into()), |
| 1291 |
}]; |
| 1292 |
let err = topo |
| 1293 |
.ensure_migration_checks_have_backups(&checks) |
| 1294 |
.unwrap_err() |
| 1295 |
.to_string(); |
| 1296 |
assert!(err.contains("which no [[backup]]"), "{err}"); |
| 1297 |
|
| 1298 |
|
| 1299 |
let shipped_checks = crate::config::default_migration_checks_for_test(); |
| 1300 |
shipped() |
| 1301 |
.ensure_migration_checks_have_backups(&shipped_checks) |
| 1302 |
.expect("the default server check resolves against the shipped topology"); |
| 1303 |
} |
| 1304 |
|
| 1305 |
#[test] |
| 1306 |
fn real_sando_toml_loads_clean() { |
| 1307 |
|
| 1308 |
|
| 1309 |
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../sando.toml"); |
| 1310 |
Topology::load(&path).expect("shipped sando.toml must validate"); |
| 1311 |
} |
| 1312 |
|
| 1313 |
fn shipped() -> Topology { |
| 1314 |
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../sando.toml"); |
| 1315 |
Topology::load(&path).expect("shipped sando.toml must validate") |
| 1316 |
} |
| 1317 |
|
| 1318 |
#[test] |
| 1319 |
fn shipping_to_the_last_provisioned_tier_needs_an_operator_signoff() { |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
let topo = shipped(); |
| 1329 |
let last = topo |
| 1330 |
.tiers |
| 1331 |
.iter() |
| 1332 |
.rposition(|t| t.provisioned) |
| 1333 |
.expect("some tier must be provisioned"); |
| 1334 |
assert!(last > 0, "the production tier cannot be the first tier"); |
| 1335 |
let guard = &topo.tiers[last - 1]; |
| 1336 |
assert!( |
| 1337 |
guard.gates.iter().any(|g| matches!(g, Gate::ManualConfirm)), |
| 1338 |
"tier {} guards promotion into the last provisioned tier ({}), so it must require an operator sign-off; its gates are {:?}", |
| 1339 |
guard.name, |
| 1340 |
topo.tiers[last].name, |
| 1341 |
guard |
| 1342 |
.gates |
| 1343 |
.iter() |
| 1344 |
.map(|g| g.kind().as_str()) |
| 1345 |
.collect::<Vec<_>>(), |
| 1346 |
); |
| 1347 |
} |
| 1348 |
|
| 1349 |
#[test] |
| 1350 |
fn every_serving_node_has_a_readiness_probe() { |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
let topo = shipped(); |
| 1355 |
for tier in topo.tiers.iter().filter(|t| t.provisioned) { |
| 1356 |
for node in &tier.nodes { |
| 1357 |
assert!( |
| 1358 |
node.health_url.is_some(), |
| 1359 |
"node {} on tier {} has no health_url, so node_health proves only that systemd thinks the unit is running", |
| 1360 |
node.name, |
| 1361 |
tier.name, |
| 1362 |
); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|