Skip to content

Commit

Permalink
fix: properly finish progress bar in Repository::get_snapshot_group (#…
Browse files Browse the repository at this point in the history
…263)

Also adds a few more integration tests for snapshot gathering methods.

---------

Co-authored-by: simonsan <[email protected]>
  • Loading branch information
aawsome and simonsan authored Sep 11, 2024
1 parent 41033bd commit 2d52c09
Show file tree
Hide file tree
Showing 16 changed files with 574 additions and 50 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/commands/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
SnapshotFile,
},
repository::{Open, Repository},
Progress,
};

/// Get the snapshots from the repository.
Expand Down Expand Up @@ -56,6 +57,7 @@ pub(crate) fn get_snapshot_group<P: ProgressBars, S: Open>(
vec![item]
}
};
p.finish();

Ok(groups)
}
96 changes: 70 additions & 26 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ use rstest::{fixture, rstest};
use rustic_core::{
repofile::SnapshotFile, BackupOptions, CheckOptions, ConfigOptions, FindMatches, FindNode,
FullIndex, IndexedFull, IndexedStatus, KeyOptions, LimitOption, LsOptions, NoProgressBars,
OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions, RusticResult,
OpenStatus, ParentOptions, PathList, Repository, RepositoryBackends, RepositoryOptions,
RusticResult, SnapshotGroupCriterion, SnapshotOptions, StringList,
};
use rustic_core::{
repofile::{Metadata, Node},
Expand Down Expand Up @@ -98,36 +99,38 @@ fn handle_option(val: Content, _: ContentPath<'_>) -> String {
}

#[fixture]
fn insta_summary_redaction() -> Settings {
fn insta_snapshotfile_redaction() -> Settings {
let mut settings = Settings::clone_current();

settings.add_redaction(".tree", "[tree_id]");
settings.add_dynamic_redaction(".program_version", |val, _| {
settings.add_redaction(".**.tree", "[tree_id]");
settings.add_dynamic_redaction(".**.program_version", |val, _| {
val.resolve_inner()
.as_str()
.map_or("[program_version]".to_string(), |v| {
v.replace(env!("CARGO_PKG_VERSION"), "[rustic_core_version]")
})
});
settings.add_redaction(".time", "[time]");
settings.add_dynamic_redaction(".parent", handle_option);
settings.add_redaction(".tags", "[tags]");
settings.add_redaction(".id", "[id]");
settings.add_redaction(".summary.backup_start", "[backup_start]");
settings.add_redaction(".summary.backup_end", "[backup_end]");
settings.add_redaction(".summary.backup_duration", "[backup_duration]");
settings.add_redaction(".summary.total_duration", "[total_duration]");
settings.add_redaction(".summary.data_added", "[data_added]");
settings.add_redaction(".summary.data_added_packed", "[data_added_packed]");
settings.add_redaction(".**.time", "[time]");
settings.add_dynamic_redaction(".**.parent", handle_option);
settings.add_redaction(".**.id", "[id]");
settings.add_redaction(".**.original", "[original]");
settings.add_redaction(".**.hostname", "[hostname]");
settings.add_redaction(".**.command", "[command]");
settings.add_redaction(".**.summary.backup_start", "[backup_start]");
settings.add_redaction(".**.summary.backup_end", "[backup_end]");
settings.add_redaction(".**.summary.backup_duration", "[backup_duration]");
settings.add_redaction(".**.summary.total_duration", "[total_duration]");
settings.add_redaction(".**.summary.data_added", "[data_added]");
settings.add_redaction(".**.summary.data_added_packed", "[data_added_packed]");
settings.add_redaction(
".summary.total_dirsize_processed",
".**.summary.total_dirsize_processed",
"[total_dirsize_processed]",
);
settings.add_redaction(
".summary.data_added_trees_packed",
".**.summary.data_added_trees_packed",
"[data_added_trees_packed]",
);
settings.add_redaction(".summary.data_added_trees", "[data_added_trees]");
settings.add_redaction(".**.summary.data_added_trees", "[data_added_trees]");

settings
}
Expand Down Expand Up @@ -176,7 +179,7 @@ fn assert_with_win<T: Serialize>(test: &str, snap: T) {
fn test_backup_with_tar_gz_passes(
tar_gz_testdata: Result<TestSource>,
set_up_repo: Result<RepoOpen>,
insta_summary_redaction: Settings,
insta_snapshotfile_redaction: Settings,
insta_node_redaction: Settings,
) -> Result<()> {
// uncomment for logging output
Expand All @@ -196,7 +199,7 @@ fn test_backup_with_tar_gz_passes(
// We can also bind to scope ( https://docs.rs/insta/latest/insta/struct.Settings.html#method.bind_to_scope )
// But I think that can get messy with a lot of tests, also checking which settings are currently applied
// will be probably harder
insta_summary_redaction.bind(|| {
insta_snapshotfile_redaction.bind(|| {
assert_with_win("backup-tar-summary-first", &first_snapshot);
});

Expand All @@ -223,21 +226,43 @@ fn test_backup_with_tar_gz_passes(
// second backup
let second_snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
insta_snapshotfile_redaction.bind(|| {
assert_with_win("backup-tar-summary-second", &second_snapshot);
});

assert_eq!(second_snapshot.parent, Some(first_snapshot.id));
assert_eq!(first_snapshot.tree, second_snapshot.tree);

// pack files should be unchanged
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs1, packs2);

// re-read index
let repo = repo.to_indexed_ids()?;
// third backup with tags and explicitely given parent
let snap = SnapshotOptions::default()
.tag([StringList::from_str("a,b")?])
.to_snapshot()?;
let opts = opts.parent_opts(ParentOptions::default().parent(second_snapshot.id.to_string()));
let third_snapshot = repo.backup(&opts, paths, snap)?;

insta_snapshotfile_redaction.bind(|| {
assert_with_win("backup-tar-summary-third", &third_snapshot);
});
assert_eq!(third_snapshot.parent, Some(second_snapshot.id));
assert_eq!(third_snapshot.tree, second_snapshot.tree);

// get all snapshots and check them
let mut all_snapshots = repo.get_all_snapshots()?;
all_snapshots.sort_unstable();
assert_eq!(vec![first_snapshot, second_snapshot], all_snapshots);
assert_eq!(
vec![first_snapshot, second_snapshot, third_snapshot],
all_snapshots
);

// pack files should be unchanged
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs1, packs2);
let packs3: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs1, packs3);

// Check if snapshots can be retrieved
let mut ids: Vec<_> = all_snapshots.iter().map(|sn| sn.id.to_string()).collect();
Expand All @@ -250,14 +275,33 @@ fn test_backup_with_tar_gz_passes(
let snaps = repo.get_snapshots(&ids)?;
assert_eq!(snaps, all_snapshots);

// get snapshot group
let group_by = SnapshotGroupCriterion::new().tags(true);
let mut groups = repo.get_snapshot_group(&[], group_by, |_| true)?;

// sort groups to get unique result
groups.iter_mut().for_each(|(_, snaps)| snaps.sort());
groups.sort_by_key(|(group, _)| group.tags.clone());

insta_snapshotfile_redaction.bind(|| {
assert_with_win("backup-tar-groups", &groups);
});

// filter snapshots by tag
let filter = |snap: &SnapshotFile| snap.tags.contains("a");
let snaps = repo.get_matching_snapshots(filter)?;
insta_snapshotfile_redaction.bind(|| {
assert_with_win("backup-tar-matching-snaps", &snaps);
});

Ok(())
}

#[rstest]
fn test_backup_dry_run_with_tar_gz_passes(
tar_gz_testdata: Result<TestSource>,
set_up_repo: Result<RepoOpen>,
insta_summary_redaction: Settings,
insta_snapshotfile_redaction: Settings,
insta_node_redaction: Settings,
) -> Result<()> {
// Fixtures
Expand All @@ -273,7 +317,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
// dry-run backup
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
insta_snapshotfile_redaction.bind(|| {
assert_with_win("dryrun-tar-summary-first", &snap_dry_run);
});

Expand Down Expand Up @@ -305,7 +349,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
let opts = opts.dry_run(true);
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;

insta_summary_redaction.bind(|| {
insta_snapshotfile_redaction.bind(|| {
assert_with_win("dryrun-tar-summary-second", &snap_dry_run);
});

Expand Down
142 changes: 142 additions & 0 deletions crates/core/tests/snapshots/integration__backup-tar-groups-nix.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
---
source: crates/core/tests/integration.rs
expression: snap
---
[
(SnapshotGroup(
tags: Some(StringList([])),
), [
SnapshotFile(
time: "[time]",
program_version: "rustic [rustic_core_version]",
tree: "[tree_id]",
paths: StringList([
"test",
]),
hostname: "[hostname]",
username: "",
uid: 0,
gid: 0,
tags: StringList([]),
original: "[original]",
summary: Some(SnapshotSummary(
files_new: 73,
files_changed: 0,
files_unmodified: 0,
total_files_processed: 73,
total_bytes_processed: 1125674,
dirs_new: 6,
dirs_changed: 0,
dirs_unmodified: 0,
total_dirs_processed: 6,
total_dirsize_processed: "[total_dirsize_processed]",
data_blobs: 70,
tree_blobs: 6,
data_added: "[data_added]",
data_added_packed: "[data_added_packed]",
data_added_files: 1125653,
data_added_files_packed: 78740,
data_added_trees: "[data_added_trees]",
data_added_trees_packed: "[data_added_trees_packed]",
command: "[command]",
backup_start: "[backup_start]",
backup_end: "[backup_end]",
backup_duration: "[backup_duration]",
total_duration: "[total_duration]",
)),
id: "[id]",
),
SnapshotFile(
time: "[time]",
program_version: "rustic [rustic_core_version]",
parent: "[some]",
tree: "[tree_id]",
paths: StringList([
"test",
]),
hostname: "[hostname]",
username: "",
uid: 0,
gid: 0,
tags: StringList([]),
original: "[original]",
summary: Some(SnapshotSummary(
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: 6,
total_dirs_processed: 6,
total_dirsize_processed: "[total_dirsize_processed]",
data_blobs: 0,
tree_blobs: 0,
data_added: "[data_added]",
data_added_packed: "[data_added_packed]",
data_added_files: 0,
data_added_files_packed: 0,
data_added_trees: "[data_added_trees]",
data_added_trees_packed: "[data_added_trees_packed]",
command: "[command]",
backup_start: "[backup_start]",
backup_end: "[backup_end]",
backup_duration: "[backup_duration]",
total_duration: "[total_duration]",
)),
id: "[id]",
),
]),
(SnapshotGroup(
tags: Some(StringList([
"a",
"b",
])),
), [
SnapshotFile(
time: "[time]",
program_version: "rustic [rustic_core_version]",
parent: "[some]",
tree: "[tree_id]",
paths: StringList([
"test",
]),
hostname: "[hostname]",
username: "",
uid: 0,
gid: 0,
tags: StringList([
"a",
"b",
]),
original: "[original]",
summary: Some(SnapshotSummary(
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: 6,
total_dirs_processed: 6,
total_dirsize_processed: "[total_dirsize_processed]",
data_blobs: 0,
tree_blobs: 0,
data_added: "[data_added]",
data_added_packed: "[data_added_packed]",
data_added_files: 0,
data_added_files_packed: 0,
data_added_trees: "[data_added_trees]",
data_added_trees_packed: "[data_added_trees_packed]",
command: "[command]",
backup_start: "[backup_start]",
backup_end: "[backup_end]",
backup_duration: "[backup_duration]",
total_duration: "[total_duration]",
)),
id: "[id]",
),
]),
]
Loading

0 comments on commit 2d52c09

Please sign in to comment.