| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
use crate::domain::{AppId, Target}; |
| 20 |
use anyhow::{Context, Result}; |
| 21 |
use serde::Deserialize; |
| 22 |
use std::collections::HashMap; |
| 23 |
use std::path::{Path, PathBuf}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#[derive(Debug, Clone)] |
| 34 |
pub struct Topology { |
| 35 |
pub hosts: Vec<Host>, |
| 36 |
|
| 37 |
pub app: HashMap<String, AppConfig>, |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
#[derive(Debug, Clone, Deserialize)] |
| 42 |
struct RawTopology { |
| 43 |
#[serde(default, rename = "host")] |
| 44 |
hosts: Vec<Host>, |
| 45 |
|
| 46 |
#[serde(default)] |
| 47 |
app: HashMap<String, AppPointer>, |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[derive(Debug, Clone, Deserialize)] |
| 56 |
struct AppPointer { |
| 57 |
|
| 58 |
repo: String, |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 63 |
#[serde(rename_all = "snake_case")] |
| 64 |
pub enum Kind { |
| 65 |
|
| 66 |
#[default] |
| 67 |
App, |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
Library, |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
Service, |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[derive(Debug, Clone, Deserialize)] |
| 97 |
pub struct DeployTarget { |
| 98 |
|
| 99 |
pub target: Target, |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
pub host: String, |
| 104 |
|
| 105 |
#[serde(default)] |
| 106 |
pub port: Option<u16>, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
pub install_path: String, |
| 111 |
|
| 112 |
|
| 113 |
pub service: String, |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#[serde(default)] |
| 118 |
pub health_url: Option<String>, |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#[derive(Debug, Clone, Deserialize)] |
| 125 |
struct AppManifest { |
| 126 |
#[serde(default)] |
| 127 |
kind: Kind, |
| 128 |
#[serde(default = "default_branch")] |
| 129 |
branch: String, |
| 130 |
#[serde(default = "default_recipe_dir")] |
| 131 |
recipe_dir: String, |
| 132 |
#[serde(default)] |
| 133 |
version_path: Option<String>, |
| 134 |
#[serde(default)] |
| 135 |
features: Vec<String>, |
| 136 |
#[serde(default)] |
| 137 |
require_all_targets: bool, |
| 138 |
targets: Vec<Target>, |
| 139 |
|
| 140 |
#[serde(default, rename = "deploy")] |
| 141 |
deploy: Vec<DeployTarget>, |
| 142 |
#[serde(default = "default_tag_format")] |
| 143 |
tag_format: String, |
| 144 |
} |
| 145 |
|
| 146 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)] |
| 147 |
#[serde(rename_all = "snake_case")] |
| 148 |
pub enum HostTransport { |
| 149 |
|
| 150 |
|
| 151 |
#[default] |
| 152 |
Ssh, |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
Agent, |
| 158 |
} |
| 159 |
|
| 160 |
#[derive(Debug, Clone, Deserialize)] |
| 161 |
pub struct Host { |
| 162 |
pub name: String, |
| 163 |
|
| 164 |
pub ssh: String, |
| 165 |
|
| 166 |
|
| 167 |
#[serde(default)] |
| 168 |
pub targets: Vec<Target>, |
| 169 |
|
| 170 |
#[serde(default)] |
| 171 |
pub transport: HostTransport, |
| 172 |
|
| 173 |
|
| 174 |
#[serde(default)] |
| 175 |
pub agent_url: Option<String>, |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
#[serde(default = "default_actuate")] |
| 180 |
pub actuate: Vec<String>, |
| 181 |
|
| 182 |
#[serde(default = "default_observe")] |
| 183 |
pub observe: Vec<String>, |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
#[serde(default)] |
| 195 |
pub pull_root: Option<PathBuf>, |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
fn default_actuate() -> Vec<String> { |
| 202 |
vec!["build".into(), "package".into()] |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
fn default_observe() -> Vec<String> { |
| 209 |
vec!["build-log".into(), "artifact".into()] |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
#[derive(Debug, Clone)] |
| 214 |
pub struct AppConfig { |
| 215 |
|
| 216 |
pub repo: String, |
| 217 |
|
| 218 |
pub kind: Kind, |
| 219 |
pub branch: String, |
| 220 |
|
| 221 |
pub recipe_dir: String, |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
pub version_path: Option<String>, |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
pub features: Vec<String>, |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
pub require_all_targets: bool, |
| 245 |
|
| 246 |
pub targets: Vec<Target>, |
| 247 |
|
| 248 |
|
| 249 |
pub deploy: Vec<DeployTarget>, |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
pub tag_format: String, |
| 260 |
} |
| 261 |
|
| 262 |
fn default_tag_format() -> String { |
| 263 |
"v{version}".into() |
| 264 |
} |
| 265 |
|
| 266 |
impl AppConfig { |
| 267 |
|
| 268 |
pub fn deploy_for(&self, target: Target) -> Option<&DeployTarget> { |
| 269 |
self.deploy.iter().find(|d| d.target == target) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
pub fn tag_for(&self, version: &crate::domain::Version) -> String { |
| 274 |
self.tag_format.replace("{version}", &version.to_string()) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
fn default_branch() -> String { |
| 279 |
"main".into() |
| 280 |
} |
| 281 |
fn default_recipe_dir() -> String { |
| 282 |
"dist/recipes".into() |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
pub const APP_MANIFEST: &str = "bento.toml"; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
fn validate_deploy(name: &str, app: &AppConfig) -> Result<()> { |
| 297 |
if app.deploy.is_empty() { |
| 298 |
anyhow::ensure!( |
| 299 |
app.kind != Kind::Service, |
| 300 |
"app `{name}` is a service but declares no [[deploy]] entries — \ |
| 301 |
a service that lands nowhere has no release" |
| 302 |
); |
| 303 |
return Ok(()); |
| 304 |
} |
| 305 |
anyhow::ensure!( |
| 306 |
app.kind == Kind::Service, |
| 307 |
"app `{name}` declares [[deploy]] entries but is not `kind = \"service\"`; \ |
| 308 |
only a service is installed onto a host" |
| 309 |
); |
| 310 |
let mut seen = Vec::new(); |
| 311 |
for d in &app.deploy { |
| 312 |
anyhow::ensure!( |
| 313 |
app.targets.contains(&d.target), |
| 314 |
"app `{name}`: [[deploy]] names target {} which the app does not ship", |
| 315 |
d.target |
| 316 |
); |
| 317 |
anyhow::ensure!( |
| 318 |
!seen.contains(&d.target), |
| 319 |
"app `{name}`: two [[deploy]] entries for target {} — \ |
| 320 |
one target installs to one place", |
| 321 |
d.target |
| 322 |
); |
| 323 |
seen.push(d.target); |
| 324 |
anyhow::ensure!( |
| 325 |
!d.host.trim().is_empty(), |
| 326 |
"app `{name}`: [[deploy]] for {} has an empty host", |
| 327 |
d.target |
| 328 |
); |
| 329 |
|
| 330 |
anyhow::ensure!( |
| 331 |
d.install_path.starts_with('/') |
| 332 |
&& !Path::new(&d.install_path) |
| 333 |
.components() |
| 334 |
.any(|c| c == std::path::Component::ParentDir), |
| 335 |
"app `{name}`: install_path `{}` must be an absolute path with no `..`", |
| 336 |
d.install_path |
| 337 |
); |
| 338 |
|
| 339 |
|
| 340 |
anyhow::ensure!( |
| 341 |
d.service.ends_with(".service") |
| 342 |
&& !d.service.contains('/') |
| 343 |
&& !d.service.chars().any(char::is_whitespace), |
| 344 |
"app `{name}`: service `{}` must be a bare unit name ending in `.service`", |
| 345 |
d.service |
| 346 |
); |
| 347 |
} |
| 348 |
|
| 349 |
for t in &app.targets { |
| 350 |
anyhow::ensure!( |
| 351 |
seen.contains(t), |
| 352 |
"app `{name}`: target {t} has no [[deploy]] entry — \ |
| 353 |
every target a service ships must say where it lands" |
| 354 |
); |
| 355 |
} |
| 356 |
Ok(()) |
| 357 |
} |
| 358 |
|
| 359 |
impl Topology { |
| 360 |
pub fn load(path: &Path) -> Result<Self> { |
| 361 |
let raw = std::fs::read_to_string(path) |
| 362 |
.with_context(|| format!("reading topology at {}", path.display()))?; |
| 363 |
let raw: RawTopology = toml::from_str(&raw) |
| 364 |
.with_context(|| format!("parsing topology at {}", path.display()))?; |
| 365 |
Self::resolve(raw) |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
fn resolve(raw: RawTopology) -> Result<Self> { |
| 375 |
let mut app = HashMap::with_capacity(raw.app.len()); |
| 376 |
for (name, ptr) in raw.app { |
| 377 |
let manifest_path = crate::engine::expand_tilde(&ptr.repo).join(APP_MANIFEST); |
| 378 |
let text = std::fs::read_to_string(&manifest_path).with_context(|| { |
| 379 |
format!( |
| 380 |
"app `{name}`: reading {}. Per-app build config lives in the app's repo; \ |
| 381 |
create it there with `targets = [...]`", |
| 382 |
manifest_path.display() |
| 383 |
) |
| 384 |
})?; |
| 385 |
let m: AppManifest = toml::from_str(&text) |
| 386 |
.with_context(|| format!("app `{name}`: parsing {}", manifest_path.display()))?; |
| 387 |
app.insert( |
| 388 |
name, |
| 389 |
AppConfig { |
| 390 |
repo: ptr.repo, |
| 391 |
kind: m.kind, |
| 392 |
branch: m.branch, |
| 393 |
recipe_dir: m.recipe_dir, |
| 394 |
version_path: m.version_path, |
| 395 |
features: m.features, |
| 396 |
require_all_targets: m.require_all_targets, |
| 397 |
targets: m.targets, |
| 398 |
deploy: m.deploy, |
| 399 |
tag_format: m.tag_format, |
| 400 |
}, |
| 401 |
); |
| 402 |
} |
| 403 |
let topo = Topology { |
| 404 |
hosts: raw.hosts, |
| 405 |
app, |
| 406 |
}; |
| 407 |
topo.validate()?; |
| 408 |
Ok(topo) |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
#[cfg(test)] |
| 416 |
pub fn from_str_for_tests(s: &str) -> Result<Self> { |
| 417 |
Self::resolve(toml::from_str(s)?) |
| 418 |
} |
| 419 |
|
| 420 |
fn validate(&self) -> Result<()> { |
| 421 |
anyhow::ensure!( |
| 422 |
!self.hosts.is_empty(), |
| 423 |
"topology must declare at least one host" |
| 424 |
); |
| 425 |
anyhow::ensure!( |
| 426 |
!self.app.is_empty(), |
| 427 |
"topology must declare at least one app" |
| 428 |
); |
| 429 |
|
| 430 |
for (name, app) in &self.app { |
| 431 |
for t in &app.targets { |
| 432 |
if self.host_for(*t).is_none() { |
| 433 |
anyhow::bail!("app `{name}` ships target {t} but no host declares it"); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
anyhow::ensure!( |
| 439 |
app.tag_format.contains("{version}"), |
| 440 |
"app `{name}`: tag_format `{}` must contain `{{version}}`, or every \ |
| 441 |
release would resolve to the same tag", |
| 442 |
app.tag_format |
| 443 |
); |
| 444 |
anyhow::ensure!( |
| 445 |
!app.tag_format |
| 446 |
.chars() |
| 447 |
.any(|c| matches!(c, '"' | '`' | '$' | ';' | '&' | '|' | '\\' | ' ')), |
| 448 |
"app `{name}`: tag_format `{}` contains shell metacharacters", |
| 449 |
app.tag_format |
| 450 |
); |
| 451 |
validate_deploy(name, app)?; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
for h in &self.hosts { |
| 457 |
if !h.targets.is_empty() && !h.actuate.iter().any(|a| a == "build") { |
| 458 |
anyhow::bail!( |
| 459 |
"host `{}` declares buildable targets but is not granted the `build` capability", |
| 460 |
h.name |
| 461 |
); |
| 462 |
} |
| 463 |
if h.transport == HostTransport::Agent && h.agent_url.is_none() { |
| 464 |
anyhow::bail!( |
| 465 |
"host `{}` uses transport = \"agent\" but sets no agent_url", |
| 466 |
h.name |
| 467 |
); |
| 468 |
} |
| 469 |
} |
| 470 |
Ok(()) |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
pub fn host_for(&self, target: Target) -> Option<&Host> { |
| 475 |
self.hosts.iter().find(|h| h.targets.contains(&target)) |
| 476 |
} |
| 477 |
|
| 478 |
pub fn app(&self, app: &AppId) -> Option<&AppConfig> { |
| 479 |
self.app.get(app.as_str()) |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
#[cfg(test)] |
| 484 |
mod tests { |
| 485 |
use super::*; |
| 486 |
|
| 487 |
const HOSTS: &str = r#" |
| 488 |
[[host]] |
| 489 |
name = "fw13" |
| 490 |
ssh = "local" |
| 491 |
targets = ["linux/x86_64"] |
| 492 |
|
| 493 |
[[host]] |
| 494 |
name = "mbp" |
| 495 |
ssh = "mbp" |
| 496 |
targets = ["macos/aarch64", "ios/universal"] |
| 497 |
"#; |
| 498 |
|
| 499 |
const MANIFEST: &str = r#"targets = ["macos/aarch64", "linux/x86_64"] |
| 500 |
"#; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
fn load_with(hosts: &str, manifest: &str) -> Result<(Topology, tempfile::TempDir)> { |
| 506 |
let dir = tempfile::tempdir().unwrap(); |
| 507 |
let repo = dir.path().join("goingson"); |
| 508 |
std::fs::create_dir_all(&repo).unwrap(); |
| 509 |
std::fs::write(repo.join(APP_MANIFEST), manifest).unwrap(); |
| 510 |
let daemon = format!("{hosts}\n[app.goingson]\nrepo = \"{}\"\n", repo.display()); |
| 511 |
Topology::from_str_for_tests(&daemon).map(|t| (t, dir)) |
| 512 |
} |
| 513 |
|
| 514 |
fn load(hosts: &str) -> Result<Topology> { |
| 515 |
load_with(hosts, MANIFEST).map(|(t, dir)| { |
| 516 |
std::mem::forget(dir); |
| 517 |
t |
| 518 |
}) |
| 519 |
} |
| 520 |
|
| 521 |
#[test] |
| 522 |
fn parses_and_resolves_hosts() { |
| 523 |
let t = load(HOSTS).unwrap(); |
| 524 |
assert_eq!(t.hosts.len(), 2); |
| 525 |
let target: Target = "macos/aarch64".parse().unwrap(); |
| 526 |
assert_eq!(t.host_for(target).unwrap().name, "mbp"); |
| 527 |
assert_eq!(t.app(&"goingson".into()).unwrap().branch, "main"); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
#[test] |
| 534 |
fn features_defaults_empty_and_parses_when_present() { |
| 535 |
let t = load(HOSTS).unwrap(); |
| 536 |
assert!(t.app(&"goingson".into()).unwrap().features.is_empty()); |
| 537 |
|
| 538 |
let (t, _dir) = load_with( |
| 539 |
HOSTS, |
| 540 |
"targets = [\"linux/x86_64\"]\nfeatures = [\"supernote\", \"extra\"]\n", |
| 541 |
) |
| 542 |
.unwrap(); |
| 543 |
assert_eq!( |
| 544 |
t.app(&"goingson".into()).unwrap().features, |
| 545 |
vec!["supernote".to_string(), "extra".to_string()] |
| 546 |
); |
| 547 |
} |
| 548 |
|
| 549 |
#[test] |
| 550 |
fn rejects_target_without_a_host() { |
| 551 |
let only_linux = r#" |
| 552 |
[[host]] |
| 553 |
name = "fw13" |
| 554 |
ssh = "local" |
| 555 |
targets = ["linux/x86_64"] |
| 556 |
"#; |
| 557 |
assert!(load_with(only_linux, "targets = [\"windows/x86_64\"]\n").is_err()); |
| 558 |
} |
| 559 |
|
| 560 |
#[test] |
| 561 |
fn capability_defaults_make_a_build_host() { |
| 562 |
let t = load(HOSTS).unwrap(); |
| 563 |
let fw13 = t.hosts.iter().find(|h| h.name == "fw13").unwrap(); |
| 564 |
assert_eq!(fw13.transport, HostTransport::Ssh); |
| 565 |
assert!(fw13.actuate.contains(&"build".to_string())); |
| 566 |
assert!(fw13.actuate.contains(&"package".to_string())); |
| 567 |
} |
| 568 |
|
| 569 |
#[test] |
| 570 |
fn agent_transport_parses_with_url_and_caps() { |
| 571 |
let (t, _dir) = load_with( |
| 572 |
r#" |
| 573 |
[[host]] |
| 574 |
name = "mbp" |
| 575 |
ssh = "mbp" |
| 576 |
targets = ["macos/aarch64"] |
| 577 |
transport = "agent" |
| 578 |
agent_url = "http://mbp:8765" |
| 579 |
actuate = ["build", "sign", "notarize", "staple"] |
| 580 |
"#, |
| 581 |
"targets = [\"macos/aarch64\"]\n", |
| 582 |
) |
| 583 |
.unwrap(); |
| 584 |
let mbp = &t.hosts[0]; |
| 585 |
assert_eq!(mbp.transport, HostTransport::Agent); |
| 586 |
assert_eq!(mbp.agent_url.as_deref(), Some("http://mbp:8765")); |
| 587 |
assert!(mbp.actuate.contains(&"sign".to_string())); |
| 588 |
} |
| 589 |
|
| 590 |
#[test] |
| 591 |
fn agent_host_without_url_is_rejected() { |
| 592 |
let bad = r#" |
| 593 |
[[host]] |
| 594 |
name = "mbp" |
| 595 |
ssh = "mbp" |
| 596 |
targets = ["macos/aarch64"] |
| 597 |
transport = "agent" |
| 598 |
|
| 599 |
"#; |
| 600 |
assert!(load(bad).is_err()); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
#[test] |
| 608 |
fn tag_format_defaults_to_v_and_can_name_the_product() { |
| 609 |
let v = |s: &str| crate::domain::Version::parse(s).unwrap(); |
| 610 |
|
| 611 |
let t = load(HOSTS).unwrap(); |
| 612 |
let app = t.app(&"goingson".into()).unwrap(); |
| 613 |
assert_eq!(app.tag_format, "v{version}"); |
| 614 |
assert_eq!(app.tag_for(&v("0.4.1")), "v0.4.1"); |
| 615 |
|
| 616 |
let (t, _dir) = load_with( |
| 617 |
HOSTS, |
| 618 |
"targets = [\"linux/x86_64\"]\ntag_format = \"pom-v{version}\"\n", |
| 619 |
) |
| 620 |
.unwrap(); |
| 621 |
assert_eq!( |
| 622 |
t.app(&"goingson".into()).unwrap().tag_for(&v("0.4.1")), |
| 623 |
"pom-v0.4.1" |
| 624 |
); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
#[test] |
| 631 |
fn tag_format_must_vary_and_stay_shell_safe() { |
| 632 |
let bad = |f: &str| { |
| 633 |
load_with( |
| 634 |
HOSTS, |
| 635 |
&format!("targets = [\"linux/x86_64\"]\ntag_format = \"{f}\"\n"), |
| 636 |
) |
| 637 |
.is_err() |
| 638 |
}; |
| 639 |
assert!(bad("release"), "a constant tag pins every release together"); |
| 640 |
assert!(bad("v{version}; rm -rf /")); |
| 641 |
assert!(bad("v{version}$(id)")); |
| 642 |
assert!(bad("v{version} extra")); |
| 643 |
assert!(!bad("pom-v{version}")); |
| 644 |
assert!(!bad("release/{version}")); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
#[test] |
| 651 |
fn service_deploy_entries_resolve_per_target() { |
| 652 |
let (t, _dir) = load_with( |
| 653 |
HOSTS, |
| 654 |
r#"kind = "service" |
| 655 |
targets = ["linux/x86_64", "macos/aarch64"] |
| 656 |
|
| 657 |
[[deploy]] |
| 658 |
target = "linux/x86_64" |
| 659 |
host = "root@prod" |
| 660 |
port = 2200 |
| 661 |
install_path = "/usr/local/bin/demo" |
| 662 |
service = "demo.service" |
| 663 |
health_url = "http://prod:9100/api/health" |
| 664 |
|
| 665 |
[[deploy]] |
| 666 |
target = "macos/aarch64" |
| 667 |
host = "mbp" |
| 668 |
install_path = "/usr/local/bin/demo" |
| 669 |
service = "demo.service" |
| 670 |
"#, |
| 671 |
) |
| 672 |
.unwrap(); |
| 673 |
let app = t.app(&"goingson".into()).unwrap(); |
| 674 |
assert_eq!(app.kind, Kind::Service); |
| 675 |
let x86 = app.deploy_for("linux/x86_64".parse().unwrap()).unwrap(); |
| 676 |
assert_eq!(x86.host, "root@prod"); |
| 677 |
assert_eq!(x86.port, Some(2200)); |
| 678 |
assert_eq!( |
| 679 |
x86.health_url.as_deref(), |
| 680 |
Some("http://prod:9100/api/health") |
| 681 |
); |
| 682 |
let mac = app.deploy_for("macos/aarch64".parse().unwrap()).unwrap(); |
| 683 |
assert_eq!(mac.host, "mbp"); |
| 684 |
assert_eq!(mac.port, None); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
#[test] |
| 691 |
fn kind_and_deploy_entries_must_agree() { |
| 692 |
let deploy = "\n[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 693 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n"; |
| 694 |
|
| 695 |
assert!( |
| 696 |
load_with(HOSTS, &format!("targets = [\"linux/x86_64\"]\n{deploy}")).is_err(), |
| 697 |
"only a service installs onto a host" |
| 698 |
); |
| 699 |
|
| 700 |
assert!( |
| 701 |
load_with(HOSTS, "kind = \"service\"\ntargets = [\"linux/x86_64\"]\n").is_err(), |
| 702 |
"a service that lands nowhere has no release" |
| 703 |
); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
#[test] |
| 709 |
fn service_target_without_a_deploy_entry_is_rejected() { |
| 710 |
let err = load_with( |
| 711 |
HOSTS, |
| 712 |
"kind = \"service\"\ntargets = [\"linux/x86_64\", \"macos/aarch64\"]\n\ |
| 713 |
[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 714 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n", |
| 715 |
) |
| 716 |
.unwrap_err(); |
| 717 |
assert!( |
| 718 |
format!("{err:#}").contains("macos/aarch64"), |
| 719 |
"must name the target with no destination: {err:#}" |
| 720 |
); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
#[test] |
| 728 |
fn deploy_rejects_paths_and_units_the_installer_would_refuse() { |
| 729 |
let entry = |install: &str, service: &str| { |
| 730 |
format!( |
| 731 |
"kind = \"service\"\ntargets = [\"linux/x86_64\"]\n\ |
| 732 |
[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 733 |
install_path = \"{install}\"\nservice = \"{service}\"\n" |
| 734 |
) |
| 735 |
}; |
| 736 |
|
| 737 |
|
| 738 |
assert!(load_with(HOSTS, &entry("usr/local/bin/d", "d.service")).is_err()); |
| 739 |
assert!(load_with(HOSTS, &entry("/opt/../etc/systemd/system/x", "d.service")).is_err()); |
| 740 |
|
| 741 |
|
| 742 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "/etc/x.service")).is_err()); |
| 743 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d.service x")).is_err()); |
| 744 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d")).is_err()); |
| 745 |
|
| 746 |
assert!(load_with(HOSTS, &entry("/usr/local/bin/d", "d.service")).is_ok()); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
#[test] |
| 752 |
fn duplicate_deploy_entries_for_one_target_are_rejected() { |
| 753 |
let e = "[[deploy]]\ntarget = \"linux/x86_64\"\nhost = \"h\"\n\ |
| 754 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n"; |
| 755 |
assert!( |
| 756 |
load_with( |
| 757 |
HOSTS, |
| 758 |
&format!("kind = \"service\"\ntargets = [\"linux/x86_64\"]\n{e}{e}") |
| 759 |
) |
| 760 |
.is_err() |
| 761 |
); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
#[test] |
| 767 |
fn deploy_entry_for_an_unshipped_target_is_rejected() { |
| 768 |
assert!( |
| 769 |
load_with( |
| 770 |
HOSTS, |
| 771 |
"kind = \"service\"\ntargets = [\"linux/x86_64\"]\n\ |
| 772 |
[[deploy]]\ntarget = \"macos/aarch64\"\nhost = \"h\"\n\ |
| 773 |
install_path = \"/usr/local/bin/d\"\nservice = \"d.service\"\n", |
| 774 |
) |
| 775 |
.is_err() |
| 776 |
); |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn build_host_without_build_capability_is_rejected() { |
| 781 |
let bad = r#" |
| 782 |
[[host]] |
| 783 |
name = "fw13" |
| 784 |
ssh = "local" |
| 785 |
targets = ["linux/x86_64"] |
| 786 |
actuate = ["package"] |
| 787 |
|
| 788 |
"#; |
| 789 |
assert!(load(bad).is_err()); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
#[cfg(test)] |
| 794 |
mod live_config_smoke { |
| 795 |
use super::*; |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
#[test] |
| 802 |
fn live_topology_loads_if_present() { |
| 803 |
let Some(home) = std::env::var_os("HOME") else { |
| 804 |
return; |
| 805 |
}; |
| 806 |
let path = Path::new(&home).join(".config/bento/bento.toml"); |
| 807 |
if !path.exists() { |
| 808 |
return; |
| 809 |
} |
| 810 |
let topo = Topology::load(&path).expect("live bento.toml must load"); |
| 811 |
let bb = topo |
| 812 |
.app(&"balanced_breakfast".into()) |
| 813 |
.expect("bb configured"); |
| 814 |
assert!( |
| 815 |
bb.features.contains(&"supernote".to_string()), |
| 816 |
"bb must ship the supernote feature, got {:?}", |
| 817 |
bb.features |
| 818 |
); |
| 819 |
let af = topo |
| 820 |
.app(&"audiofiles".into()) |
| 821 |
.expect("audiofiles configured"); |
| 822 |
assert_eq!( |
| 823 |
af.version_path.as_deref(), |
| 824 |
Some("crates/audiofiles-app/Cargo.toml") |
| 825 |
); |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
for name in ["makeover", "pter", "everycycle", "supernote-push"] { |
| 830 |
let c = topo |
| 831 |
.app(&name.into()) |
| 832 |
.unwrap_or_else(|| panic!("{name} configured")); |
| 833 |
assert_eq!(c.kind, Kind::Library, "{name} must be a library"); |
| 834 |
} |
| 835 |
assert_eq!(topo.app(&"goingson".into()).unwrap().kind, Kind::App); |
| 836 |
} |
| 837 |
} |
| 838 |
|