| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use super::git::{ |
| 9 |
git_fetch_cmd, git_rev_parse_cmd, git_tag_exists_cmd, git_worktree_pin_cmd, |
| 10 |
worktree_failure_reason, |
| 11 |
}; |
| 12 |
use super::{StepState, action_for, default_step_budget}; |
| 13 |
use crate::config::Config; |
| 14 |
use crate::domain::{AppId, Status, Step, StepRunId, Target, Version}; |
| 15 |
use crate::events::{self, Event, EventTx}; |
| 16 |
use crate::ota::OtaRegistry; |
| 17 |
use crate::state::ExecutorMap; |
| 18 |
use crate::topology::{DeployTarget, Kind}; |
| 19 |
use anyhow::{Context as _, Result}; |
| 20 |
use ops_core::live_log::LiveLog; |
| 21 |
use ops_exec::{Action, Executor, Step as OpStep}; |
| 22 |
use sqlx::SqlitePool; |
| 23 |
use std::collections::HashMap; |
| 24 |
use std::path::PathBuf; |
| 25 |
use std::sync::atomic::{AtomicBool, Ordering}; |
| 26 |
use std::sync::{Arc, Mutex}; |
| 27 |
use tokio::runtime::Handle; |
| 28 |
use tokio::sync::Mutex as AsyncMutex; |
| 29 |
|
| 30 |
|
| 31 |
pub struct RecipeCtx { |
| 32 |
pub app: AppId, |
| 33 |
pub version: Version, |
| 34 |
pub target: Target, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
pub build_host: String, |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub build_host_ssh: String, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
pub tag: String, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pub repo: String, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub repo_by_host: HashMap<String, String>, |
| 59 |
|
| 60 |
|
| 61 |
pub features: Vec<String>, |
| 62 |
|
| 63 |
|
| 64 |
pub kind: Kind, |
| 65 |
pub target_run_id: i64, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub execs: Arc<ExecutorMap>, |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub syncs: Arc<ExecutorMap>, |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
pub deploy: Option<DeployTarget>, |
| 83 |
pub pool: SqlitePool, |
| 84 |
pub events: EventTx, |
| 85 |
pub cfg: Arc<Config>, |
| 86 |
pub ota: Arc<OtaRegistry>, |
| 87 |
pub rt: Handle, |
| 88 |
current: Mutex<Option<StepState>>, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
gatekeeper_ok: Mutex<Option<bool>>, |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
failed_steps: Mutex<Vec<Step>>, |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
cancel: Arc<AtomicBool>, |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
all_green_required: Option<Vec<Target>>, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
artifact_hashes: Mutex<HashMap<String, String>>, |
| 112 |
} |
| 113 |
|
| 114 |
impl RecipeCtx { |
| 115 |
#[allow(clippy::too_many_arguments)] |
| 116 |
pub fn new( |
| 117 |
app: AppId, |
| 118 |
version: Version, |
| 119 |
target: Target, |
| 120 |
build_host: String, |
| 121 |
build_host_ssh: String, |
| 122 |
tag: String, |
| 123 |
repo: String, |
| 124 |
features: Vec<String>, |
| 125 |
kind: Kind, |
| 126 |
target_run_id: i64, |
| 127 |
execs: Arc<ExecutorMap>, |
| 128 |
syncs: Arc<ExecutorMap>, |
| 129 |
deploy: Option<DeployTarget>, |
| 130 |
pool: SqlitePool, |
| 131 |
events: EventTx, |
| 132 |
cfg: Arc<Config>, |
| 133 |
ota: Arc<OtaRegistry>, |
| 134 |
rt: Handle, |
| 135 |
cancel: Arc<AtomicBool>, |
| 136 |
all_green_required: Option<Vec<Target>>, |
| 137 |
) -> Self { |
| 138 |
Self { |
| 139 |
app, |
| 140 |
version, |
| 141 |
target, |
| 142 |
build_host, |
| 143 |
build_host_ssh, |
| 144 |
tag, |
| 145 |
repo, |
| 146 |
repo_by_host: HashMap::new(), |
| 147 |
features, |
| 148 |
kind, |
| 149 |
target_run_id, |
| 150 |
execs, |
| 151 |
syncs, |
| 152 |
deploy, |
| 153 |
pool, |
| 154 |
events, |
| 155 |
cfg, |
| 156 |
ota, |
| 157 |
rt, |
| 158 |
current: Mutex::new(None), |
| 159 |
gatekeeper_ok: Mutex::new(None), |
| 160 |
failed_steps: Mutex::new(Vec::new()), |
| 161 |
cancel, |
| 162 |
all_green_required, |
| 163 |
artifact_hashes: Mutex::new(HashMap::new()), |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
#[must_use] |
| 173 |
pub fn with_repo_by_host(mut self, repo_by_host: HashMap<String, String>) -> Self { |
| 174 |
self.repo_by_host = repo_by_host; |
| 175 |
self |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
pub fn repo_for(&self, host: &str) -> &str { |
| 183 |
self.repo_by_host |
| 184 |
.get(host) |
| 185 |
.map_or(self.repo.as_str(), String::as_str) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
pub(super) fn is_cancelled(&self) -> bool { |
| 190 |
self.cancel.load(Ordering::SeqCst) |
| 191 |
} |
| 192 |
|
| 193 |
pub(super) fn now() -> String { |
| 194 |
chrono::Utc::now().to_rfc3339() |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
fn log_path(&self, step: Step, run_id: StepRunId) -> PathBuf { |
| 206 |
self.log_dir() |
| 207 |
.join(format!("{}.{}.log", step.as_str(), run_id.0)) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
fn log_dir(&self) -> PathBuf { |
| 212 |
let target_dir = self.target.to_string().replace('/', "-"); |
| 213 |
self.cfg |
| 214 |
.logs_root |
| 215 |
.join(self.app.as_str()) |
| 216 |
.join(self.version.to_string()) |
| 217 |
.join(target_dir) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pub(super) fn begin_step(self: &Arc<Self>, step: Step) -> Result<()> { |
| 223 |
anyhow::ensure!( |
| 224 |
!self.is_cancelled(), |
| 225 |
"build superseded by a newer request; aborting before `{}`", |
| 226 |
step.as_str() |
| 227 |
); |
| 228 |
self.finish_step(Status::Ok)?; |
| 229 |
let me = self.clone(); |
| 230 |
let started = Self::now(); |
| 231 |
let started_for_header = started.clone(); |
| 232 |
let run_id = self.rt.block_on(async move { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
let mut tx = me.pool.begin().await.context("begin step tx")?; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
let id: i64 = sqlx::query_scalar( |
| 241 |
"INSERT INTO step_runs (target_run_id, step, status, started_at) |
| 242 |
VALUES (?, ?, 'running', ?) RETURNING id", |
| 243 |
) |
| 244 |
.bind(me.target_run_id) |
| 245 |
.bind(step.as_str()) |
| 246 |
.bind(&started) |
| 247 |
.fetch_one(&mut *tx) |
| 248 |
.await |
| 249 |
.context("insert step_run")?; |
| 250 |
let log_ref = me |
| 251 |
.log_path(step, StepRunId(id)) |
| 252 |
.to_string_lossy() |
| 253 |
.into_owned(); |
| 254 |
sqlx::query("UPDATE step_runs SET log_ref = ? WHERE id = ?") |
| 255 |
.bind(&log_ref) |
| 256 |
.bind(id) |
| 257 |
.execute(&mut *tx) |
| 258 |
.await |
| 259 |
.context("set step_run log_ref")?; |
| 260 |
sqlx::query("UPDATE target_runs SET current_step = ? WHERE id = ?") |
| 261 |
.bind(step.as_str()) |
| 262 |
.bind(me.target_run_id) |
| 263 |
.execute(&mut *tx) |
| 264 |
.await |
| 265 |
.context("update current_step")?; |
| 266 |
tx.commit().await.context("commit step tx")?; |
| 267 |
anyhow::Ok(StepRunId(id)) |
| 268 |
})?; |
| 269 |
|
| 270 |
|
| 271 |
let events = self.events.clone(); |
| 272 |
let cb_run_id = run_id; |
| 273 |
let mut log = self.rt.block_on(LiveLog::open( |
| 274 |
self.log_path(step, run_id), |
| 275 |
Box::new(move |seq, text| { |
| 276 |
events::emit( |
| 277 |
&events, |
| 278 |
Event::StepLogChunk { |
| 279 |
run_id: cb_run_id, |
| 280 |
seq, |
| 281 |
text: text.to_string(), |
| 282 |
}, |
| 283 |
); |
| 284 |
}), |
| 285 |
)); |
| 286 |
|
| 287 |
events::emit( |
| 288 |
&self.events, |
| 289 |
Event::StepStart { |
| 290 |
run_id, |
| 291 |
app: self.app.clone(), |
| 292 |
version: self.version.clone(), |
| 293 |
target: self.target, |
| 294 |
step, |
| 295 |
}, |
| 296 |
); |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
let header = format!( |
| 304 |
"=== bento {app} {version} {target} step={step} run_id={run_id} started={started_for_header} ===\n", |
| 305 |
app = self.app.as_str(), |
| 306 |
version = self.version, |
| 307 |
target = self.target, |
| 308 |
step = step.as_str(), |
| 309 |
); |
| 310 |
self.rt.block_on(async { |
| 311 |
use ops_core::remote::LogSink as _; |
| 312 |
log.write_chunk(header.as_bytes()).await; |
| 313 |
}); |
| 314 |
|
| 315 |
*self.current.lock().unwrap() = Some(StepState { |
| 316 |
run_id, |
| 317 |
step, |
| 318 |
log: Arc::new(AsyncMutex::new(log)), |
| 319 |
failed: false, |
| 320 |
deadline: std::time::Instant::now() + self.step_budget(step), |
| 321 |
}); |
| 322 |
Ok(()) |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
fn step_budget(&self, step: Step) -> std::time::Duration { |
| 328 |
self.cfg |
| 329 |
.step_timeout_secs |
| 330 |
.map_or_else(|| default_step_budget(step), std::time::Duration::from_secs) |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
fn step_deadline(&self) -> std::time::Instant { |
| 337 |
self.current.lock().unwrap().as_ref().map_or_else( |
| 338 |
|| std::time::Instant::now() + self.step_budget(Step::Build), |
| 339 |
|s| s.deadline, |
| 340 |
) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
pub(super) fn run_bounded<F, T>(&self, what: &str, fut: F) -> Result<T> |
| 350 |
where |
| 351 |
F: std::future::Future<Output = Result<T>>, |
| 352 |
{ |
| 353 |
let deadline = self.step_deadline(); |
| 354 |
let cancel = self.cancel.clone(); |
| 355 |
self.rt.block_on(async move { |
| 356 |
tokio::pin!(fut); |
| 357 |
let watch = async { |
| 358 |
|
| 359 |
|
| 360 |
while !cancel.load(Ordering::SeqCst) { |
| 361 |
tokio::time::sleep(std::time::Duration::from_millis(250)).await; |
| 362 |
} |
| 363 |
}; |
| 364 |
tokio::select! { |
| 365 |
r = &mut fut => r, |
| 366 |
() = tokio::time::sleep_until(deadline.into()) => { |
| 367 |
Err(anyhow::anyhow!("`{what}` exceeded its per-step deadline")) |
| 368 |
} |
| 369 |
() = watch => { |
| 370 |
Err(anyhow::anyhow!("build superseded by a newer request; aborting `{what}`")) |
| 371 |
} |
| 372 |
} |
| 373 |
}) |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
pub(super) fn fail_current_step(&self) { |
| 380 |
if let Some(st) = self.current.lock().unwrap().as_mut() { |
| 381 |
st.failed = true; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
pub fn finish_step(self: &Arc<Self>, status: Status) -> Result<()> { |
| 390 |
let st = self.current.lock().unwrap().take(); |
| 391 |
let Some(st) = st else { return Ok(()) }; |
| 392 |
let status = if st.failed { Status::Failed } else { status }; |
| 393 |
if status == Status::Failed { |
| 394 |
self.failed_steps.lock().unwrap().push(st.step); |
| 395 |
} |
| 396 |
let me = self.clone(); |
| 397 |
self.rt.block_on(async move { |
| 398 |
|
| 399 |
if let Ok(m) = Arc::try_unwrap(st.log) { |
| 400 |
m.into_inner().close().await; |
| 401 |
} |
| 402 |
if let Err(e) = sqlx::query( |
| 403 |
"UPDATE step_runs SET status = ?, finished_at = ? WHERE id = ?", |
| 404 |
) |
| 405 |
.bind(status.as_str()) |
| 406 |
.bind(Self::now()) |
| 407 |
.bind(st.run_id.0) |
| 408 |
.execute(&me.pool) |
| 409 |
.await |
| 410 |
{ |
| 411 |
tracing::error!(step = st.step.as_str(), error = %e, "could not stamp step_run status"); |
| 412 |
} |
| 413 |
}); |
| 414 |
events::emit( |
| 415 |
&self.events, |
| 416 |
Event::StepDone { |
| 417 |
run_id: st.run_id, |
| 418 |
app: self.app.clone(), |
| 419 |
target: self.target, |
| 420 |
step: st.step, |
| 421 |
status, |
| 422 |
}, |
| 423 |
); |
| 424 |
Ok(()) |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
pub(super) fn ensure_step(self: &Arc<Self>) -> Result<Arc<AsyncMutex<LiveLog>>> { |
| 430 |
if self.current.lock().unwrap().is_none() { |
| 431 |
self.begin_step(Step::Build)?; |
| 432 |
} |
| 433 |
Ok(self.current.lock().unwrap().as_ref().unwrap().log.clone()) |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
pub fn current_step(&self) -> Step { |
| 439 |
self.current |
| 440 |
.lock() |
| 441 |
.unwrap() |
| 442 |
.as_ref() |
| 443 |
.map_or(Step::Build, |s| s.step) |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
pub(super) fn exec(&self, name: &str) -> Result<Arc<dyn ops_exec::Executor>> { |
| 449 |
self.execs |
| 450 |
.get(name) |
| 451 |
.cloned() |
| 452 |
.ok_or_else(|| anyhow::anyhow!("unknown build host `{name}` (not in topology)")) |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
pub(super) fn host_sync(&self, name: &str) -> Result<Arc<dyn Executor>> { |
| 460 |
self.syncs |
| 461 |
.get(name) |
| 462 |
.cloned() |
| 463 |
.ok_or_else(|| anyhow::anyhow!("unknown build host `{name}` (not in topology)")) |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
pub(super) fn run(self: &Arc<Self>, host: &str, cmd: &str) -> Result<(i32, String)> { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
let action = match &self.deploy { |
| 478 |
Some(d) if d.host == host => Action::Deploy, |
| 479 |
_ => action_for(self.current_step(), self.kind), |
| 480 |
}; |
| 481 |
self.run_as(host, cmd, action) |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
fn run_as(self: &Arc<Self>, host: &str, cmd: &str, action: Action) -> Result<(i32, String)> { |
| 487 |
let sink = self.ensure_step()?; |
| 488 |
let exec = self.exec(host)?; |
| 489 |
let cur = self.current_step(); |
| 490 |
let step = OpStep::shell(action, cmd.to_string()); |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
let echo = format!("$ [{host}] {cmd}\n"); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
let label = format!("{cur} command on `{host}`"); |
| 500 |
let out = self.run_bounded(&label, async move { |
| 501 |
use ops_core::remote::LogSink as _; |
| 502 |
let mut guard = sink.lock().await; |
| 503 |
guard.write_chunk(echo.as_bytes()).await; |
| 504 |
exec.run_streaming(&step, &mut *guard).await |
| 505 |
})?; |
| 506 |
let code = out.status.code().unwrap_or(-1); |
| 507 |
let stdout = String::from_utf8_lossy(&out.stdout); |
| 508 |
let tail: String = stdout |
| 509 |
.chars() |
| 510 |
.rev() |
| 511 |
.take(2000) |
| 512 |
.collect::<Vec<_>>() |
| 513 |
.into_iter() |
| 514 |
.rev() |
| 515 |
.collect(); |
| 516 |
Ok((code, tail)) |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
pub(super) fn checkout_sha(self: &Arc<Self>, host: &str) -> Result<String> { |
| 531 |
|
| 532 |
|
| 533 |
let repo = self.repo_for(host).to_string(); |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
let _ = self.run(host, &git_fetch_cmd(&repo))?; |
| 538 |
let (code, err) = self.run(host, &git_worktree_pin_cmd(&repo, &self.tag))?; |
| 539 |
if code != 0 { |
| 540 |
let (probe, _) = self.run(host, &git_tag_exists_cmd(&repo, &self.tag))?; |
| 541 |
anyhow::bail!( |
| 542 |
"checkout of {} failed on `{host}`: {}", |
| 543 |
self.tag, |
| 544 |
worktree_failure_reason(&self.tag, probe == 0, &err) |
| 545 |
); |
| 546 |
} |
| 547 |
let (code, tail) = self.run(host, &git_rev_parse_cmd(&repo))?; |
| 548 |
anyhow::ensure!(code == 0, "rev-parse failed on `{host}`"); |
| 549 |
Ok(tail.trim().to_string()) |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
pub fn artifact_hashes(&self) -> HashMap<String, String> { |
| 557 |
self.artifact_hashes.lock().unwrap().clone() |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
pub(super) fn failed_steps_snapshot(&self) -> Vec<Step> { |
| 566 |
self.failed_steps.lock().unwrap().clone() |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
pub(super) fn record_artifact_hash(&self, name: String, digest: String) { |
| 571 |
self.artifact_hashes.lock().unwrap().insert(name, digest); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
pub(super) fn artifact_hash(&self, name: &str) -> Option<String> { |
| 579 |
self.artifact_hashes.lock().unwrap().get(name).cloned() |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
pub(super) fn all_green_required(&self) -> Option<Vec<Target>> { |
| 584 |
self.all_green_required.clone() |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
pub(super) fn gatekeeper_ok(&self) -> Option<bool> { |
| 589 |
*self.gatekeeper_ok.lock().unwrap() |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
pub(super) fn set_gatekeeper_ok(&self, accepted: bool) { |
| 594 |
*self.gatekeeper_ok.lock().unwrap() = Some(accepted); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
#[cfg(test)] |
| 599 |
mod tests { |
| 600 |
use super::super::build_engine; |
| 601 |
use super::*; |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
#[tokio::test] |
| 607 |
async fn begin_step_bails_when_cancelled() { |
| 608 |
let dir = tempfile::tempdir().unwrap(); |
| 609 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 610 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 611 |
let cancel = Arc::new(AtomicBool::new(true)); |
| 612 |
let ctx = Arc::new(RecipeCtx::new( |
| 613 |
AppId::new("demo"), |
| 614 |
Version::parse("0.1.0").unwrap(), |
| 615 |
"linux/x86_64".parse().unwrap(), |
| 616 |
"fw13".into(), |
| 617 |
"local".into(), |
| 618 |
"v0.1.0".into(), |
| 619 |
"/tmp".into(), |
| 620 |
vec![], |
| 621 |
Kind::App, |
| 622 |
1, |
| 623 |
Arc::new(std::collections::HashMap::new()), |
| 624 |
Arc::new(std::collections::HashMap::new()), |
| 625 |
None, |
| 626 |
pool, |
| 627 |
crate::events::channel(), |
| 628 |
cfg, |
| 629 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 630 |
tokio::runtime::Handle::current(), |
| 631 |
cancel.clone(), |
| 632 |
None, |
| 633 |
)); |
| 634 |
|
| 635 |
|
| 636 |
let err = ctx.begin_step(Step::Build).unwrap_err(); |
| 637 |
assert!(err.to_string().contains("supersede"), "got: {err}"); |
| 638 |
assert!(ctx.is_cancelled()); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
#[tokio::test] |
| 645 |
async fn feature_flags_renders_whole_flag_or_empty() { |
| 646 |
async fn flags_for(features: Vec<String>) -> String { |
| 647 |
let dir = tempfile::tempdir().unwrap(); |
| 648 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 649 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 650 |
let ctx = Arc::new(RecipeCtx::new( |
| 651 |
AppId::new("demo"), |
| 652 |
Version::parse("0.1.0").unwrap(), |
| 653 |
"linux/x86_64".parse().unwrap(), |
| 654 |
"fw13".into(), |
| 655 |
"local".into(), |
| 656 |
"v0.1.0".into(), |
| 657 |
"/tmp".into(), |
| 658 |
features, |
| 659 |
Kind::App, |
| 660 |
1, |
| 661 |
Arc::new(std::collections::HashMap::new()), |
| 662 |
Arc::new(std::collections::HashMap::new()), |
| 663 |
None, |
| 664 |
pool, |
| 665 |
crate::events::channel(), |
| 666 |
cfg, |
| 667 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 668 |
tokio::runtime::Handle::current(), |
| 669 |
Arc::new(AtomicBool::new(false)), |
| 670 |
None, |
| 671 |
)); |
| 672 |
let engine = build_engine(&ctx); |
| 673 |
engine.eval::<String>("feature_flags()").unwrap() |
| 674 |
} |
| 675 |
|
| 676 |
assert_eq!(flags_for(vec![]).await, ""); |
| 677 |
assert_eq!( |
| 678 |
flags_for(vec!["supernote".into()]).await, |
| 679 |
"--features supernote" |
| 680 |
); |
| 681 |
assert_eq!( |
| 682 |
flags_for(vec!["supernote".into(), "extra".into()]).await, |
| 683 |
"--features supernote,extra" |
| 684 |
); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
#[tokio::test] |
| 694 |
async fn repo_resolves_per_build_host() { |
| 695 |
async fn repo_on(build_host: &str) -> String { |
| 696 |
let dir = tempfile::tempdir().unwrap(); |
| 697 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 698 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 699 |
let ctx = Arc::new( |
| 700 |
RecipeCtx::new( |
| 701 |
AppId::new("demo"), |
| 702 |
Version::parse("0.1.0").unwrap(), |
| 703 |
"linux/x86_64".parse().unwrap(), |
| 704 |
build_host.into(), |
| 705 |
"local".into(), |
| 706 |
"v0.1.0".into(), |
| 707 |
"~/Code/Apps/demo".into(), |
| 708 |
vec![], |
| 709 |
Kind::App, |
| 710 |
1, |
| 711 |
Arc::new(std::collections::HashMap::new()), |
| 712 |
Arc::new(std::collections::HashMap::new()), |
| 713 |
None, |
| 714 |
pool, |
| 715 |
crate::events::channel(), |
| 716 |
cfg, |
| 717 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 718 |
tokio::runtime::Handle::current(), |
| 719 |
Arc::new(AtomicBool::new(false)), |
| 720 |
None, |
| 721 |
) |
| 722 |
.with_repo_by_host(HashMap::from([( |
| 723 |
"windows-x86".to_string(), |
| 724 |
"C:/Users/me/Code/Apps/demo".to_string(), |
| 725 |
)])), |
| 726 |
); |
| 727 |
build_engine(&ctx).eval::<String>("repo()").unwrap() |
| 728 |
} |
| 729 |
|
| 730 |
assert_eq!(repo_on("windows-x86").await, "C:/Users/me/Code/Apps/demo"); |
| 731 |
assert_eq!(repo_on("fw13").await, "~/Code/Apps/demo"); |
| 732 |
} |
| 733 |
} |
| 734 |
|