Help with setting up an integration test (write snapshot, modify metadata, assert metadata changed)

Hello everybody,

I am trying to get to know restic’s codebase (and maybe to contribute at some point in time, but currently I am doing this just out of curiosity).

Inspired by this issue I’m trying to allow rewriting the time field of a snapshot.

Ideally, I would like to start with a simple test, something like this:

// pseudo code
func testRewriteTime(){
    oldTime = "1900-01-01 00:00:00"
    newTime = 2000-01-01 01:00:00"
    id = setupTestRepoWithSingleSnapshot(oldTime)
    rewriteTime(id, newTime)
    Assert(TimeOf(id) == newTime)

In the test file cmd/restic/cmd_rewrite_integration_test.go I found the function TestRewriteReplace, which looks to me like a good template for my new test.

So my question to the experts: is this more or less the right approach or is there a better way?
I would be also grateful for any tips / best practices for writing tests for restic (which helper functions to use etc).

Thanks for your help!

So, after a bit of trial and error I actually got a working test:

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) {
	new_host := "new_host"
	new_time := "1999-01-01 11:11:11"

	for _, metadata := range []SnapshotMetadataArgs{
		{Hostname: "", Time: new_time},
		{Hostname: new_host, Time: ""},
		{Hostname: new_host, Time: new_time},
	} {
		testRewriteMetadata(t, metadata)
1 Like