-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
use std::{env, fs::File, path::Path, str::FromStr, sync::Arc}; | ||
|
||
use anyhow::Result; | ||
use flate2::read::GzDecoder; | ||
use insta::assert_debug_snapshot; | ||
use pretty_assertions::assert_eq; | ||
use rustic_core::{ | ||
repofile::SnapshotFile, BackupOptions, ConfigOptions, InMemoryBackend, KeyOptions, | ||
NoProgressBars, OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions, | ||
StringList, | ||
}; | ||
use tar::Archive; | ||
use tempfile::{tempdir, TempDir}; | ||
|
||
fn set_up_repo() -> Result<Repository<NoProgressBars, OpenStatus>> { | ||
let be = InMemoryBackend::new(); | ||
let be = RepositoryBackends::new(Arc::new(be), None); | ||
let options = RepositoryOptions::default().password("test"); | ||
let repo = Repository::new(&options, be)?; | ||
let key_opts = KeyOptions::default(); | ||
let config_opts = &ConfigOptions::default(); | ||
let repo = repo.init(&key_opts, config_opts)?; | ||
Ok(repo) | ||
} | ||
|
||
struct TestSource(TempDir); | ||
|
||
impl TestSource { | ||
fn new(tmp: TempDir) -> Self { | ||
Self(tmp) | ||
} | ||
|
||
fn paths(&self) -> Result<PathList> { | ||
Ok(PathList::from_string(self.0.path().to_str().unwrap())?) | ||
} | ||
|
||
fn strings(&self) -> Result<StringList> { | ||
Ok(StringList::from_str(self.0.path().to_str().unwrap())?) | ||
} | ||
} | ||
|
||
fn set_up_testdata(path: impl AsRef<Path>) -> Result<TestSource> { | ||
let dir = tempdir()?; | ||
let path = Path::new("tests/testdata").join(path); | ||
let tar_gz = File::open(path)?; | ||
let tar = GzDecoder::new(tar_gz); | ||
let mut archive = Archive::new(tar); | ||
archive.set_preserve_permissions(true); | ||
archive.set_preserve_mtime(true); | ||
archive.unpack(&dir)?; | ||
Ok(TestSource::new(dir)) | ||
} | ||
|
||
// Parts of the snapshot summary we want to test against references | ||
struct TestSummary<'a>(&'a SnapshotFile); | ||
|
||
impl<'a> std::fmt::Debug for TestSummary<'a> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let s = self.0.summary.as_ref().unwrap(); | ||
// leave out info we expect to change: | ||
// Ids, times, tree sizes (as used uid/username is saved in trees) | ||
let mut b = f.debug_struct("TestSnap"); | ||
b.field("files_new", &s.files_new); | ||
b.field("files_changed", &s.files_changed); | ||
b.field("files_unmodified", &s.files_unmodified); | ||
b.field("total_files_processed", &s.total_files_processed); | ||
b.field("total_bytes_processed", &s.total_bytes_processed); | ||
b.field("dirs_new", &s.dirs_new); | ||
b.field("dirs_changed", &s.dirs_changed); | ||
b.field("dirs_unmodified", &s.dirs_unmodified); | ||
b.field("total_dirs_processed", &s.total_dirs_processed); | ||
b.field("data_blobs", &s.data_blobs); | ||
b.field("tree_blobs", &s.tree_blobs); | ||
b.field("data_added_files", &s.data_added_files); | ||
b.field("data_added_files_packed", &s.data_added_files_packed); | ||
b.finish() | ||
} | ||
} | ||
|
||
#[test] | ||
fn backup() -> Result<()> { | ||
// SimpleLogger::init(log::LevelFilter::Debug, Config::default())?; | ||
let source = set_up_testdata("backup-data.tar.gz")?; | ||
let paths = &source.paths()?; | ||
let repo = set_up_repo()?.to_indexed_ids()?; | ||
let opts = BackupOptions::default(); | ||
|
||
// first backup | ||
let snap1 = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_debug_snapshot!(TestSummary(&snap1)); | ||
assert_eq!(snap1.parent, None); | ||
assert_eq!(snap1.paths, source.strings()?); | ||
|
||
// get all snapshots and check them | ||
let snaps = repo.get_all_snapshots()?; | ||
assert_eq!(vec![snap1.clone()], snaps); | ||
// save list of pack files | ||
let packs1: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect(); | ||
|
||
// re-read index | ||
let repo = repo.to_indexed_ids()?; | ||
// second backup | ||
let snap2 = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_debug_snapshot!(TestSummary(&snap2)); | ||
assert_eq!(snap2.parent, Some(snap1.id)); | ||
assert_eq!(snap1.tree, snap2.tree); | ||
|
||
// get all snapshots and check them | ||
let mut snaps = repo.get_all_snapshots()?; | ||
snaps.sort_unstable(); | ||
assert_eq!(vec![snap1, snap2], snaps); | ||
|
||
// pack files should be unchanged | ||
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect(); | ||
assert_eq!(packs1, packs2); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn backup_dry_run() -> Result<()> { | ||
let source = &set_up_testdata("backup-data.tar.gz")?; | ||
let paths = &source.paths()?; | ||
let repo = set_up_repo()?.to_indexed_ids()?; | ||
let opts = BackupOptions::default().dry_run(true); | ||
|
||
// dry-run backup | ||
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_debug_snapshot!(TestSummary(&snap_dry_run)); | ||
// check that repo is still empty | ||
let snaps = repo.get_all_snapshots()?; | ||
assert_eq!(snaps.len(), 0); | ||
let packs: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect(); | ||
assert_eq!(packs.len(), 0); | ||
let indexes: Vec<_> = repo.list(rustic_core::FileType::Index)?.collect(); | ||
assert_eq!(indexes.len(), 0); | ||
|
||
// first real backup | ||
let opts = opts.dry_run(false); | ||
let snap1 = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_eq!(snap_dry_run.tree, snap1.tree); | ||
let packs: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect(); | ||
|
||
// re-read index | ||
let repo = repo.to_indexed_ids()?; | ||
// second dry-run backup | ||
let opts = opts.dry_run(true); | ||
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_debug_snapshot!(TestSummary(&snap_dry_run)); | ||
// check that no data has been added | ||
let snaps = repo.get_all_snapshots()?; | ||
assert_eq!(snaps, vec![snap1.clone()]); | ||
let packs_dry_run: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect(); | ||
assert_eq!(packs_dry_run, packs); | ||
|
||
// re-read index | ||
let repo = repo.to_indexed_ids()?; | ||
// second real backup | ||
let opts = opts.dry_run(false); | ||
let snap2 = repo.backup(&opts, paths, SnapshotFile::default())?; | ||
assert_eq!(snap_dry_run.tree, snap2.tree); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/core/tests/integration.rs | ||
expression: TestSnap(&snap2) | ||
--- | ||
TestSnap { | ||
files_new: 0, | ||
files_changed: 0, | ||
files_unmodified: 73, | ||
total_files_processed: 73, | ||
total_bytes_processed: 1125682, | ||
dirs_new: 0, | ||
dirs_changed: 0, | ||
dirs_unmodified: 7, | ||
total_dirs_processed: 7, | ||
data_blobs: 0, | ||
tree_blobs: 0, | ||
data_added_files: 0, | ||
data_added_files_packed: 0, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/core/tests/integration.rs | ||
expression: TestSnap(&snap1) | ||
--- | ||
TestSnap { | ||
files_new: 73, | ||
files_changed: 0, | ||
files_unmodified: 0, | ||
total_files_processed: 73, | ||
total_bytes_processed: 1125674, | ||
dirs_new: 7, | ||
dirs_changed: 0, | ||
dirs_unmodified: 0, | ||
total_dirs_processed: 7, | ||
data_blobs: 70, | ||
tree_blobs: 7, | ||
data_added_files: 1125653, | ||
data_added_files_packed: 78740, | ||
} |
19 changes: 19 additions & 0 deletions
19
crates/core/tests/snapshots/integration__backup_dry_run-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/core/tests/integration.rs | ||
expression: TestSnap(&snap_dry_run) | ||
--- | ||
TestSnap { | ||
files_new: 0, | ||
files_changed: 0, | ||
files_unmodified: 73, | ||
total_files_processed: 73, | ||
total_bytes_processed: 1125682, | ||
dirs_new: 0, | ||
dirs_changed: 0, | ||
dirs_unmodified: 7, | ||
total_dirs_processed: 7, | ||
data_blobs: 0, | ||
tree_blobs: 0, | ||
data_added_files: 0, | ||
data_added_files_packed: 0, | ||
} |
19 changes: 19 additions & 0 deletions
19
crates/core/tests/snapshots/integration__backup_dry_run.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/core/tests/integration.rs | ||
expression: TestSnap(&snap_dry_run) | ||
--- | ||
TestSnap { | ||
files_new: 73, | ||
files_changed: 0, | ||
files_unmodified: 0, | ||
total_files_processed: 73, | ||
total_bytes_processed: 1125674, | ||
dirs_new: 7, | ||
dirs_changed: 0, | ||
dirs_unmodified: 0, | ||
total_dirs_processed: 7, | ||
data_blobs: 70, | ||
tree_blobs: 7, | ||
data_added_files: 1125653, | ||
data_added_files_packed: 78740, | ||
} |
Binary file not shown.