From 7bf38b6c50b641bfaaf913ff0732e84663401656 Mon Sep 17 00:00:00 2001 From: Gabriel Kabbe Date: Mon, 27 Nov 2023 21:29:20 +0100 Subject: [PATCH] rewrite: Add test TestRewriteMetadata --- cmd/restic/cmd_rewrite_integration_test.go | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmd/restic/cmd_rewrite_integration_test.go b/cmd/restic/cmd_rewrite_integration_test.go index 7d4226691..5cb205262 100644 --- a/cmd/restic/cmd_rewrite_integration_test.go +++ b/cmd/restic/cmd_rewrite_integration_test.go @@ -72,3 +72,38 @@ func TestRewriteReplace(t *testing.T) { testRunPrune(t, env.gopts, PruneOptions{MaxUnused: "0"}) testRunCheck(t, env.gopts) } + +func testRewriteMetadata(t *testing.T, metadata SnapshotMetadataArgs) { + env, cleanup := withTestEnvironment(t) + env.gopts.backendTestHook = nil + defer cleanup() + createBasicRewriteRepo(t, env) + repo, _ := OpenRepository(context.TODO(), env.gopts) + + testRunRewriteExclude(t, env.gopts, []string{}, true, &metadata) + + snapshots := FindFilteredSnapshots(context.TODO(), repo, repo, &restic.SnapshotFilter{}, []string{}) + + newSnapshot := <-snapshots + + if metadata.Time != "" { + rtest.Assert(t, newSnapshot.Time.Format(TimeFormat) == metadata.Time, "New snapshot should have time %s", metadata.Time) + } + + if metadata.Hostname != "" { + rtest.Assert(t, newSnapshot.Hostname == metadata.Hostname, "New snapshot should have host %s", metadata.Hostname) + } +} + +func TestRewriteMetadata(t *testing.T) { + newHost := "new host" + newTime := "1999-01-01 11:11:11" + + for _, metadata := range []SnapshotMetadataArgs{ + {Hostname: "", Time: newTime}, + {Hostname: newHost, Time: ""}, + {Hostname: newHost, Time: newTime}, + } { + testRewriteMetadata(t, metadata) + } +}