| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use crate::config::Config; |
| 14 |
use crate::topology::Topology; |
| 15 |
use anyhow::{Context, Result, bail}; |
| 16 |
use chrono::Utc; |
| 17 |
use ops_exec::{CapabilitySet, Executor, SshExec, SyncOpts}; |
| 18 |
use sqlx::SqlitePool; |
| 19 |
use std::path::Path; |
| 20 |
use std::sync::Arc; |
| 21 |
use tokio::process::Command; |
| 22 |
|
| 23 |
#[derive(Debug, Clone)] |
| 24 |
pub struct FetchedBackup { |
| 25 |
pub source: String, |
| 26 |
pub local_path: String, |
| 27 |
pub byte_size: Option<i64>, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 33 |
pub(crate) enum BackupSource { |
| 34 |
|
| 35 |
File { path: String }, |
| 36 |
|
| 37 |
RsyncDaemon { url: String }, |
| 38 |
|
| 39 |
Ssh { |
| 40 |
user_host: String, |
| 41 |
port: Option<u16>, |
| 42 |
path: String, |
| 43 |
}, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
pub(crate) fn parse_source(s: &str) -> Result<BackupSource> { |
| 49 |
if let Some(rest) = s.strip_prefix("file://") { |
| 50 |
if rest.is_empty() { |
| 51 |
bail!("file:// URL is missing a path: {s}"); |
| 52 |
} |
| 53 |
return Ok(BackupSource::File { path: rest.into() }); |
| 54 |
} |
| 55 |
if s.starts_with("rsync://") { |
| 56 |
return Ok(BackupSource::RsyncDaemon { url: s.into() }); |
| 57 |
} |
| 58 |
if let Some(rest) = s.strip_prefix("ssh://") { |
| 59 |
let (user_host_port, path_rest) = rest |
| 60 |
.split_once('/') |
| 61 |
.with_context(|| format!("ssh:// URL missing path: {s}"))?; |
| 62 |
if user_host_port.is_empty() { |
| 63 |
bail!("ssh:// URL missing user@host: {s}"); |
| 64 |
} |
| 65 |
let path = format!("/{path_rest}"); |
| 66 |
let (user_host, port) = match user_host_port.rsplit_once(':') { |
| 67 |
Some((uh, p)) => { |
| 68 |
|
| 69 |
|
| 70 |
match p.parse::<u16>() { |
| 71 |
Ok(n) => (uh.to_string(), Some(n)), |
| 72 |
Err(_) => (user_host_port.to_string(), None), |
| 73 |
} |
| 74 |
} |
| 75 |
None => (user_host_port.to_string(), None), |
| 76 |
}; |
| 77 |
if user_host.is_empty() { |
| 78 |
bail!("ssh:// URL has empty host (port {port:?})"); |
| 79 |
} |
| 80 |
return Ok(BackupSource::Ssh { |
| 81 |
user_host, |
| 82 |
port, |
| 83 |
path, |
| 84 |
}); |
| 85 |
} |
| 86 |
bail!("unsupported backup source scheme: {s}"); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
const MIN_BACKUP_BYTES: u64 = 64; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
const MIN_BACKUP_FRACTION_DENOM: i64 = 2; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
async fn verify_backup(tmp_path: &str, is_gz: bool, min_bytes: u64) -> Result<()> { |
| 117 |
let meta = tokio::fs::metadata(tmp_path) |
| 118 |
.await |
| 119 |
.with_context(|| format!("stat fetched backup {tmp_path}"))?; |
| 120 |
anyhow::ensure!( |
| 121 |
meta.len() >= min_bytes, |
| 122 |
"fetched backup {tmp_path} is implausibly small ({} bytes, floor {min_bytes}); \ |
| 123 |
treating as a failed/truncated transfer", |
| 124 |
meta.len(), |
| 125 |
); |
| 126 |
if is_gz { |
| 127 |
let out = Command::new("gzip") |
| 128 |
.arg("-t") |
| 129 |
.arg(tmp_path) |
| 130 |
.output() |
| 131 |
.await |
| 132 |
.with_context(|| format!("spawning gzip -t {tmp_path}"))?; |
| 133 |
anyhow::ensure!( |
| 134 |
out.status.success(), |
| 135 |
"fetched backup {tmp_path} failed gzip integrity check (truncated/corrupt): {}", |
| 136 |
String::from_utf8_lossy(&out.stderr), |
| 137 |
); |
| 138 |
} |
| 139 |
Ok(()) |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
pub async fn fetch( |
| 148 |
pool: &SqlitePool, |
| 149 |
_cfg: &Arc<Config>, |
| 150 |
topo: &Arc<Topology>, |
| 151 |
force: bool, |
| 152 |
) -> Result<FetchedBackup> { |
| 153 |
let source = topo.backup.source.clone(); |
| 154 |
let local_path = topo.backup.local_path.clone(); |
| 155 |
|
| 156 |
if let Some(parent) = Path::new(&local_path).parent() { |
| 157 |
tokio::fs::create_dir_all(parent).await?; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
let tmp_path = format!("{local_path}.partial"); |
| 166 |
let is_gz = std::path::Path::new(&local_path) |
| 167 |
.extension() |
| 168 |
.is_some_and(|ext| ext.eq_ignore_ascii_case("gz")); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
let last_size: Option<i64> = |
| 174 |
sqlx::query_scalar("SELECT byte_size FROM backups ORDER BY fetched_at DESC LIMIT 1") |
| 175 |
.fetch_optional(pool) |
| 176 |
.await?; |
| 177 |
let min_bytes = if force { |
| 178 |
tracing::warn!( |
| 179 |
last_verified_bytes = last_size, |
| 180 |
"force: re-baselining the backup plausibility floor to the absolute minimum; \ |
| 181 |
this fetch's size becomes the new reference" |
| 182 |
); |
| 183 |
MIN_BACKUP_BYTES |
| 184 |
} else { |
| 185 |
last_size.map_or(MIN_BACKUP_BYTES, |s| { |
| 186 |
((s / MIN_BACKUP_FRACTION_DENOM) as u64).max(MIN_BACKUP_BYTES) |
| 187 |
}) |
| 188 |
}; |
| 189 |
|
| 190 |
let parsed = parse_source(&source)?; |
| 191 |
let downloaded: Result<()> = async { |
| 192 |
match parsed { |
| 193 |
BackupSource::File { path } => { |
| 194 |
tokio::fs::copy(&path, &tmp_path) |
| 195 |
.await |
| 196 |
.with_context(|| format!("copy {path} -> {tmp_path}"))?; |
| 197 |
} |
| 198 |
BackupSource::RsyncDaemon { url } => { |
| 199 |
let out = Command::new("rsync") |
| 200 |
.args(["-az", &url, &tmp_path]) |
| 201 |
.output() |
| 202 |
.await |
| 203 |
.context("spawning rsync")?; |
| 204 |
anyhow::ensure!( |
| 205 |
out.status.success(), |
| 206 |
"rsync (daemon) failed: {}", |
| 207 |
String::from_utf8_lossy(&out.stderr), |
| 208 |
); |
| 209 |
} |
| 210 |
BackupSource::Ssh { |
| 211 |
user_host, |
| 212 |
path, |
| 213 |
port, |
| 214 |
} => { |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
let caps = CapabilitySet::from_tokens(Vec::<&str>::new(), ["artifact"]); |
| 224 |
let exec = SshExec::new(user_host, caps).with_port(port); |
| 225 |
let exec = match Path::new(&path).parent() { |
| 226 |
Some(dir) if !dir.as_os_str().is_empty() => exec.with_pull_root(dir), |
| 227 |
|
| 228 |
|
| 229 |
_ => exec.with_pull_root(&path), |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
let opts = SyncOpts { |
| 236 |
compress: false, |
| 237 |
partial: false, |
| 238 |
..SyncOpts::default() |
| 239 |
}; |
| 240 |
exec.pull_file(Path::new(&path), Path::new(&tmp_path), &opts) |
| 241 |
.await |
| 242 |
.context("rsync (ssh) failed")?; |
| 243 |
} |
| 244 |
} |
| 245 |
verify_backup(&tmp_path, is_gz, min_bytes).await |
| 246 |
} |
| 247 |
.await; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if let Err(e) = downloaded { |
| 252 |
let _ = tokio::fs::remove_file(&tmp_path).await; |
| 253 |
return Err(e); |
| 254 |
} |
| 255 |
|
| 256 |
tokio::fs::rename(&tmp_path, &local_path) |
| 257 |
.await |
| 258 |
.with_context(|| format!("atomic rename {tmp_path} -> {local_path}"))?; |
| 259 |
|
| 260 |
let meta = tokio::fs::metadata(&local_path).await?; |
| 261 |
let size = meta.len() as i64; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
let mut tx = pool.begin().await?; |
| 269 |
sqlx::query( |
| 270 |
"INSERT INTO backups (fetched_at, source, local_path, byte_size) VALUES (?, ?, ?, ?)", |
| 271 |
) |
| 272 |
.bind(Utc::now().to_rfc3339()) |
| 273 |
.bind(&source) |
| 274 |
.bind(&local_path) |
| 275 |
.bind(size) |
| 276 |
.execute(&mut *tx) |
| 277 |
.await?; |
| 278 |
sqlx::query("DELETE FROM backups WHERE fetched_at < datetime('now', '-30 days')") |
| 279 |
.execute(&mut *tx) |
| 280 |
.await?; |
| 281 |
tx.commit().await?; |
| 282 |
|
| 283 |
Ok(FetchedBackup { |
| 284 |
source, |
| 285 |
local_path, |
| 286 |
byte_size: Some(size), |
| 287 |
}) |
| 288 |
} |
| 289 |
|
| 290 |
#[cfg(test)] |
| 291 |
mod tests { |
| 292 |
use super::*; |
| 293 |
use crate::topology::{BackupConfig, RepoConfig}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
async fn mem_pool() -> SqlitePool { |
| 298 |
let pool = sqlx::sqlite::SqlitePoolOptions::new() |
| 299 |
.max_connections(1) |
| 300 |
.connect("sqlite::memory:") |
| 301 |
.await |
| 302 |
.unwrap(); |
| 303 |
crate::db::migrate(&pool).await.unwrap(); |
| 304 |
pool |
| 305 |
} |
| 306 |
|
| 307 |
fn topo_with_backup(source: String, local_path: String) -> Topology { |
| 308 |
Topology { |
| 309 |
repo: RepoConfig { |
| 310 |
bare_path: "/tmp/x.git".into(), |
| 311 |
branch: "main".into(), |
| 312 |
upstream: None, |
| 313 |
}, |
| 314 |
backup: BackupConfig { source, local_path }, |
| 315 |
tiers: vec![], |
| 316 |
aux_repos: Vec::new(), |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
fn incompressible(n: usize) -> Vec<u8> { |
| 323 |
(0..n) |
| 324 |
.map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8) |
| 325 |
.collect() |
| 326 |
} |
| 327 |
|
| 328 |
async fn write_valid_gz(path: &Path) { |
| 329 |
let plain = path.with_extension("plain"); |
| 330 |
tokio::fs::write(&plain, incompressible(4096)) |
| 331 |
.await |
| 332 |
.unwrap(); |
| 333 |
let out = Command::new("sh") |
| 334 |
.arg("-c") |
| 335 |
.arg(format!("gzip -c {} > {}", plain.display(), path.display())) |
| 336 |
.output() |
| 337 |
.await |
| 338 |
.unwrap(); |
| 339 |
assert!(out.status.success(), "gzip shim failed"); |
| 340 |
} |
| 341 |
|
| 342 |
#[tokio::test] |
| 343 |
async fn fetch_file_source_writes_atomically_and_records_row() { |
| 344 |
let tmp = tempfile::tempdir().unwrap(); |
| 345 |
let src = tmp.path().join("src.sql.gz"); |
| 346 |
write_valid_gz(&src).await; |
| 347 |
let dest = tmp.path().join("backups/latest.sql.gz"); |
| 348 |
let topo = Arc::new(topo_with_backup( |
| 349 |
format!("file://{}", src.display()), |
| 350 |
dest.to_string_lossy().into_owned(), |
| 351 |
)); |
| 352 |
let pool = mem_pool().await; |
| 353 |
let cfg = Arc::new(Config::for_tests()); |
| 354 |
|
| 355 |
let fb = fetch(&pool, &cfg, &topo, false).await.unwrap(); |
| 356 |
assert!(dest.exists(), "live backup written"); |
| 357 |
assert!( |
| 358 |
!dest.with_file_name("latest.sql.gz.partial").exists(), |
| 359 |
"temp file consumed by the atomic rename", |
| 360 |
); |
| 361 |
assert!(fb.byte_size.unwrap() > 0); |
| 362 |
let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM backups") |
| 363 |
.fetch_one(&pool) |
| 364 |
.await |
| 365 |
.unwrap(); |
| 366 |
assert_eq!(count.0, 1, "a row is recorded for a successful fetch"); |
| 367 |
} |
| 368 |
|
| 369 |
#[tokio::test] |
| 370 |
async fn fetch_rejects_a_dump_far_below_the_last_backup_size() { |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
let tmp = tempfile::tempdir().unwrap(); |
| 375 |
let src = tmp.path().join("src.sql.gz"); |
| 376 |
write_valid_gz(&src).await; |
| 377 |
let dest = tmp.path().join("backups/latest.sql.gz"); |
| 378 |
let topo = Arc::new(topo_with_backup( |
| 379 |
format!("file://{}", src.display()), |
| 380 |
dest.to_string_lossy().into_owned(), |
| 381 |
)); |
| 382 |
let pool = mem_pool().await; |
| 383 |
let cfg = Arc::new(Config::for_tests()); |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
sqlx::query("INSERT INTO backups (fetched_at, source, local_path, byte_size) VALUES (?, 'x', ?, 1000000)") |
| 388 |
.bind(Utc::now().to_rfc3339()) |
| 389 |
.bind(dest.to_string_lossy().into_owned()) |
| 390 |
.execute(&pool).await.unwrap(); |
| 391 |
|
| 392 |
let err = fetch(&pool, &cfg, &topo, false) |
| 393 |
.await |
| 394 |
.unwrap_err() |
| 395 |
.to_string(); |
| 396 |
assert!(err.contains("implausibly small"), "{err}"); |
| 397 |
assert!( |
| 398 |
!dest.exists(), |
| 399 |
"a rejected dump never becomes the live backup" |
| 400 |
); |
| 401 |
let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM backups") |
| 402 |
.fetch_one(&pool) |
| 403 |
.await |
| 404 |
.unwrap(); |
| 405 |
assert_eq!(count.0, 1, "the rejected fetch records no new row"); |
| 406 |
} |
| 407 |
|
| 408 |
#[tokio::test] |
| 409 |
async fn force_rebaselines_the_floor_after_a_legitimate_shrink() { |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
let tmp = tempfile::tempdir().unwrap(); |
| 415 |
let src = tmp.path().join("src.sql.gz"); |
| 416 |
write_valid_gz(&src).await; |
| 417 |
let dest = tmp.path().join("backups/latest.sql.gz"); |
| 418 |
let topo = Arc::new(topo_with_backup( |
| 419 |
format!("file://{}", src.display()), |
| 420 |
dest.to_string_lossy().into_owned(), |
| 421 |
)); |
| 422 |
let pool = mem_pool().await; |
| 423 |
let cfg = Arc::new(Config::for_tests()); |
| 424 |
sqlx::query("INSERT INTO backups (fetched_at, source, local_path, byte_size) VALUES (?, 'x', ?, 1000000)") |
| 425 |
.bind(Utc::now().to_rfc3339()) |
| 426 |
.bind(dest.to_string_lossy().into_owned()) |
| 427 |
.execute(&pool).await.unwrap(); |
| 428 |
|
| 429 |
|
| 430 |
let fb = fetch(&pool, &cfg, &topo, true).await.unwrap(); |
| 431 |
assert!(dest.exists(), "the forced dump becomes the live backup"); |
| 432 |
let recorded = fb.byte_size.unwrap(); |
| 433 |
assert!( |
| 434 |
recorded < 1_000_000, |
| 435 |
"the accepted dump really is the small one" |
| 436 |
); |
| 437 |
|
| 438 |
|
| 439 |
fetch(&pool, &cfg, &topo, false) |
| 440 |
.await |
| 441 |
.expect("floor re-baselined to the forced fetch's size"); |
| 442 |
} |
| 443 |
|
| 444 |
#[tokio::test] |
| 445 |
async fn force_still_rejects_a_corrupt_gzip() { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
let tmp = tempfile::tempdir().unwrap(); |
| 450 |
let src = tmp.path().join("src.sql.gz"); |
| 451 |
write_valid_gz(&src).await; |
| 452 |
let whole = tokio::fs::read(&src).await.unwrap(); |
| 453 |
tokio::fs::write(&src, &whole[..whole.len() / 2]) |
| 454 |
.await |
| 455 |
.unwrap(); |
| 456 |
let dest = tmp.path().join("backups/latest.sql.gz"); |
| 457 |
let topo = Arc::new(topo_with_backup( |
| 458 |
format!("file://{}", src.display()), |
| 459 |
dest.to_string_lossy().into_owned(), |
| 460 |
)); |
| 461 |
let pool = mem_pool().await; |
| 462 |
let cfg = Arc::new(Config::for_tests()); |
| 463 |
|
| 464 |
let err = fetch(&pool, &cfg, &topo, true) |
| 465 |
.await |
| 466 |
.unwrap_err() |
| 467 |
.to_string(); |
| 468 |
assert!(err.contains("gzip integrity check"), "{err}"); |
| 469 |
assert!( |
| 470 |
!dest.exists(), |
| 471 |
"a corrupt dump never becomes the live backup" |
| 472 |
); |
| 473 |
} |
| 474 |
|
| 475 |
#[tokio::test] |
| 476 |
async fn fetch_rejects_truncated_gz_and_leaves_no_live_file() { |
| 477 |
let tmp = tempfile::tempdir().unwrap(); |
| 478 |
let src = tmp.path().join("src.sql.gz"); |
| 479 |
write_valid_gz(&src).await; |
| 480 |
|
| 481 |
let full = tokio::fs::read(&src).await.unwrap(); |
| 482 |
assert!( |
| 483 |
full.len() / 2 > MIN_BACKUP_BYTES as usize, |
| 484 |
"half must clear the size floor to exercise gzip -t" |
| 485 |
); |
| 486 |
tokio::fs::write(&src, &full[..full.len() / 2]) |
| 487 |
.await |
| 488 |
.unwrap(); |
| 489 |
|
| 490 |
let dest = tmp.path().join("backups/latest.sql.gz"); |
| 491 |
let topo = Arc::new(topo_with_backup( |
| 492 |
format!("file://{}", src.display()), |
| 493 |
dest.to_string_lossy().into_owned(), |
| 494 |
)); |
| 495 |
let pool = mem_pool().await; |
| 496 |
let cfg = Arc::new(Config::for_tests()); |
| 497 |
|
| 498 |
let res = fetch(&pool, &cfg, &topo, false).await; |
| 499 |
assert!(res.is_err(), "a truncated gzip must fail the fetch"); |
| 500 |
assert!( |
| 501 |
!dest.exists(), |
| 502 |
"no live backup file results from a failed fetch" |
| 503 |
); |
| 504 |
assert!( |
| 505 |
!dest.with_file_name("latest.sql.gz.partial").exists(), |
| 506 |
"the corrupt temp file is cleaned up", |
| 507 |
); |
| 508 |
let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM backups") |
| 509 |
.fetch_one(&pool) |
| 510 |
.await |
| 511 |
.unwrap(); |
| 512 |
assert_eq!(count.0, 0, "no row recorded for a failed fetch"); |
| 513 |
} |
| 514 |
|
| 515 |
#[test] |
| 516 |
fn parses_file_url() { |
| 517 |
let s = parse_source("file:///opt/backups/latest.sql.gz").unwrap(); |
| 518 |
assert_eq!( |
| 519 |
s, |
| 520 |
BackupSource::File { |
| 521 |
path: "/opt/backups/latest.sql.gz".into() |
| 522 |
} |
| 523 |
); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn file_url_without_path_errors() { |
| 528 |
assert!(parse_source("file://").is_err()); |
| 529 |
} |
| 530 |
|
| 531 |
#[test] |
| 532 |
fn parses_rsync_daemon_url() { |
| 533 |
let s = parse_source("rsync://astra/mnw/latest.sql.gz").unwrap(); |
| 534 |
assert_eq!( |
| 535 |
s, |
| 536 |
BackupSource::RsyncDaemon { |
| 537 |
url: "rsync://astra/mnw/latest.sql.gz".into() |
| 538 |
} |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
#[test] |
| 543 |
fn parses_ssh_url_with_port() { |
| 544 |
let s = parse_source("ssh://backup-puller@alpha-west-1:2200/latest.sql.gz").unwrap(); |
| 545 |
assert_eq!( |
| 546 |
s, |
| 547 |
BackupSource::Ssh { |
| 548 |
user_host: "backup-puller@alpha-west-1".into(), |
| 549 |
port: Some(2200), |
| 550 |
path: "/latest.sql.gz".into(), |
| 551 |
} |
| 552 |
); |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn parses_ssh_url_without_port() { |
| 557 |
let s = parse_source("ssh://max@astra/opt/backups/mnw/latest.sql.gz").unwrap(); |
| 558 |
assert_eq!( |
| 559 |
s, |
| 560 |
BackupSource::Ssh { |
| 561 |
user_host: "max@astra".into(), |
| 562 |
port: None, |
| 563 |
path: "/opt/backups/mnw/latest.sql.gz".into(), |
| 564 |
} |
| 565 |
); |
| 566 |
} |
| 567 |
|
| 568 |
#[test] |
| 569 |
fn ssh_url_without_path_errors() { |
| 570 |
|
| 571 |
assert!(parse_source("ssh://backup-puller@alpha-west-1").is_err()); |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn ssh_url_without_user_host_errors() { |
| 576 |
|
| 577 |
assert!(parse_source("ssh:///latest.sql.gz").is_err()); |
| 578 |
} |
| 579 |
|
| 580 |
#[test] |
| 581 |
fn ssh_url_with_non_numeric_after_colon_treats_as_part_of_host() { |
| 582 |
|
| 583 |
|
| 584 |
let s = parse_source("ssh://user@host:notaport/path").unwrap(); |
| 585 |
assert_eq!( |
| 586 |
s, |
| 587 |
BackupSource::Ssh { |
| 588 |
user_host: "user@host:notaport".into(), |
| 589 |
port: None, |
| 590 |
path: "/path".into(), |
| 591 |
} |
| 592 |
); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn rejects_unknown_scheme() { |
| 597 |
assert!(parse_source("ftp://example.com/file").is_err()); |
| 598 |
assert!(parse_source("just-a-path.sql.gz").is_err()); |
| 599 |
assert!(parse_source("").is_err()); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn ssh_url_preserves_multi_segment_path() { |
| 604 |
let s = parse_source("ssh://a@b:22/opt/foo/bar/baz.sql.gz").unwrap(); |
| 605 |
match s { |
| 606 |
BackupSource::Ssh { path, .. } => assert_eq!(path, "/opt/foo/bar/baz.sql.gz"), |
| 607 |
_ => panic!("wrong variant"), |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|