| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use crate::domain::AppId; |
| 25 |
use anyhow::{Context, Result}; |
| 26 |
use serde::Deserialize; |
| 27 |
use std::collections::BTreeMap; |
| 28 |
use std::net::{IpAddr, ToSocketAddrs}; |
| 29 |
use std::path::{Path, PathBuf}; |
| 30 |
use std::sync::Arc; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#[derive(Debug, Clone, Deserialize)] |
| 40 |
pub struct DaemonConfig { |
| 41 |
pub listen: String, |
| 42 |
pub db_path: PathBuf, |
| 43 |
|
| 44 |
|
| 45 |
#[serde(default, rename = "app")] |
| 46 |
pub apps: BTreeMap<String, AppSource>, |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
#[derive(Debug, Clone, Deserialize)] |
| 51 |
pub struct AppSource { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub config: PathBuf, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[derive(Debug, Clone, Deserialize)] |
| 60 |
pub struct AppConfig { |
| 61 |
|
| 62 |
|
| 63 |
#[serde(skip)] |
| 64 |
pub id: AppId, |
| 65 |
pub topology_path: PathBuf, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
#[serde(default)] |
| 76 |
pub platform: Option<crate::domain::Platform>, |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#[serde(default)] |
| 90 |
pub build_host: Option<String>, |
| 91 |
|
| 92 |
pub workdir: PathBuf, |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub release_root: PathBuf, |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#[serde(default)] |
| 104 |
pub scratch_db_url: Option<String>, |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#[serde(default = "default_scratch_owner_role")] |
| 114 |
pub scratch_owner_role: String, |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#[serde(default = "default_boot_smoke_port")] |
| 120 |
pub boot_smoke_port: u16, |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[serde(default = "default_code_smoke_port")] |
| 127 |
pub code_smoke_port: u16, |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#[serde(default = "default_bin_names")] |
| 133 |
pub bin_names: Vec<String>, |
| 134 |
|
| 135 |
|
| 136 |
#[serde(default = "default_logs_root")] |
| 137 |
pub logs_root: PathBuf, |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
#[serde(default)] |
| 147 |
pub cargo_target_dir: Option<PathBuf>, |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#[serde(default)] |
| 155 |
pub release_contents: Vec<ReleaseEntry>, |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
#[serde(default = "default_gate_timeout_secs")] |
| 162 |
pub gate_timeout_secs: u64, |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#[serde(default, rename = "companion")] |
| 172 |
pub companions: Vec<Companion>, |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[serde(default = "default_test_targets", rename = "test_target")] |
| 179 |
pub test_targets: Vec<TestTarget>, |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#[serde(default, rename = "frontend_build")] |
| 191 |
pub frontend_builds: Vec<FrontendBuild>, |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[serde(default = "default_migration_checks", rename = "migration_check")] |
| 203 |
pub migration_checks: Vec<MigrationCheck>, |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#[serde(default = "default_backup_max_age_hours")] |
| 212 |
pub backup_max_age_hours: u32, |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
#[derive(Debug, Clone, Deserialize)] |
| 217 |
pub struct FrontendBuild { |
| 218 |
|
| 219 |
|
| 220 |
pub dir: PathBuf, |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#[serde(default = "default_frontend_script")] |
| 225 |
pub script: String, |
| 226 |
} |
| 227 |
|
| 228 |
fn default_frontend_script() -> String { |
| 229 |
"build".into() |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
#[derive(Debug, Clone, Deserialize)] |
| 234 |
pub struct TestTarget { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#[serde(default)] |
| 239 |
pub dir: PathBuf, |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#[serde(default)] |
| 254 |
pub aux_repo: Option<String>, |
| 255 |
|
| 256 |
|
| 257 |
#[serde(default)] |
| 258 |
pub features: Vec<String>, |
| 259 |
|
| 260 |
|
| 261 |
#[serde(default)] |
| 262 |
pub all_features: bool, |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#[serde(default)] |
| 268 |
pub scratch_db: bool, |
| 269 |
} |
| 270 |
|
| 271 |
impl TestTarget { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
pub fn label(&self) -> String { |
| 276 |
let dir = self.dir.display().to_string(); |
| 277 |
match (self.aux_repo.as_deref(), dir.is_empty()) { |
| 278 |
(None, _) => dir, |
| 279 |
(Some(repo), true) => format!("{repo} (aux)"), |
| 280 |
(Some(repo), false) => format!("{repo}/{dir} (aux)"), |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
#[derive(Debug, Clone, Deserialize)] |
| 289 |
pub struct MigrationCheck { |
| 290 |
|
| 291 |
|
| 292 |
pub dir: PathBuf, |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
#[serde(default = "default_backup_name")] |
| 301 |
pub backup: String, |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
#[serde(default)] |
| 310 |
pub scratch_db: Option<String>, |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
#[serde(default)] |
| 320 |
pub owner_role: Option<String>, |
| 321 |
} |
| 322 |
|
| 323 |
fn default_backup_name() -> String { |
| 324 |
"server".into() |
| 325 |
} |
| 326 |
|
| 327 |
fn default_migration_checks() -> Vec<MigrationCheck> { |
| 328 |
vec![MigrationCheck { |
| 329 |
dir: PathBuf::from("server").join("migrations"), |
| 330 |
backup: default_backup_name(), |
| 331 |
scratch_db: None, |
| 332 |
owner_role: None, |
| 333 |
}] |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
#[cfg(test)] |
| 338 |
pub(crate) fn default_migration_checks_for_test() -> Vec<MigrationCheck> { |
| 339 |
default_migration_checks() |
| 340 |
} |
| 341 |
|
| 342 |
fn default_test_targets() -> Vec<TestTarget> { |
| 343 |
vec![TestTarget { |
| 344 |
dir: PathBuf::from("server"), |
| 345 |
aux_repo: None, |
| 346 |
features: vec!["fast-tests".into()], |
| 347 |
all_features: false, |
| 348 |
scratch_db: true, |
| 349 |
}] |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
#[derive(Debug, Clone, Deserialize)] |
| 356 |
pub struct Companion { |
| 357 |
|
| 358 |
pub name: String, |
| 359 |
|
| 360 |
|
| 361 |
pub manifest_dir: PathBuf, |
| 362 |
|
| 363 |
pub bin: String, |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
#[derive(Debug, Clone, Deserialize)] |
| 370 |
pub struct ReleaseEntry { |
| 371 |
|
| 372 |
pub src: PathBuf, |
| 373 |
|
| 374 |
|
| 375 |
pub dst: PathBuf, |
| 376 |
|
| 377 |
#[serde(default)] |
| 378 |
pub required: bool, |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
fn scratch_db_host(url: &str) -> Option<String> { |
| 385 |
let after = url.find("://").map(|i| i + 3)?; |
| 386 |
let authority_end = url[after..] |
| 387 |
.find(['/', '?', '#']) |
| 388 |
.map_or(url.len(), |i| after + i); |
| 389 |
let authority = &url[after..authority_end]; |
| 390 |
|
| 391 |
let host_port = authority.rsplit('@').next().unwrap_or(authority); |
| 392 |
|
| 393 |
|
| 394 |
let host = if let Some(rest) = host_port.strip_prefix('[') { |
| 395 |
rest.split(']').next().unwrap_or("") |
| 396 |
} else { |
| 397 |
host_port.split(':').next().unwrap_or("") |
| 398 |
}; |
| 399 |
if host.is_empty() { |
| 400 |
None |
| 401 |
} else { |
| 402 |
Some(host.to_string()) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
fn assert_scratch_db_loopback(url: &str) -> Result<()> { |
| 412 |
let Some(host) = scratch_db_host(url) else { |
| 413 |
return Ok(()); |
| 414 |
}; |
| 415 |
|
| 416 |
|
| 417 |
if host.starts_with('/') || host.to_ascii_lowercase().starts_with("%2f") { |
| 418 |
return Ok(()); |
| 419 |
} |
| 420 |
|
| 421 |
if let Ok(ip) = host.parse::<IpAddr>() { |
| 422 |
anyhow::ensure!( |
| 423 |
ip.is_loopback(), |
| 424 |
"scratch_db_url host {host} is not loopback ({ip}); migration_dry_run DROPs and \ |
| 425 |
recreates this database, so it must never point off-box (use 127.0.0.1, ::1, \ |
| 426 |
localhost, or a local unix socket)", |
| 427 |
); |
| 428 |
return Ok(()); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
let addrs: Vec<_> = (host.as_str(), 0u16) |
| 434 |
.to_socket_addrs() |
| 435 |
.with_context(|| format!("resolving scratch_db_url host {host} to confirm it is loopback"))? |
| 436 |
.collect(); |
| 437 |
anyhow::ensure!( |
| 438 |
!addrs.is_empty(), |
| 439 |
"scratch_db_url host {host} resolved to no addresses; cannot confirm it is loopback", |
| 440 |
); |
| 441 |
anyhow::ensure!( |
| 442 |
addrs.iter().all(|a| a.ip().is_loopback()), |
| 443 |
"scratch_db_url host {host} resolves off-box (not loopback); migration_dry_run DROPs and \ |
| 444 |
recreates this database, so it must never point off-box", |
| 445 |
); |
| 446 |
Ok(()) |
| 447 |
} |
| 448 |
|
| 449 |
fn default_bin_names() -> Vec<String> { |
| 450 |
vec!["server".into()] |
| 451 |
} |
| 452 |
fn default_scratch_owner_role() -> String { |
| 453 |
"makenotwork".into() |
| 454 |
} |
| 455 |
fn default_logs_root() -> PathBuf { |
| 456 |
PathBuf::from("/srv/sando/logs") |
| 457 |
} |
| 458 |
fn default_boot_smoke_port() -> u16 { |
| 459 |
18181 |
| 460 |
} |
| 461 |
fn default_code_smoke_port() -> u16 { |
| 462 |
18182 |
| 463 |
} |
| 464 |
fn default_gate_timeout_secs() -> u64 { |
| 465 |
2400 |
| 466 |
} |
| 467 |
fn default_backup_max_age_hours() -> u32 { |
| 468 |
48 |
| 469 |
} |
| 470 |
|
| 471 |
impl DaemonConfig { |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
pub fn load() -> Result<(Self, BTreeMap<AppId, Arc<AppConfig>>)> { |
| 478 |
let path = std::env::var("SANDO_CONFIG").unwrap_or_else(|_| "sando-daemon.toml".into()); |
| 479 |
Self::load_from(Path::new(&path)) |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
pub fn load_from(path: &Path) -> Result<(Self, BTreeMap<AppId, Arc<AppConfig>>)> { |
| 485 |
let raw = std::fs::read_to_string(path) |
| 486 |
.with_context(|| format!("reading daemon config at {}", path.display()))?; |
| 487 |
let daemon: Self = toml::from_str(&raw) |
| 488 |
.with_context(|| format!("parsing daemon config at {}", path.display()))?; |
| 489 |
|
| 490 |
let mut apps: BTreeMap<AppId, Arc<AppConfig>> = BTreeMap::new(); |
| 491 |
if daemon.apps.is_empty() { |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
let id = AppId::default(); |
| 497 |
let mut cfg: AppConfig = toml::from_str(&raw) |
| 498 |
.with_context(|| format!("parsing {} as app `{id}`", path.display()))?; |
| 499 |
cfg.id = id.clone(); |
| 500 |
cfg.resolve_paths_against(path); |
| 501 |
cfg.validate()?; |
| 502 |
apps.insert(id, Arc::new(cfg)); |
| 503 |
} else { |
| 504 |
for (name, src) in &daemon.apps { |
| 505 |
let id = AppId::new(name.clone()); |
| 506 |
let app_path = resolve_against(path, &src.config); |
| 507 |
let raw = std::fs::read_to_string(&app_path).with_context(|| { |
| 508 |
format!("reading config for app `{id}` at {}", app_path.display()) |
| 509 |
})?; |
| 510 |
let mut cfg: AppConfig = toml::from_str(&raw).with_context(|| { |
| 511 |
format!("parsing config for app `{id}` at {}", app_path.display()) |
| 512 |
})?; |
| 513 |
cfg.id = id.clone(); |
| 514 |
cfg.resolve_paths_against(&app_path); |
| 515 |
cfg.validate() |
| 516 |
.with_context(|| format!("validating app `{id}`"))?; |
| 517 |
apps.insert(id, Arc::new(cfg)); |
| 518 |
} |
| 519 |
} |
| 520 |
anyhow::ensure!( |
| 521 |
!apps.is_empty(), |
| 522 |
"no apps configured; a daemon that ships nothing has nothing to do" |
| 523 |
); |
| 524 |
Ok((daemon, apps)) |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
fn resolve_against(base: &Path, p: &Path) -> PathBuf { |
| 532 |
if p.is_absolute() { |
| 533 |
return p.to_path_buf(); |
| 534 |
} |
| 535 |
base.parent().unwrap_or(Path::new(".")).join(p) |
| 536 |
} |
| 537 |
|
| 538 |
impl AppConfig { |
| 539 |
|
| 540 |
pub fn primary_bin(&self) -> &str { |
| 541 |
self.bin_names |
| 542 |
.first() |
| 543 |
.map_or("server", std::string::String::as_str) |
| 544 |
} |
| 545 |
|
| 546 |
fn resolve_paths_against(&mut self, config_path: &Path) { |
| 547 |
self.topology_path = resolve_against(config_path, &self.topology_path); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
pub fn validate(&self) -> Result<()> { |
| 554 |
anyhow::ensure!( |
| 555 |
!self.scratch_owner_role.is_empty() |
| 556 |
&& self |
| 557 |
.scratch_owner_role |
| 558 |
.bytes() |
| 559 |
.all(|b| b.is_ascii_alphanumeric() || b == b'_'), |
| 560 |
"scratch_owner_role must be non-empty and match [A-Za-z0-9_]+ (got {:?}); it is \ |
| 561 |
interpolated into DDL as a bare identifier", |
| 562 |
self.scratch_owner_role, |
| 563 |
); |
| 564 |
anyhow::ensure!( |
| 565 |
!self.test_targets.is_empty(), |
| 566 |
"test_target list is empty; cargo_test would run nothing and pass. Omit the \ |
| 567 |
key entirely to get the default `server` target.", |
| 568 |
); |
| 569 |
for t in &self.test_targets { |
| 570 |
anyhow::ensure!( |
| 571 |
!t.all_features || t.features.is_empty(), |
| 572 |
"test_target {} sets both all_features and features; pick one", |
| 573 |
t.dir.display(), |
| 574 |
); |
| 575 |
} |
| 576 |
anyhow::ensure!( |
| 577 |
!self.migration_checks.is_empty(), |
| 578 |
"migration_check list is empty; migration_dry_run would restore nothing and pass. \ |
| 579 |
Omit the key entirely to get the default `server/migrations` check.", |
| 580 |
); |
| 581 |
for (i, m) in self.migration_checks.iter().enumerate() { |
| 582 |
anyhow::ensure!( |
| 583 |
!m.backup.is_empty(), |
| 584 |
"migration_check {} has an empty backup name; omit the key for the default \ |
| 585 |
`server`", |
| 586 |
m.dir.display(), |
| 587 |
); |
| 588 |
for role in [m.owner_role.as_deref(), m.scratch_db.as_deref()] |
| 589 |
.into_iter() |
| 590 |
.flatten() |
| 591 |
{ |
| 592 |
anyhow::ensure!( |
| 593 |
!role.is_empty() |
| 594 |
&& role.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'), |
| 595 |
"migration_check {} has {role:?} as an owner_role/scratch_db; both are \ |
| 596 |
interpolated into DDL as bare identifiers and must match [A-Za-z0-9_]+", |
| 597 |
m.dir.display(), |
| 598 |
); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
for prior in &self.migration_checks[..i] { |
| 604 |
anyhow::ensure!( |
| 605 |
prior.dir != m.dir, |
| 606 |
"two migration_check entries share dir {}", |
| 607 |
m.dir.display(), |
| 608 |
); |
| 609 |
anyhow::ensure!( |
| 610 |
prior.scratch_db != m.scratch_db, |
| 611 |
"migration_check {} and {} share a scratch database ({}); each check drops \ |
| 612 |
and recreates its own, so they would clobber each other", |
| 613 |
prior.dir.display(), |
| 614 |
m.dir.display(), |
| 615 |
m.scratch_db |
| 616 |
.as_deref() |
| 617 |
.unwrap_or("the configured scratch_db_url"), |
| 618 |
); |
| 619 |
} |
| 620 |
} |
| 621 |
for f in &self.frontend_builds { |
| 622 |
anyhow::ensure!( |
| 623 |
!f.script.is_empty(), |
| 624 |
"frontend_build {} has an empty script; omit the key for the default `build`", |
| 625 |
f.dir.display(), |
| 626 |
); |
| 627 |
} |
| 628 |
if let Some(url) = self.scratch_db_url.as_deref() { |
| 629 |
assert_scratch_db_loopback(url) |
| 630 |
.context("scratch_db_url must address a loopback (on-box) database")?; |
| 631 |
} |
| 632 |
Ok(()) |
| 633 |
} |
| 634 |
|
| 635 |
#[cfg(test)] |
| 636 |
pub fn for_tests() -> Self { |
| 637 |
Self { |
| 638 |
platform: None, |
| 639 |
id: crate::domain::AppId::default(), |
| 640 |
topology_path: PathBuf::from("/tmp/sando-test-topology.toml"), |
| 641 |
build_host: Some("test-host".into()), |
| 642 |
workdir: PathBuf::from("/tmp/sando-test-workdir"), |
| 643 |
release_root: PathBuf::from("/tmp/sando-test-release-root"), |
| 644 |
scratch_db_url: None, |
| 645 |
scratch_owner_role: default_scratch_owner_role(), |
| 646 |
boot_smoke_port: default_boot_smoke_port(), |
| 647 |
code_smoke_port: default_code_smoke_port(), |
| 648 |
bin_names: vec!["server".into()], |
| 649 |
logs_root: PathBuf::from("/tmp/sando-test-logs"), |
| 650 |
release_contents: Vec::new(), |
| 651 |
cargo_target_dir: None, |
| 652 |
gate_timeout_secs: default_gate_timeout_secs(), |
| 653 |
companions: Vec::new(), |
| 654 |
test_targets: default_test_targets(), |
| 655 |
migration_checks: default_migration_checks(), |
| 656 |
frontend_builds: Vec::new(), |
| 657 |
backup_max_age_hours: default_backup_max_age_hours(), |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
#[cfg(test)] |
| 663 |
mod tests { |
| 664 |
use super::*; |
| 665 |
|
| 666 |
const MINIMAL: &str = r#" |
| 667 |
listen = "127.0.0.1:7766" |
| 668 |
db_path = "./sando.db" |
| 669 |
topology_path = "../sando.toml" |
| 670 |
build_host = "fw13" |
| 671 |
workdir = "./work" |
| 672 |
release_root = "./releases" |
| 673 |
"#; |
| 674 |
|
| 675 |
|
| 676 |
fn file(dir: &Path, name: &str, body: &str) -> PathBuf { |
| 677 |
let p = dir.join(name); |
| 678 |
std::fs::write(&p, body).unwrap(); |
| 679 |
p |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
#[test] |
| 690 |
fn a_config_with_no_app_tables_loads_as_the_default_app() { |
| 691 |
let dir = tempfile::tempdir().unwrap(); |
| 692 |
let path = file(dir.path(), "sando-daemon.toml", MINIMAL); |
| 693 |
let (daemon, apps) = DaemonConfig::load_from(&path).unwrap(); |
| 694 |
|
| 695 |
assert_eq!(daemon.listen, "127.0.0.1:7766"); |
| 696 |
assert_eq!(apps.len(), 1); |
| 697 |
let id = AppId::new(crate::domain::DEFAULT_APP); |
| 698 |
let app = &apps[&id]; |
| 699 |
assert_eq!(app.id, id, "the app must know its own name"); |
| 700 |
assert_eq!(app.build_host.as_deref(), Some("fw13")); |
| 701 |
|
| 702 |
|
| 703 |
assert_eq!(app.topology_path, dir.path().join("../sando.toml")); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
#[test] |
| 708 |
fn apps_load_their_own_configs_and_keep_their_own_names() { |
| 709 |
let dir = tempfile::tempdir().unwrap(); |
| 710 |
file( |
| 711 |
dir.path(), |
| 712 |
"mnw.toml", |
| 713 |
r#" |
| 714 |
topology_path = "mnw-topology.toml" |
| 715 |
build_host = "fw13" |
| 716 |
workdir = "./work/mnw" |
| 717 |
release_root = "./releases/mnw" |
| 718 |
bin_names = ["makenotwork", "mnw-admin"] |
| 719 |
"#, |
| 720 |
); |
| 721 |
file( |
| 722 |
dir.path(), |
| 723 |
"pom.toml", |
| 724 |
r#" |
| 725 |
topology_path = "pom-topology.toml" |
| 726 |
build_host = "fw13" |
| 727 |
workdir = "./work/pom" |
| 728 |
release_root = "./releases/pom" |
| 729 |
bin_names = ["pom"] |
| 730 |
[[test_target]] |
| 731 |
dir = "pom" |
| 732 |
[[migration_check]] |
| 733 |
dir = "pom/migrations" |
| 734 |
"#, |
| 735 |
); |
| 736 |
let path = file( |
| 737 |
dir.path(), |
| 738 |
"sando-daemon.toml", |
| 739 |
r#" |
| 740 |
listen = "127.0.0.1:7766" |
| 741 |
db_path = "./sando.db" |
| 742 |
[app.mnw] |
| 743 |
config = "mnw.toml" |
| 744 |
[app.pom] |
| 745 |
config = "pom.toml" |
| 746 |
"#, |
| 747 |
); |
| 748 |
|
| 749 |
let (_daemon, apps) = DaemonConfig::load_from(&path).unwrap(); |
| 750 |
assert_eq!(apps.len(), 2); |
| 751 |
let mnw = &apps[&AppId::new("mnw")]; |
| 752 |
let pom = &apps[&AppId::new("pom")]; |
| 753 |
assert_eq!(mnw.primary_bin(), "makenotwork"); |
| 754 |
assert_eq!(pom.primary_bin(), "pom"); |
| 755 |
|
| 756 |
|
| 757 |
assert_ne!(mnw.release_root, pom.release_root); |
| 758 |
assert_ne!(mnw.workdir, pom.workdir); |
| 759 |
assert_eq!(pom.topology_path, dir.path().join("pom-topology.toml")); |
| 760 |
assert_eq!(pom.id, AppId::new("pom")); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
#[test] |
| 766 |
fn a_bad_app_config_is_refused_at_load_and_names_the_app() { |
| 767 |
let dir = tempfile::tempdir().unwrap(); |
| 768 |
file( |
| 769 |
dir.path(), |
| 770 |
"pom.toml", |
| 771 |
r#" |
| 772 |
topology_path = "t.toml" |
| 773 |
build_host = "fw13" |
| 774 |
workdir = "./w" |
| 775 |
release_root = "./r" |
| 776 |
scratch_owner_role = "not a valid identifier" |
| 777 |
"#, |
| 778 |
); |
| 779 |
let path = file( |
| 780 |
dir.path(), |
| 781 |
"sando-daemon.toml", |
| 782 |
"listen = \"127.0.0.1:7766\"\ndb_path = \"./sando.db\"\n[app.pom]\nconfig = \"pom.toml\"\n", |
| 783 |
); |
| 784 |
let err = format!("{:#}", DaemonConfig::load_from(&path).unwrap_err()); |
| 785 |
assert!(err.contains("pom"), "{err}"); |
| 786 |
assert!(err.contains("scratch_owner_role"), "{err}"); |
| 787 |
} |
| 788 |
|
| 789 |
#[test] |
| 790 |
fn an_app_whose_config_is_missing_says_so() { |
| 791 |
let dir = tempfile::tempdir().unwrap(); |
| 792 |
let path = file( |
| 793 |
dir.path(), |
| 794 |
"sando-daemon.toml", |
| 795 |
"listen = \"127.0.0.1:7766\"\ndb_path = \"./sando.db\"\n[app.pom]\nconfig = \"absent.toml\"\n", |
| 796 |
); |
| 797 |
let err = format!("{:#}", DaemonConfig::load_from(&path).unwrap_err()); |
| 798 |
assert!( |
| 799 |
err.contains("app `pom`") && err.contains("absent.toml"), |
| 800 |
"{err}" |
| 801 |
); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn test_targets_default_to_the_historical_server_entry() { |
| 806 |
|
| 807 |
|
| 808 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 809 |
assert_eq!(cfg.test_targets.len(), 1); |
| 810 |
let t = &cfg.test_targets[0]; |
| 811 |
assert_eq!(t.dir, PathBuf::from("server")); |
| 812 |
assert_eq!(t.features, ["fast-tests"]); |
| 813 |
assert!(t.scratch_db); |
| 814 |
assert!(!t.all_features); |
| 815 |
} |
| 816 |
|
| 817 |
#[test] |
| 818 |
fn migration_checks_default_to_the_historical_server_entry() { |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 823 |
assert_eq!(cfg.migration_checks.len(), 1); |
| 824 |
let m = &cfg.migration_checks[0]; |
| 825 |
assert_eq!(m.dir, PathBuf::from("server/migrations")); |
| 826 |
assert_eq!(m.backup, "server"); |
| 827 |
assert!(m.scratch_db.is_none()); |
| 828 |
assert!(m.owner_role.is_none()); |
| 829 |
} |
| 830 |
|
| 831 |
#[test] |
| 832 |
fn migration_checks_parse_as_a_list() { |
| 833 |
let raw = format!( |
| 834 |
"{MINIMAL}\n\ |
| 835 |
[[migration_check]]\ndir = \"server/migrations\"\n\ |
| 836 |
[[migration_check]]\ndir = \"multithreaded/migrations\"\n\ |
| 837 |
backup = \"multithreaded\"\nscratch_db = \"sando_scratch_mt\"\n\ |
| 838 |
owner_role = \"multithreaded\"\n" |
| 839 |
); |
| 840 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 841 |
cfg.validate().unwrap(); |
| 842 |
assert_eq!(cfg.migration_checks[0].backup, "server", "backup defaults"); |
| 843 |
let mt = &cfg.migration_checks[1]; |
| 844 |
assert_eq!(mt.scratch_db.as_deref(), Some("sando_scratch_mt")); |
| 845 |
assert_eq!(mt.owner_role.as_deref(), Some("multithreaded")); |
| 846 |
} |
| 847 |
|
| 848 |
#[test] |
| 849 |
fn two_migration_checks_sharing_a_scratch_db_are_rejected() { |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
let raw = format!( |
| 855 |
"{MINIMAL}\n\ |
| 856 |
[[migration_check]]\ndir = \"server/migrations\"\n\ |
| 857 |
[[migration_check]]\ndir = \"multithreaded/migrations\"\nbackup = \"multithreaded\"\n" |
| 858 |
); |
| 859 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 860 |
let err = cfg.validate().unwrap_err().to_string(); |
| 861 |
assert!(err.contains("share a scratch database"), "{err}"); |
| 862 |
} |
| 863 |
|
| 864 |
#[test] |
| 865 |
fn a_migration_check_scratch_db_that_is_not_an_identifier_is_rejected() { |
| 866 |
|
| 867 |
let raw = |
| 868 |
format!("{MINIMAL}\n[[migration_check]]\ndir = \"m\"\nscratch_db = \"drop; --\"\n"); |
| 869 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 870 |
let err = cfg.validate().unwrap_err().to_string(); |
| 871 |
assert!(err.contains("[A-Za-z0-9_]+"), "{err}"); |
| 872 |
} |
| 873 |
|
| 874 |
#[test] |
| 875 |
fn an_empty_migration_check_list_is_rejected() { |
| 876 |
|
| 877 |
|
| 878 |
let raw = format!("{MINIMAL}\nmigration_check = []\n"); |
| 879 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 880 |
let err = cfg.validate().unwrap_err().to_string(); |
| 881 |
assert!(err.contains("migration_check list is empty"), "{err}"); |
| 882 |
} |
| 883 |
|
| 884 |
#[test] |
| 885 |
fn test_targets_parse_as_a_list() { |
| 886 |
let raw = format!( |
| 887 |
"{MINIMAL}\nscratch_db_url = \"postgres:///x\"\n\ |
| 888 |
[[test_target]]\ndir = \"server\"\nfeatures = [\"fast-tests\"]\nscratch_db = true\n\ |
| 889 |
[[test_target]]\ndir = \"shared/tagtree\"\n\ |
| 890 |
[[test_target]]\ndir = \"shared/ops-exec\"\nall_features = true\n" |
| 891 |
); |
| 892 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 893 |
cfg.validate().unwrap(); |
| 894 |
let dirs: Vec<_> = cfg |
| 895 |
.test_targets |
| 896 |
.iter() |
| 897 |
.map(|t| t.dir.display().to_string()) |
| 898 |
.collect(); |
| 899 |
assert_eq!(dirs, ["server", "shared/tagtree", "shared/ops-exec"]); |
| 900 |
|
| 901 |
assert!(cfg.test_targets[1].features.is_empty()); |
| 902 |
assert!(!cfg.test_targets[1].scratch_db); |
| 903 |
assert!(cfg.test_targets[2].all_features); |
| 904 |
} |
| 905 |
|
| 906 |
#[test] |
| 907 |
fn validate_rejects_an_empty_test_target_list() { |
| 908 |
|
| 909 |
let raw = format!("{MINIMAL}\ntest_target = []\n"); |
| 910 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 911 |
let err = cfg.validate().unwrap_err().to_string(); |
| 912 |
assert!(err.contains("test_target list is empty"), "got: {err}"); |
| 913 |
} |
| 914 |
|
| 915 |
#[test] |
| 916 |
fn validate_rejects_all_features_together_with_features() { |
| 917 |
let raw = format!( |
| 918 |
"{MINIMAL}\n[[test_target]]\ndir = \"x\"\nall_features = true\nfeatures = [\"y\"]\n" |
| 919 |
); |
| 920 |
let err = toml::from_str::<AppConfig>(&raw) |
| 921 |
.unwrap() |
| 922 |
.validate() |
| 923 |
.unwrap_err() |
| 924 |
.to_string(); |
| 925 |
assert!(err.contains("pick one"), "got: {err}"); |
| 926 |
} |
| 927 |
|
| 928 |
#[test] |
| 929 |
fn scratch_db_without_a_url_is_not_a_config_error() { |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
let raw = format!("{MINIMAL}\n[[test_target]]\ndir = \"server\"\nscratch_db = true\n"); |
| 935 |
toml::from_str::<AppConfig>(&raw) |
| 936 |
.unwrap() |
| 937 |
.validate() |
| 938 |
.unwrap(); |
| 939 |
} |
| 940 |
|
| 941 |
#[test] |
| 942 |
fn shipped_daemon_config_parses_and_validates() { |
| 943 |
|
| 944 |
|
| 945 |
let raw = std::fs::read_to_string( |
| 946 |
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("sando-daemon.toml"), |
| 947 |
) |
| 948 |
.expect("sando-daemon.toml ships with the crate"); |
| 949 |
let cfg: AppConfig = toml::from_str(&raw).expect("shipped config parses"); |
| 950 |
cfg.validate().expect("shipped config validates"); |
| 951 |
assert!( |
| 952 |
cfg.test_targets |
| 953 |
.iter() |
| 954 |
.any(|t| t.dir == std::path::Path::new("mnw-cli")), |
| 955 |
"the companion that installs onto prod-1 must be gated", |
| 956 |
); |
| 957 |
|
| 958 |
|
| 959 |
for dir in ["server/frontend", "multithreaded/frontend"] { |
| 960 |
assert!( |
| 961 |
cfg.frontend_builds |
| 962 |
.iter() |
| 963 |
.any(|f| f.dir == std::path::Path::new(dir)), |
| 964 |
"{dir} must be gated: its build script swallows a tsc error", |
| 965 |
); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
#[test] |
| 970 |
fn frontend_builds_default_to_empty_and_to_the_build_script() { |
| 971 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 972 |
assert!( |
| 973 |
cfg.frontend_builds.is_empty(), |
| 974 |
"a project with no frontend must configure nothing" |
| 975 |
); |
| 976 |
|
| 977 |
let raw = format!("{MINIMAL}\n[[frontend_build]]\ndir = \"server/frontend\"\n"); |
| 978 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 979 |
cfg.validate().unwrap(); |
| 980 |
assert_eq!(cfg.frontend_builds[0].script, "build"); |
| 981 |
} |
| 982 |
|
| 983 |
#[test] |
| 984 |
fn validate_rejects_an_empty_frontend_script() { |
| 985 |
let raw = format!("{MINIMAL}\n[[frontend_build]]\ndir = \"x\"\nscript = \"\"\n"); |
| 986 |
let err = toml::from_str::<AppConfig>(&raw) |
| 987 |
.unwrap() |
| 988 |
.validate() |
| 989 |
.unwrap_err() |
| 990 |
.to_string(); |
| 991 |
assert!(err.contains("empty script"), "got: {err}"); |
| 992 |
} |
| 993 |
|
| 994 |
#[test] |
| 995 |
fn cargo_target_dir_parses_when_present() { |
| 996 |
let raw = format!("{MINIMAL}\ncargo_target_dir = \"/srv/sando/cargo-target\"\n"); |
| 997 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 998 |
assert_eq!( |
| 999 |
cfg.cargo_target_dir.as_deref(), |
| 1000 |
Some(std::path::Path::new("/srv/sando/cargo-target")) |
| 1001 |
); |
| 1002 |
} |
| 1003 |
|
| 1004 |
#[test] |
| 1005 |
fn cargo_target_dir_defaults_to_none() { |
| 1006 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 1007 |
assert!( |
| 1008 |
cfg.cargo_target_dir.is_none(), |
| 1009 |
"omitting it keeps the per-worktree target/" |
| 1010 |
); |
| 1011 |
} |
| 1012 |
|
| 1013 |
#[test] |
| 1014 |
fn gate_timeout_defaults_when_omitted() { |
| 1015 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 1016 |
assert_eq!( |
| 1017 |
cfg.gate_timeout_secs, 2400, |
| 1018 |
"omitting it keeps the 40-min ceiling" |
| 1019 |
); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
fn gate_timeout_parses_when_present() { |
| 1024 |
let raw = format!("{MINIMAL}\ngate_timeout_secs = 600\n"); |
| 1025 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 1026 |
assert_eq!(cfg.gate_timeout_secs, 600); |
| 1027 |
} |
| 1028 |
|
| 1029 |
#[test] |
| 1030 |
fn companions_default_empty_and_parse_when_present() { |
| 1031 |
let base: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 1032 |
assert!( |
| 1033 |
base.companions.is_empty(), |
| 1034 |
"omitting [[companion]] keeps it empty" |
| 1035 |
); |
| 1036 |
|
| 1037 |
let raw = format!( |
| 1038 |
"{MINIMAL}\n[[companion]]\nname = \"mnw-cli\"\nmanifest_dir = \"mnw-cli\"\nbin = \"mnw-cli\"\n" |
| 1039 |
); |
| 1040 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 1041 |
assert_eq!(cfg.companions.len(), 1); |
| 1042 |
assert_eq!(cfg.companions[0].name, "mnw-cli"); |
| 1043 |
assert_eq!( |
| 1044 |
cfg.companions[0].manifest_dir, |
| 1045 |
std::path::Path::new("mnw-cli") |
| 1046 |
); |
| 1047 |
assert_eq!(cfg.companions[0].bin, "mnw-cli"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
#[test] |
| 1051 |
fn scratch_owner_role_defaults_to_makenotwork() { |
| 1052 |
let cfg: AppConfig = toml::from_str(MINIMAL).unwrap(); |
| 1053 |
assert_eq!(cfg.scratch_owner_role, "makenotwork"); |
| 1054 |
cfg.validate().unwrap(); |
| 1055 |
} |
| 1056 |
|
| 1057 |
#[test] |
| 1058 |
fn scratch_owner_role_rejects_non_identifiers() { |
| 1059 |
|
| 1060 |
|
| 1061 |
for bad in ["", "mnw-owner", "own er", "own\"er", "x; DROP ROLE sando"] { |
| 1062 |
let raw = format!("{MINIMAL}\nscratch_owner_role = {bad:?}\n"); |
| 1063 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 1064 |
assert!(cfg.validate().is_err(), "should reject {bad:?}"); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[test] |
| 1069 |
fn scratch_owner_role_accepts_a_plain_identifier() { |
| 1070 |
let raw = format!("{MINIMAL}\nscratch_owner_role = \"app_owner_2\"\n"); |
| 1071 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 1072 |
cfg.validate().unwrap(); |
| 1073 |
assert_eq!(cfg.scratch_owner_role, "app_owner_2"); |
| 1074 |
} |
| 1075 |
|
| 1076 |
#[test] |
| 1077 |
fn scratch_db_url_accepts_loopback_and_socket_forms() { |
| 1078 |
|
| 1079 |
|
| 1080 |
for ok in [ |
| 1081 |
"postgres://sando@127.0.0.1/sando_scratch", |
| 1082 |
"postgres://sando:s3cret@127.0.0.1:5432/scratch", |
| 1083 |
"postgres://sando@[::1]:5432/scratch", |
| 1084 |
"postgres:///scratch", |
| 1085 |
"postgres://sando@%2Fvar%2Frun%2Fpostgresql/scratch", |
| 1086 |
"postgres://localhost/scratch", |
| 1087 |
] { |
| 1088 |
assert!( |
| 1089 |
assert_scratch_db_loopback(ok).is_ok(), |
| 1090 |
"should accept on-box url {ok}" |
| 1091 |
); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
#[test] |
| 1096 |
fn scratch_db_url_rejects_off_box_hosts() { |
| 1097 |
|
| 1098 |
for bad in [ |
| 1099 |
"postgres://sando@10.1.2.3/scratch", |
| 1100 |
"postgres://sando:pw@192.168.1.5:5432/scratch", |
| 1101 |
"postgres://sando@[2001:db8::1]/scratch", |
| 1102 |
] { |
| 1103 |
let err = assert_scratch_db_loopback(bad).unwrap_err().to_string(); |
| 1104 |
assert!( |
| 1105 |
err.contains("loopback") || err.contains("off-box"), |
| 1106 |
"got: {err}" |
| 1107 |
); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
#[test] |
| 1112 |
fn validate_rejects_a_non_loopback_scratch_db_url() { |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
let raw = format!( |
| 1117 |
"{MINIMAL}\nscratch_db_url = \"postgres://sando@db.internal.example:5432/scratch\"\n" |
| 1118 |
); |
| 1119 |
let cfg: AppConfig = toml::from_str(&raw).unwrap(); |
| 1120 |
assert!( |
| 1121 |
cfg.validate().is_err(), |
| 1122 |
"a non-loopback scratch_db_url must fail startup" |
| 1123 |
); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn an_absent_build_host_declares_the_product_intake_only() { |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
let without = MINIMAL.replace("build_host = \"fw13\"\n", ""); |
| 1133 |
let cfg: AppConfig = toml::from_str(&without).expect("intake-only is a valid product"); |
| 1134 |
assert_eq!(cfg.build_host, None); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|