| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use anyhow::{bail, Context, Result}; |
| 9 |
use serde::{Deserialize, Serialize}; |
| 10 |
use std::path::{Path, PathBuf}; |
| 11 |
use std::time::Duration; |
| 12 |
|
| 13 |
|
| 14 |
pub const NEW: &str = "flow.new"; |
| 15 |
|
| 16 |
pub const PREV: &str = "flow.prev"; |
| 17 |
|
| 18 |
const PENDING: &str = "update.json"; |
| 19 |
|
| 20 |
const LOG: &str = "updates.log"; |
| 21 |
|
| 22 |
const PID: &str = "runner.pid"; |
| 23 |
|
| 24 |
|
| 25 |
const REQUEST: &str = "update.request"; |
| 26 |
|
| 27 |
pub const MAX_STARTS: u32 = 3; |
| 28 |
|
| 29 |
pub const PROVE_WITHIN: Duration = Duration::from_secs(10 * 60); |
| 30 |
|
| 31 |
|
| 32 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 33 |
pub struct Pending { |
| 34 |
pub from: String, |
| 35 |
pub to: String, |
| 36 |
|
| 37 |
pub exe: PathBuf, |
| 38 |
pub at_ms: u64, |
| 39 |
|
| 40 |
pub notice_secs: u64, |
| 41 |
|
| 42 |
pub starts: u32, |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
#[derive(Debug, PartialEq, Eq)] |
| 47 |
pub struct Installed { |
| 48 |
pub from: String, |
| 49 |
pub to: String, |
| 50 |
pub exe: PathBuf, |
| 51 |
|
| 52 |
pub told: Option<u32>, |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
pub fn parse_version(line: &str) -> Option<String> { |
| 57 |
let v = line.trim().strip_prefix("flow ")?.split_whitespace().next()?; |
| 58 |
v.split('.').all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())).then(|| v.to_string()) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub fn older(a: &str, b: &str) -> bool { |
| 63 |
let parts = |v: &str| v.split('.').map(|p| p.parse::<u64>().unwrap_or(0)).collect::<Vec<_>>(); |
| 64 |
parts(a) < parts(b) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub fn beside(exe: &Path, stem: &str) -> PathBuf { |
| 70 |
match exe.extension() { |
| 71 |
Some(e) => exe.with_file_name(format!("{stem}.{}", e.to_string_lossy())), |
| 72 |
None => exe.with_file_name(stem), |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
fn pe_machine_here() -> Option<u16> { |
| 78 |
Some(match std::env::consts::ARCH { |
| 79 |
"x86_64" => 0x8664, |
| 80 |
"aarch64" => 0xAA64, |
| 81 |
_ => return None, |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
pub fn check_pe(bytes: &[u8]) -> Result<()> { |
| 87 |
if bytes.len() < 0x40 || &bytes[..2] != b"MZ" { |
| 88 |
bail!("not a Windows executable"); |
| 89 |
} |
| 90 |
let at = u32::from_le_bytes([bytes[0x3c], bytes[0x3d], bytes[0x3e], bytes[0x3f]]) as usize; |
| 91 |
if bytes.len() < at + 6 || &bytes[at..at + 4] != b"PE\0\0" { |
| 92 |
bail!("not a Windows executable (no PE header in its first {} bytes)", bytes.len()); |
| 93 |
} |
| 94 |
let machine = u16::from_le_bytes([bytes[at + 4], bytes[at + 5]]); |
| 95 |
match pe_machine_here() { |
| 96 |
Some(m) if m == machine => Ok(()), |
| 97 |
Some(_) => bail!("a build for another machine (PE machine {machine:#x}); this runner is {}", std::env::consts::ARCH), |
| 98 |
None => bail!("updates are not supported on {}", std::env::consts::ARCH), |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
fn machine_here() -> Option<u16> { |
| 104 |
Some(match std::env::consts::ARCH { |
| 105 |
"x86_64" => 62, |
| 106 |
"aarch64" => 183, |
| 107 |
"riscv64" => 243, |
| 108 |
_ => return None, |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
pub fn check_target(bytes: &[u8]) -> Result<()> { |
| 116 |
if bytes.len() < 20 || &bytes[..4] != b"\x7fELF" { |
| 117 |
bail!("not an ELF executable"); |
| 118 |
} |
| 119 |
if bytes[4] != 2 || bytes[5] != 1 { |
| 120 |
bail!("not a 64-bit little-endian build"); |
| 121 |
} |
| 122 |
let machine = u16::from_le_bytes([bytes[18], bytes[19]]); |
| 123 |
match machine_here() { |
| 124 |
Some(m) if m == machine => Ok(()), |
| 125 |
Some(_) => bail!("a build for another machine (ELF machine {machine}); this runner is {}", std::env::consts::ARCH), |
| 126 |
None => bail!("updates are not supported on {}", std::env::consts::ARCH), |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
fn run(file: &Path, args: &[&str], limit: Duration) -> Result<String> { |
| 132 |
let mut child = std::process::Command::new(file) |
| 133 |
.args(args) |
| 134 |
.stdin(std::process::Stdio::null()) |
| 135 |
.stdout(std::process::Stdio::piped()) |
| 136 |
.stderr(std::process::Stdio::piped()) |
| 137 |
.spawn() |
| 138 |
.with_context(|| format!("starting {}", file.display()))?; |
| 139 |
let start = std::time::Instant::now(); |
| 140 |
loop { |
| 141 |
if child.try_wait()?.is_some() { |
| 142 |
break; |
| 143 |
} |
| 144 |
if start.elapsed() > limit { |
| 145 |
let _ = child.kill(); |
| 146 |
let _ = child.wait(); |
| 147 |
bail!("`{} {}` did not finish within {} s", file.display(), args.join(" "), limit.as_secs()); |
| 148 |
} |
| 149 |
std::thread::sleep(Duration::from_millis(50)); |
| 150 |
} |
| 151 |
let out = child.wait_with_output()?; |
| 152 |
if !out.status.success() { |
| 153 |
bail!("`{} {}` failed ({}): {}", file.display(), args.join(" "), out.status, String::from_utf8_lossy(&out.stderr).trim()); |
| 154 |
} |
| 155 |
Ok(String::from_utf8_lossy(&out.stdout).into_owned()) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
pub fn check(file: &Path, home: &Path) -> Result<String> { |
| 162 |
let mut head = vec![0u8; 4096]; |
| 163 |
let n = std::io::Read::read(&mut std::fs::File::open(file).with_context(|| format!("opening {}", file.display()))?, &mut head)?; |
| 164 |
if cfg!(windows) { |
| 165 |
check_pe(&head[..n])?; |
| 166 |
} else { |
| 167 |
check_target(&head[..n])?; |
| 168 |
} |
| 169 |
let limit = Duration::from_secs(30); |
| 170 |
let line = run(file, &["--version"], limit)?; |
| 171 |
let version = parse_version(&line).with_context(|| format!("{} does not say it is flow: {:?}", file.display(), line.trim()))?; |
| 172 |
let home_s = home.to_string_lossy(); |
| 173 |
let tiers = run(file, &["--home", &home_s, "tiers"], limit)?; |
| 174 |
if !tiers.lines().any(|l| l.ends_with(": available")) { |
| 175 |
bail!("flow {version} finds no tier it can run here:\n{}", tiers.trim()); |
| 176 |
} |
| 177 |
Ok(version) |
| 178 |
} |
| 179 |
|
| 180 |
fn now_ms() -> u64 { |
| 181 |
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) |
| 182 |
} |
| 183 |
|
| 184 |
fn log(home: &Path, event: &str, from: &str, to: &str, why: &str) { |
| 185 |
use std::io::Write; |
| 186 |
let line = serde_json::json!({"at_ms": now_ms(), "event": event, "from": from, "to": to, "why": why}); |
| 187 |
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(home.join(LOG)) { |
| 188 |
let _ = writeln!(f, "{line}"); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
pub fn pending(home: &Path) -> Option<Pending> { |
| 194 |
serde_json::from_slice(&std::fs::read(home.join(PENDING)).ok()?).ok() |
| 195 |
} |
| 196 |
|
| 197 |
fn save(home: &Path, p: &Pending) -> Result<()> { |
| 198 |
let tmp = home.join(format!("{PENDING}.tmp")); |
| 199 |
std::fs::write(&tmp, serde_json::to_vec_pretty(p)?)?; |
| 200 |
std::fs::rename(&tmp, home.join(PENDING))?; |
| 201 |
Ok(()) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
fn is_self(p: &Path) -> bool { |
| 206 |
let canon = |p: &Path| std::fs::canonicalize(p).ok(); |
| 207 |
std::env::current_exe().ok().and_then(|me| canon(&me)).is_some_and(|me| Some(me) == canon(p)) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
fn rename_exe(from: &Path, to: &Path) -> Result<()> { |
| 214 |
if cfg!(windows) && is_self(from) { |
| 215 |
let helper = beside(from, "flow.rename"); |
| 216 |
std::fs::copy(from, &helper).with_context(|| format!("copying {} to rename it", from.display()))?; |
| 217 |
let out = std::process::Command::new(&helper).arg("__rename").arg(from).arg(to).output(); |
| 218 |
let _ = std::fs::remove_file(&helper); |
| 219 |
let out = out.with_context(|| format!("starting {}", helper.display()))?; |
| 220 |
if !out.status.success() { |
| 221 |
bail!("{}", String::from_utf8_lossy(&out.stderr).trim()); |
| 222 |
} |
| 223 |
return Ok(()); |
| 224 |
} |
| 225 |
Ok(crate::portable::rename(from, to)?) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
pub fn swap(exe: &Path, staged: &Path) -> Result<()> { |
| 233 |
let prev = beside(exe, PREV); |
| 234 |
if cfg!(windows) { |
| 235 |
let _ = std::fs::remove_file(&prev); |
| 236 |
rename_exe(exe, &prev).with_context(|| format!("keeping {} as {}", exe.display(), prev.display()))?; |
| 237 |
if let Err(e) = crate::portable::rename(staged, exe) { |
| 238 |
let _ = crate::portable::rename(&prev, exe); |
| 239 |
return Err(e).with_context(|| format!("putting {} in place of {}", staged.display(), exe.display())); |
| 240 |
} |
| 241 |
return Ok(()); |
| 242 |
} |
| 243 |
let _ = std::fs::remove_file(&prev); |
| 244 |
if std::fs::hard_link(exe, &prev).is_err() { |
| 245 |
std::fs::copy(exe, &prev).with_context(|| format!("keeping {} as {}", exe.display(), prev.display()))?; |
| 246 |
} |
| 247 |
std::fs::rename(staged, exe).with_context(|| format!("putting {} in place of {}", staged.display(), exe.display()))?; |
| 248 |
Ok(()) |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
pub fn restore(exe: &Path) -> Result<()> { |
| 253 |
let prev = beside(exe, PREV); |
| 254 |
if !prev.is_file() { |
| 255 |
bail!("{} is not there to go back to", prev.display()); |
| 256 |
} |
| 257 |
if cfg!(windows) { |
| 258 |
|
| 259 |
let failed = beside(exe, "flow.failed"); |
| 260 |
let _ = std::fs::remove_file(&failed); |
| 261 |
rename_exe(exe, &failed).with_context(|| format!("moving {} aside", exe.display()))?; |
| 262 |
return crate::portable::rename(&prev, exe).with_context(|| format!("putting {} back", prev.display())); |
| 263 |
} |
| 264 |
std::fs::rename(&prev, exe).with_context(|| format!("putting {} back", prev.display()))?; |
| 265 |
Ok(()) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
pub fn install(home: &crate::home::Home, exe: &Path, file: &Path, notice: Duration, dry_run: bool, allow_older: bool) -> Result<Installed> { |
| 272 |
if home.config.runner.as_ref().and_then(|r| r.update.as_ref()).is_some_and(|u| !u.allow) { |
| 273 |
bail!("flow.toml says [runner.update] allow = false: this runner takes no updates"); |
| 274 |
} |
| 275 |
if let Some(p) = pending(&home.root) { |
| 276 |
bail!("the update from {} to {} is not proven yet; wait for it (or for its rollback)", p.from, p.to); |
| 277 |
} |
| 278 |
let from = env!("CARGO_PKG_VERSION").to_string(); |
| 279 |
let staged = beside(exe, NEW); |
| 280 |
let _ = std::fs::remove_file(&staged); |
| 281 |
std::fs::copy(file, &staged).with_context(|| format!("staging {} as {}", file.display(), staged.display()))?; |
| 282 |
crate::portable::set_mode(&staged, 0o755)?; |
| 283 |
let checked = check(&staged, &home.root).and_then(|to| { |
| 284 |
if older(&to, &from) && !allow_older { |
| 285 |
bail!("flow {to} is older than this runner's {from} (--allow-older to go back)"); |
| 286 |
} |
| 287 |
Ok(to) |
| 288 |
}); |
| 289 |
let to = match checked { |
| 290 |
Ok(v) => v, |
| 291 |
Err(e) => { |
| 292 |
let _ = std::fs::remove_file(&staged); |
| 293 |
return Err(e); |
| 294 |
} |
| 295 |
}; |
| 296 |
if dry_run { |
| 297 |
let _ = std::fs::remove_file(&staged); |
| 298 |
return Ok(Installed { from, to, exe: exe.to_path_buf(), told: None }); |
| 299 |
} |
| 300 |
if let Err(e) = swap(exe, &staged) { |
| 301 |
let _ = std::fs::remove_file(&staged); |
| 302 |
return Err(e); |
| 303 |
} |
| 304 |
let p = |
| 305 |
Pending { from: from.clone(), to: to.clone(), exe: exe.to_path_buf(), at_ms: now_ms(), notice_secs: notice.as_secs(), starts: 0 }; |
| 306 |
save(&home.root, &p)?; |
| 307 |
log(&home.root, "installed", &from, &to, "flow runner update --from"); |
| 308 |
let told = running(&home.root).filter(|pid| cfg!(not(unix)) || tell(*pid)); |
| 309 |
Ok(Installed { from, to, exe: exe.to_path_buf(), told }) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
#[cfg(unix)] |
| 314 |
fn tell(pid: u32) -> bool { |
| 315 |
unsafe { libc::kill(pid as libc::pid_t, libc::SIGUSR1) == 0 } |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
#[cfg(not(unix))] |
| 320 |
fn tell(_pid: u32) -> bool { |
| 321 |
false |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
pub fn requested(home: &Path) -> bool { |
| 327 |
std::fs::remove_file(home.join(REQUEST)).is_ok() |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
#[cfg(unix)] |
| 335 |
fn running(home: &Path) -> Option<u32> { |
| 336 |
let pid: u32 = std::fs::read_to_string(home.join(PID)).ok()?.trim().parse().ok()?; |
| 337 |
let cmdline = std::fs::read(format!("/proc/{pid}/cmdline")).ok()?; |
| 338 |
is_runner(&cmdline).then_some(pid) |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
#[cfg(not(unix))] |
| 344 |
fn running(home: &Path) -> Option<u32> { |
| 345 |
let pid: u32 = std::fs::read_to_string(home.join(PID)).ok()?.trim().parse().ok()?; |
| 346 |
std::fs::write(home.join(REQUEST), pid.to_string()).ok()?; |
| 347 |
Some(pid) |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
#[cfg_attr(not(unix), allow(dead_code))] |
| 352 |
fn is_runner(cmdline: &[u8]) -> bool { |
| 353 |
let args: Vec<&[u8]> = cmdline.split(|b| *b == 0).filter(|a| !a.is_empty()).collect(); |
| 354 |
let Some((argv0, rest)) = args.split_first() else { return false }; |
| 355 |
let name = argv0.rsplit(|b| *b == b'/').next().unwrap_or(argv0); |
| 356 |
name.starts_with(b"flow") && rest.last() == Some(&&b"runner"[..]) |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
pub fn note_runner(home: &Path) { |
| 361 |
let _ = std::fs::remove_file(home.join(REQUEST)); |
| 362 |
let _ = std::fs::write(home.join(PID), std::process::id().to_string()); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
pub fn forget_runner(home: &Path) { |
| 367 |
if std::fs::read_to_string(home.join(PID)).is_ok_and(|p| p.trim() == std::process::id().to_string()) { |
| 368 |
let _ = std::fs::remove_file(home.join(PID)); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
#[derive(Debug, PartialEq, Eq)] |
| 374 |
pub enum Start { |
| 375 |
|
| 376 |
Settled, |
| 377 |
|
| 378 |
Trial(Pending), |
| 379 |
|
| 380 |
RolledBack(Pending), |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
pub fn on_start(home: &Path) -> Result<Start> { |
| 386 |
let Some(mut p) = pending(home) else { return Ok(Start::Settled) }; |
| 387 |
p.starts += 1; |
| 388 |
if p.starts > MAX_STARTS { |
| 389 |
roll_back(home, &p, &format!("{MAX_STARTS} starts without reaching the hub"))?; |
| 390 |
return Ok(Start::RolledBack(p)); |
| 391 |
} |
| 392 |
save(home, &p)?; |
| 393 |
Ok(Start::Trial(p)) |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
const NOTE: &str = "update.note"; |
| 398 |
|
| 399 |
|
| 400 |
fn note(home: &Path, note: &str) { |
| 401 |
let _ = std::fs::write(home.join(NOTE), note); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
pub fn take_note(home: &Path) -> Option<String> { |
| 406 |
let n = std::fs::read_to_string(home.join(NOTE)).ok()?; |
| 407 |
let _ = std::fs::remove_file(home.join(NOTE)); |
| 408 |
Some(n).filter(|n| !n.trim().is_empty()) |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
pub fn proven(home: &Path) { |
| 413 |
if let Some(p) = pending(home) { |
| 414 |
let _ = std::fs::remove_file(home.join(PENDING)); |
| 415 |
log(home, "proven", &p.from, &p.to, "reached the hub"); |
| 416 |
note(home, &format!("runs flow {} (was {}); proven", p.to, p.from)); |
| 417 |
eprintln!("flow runner: flow {} proven (it reached the hub); flow {} stays as {}", p.to, p.from, beside(&p.exe, PREV).display()); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
pub fn roll_back(home: &Path, p: &Pending, why: &str) -> Result<()> { |
| 423 |
restore(&p.exe)?; |
| 424 |
let _ = std::fs::remove_file(home.join(PENDING)); |
| 425 |
log(home, "rolled back", &p.to, &p.from, why); |
| 426 |
note(home, &format!("rolled back from flow {} to {}: {why}", p.to, p.from)); |
| 427 |
eprintln!("flow runner: rolled back from flow {} to {} ({why})", p.to, p.from); |
| 428 |
Ok(()) |
| 429 |
} |
| 430 |
|
| 431 |
#[cfg(test)] |
| 432 |
mod tests { |
| 433 |
use super::*; |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn versions_are_read_and_compared() { |
| 437 |
assert_eq!(parse_version("flow 0.3.1 (local build)\n").as_deref(), Some("0.3.1")); |
| 438 |
assert_eq!(parse_version("flow 0.10.0"), Some("0.10.0".into())); |
| 439 |
assert_eq!(parse_version("oxy 0.6.0"), None); |
| 440 |
assert_eq!(parse_version("flow banana"), None); |
| 441 |
assert!(older("0.3.1", "0.10.0"), "by number, not by text"); |
| 442 |
assert!(!older("0.3.1", "0.3.1")); |
| 443 |
assert!(older("0.3", "0.3.1")); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn only_a_build_for_this_machine_passes() { |
| 448 |
let me = std::fs::read(std::env::current_exe().unwrap()).unwrap(); |
| 449 |
check_target(&me[..64]).unwrap(); |
| 450 |
assert!(check_target(b"#!/bin/sh\necho flow 9.9.9\n").unwrap_err().to_string().contains("not an ELF")); |
| 451 |
let mut other = me[..64].to_vec(); |
| 452 |
other[18..20].copy_from_slice(&(if std::env::consts::ARCH == "riscv64" { 62u16 } else { 243u16 }).to_le_bytes()); |
| 453 |
assert!(check_target(&other).unwrap_err().to_string().contains("another machine")); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn a_swap_keeps_the_old_build_and_three_failed_starts_bring_it_back() { |
| 458 |
let dir = tempfile::tempdir().unwrap(); |
| 459 |
let (bin, home) = (dir.path().join("bin"), dir.path().join("home")); |
| 460 |
std::fs::create_dir_all(&bin).unwrap(); |
| 461 |
std::fs::create_dir_all(&home).unwrap(); |
| 462 |
let exe = bin.join("flow"); |
| 463 |
std::fs::write(&exe, "old").unwrap(); |
| 464 |
std::fs::write(bin.join(NEW), "new").unwrap(); |
| 465 |
swap(&exe, &bin.join(NEW)).unwrap(); |
| 466 |
assert_eq!(std::fs::read_to_string(&exe).unwrap(), "new"); |
| 467 |
assert_eq!(std::fs::read_to_string(bin.join(PREV)).unwrap(), "old"); |
| 468 |
assert!(!bin.join(NEW).exists()); |
| 469 |
|
| 470 |
assert_eq!(on_start(&home).unwrap(), Start::Settled, "no update, nothing to do"); |
| 471 |
let p = Pending { from: "0.3.1".into(), to: "0.3.2".into(), exe: exe.clone(), at_ms: 1, notice_secs: 600, starts: 0 }; |
| 472 |
save(&home, &p).unwrap(); |
| 473 |
for n in 1..=MAX_STARTS { |
| 474 |
assert_eq!(on_start(&home).unwrap(), Start::Trial(Pending { starts: n, ..p.clone() })); |
| 475 |
} |
| 476 |
assert!(matches!(on_start(&home).unwrap(), Start::RolledBack(_))); |
| 477 |
assert_eq!(std::fs::read_to_string(&exe).unwrap(), "old", "the previous build is back"); |
| 478 |
assert!(pending(&home).is_none()); |
| 479 |
let log = std::fs::read_to_string(home.join(LOG)).unwrap(); |
| 480 |
assert!(log.contains("rolled back") && log.contains("0.3.2"), "{log}"); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn a_windows_build_is_checked_as_pe_and_named_beside_with_its_extension() { |
| 485 |
let mut pe = vec![0u8; 0x100]; |
| 486 |
pe[..2].copy_from_slice(b"MZ"); |
| 487 |
pe[0x3c..0x40].copy_from_slice(&0x80u32.to_le_bytes()); |
| 488 |
pe[0x80..0x84].copy_from_slice(b"PE\0\0"); |
| 489 |
pe[0x84..0x86].copy_from_slice(&0x8664u16.to_le_bytes()); |
| 490 |
if std::env::consts::ARCH == "x86_64" { |
| 491 |
check_pe(&pe).unwrap(); |
| 492 |
} |
| 493 |
pe[0x84..0x86].copy_from_slice(&0x01c4u16.to_le_bytes()); |
| 494 |
assert!(check_pe(&pe).is_err(), "another machine"); |
| 495 |
assert!(check_pe(b"\x7fELF and more bytes than enough for a header, really, quite a few more") |
| 496 |
.unwrap_err() |
| 497 |
.to_string() |
| 498 |
.contains("not a Windows")); |
| 499 |
assert_eq!(beside(Path::new("C:/flow/flow.exe"), PREV), Path::new("C:/flow/flow.prev.exe")); |
| 500 |
assert_eq!(beside(Path::new("/usr/local/bin/flow"), NEW), Path::new("/usr/local/bin/flow.new")); |
| 501 |
} |
| 502 |
|
| 503 |
#[test] |
| 504 |
fn a_request_file_asks_once() { |
| 505 |
let dir = tempfile::tempdir().unwrap(); |
| 506 |
assert!(!requested(dir.path())); |
| 507 |
std::fs::write(dir.path().join(REQUEST), "1").unwrap(); |
| 508 |
assert!(requested(dir.path()) && !requested(dir.path()), "read once"); |
| 509 |
std::fs::write(dir.path().join(REQUEST), "1").unwrap(); |
| 510 |
note_runner(dir.path()); |
| 511 |
assert!(!requested(dir.path()), "a starting runner clears a stale request"); |
| 512 |
} |
| 513 |
|
| 514 |
#[test] |
| 515 |
fn only_a_running_runner_is_told() { |
| 516 |
assert!(is_runner(b"/usr/local/bin/flow\0runner\0")); |
| 517 |
assert!(is_runner(b"flow\0--home\0/srv/flow\0runner\0")); |
| 518 |
assert!(!is_runner(b"flow\0runner\0update\0--from\0x\0"), "not the update command itself"); |
| 519 |
assert!(!is_runner(b"/usr/bin/vim\0runner\0")); |
| 520 |
assert!(!is_runner(b"")); |
| 521 |
let dir = tempfile::tempdir().unwrap(); |
| 522 |
std::fs::write(dir.path().join(PID), std::process::id().to_string()).unwrap(); |
| 523 |
assert_eq!(running(dir.path()), None, "this test is not a flow runner"); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn a_proven_build_stays() { |
| 528 |
let dir = tempfile::tempdir().unwrap(); |
| 529 |
let exe = dir.path().join("flow"); |
| 530 |
std::fs::write(&exe, "new").unwrap(); |
| 531 |
let p = Pending { from: "0.3.1".into(), to: "0.3.2".into(), exe: exe.clone(), at_ms: 1, notice_secs: 0, starts: 1 }; |
| 532 |
save(dir.path(), &p).unwrap(); |
| 533 |
proven(dir.path()); |
| 534 |
assert!(pending(dir.path()).is_none()); |
| 535 |
assert_eq!(on_start(dir.path()).unwrap(), Start::Settled); |
| 536 |
assert!(std::fs::read_to_string(dir.path().join(LOG)).unwrap().contains("proven")); |
| 537 |
} |
| 538 |
} |