| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | use std::{ |
| 26 | path::{Path, PathBuf}, |
| 27 | time::{SystemTime, UNIX_EPOCH}, |
| 28 | }; |
| 29 | |
| 30 | use serde::{Deserialize, Serialize}; |
| 31 | |
| 32 | use crate::domain::{ |
| 33 | Actor, DomainError, |
| 34 | repository::{OrgRepository, RepoRepository, UserRepository}, |
| 35 | }; |
| 36 | |
| 37 | use super::{ |
| 38 | access::instance_owner, |
| 39 | error::Result, |
| 40 | port::{ |
| 41 | BackupArchive, BackupError, BackupPlan, DatabaseSnapshot, ExistingData, GitStorage, |
| 42 | RestorePlan, |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | pub const VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 55 | pub struct BackupManifest { |
| 56 | |
| 57 | pub version: String, |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | pub taken_at: String, |
| 63 | pub counts: BackupCounts, |
| 64 | } |
| 65 | |
| 66 | |
| 67 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 68 | pub struct BackupCounts { |
| 69 | pub users: u64, |
| 70 | pub organisations: u64, |
| 71 | pub repositories: u64, |
| 72 | } |
| 73 | |
| 74 | |
| 75 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 76 | pub struct BackupWritten { |
| 77 | pub path: PathBuf, |
| 78 | pub bytes: u64, |
| 79 | pub manifest: BackupManifest, |
| 80 | } |
| 81 | |
| 82 | |
| 83 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 84 | pub struct Restored { |
| 85 | pub manifest: BackupManifest, |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | pub replaced: Option<PathBuf>, |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 102 | pub enum RestoreMode { |
| 103 | |
| 104 | |
| 105 | |
| 106 | #[default] |
| 107 | OnlyIfEmpty, |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | Replace, |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub const REPLACED_PREFIX: &str = ".replaced-"; |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | #[must_use] |
| 134 | pub fn archive_name(now: SystemTime) -> String { |
| 135 | format!("steid-{VERSION}-{}.tar.gz", stamp(now)) |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | fn stamp(now: SystemTime) -> String { |
| 144 | let (date, time) = utc_parts(now); |
| 145 | |
| 146 | format!( |
| 147 | "{:04}{:02}{:02}T{:02}{:02}{:02}Z", |
| 148 | date.0, date.1, date.2, time.0, time.1, time.2 |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[allow(clippy::too_many_arguments)] |
| 164 | pub async fn back_up( |
| 165 | now: SystemTime, |
| 166 | destination: &Path, |
| 167 | data_dir: &Path, |
| 168 | workspace: &Path, |
| 169 | users: &impl UserRepository, |
| 170 | orgs: &impl OrgRepository, |
| 171 | repos: &impl RepoRepository, |
| 172 | snapshots: &impl DatabaseSnapshot, |
| 173 | archives: &impl BackupArchive, |
| 174 | ) -> Result<BackupWritten> { |
| 175 | let manifest = BackupManifest { |
| 176 | version: VERSION.to_owned(), |
| 177 | taken_at: rfc3339(now), |
| 178 | counts: BackupCounts { |
| 179 | users: users.count().await?, |
| 180 | organisations: orgs.count().await?, |
| 181 | repositories: repos.count().await?, |
| 182 | }, |
| 183 | }; |
| 184 | |
| 185 | let database = snapshots.snapshot(workspace).await?; |
| 186 | |
| 187 | let bytes = archives |
| 188 | .pack(BackupPlan { |
| 189 | destination: destination.to_path_buf(), |
| 190 | database, |
| 191 | data_dir: data_dir.to_path_buf(), |
| 192 | manifest: manifest.clone(), |
| 193 | }) |
| 194 | .await?; |
| 195 | |
| 196 | Ok(BackupWritten { |
| 197 | path: destination.to_path_buf(), |
| 198 | bytes, |
| 199 | manifest, |
| 200 | }) |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | #[allow(clippy::too_many_arguments)] |
| 210 | pub async fn back_up_for( |
| 211 | actor: &Actor, |
| 212 | now: SystemTime, |
| 213 | destination: &Path, |
| 214 | data_dir: &Path, |
| 215 | workspace: &Path, |
| 216 | users: &impl UserRepository, |
| 217 | orgs: &impl OrgRepository, |
| 218 | repos: &impl RepoRepository, |
| 219 | snapshots: &impl DatabaseSnapshot, |
| 220 | archives: &impl BackupArchive, |
| 221 | ) -> Result<BackupWritten> { |
| 222 | if !instance_owner(actor, users).await? { |
| 223 | return Err(DomainError::Forbidden.into()); |
| 224 | } |
| 225 | |
| 226 | back_up( |
| 227 | now, |
| 228 | destination, |
| 229 | data_dir, |
| 230 | workspace, |
| 231 | users, |
| 232 | orgs, |
| 233 | repos, |
| 234 | snapshots, |
| 235 | archives, |
| 236 | ) |
| 237 | .await |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | #[allow(clippy::too_many_arguments)] |
| 287 | pub async fn restore( |
| 288 | now: SystemTime, |
| 289 | archive: &Path, |
| 290 | data_dir: &Path, |
| 291 | workspace: &Path, |
| 292 | mode: RestoreMode, |
| 293 | users: &impl UserRepository, |
| 294 | storage: &impl GitStorage, |
| 295 | snapshots: &impl DatabaseSnapshot, |
| 296 | archives: &impl BackupArchive, |
| 297 | ) -> Result<Restored> { |
| 298 | if mode == RestoreMode::OnlyIfEmpty { |
| 299 | if users.any_exist().await? { |
| 300 | return Err(DomainError::Unavailable { |
| 301 | entity: "instance", |
| 302 | reason: "it already has a user; restore only into an unclaimed instance, or \ |
| 303 | pass --replace to replace everything it holds" |
| 304 | .to_owned(), |
| 305 | } |
| 306 | .into()); |
| 307 | } |
| 308 | |
| 309 | let existing = storage.list_repo_dirs().await?; |
| 310 | if !existing.is_empty() { |
| 311 | return Err(DomainError::Unavailable { |
| 312 | entity: "instance", |
| 313 | reason: format!( |
| 314 | "the data directory already holds {} repository/repositories; pass \ |
| 315 | --replace to replace everything it holds", |
| 316 | existing.len() |
| 317 | ), |
| 318 | } |
| 319 | .into()); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | let manifest = archives.manifest(archive).await?; |
| 324 | |
| 325 | if is_newer(&manifest.version, VERSION)? { |
| 326 | return Err(BackupError::refused(format!( |
| 327 | "the archive was written by Steid {} and this is {VERSION}; upgrade first", |
| 328 | manifest.version |
| 329 | )) |
| 330 | .into()); |
| 331 | } |
| 332 | |
| 333 | let staged = workspace.join("steid.db"); |
| 334 | |
| 335 | let replaced = match mode { |
| 336 | RestoreMode::OnlyIfEmpty => None, |
| 337 | RestoreMode::Replace => Some(data_dir.join(format!("{REPLACED_PREFIX}{}", stamp(now)))), |
| 338 | }; |
| 339 | |
| 340 | archives |
| 341 | .unpack(RestorePlan { |
| 342 | archive: archive.to_path_buf(), |
| 343 | database: staged.clone(), |
| 344 | data_dir: data_dir.to_path_buf(), |
| 345 | existing: match &replaced { |
| 346 | None => ExistingData::Refuse, |
| 347 | Some(aside) => ExistingData::Replace { |
| 348 | staging: workspace.join(INCOMING), |
| 349 | aside: aside.clone(), |
| 350 | }, |
| 351 | }, |
| 352 | }) |
| 353 | .await?; |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | if let Some(aside) = &replaced { |
| 359 | snapshots.snapshot(aside).await?; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | snapshots.replace(&staged).await?; |
| 366 | |
| 367 | Ok(Restored { manifest, replaced }) |
| 368 | } |
| 369 | |
| 370 | |
| 371 | const INCOMING: &str = "incoming"; |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | fn is_newer(candidate: &str, current: &str) -> Result<bool> { |
| 381 | Ok(release(candidate)? > release(current)?) |
| 382 | } |
| 383 | |
| 384 | fn release(value: &str) -> Result<(u64, u64, u64)> { |
| 385 | let core = value |
| 386 | .split(['-', '+']) |
| 387 | .next() |
| 388 | .unwrap_or_default() |
| 389 | .trim() |
| 390 | .to_owned(); |
| 391 | |
| 392 | let mut parts = core.split('.'); |
| 393 | let mut number = || { |
| 394 | parts |
| 395 | .next() |
| 396 | .and_then(|part| part.parse::<u64>().ok()) |
| 397 | .ok_or_else(|| { |
| 398 | DomainError::validation("version", format!("{value:?} is not a Steid version")) |
| 399 | }) |
| 400 | }; |
| 401 | |
| 402 | Ok((number()?, number()?, number()?)) |
| 403 | } |
| 404 | |
| 405 | |
| 406 | fn rfc3339(time: SystemTime) -> String { |
| 407 | let ((year, month, day), (hour, minute, second)) = utc_parts(time); |
| 408 | |
| 409 | format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z") |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | fn utc_parts(time: SystemTime) -> ((i64, u32, u32), (u64, u64, u64)) { |
| 420 | let seconds = time |
| 421 | .duration_since(UNIX_EPOCH) |
| 422 | .map(|since| since.as_secs() as i64) |
| 423 | .unwrap_or(0); |
| 424 | |
| 425 | let rest = seconds.rem_euclid(86_400) as u64; |
| 426 | |
| 427 | ( |
| 428 | civil_from_days(seconds.div_euclid(86_400)), |
| 429 | (rest / 3600, (rest % 3600) / 60, rest % 60), |
| 430 | ) |
| 431 | } |
| 432 | |
| 433 | |
| 434 | fn civil_from_days(days: i64) -> (i64, u32, u32) { |
| 435 | let z = days + 719_468; |
| 436 | let era = z.div_euclid(146_097); |
| 437 | let doe = z.rem_euclid(146_097); |
| 438 | let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; |
| 439 | let year = yoe + era * 400; |
| 440 | let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); |
| 441 | let mp = (5 * doy + 2) / 153; |
| 442 | let day = (doy - (153 * mp + 2) / 5 + 1) as u32; |
| 443 | let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32; |
| 444 | |
| 445 | (year + i64::from(month <= 2), month, day) |
| 446 | } |
| 447 | |
| 448 | #[cfg(test)] |
| 449 | mod tests { |
| 450 | use std::time::Duration; |
| 451 | |
| 452 | use super::super::port::ExistingData; |
| 453 | use super::*; |
| 454 | use crate::{ |
| 455 | application::error::Error, |
| 456 | domain::{ |
| 457 | Email, OrgId, Organization, PasswordHash, RepoId, Repository, User, UserId, Visibility, |
| 458 | }, |
| 459 | infrastructure::{ |
| 460 | backup::{InMemoryBackupArchive, InMemoryDatabaseSnapshot}, |
| 461 | git::InMemoryGitStorage, |
| 462 | repository::{InMemoryOrgRepo, InMemoryRepoRepo, InMemoryUserRepo}, |
| 463 | }, |
| 464 | }; |
| 465 | |
| 466 | |
| 467 | fn at() -> SystemTime { |
| 468 | UNIX_EPOCH + Duration::from_secs(1_788_877_530) |
| 469 | } |
| 470 | |
| 471 | struct Fixture { |
| 472 | users: InMemoryUserRepo, |
| 473 | orgs: InMemoryOrgRepo, |
| 474 | repos: InMemoryRepoRepo, |
| 475 | storage: InMemoryGitStorage, |
| 476 | snapshots: InMemoryDatabaseSnapshot, |
| 477 | archives: InMemoryBackupArchive, |
| 478 | owner: Actor, |
| 479 | } |
| 480 | |
| 481 | impl Fixture { |
| 482 | |
| 483 | fn empty() -> Self { |
| 484 | Self { |
| 485 | users: InMemoryUserRepo::new(), |
| 486 | orgs: InMemoryOrgRepo::new(), |
| 487 | repos: InMemoryRepoRepo::new(), |
| 488 | storage: InMemoryGitStorage::new(), |
| 489 | snapshots: InMemoryDatabaseSnapshot::new(), |
| 490 | archives: InMemoryBackupArchive::new(), |
| 491 | owner: Actor::Anonymous, |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | |
| 496 | async fn claimed() -> Self { |
| 497 | let mut f = Self::empty(); |
| 498 | |
| 499 | let org = Organization::new(OrgId::generate(), "james", None).expect("valid org"); |
| 500 | f.orgs.save(&org).await.expect("save org"); |
| 501 | |
| 502 | let user = User::new( |
| 503 | UserId::generate(), |
| 504 | Email::new("dev@example.com").expect("valid email"), |
| 505 | PasswordHash::from_trusted("$argon2id$test"), |
| 506 | org.id.clone(), |
| 507 | ); |
| 508 | f.users.save(&user).await.expect("save user"); |
| 509 | f.repos |
| 510 | .save( |
| 511 | &Repository::new( |
| 512 | RepoId::generate(), |
| 513 | org.id.clone(), |
| 514 | "steid", |
| 515 | None, |
| 516 | Visibility::Public, |
| 517 | at(), |
| 518 | ) |
| 519 | .expect("valid repository"), |
| 520 | ) |
| 521 | .await |
| 522 | .expect("save repository"); |
| 523 | |
| 524 | f.owner = Actor::User(user.id); |
| 525 | f |
| 526 | } |
| 527 | |
| 528 | async fn back_up(&self, to: &str) -> Result<BackupWritten> { |
| 529 | back_up( |
| 530 | at(), |
| 531 | Path::new(to), |
| 532 | Path::new("/data"), |
| 533 | Path::new("/work"), |
| 534 | &self.users, |
| 535 | &self.orgs, |
| 536 | &self.repos, |
| 537 | &self.snapshots, |
| 538 | &self.archives, |
| 539 | ) |
| 540 | .await |
| 541 | } |
| 542 | |
| 543 | async fn restore(&self, from: &str) -> Result<Restored> { |
| 544 | self.restore_with(from, RestoreMode::OnlyIfEmpty).await |
| 545 | } |
| 546 | |
| 547 | async fn restore_with(&self, from: &str, mode: RestoreMode) -> Result<Restored> { |
| 548 | restore( |
| 549 | at(), |
| 550 | Path::new(from), |
| 551 | Path::new("/data"), |
| 552 | Path::new("/work"), |
| 553 | mode, |
| 554 | &self.users, |
| 555 | &self.storage, |
| 556 | &self.snapshots, |
| 557 | &self.archives, |
| 558 | ) |
| 559 | .await |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | #[test] |
| 564 | fn an_archive_is_named_for_the_version_and_the_moment() { |
| 565 | assert_eq!( |
| 566 | archive_name(at()), |
| 567 | format!("steid-{VERSION}-20260908T142530Z.tar.gz") |
| 568 | ); |
| 569 | } |
| 570 | |
| 571 | #[test] |
| 572 | fn a_manifest_timestamp_is_rfc_3339_in_utc() { |
| 573 | assert_eq!(rfc3339(at()), "2026-09-08T14:25:30Z"); |
| 574 | assert_eq!(rfc3339(UNIX_EPOCH), "1970-01-01T00:00:00Z"); |
| 575 | } |
| 576 | |
| 577 | #[tokio::test] |
| 578 | async fn a_backup_records_what_the_instance_held() { |
| 579 | let f = Fixture::claimed().await; |
| 580 | |
| 581 | let written = f.back_up("/out.tar.gz").await.expect("should back up"); |
| 582 | |
| 583 | assert_eq!(written.path, PathBuf::from("/out.tar.gz")); |
| 584 | assert_eq!(written.manifest.version, VERSION); |
| 585 | assert_eq!(written.manifest.taken_at, "2026-09-08T14:25:30Z"); |
| 586 | assert_eq!( |
| 587 | written.manifest.counts, |
| 588 | BackupCounts { |
| 589 | users: 1, |
| 590 | organisations: 1, |
| 591 | repositories: 1, |
| 592 | } |
| 593 | ); |
| 594 | } |
| 595 | |
| 596 | #[tokio::test] |
| 597 | async fn a_backup_packs_the_snapshot_rather_than_the_live_database() { |
| 598 | |
| 599 | |
| 600 | let f = Fixture::claimed().await; |
| 601 | |
| 602 | f.back_up("/out.tar.gz").await.expect("should back up"); |
| 603 | |
| 604 | let plan = f.archives.packed().pop().expect("one archive"); |
| 605 | assert_eq!(plan.database, PathBuf::from("/work/snapshot-1.db")); |
| 606 | assert_eq!(plan.data_dir, PathBuf::from("/data")); |
| 607 | assert_eq!(f.snapshots.taken(), 1); |
| 608 | } |
| 609 | |
| 610 | #[tokio::test] |
| 611 | async fn only_the_instance_owner_may_download_a_backup() { |
| 612 | let f = Fixture::claimed().await; |
| 613 | |
| 614 | let refused = back_up_for( |
| 615 | &Actor::Anonymous, |
| 616 | at(), |
| 617 | Path::new("/out.tar.gz"), |
| 618 | Path::new("/data"), |
| 619 | Path::new("/work"), |
| 620 | &f.users, |
| 621 | &f.orgs, |
| 622 | &f.repos, |
| 623 | &f.snapshots, |
| 624 | &f.archives, |
| 625 | ) |
| 626 | .await; |
| 627 | |
| 628 | assert!(matches!( |
| 629 | refused, |
| 630 | Err(Error::Domain(DomainError::Forbidden)) |
| 631 | )); |
| 632 | assert!(f.archives.packed().is_empty(), "nothing was written"); |
| 633 | |
| 634 | assert!( |
| 635 | back_up_for( |
| 636 | &f.owner, |
| 637 | at(), |
| 638 | Path::new("/out.tar.gz"), |
| 639 | Path::new("/data"), |
| 640 | Path::new("/work"), |
| 641 | &f.users, |
| 642 | &f.orgs, |
| 643 | &f.repos, |
| 644 | &f.snapshots, |
| 645 | &f.archives, |
| 646 | ) |
| 647 | .await |
| 648 | .is_ok() |
| 649 | ); |
| 650 | } |
| 651 | |
| 652 | #[tokio::test] |
| 653 | async fn a_signed_in_stranger_is_not_the_instance_owner() { |
| 654 | |
| 655 | |
| 656 | let f = Fixture::claimed().await; |
| 657 | |
| 658 | assert!( |
| 659 | !instance_owner(&Actor::User(UserId::generate()), &f.users) |
| 660 | .await |
| 661 | .expect("should read") |
| 662 | ); |
| 663 | } |
| 664 | |
| 665 | #[tokio::test] |
| 666 | async fn a_backup_restores_into_an_empty_instance() { |
| 667 | let source = Fixture::claimed().await; |
| 668 | let written = source.back_up("/out.tar.gz").await.expect("should back up"); |
| 669 | |
| 670 | let target = Fixture::empty(); |
| 671 | |
| 672 | let target = Fixture { |
| 673 | archives: source.archives.clone(), |
| 674 | ..target |
| 675 | }; |
| 676 | |
| 677 | let restored = target.restore("/out.tar.gz").await.expect("should restore"); |
| 678 | |
| 679 | assert_eq!(restored.manifest, written.manifest); |
| 680 | assert_eq!( |
| 681 | target |
| 682 | .archives |
| 683 | .unpacked() |
| 684 | .pop() |
| 685 | .expect("one restore") |
| 686 | .data_dir, |
| 687 | PathBuf::from("/data") |
| 688 | ); |
| 689 | |
| 690 | assert_eq!( |
| 691 | target.snapshots.installed(), |
| 692 | vec![PathBuf::from("/work/steid.db")] |
| 693 | ); |
| 694 | } |
| 695 | |
| 696 | #[tokio::test] |
| 697 | async fn a_restore_refuses_an_instance_that_already_has_a_user() { |
| 698 | let f = Fixture::claimed().await; |
| 699 | f.back_up("/out.tar.gz").await.expect("should back up"); |
| 700 | |
| 701 | let refused = f.restore("/out.tar.gz").await; |
| 702 | |
| 703 | assert!(matches!( |
| 704 | refused, |
| 705 | Err(Error::Domain(DomainError::Unavailable { |
| 706 | entity: "instance", |
| 707 | .. |
| 708 | })) |
| 709 | )); |
| 710 | assert!(f.snapshots.installed().is_empty(), "nothing was replaced"); |
| 711 | assert!(f.archives.unpacked().is_empty(), "nothing was extracted"); |
| 712 | } |
| 713 | |
| 714 | #[tokio::test] |
| 715 | async fn a_restore_refuses_a_data_directory_that_already_holds_repositories() { |
| 716 | let source = Fixture::claimed().await; |
| 717 | source.back_up("/out.tar.gz").await.expect("should back up"); |
| 718 | |
| 719 | let target = Fixture { |
| 720 | archives: source.archives.clone(), |
| 721 | ..Fixture::empty() |
| 722 | }; |
| 723 | target |
| 724 | .storage |
| 725 | .init_bare( |
| 726 | &crate::domain::OrgName::new("someone").expect("valid handle"), |
| 727 | &crate::domain::RepoName::new("thing").expect("valid name"), |
| 728 | ) |
| 729 | .await |
| 730 | .expect("init"); |
| 731 | |
| 732 | let refused = target.restore("/out.tar.gz").await; |
| 733 | |
| 734 | assert!(matches!( |
| 735 | refused, |
| 736 | Err(Error::Domain(DomainError::Unavailable { |
| 737 | entity: "instance", |
| 738 | .. |
| 739 | })) |
| 740 | )); |
| 741 | assert!( |
| 742 | target.archives.unpacked().is_empty(), |
| 743 | "nothing was extracted" |
| 744 | ); |
| 745 | } |
| 746 | |
| 747 | #[tokio::test] |
| 748 | async fn replace_restores_over_an_instance_that_already_holds_somebody() { |
| 749 | |
| 750 | |
| 751 | let source = Fixture::claimed().await; |
| 752 | source.back_up("/out.tar.gz").await.expect("should back up"); |
| 753 | |
| 754 | let target = Fixture { |
| 755 | archives: source.archives.clone(), |
| 756 | ..Fixture::claimed().await |
| 757 | }; |
| 758 | target |
| 759 | .storage |
| 760 | .init_bare( |
| 761 | &crate::domain::OrgName::new("someone").expect("valid handle"), |
| 762 | &crate::domain::RepoName::new("thing").expect("valid name"), |
| 763 | ) |
| 764 | .await |
| 765 | .expect("init"); |
| 766 | |
| 767 | assert!( |
| 768 | target.restore("/out.tar.gz").await.is_err(), |
| 769 | "the refusal is the default and must stay it" |
| 770 | ); |
| 771 | |
| 772 | let restored = target |
| 773 | .restore_with("/out.tar.gz", RestoreMode::Replace) |
| 774 | .await |
| 775 | .expect("--replace is the consent"); |
| 776 | |
| 777 | let aside = restored |
| 778 | .replaced |
| 779 | .expect("a replace says where the old state went"); |
| 780 | assert_eq!(aside, PathBuf::from("/data/.replaced-20260908T142530Z")); |
| 781 | |
| 782 | |
| 783 | |
| 784 | let plan = target.archives.unpacked().pop().expect("one restore"); |
| 785 | assert_eq!(plan.data_dir, PathBuf::from("/data")); |
| 786 | assert_eq!( |
| 787 | plan.existing, |
| 788 | ExistingData::Replace { |
| 789 | staging: PathBuf::from("/work/incoming"), |
| 790 | aside: aside.clone(), |
| 791 | } |
| 792 | ); |
| 793 | |
| 794 | |
| 795 | |
| 796 | assert_eq!(target.snapshots.taken(), 1); |
| 797 | assert_eq!( |
| 798 | target.snapshots.installed(), |
| 799 | vec![PathBuf::from("/work/steid.db")] |
| 800 | ); |
| 801 | } |
| 802 | |
| 803 | #[tokio::test] |
| 804 | async fn replace_still_refuses_an_archive_from_a_newer_steid() { |
| 805 | |
| 806 | |
| 807 | let f = Fixture::claimed().await; |
| 808 | f.archives.plant( |
| 809 | "/future.tar.gz", |
| 810 | BackupManifest { |
| 811 | version: "99.0.0".to_owned(), |
| 812 | taken_at: "2030-01-01T00:00:00Z".to_owned(), |
| 813 | counts: BackupCounts::default(), |
| 814 | }, |
| 815 | ); |
| 816 | |
| 817 | assert!(matches!( |
| 818 | f.restore_with("/future.tar.gz", RestoreMode::Replace).await, |
| 819 | Err(Error::Backup(BackupError::Refused(_))), |
| 820 | )); |
| 821 | assert!(f.archives.unpacked().is_empty(), "nothing was extracted"); |
| 822 | } |
| 823 | |
| 824 | #[tokio::test] |
| 825 | async fn an_empty_instance_restored_without_replace_moves_nothing_aside() { |
| 826 | let source = Fixture::claimed().await; |
| 827 | source.back_up("/out.tar.gz").await.expect("should back up"); |
| 828 | |
| 829 | let target = Fixture { |
| 830 | archives: source.archives.clone(), |
| 831 | ..Fixture::empty() |
| 832 | }; |
| 833 | |
| 834 | let restored = target.restore("/out.tar.gz").await.expect("should restore"); |
| 835 | |
| 836 | assert_eq!(restored.replaced, None); |
| 837 | assert_eq!( |
| 838 | target |
| 839 | .archives |
| 840 | .unpacked() |
| 841 | .pop() |
| 842 | .expect("one restore") |
| 843 | .existing, |
| 844 | ExistingData::Refuse |
| 845 | ); |
| 846 | |
| 847 | assert_eq!(target.snapshots.taken(), 0); |
| 848 | } |
| 849 | |
| 850 | #[tokio::test] |
| 851 | async fn a_restore_refuses_an_archive_from_a_newer_steid() { |
| 852 | let f = Fixture::empty(); |
| 853 | f.archives.plant( |
| 854 | "/future.tar.gz", |
| 855 | BackupManifest { |
| 856 | version: "99.0.0".to_owned(), |
| 857 | taken_at: "2030-01-01T00:00:00Z".to_owned(), |
| 858 | counts: BackupCounts::default(), |
| 859 | }, |
| 860 | ); |
| 861 | |
| 862 | let refused = f.restore("/future.tar.gz").await; |
| 863 | |
| 864 | assert!(matches!( |
| 865 | refused, |
| 866 | Err(Error::Backup(BackupError::Refused(_))), |
| 867 | )); |
| 868 | assert!(f.archives.unpacked().is_empty(), "nothing was extracted"); |
| 869 | } |
| 870 | |
| 871 | #[tokio::test] |
| 872 | async fn a_restore_accepts_an_archive_from_an_older_steid() { |
| 873 | let f = Fixture::empty(); |
| 874 | f.archives.plant( |
| 875 | "/old.tar.gz", |
| 876 | BackupManifest { |
| 877 | version: "0.0.1".to_owned(), |
| 878 | taken_at: "2026-01-01T00:00:00Z".to_owned(), |
| 879 | counts: BackupCounts::default(), |
| 880 | }, |
| 881 | ); |
| 882 | |
| 883 | assert!(f.restore("/old.tar.gz").await.is_ok()); |
| 884 | } |
| 885 | |
| 886 | #[test] |
| 887 | fn a_version_is_compared_by_its_three_numbers() { |
| 888 | assert!(is_newer("0.5.0", "0.4.0").expect("parses")); |
| 889 | assert!(is_newer("0.4.1", "0.4.0").expect("parses")); |
| 890 | assert!(!is_newer("0.4.0", "0.4.0").expect("parses")); |
| 891 | assert!(!is_newer("0.3.9", "0.4.0").expect("parses")); |
| 892 | |
| 893 | assert!(!is_newer("0.4.0-rc.1", "0.4.0").expect("parses")); |
| 894 | |
| 895 | assert!(is_newer("banana", "0.4.0").is_err()); |
| 896 | assert!(is_newer("0.4", "0.4.0").is_err()); |
| 897 | } |
| 898 | } |