summaryrefslogtreecommitdiff
path: root/cmd/restic/integration_fuse_test.go
blob: a99064b8fe2e2569c9ad494f39e7b9f66914c28b (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//go:build darwin || freebsd || linux
// +build darwin freebsd linux

package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"sync"
	"testing"
	"time"

	"github.com/restic/restic/internal/repository"
	"github.com/restic/restic/internal/restic"
	rtest "github.com/restic/restic/internal/test"
)

const (
	mountWait       = 20
	mountSleep      = 100 * time.Millisecond
	mountTestSubdir = "snapshots"
)

func snapshotsDirExists(t testing.TB, dir string) bool {
	f, err := os.Open(filepath.Join(dir, mountTestSubdir))
	if err != nil && os.IsNotExist(err) {
		return false
	}

	if err != nil {
		t.Error(err)
	}

	if err := f.Close(); err != nil {
		t.Error(err)
	}

	return true
}

// waitForMount blocks (max mountWait * mountSleep) until the subdir
// "snapshots" appears in the dir.
func waitForMount(t testing.TB, dir string) {
	for i := 0; i < mountWait; i++ {
		if snapshotsDirExists(t, dir) {
			t.Log("mounted directory is ready")
			return
		}

		time.Sleep(mountSleep)
	}

	t.Errorf("subdir %q of dir %s never appeared", mountTestSubdir, dir)
}

func testRunMount(t testing.TB, gopts GlobalOptions, dir string, wg *sync.WaitGroup) {
	defer wg.Done()
	opts := MountOptions{
		TimeTemplate: time.RFC3339,
	}
	rtest.OK(t, runMount(context.TODO(), opts, gopts, []string{dir}))
}

func testRunUmount(t testing.TB, gopts GlobalOptions, dir string) {
	var err error
	for i := 0; i < mountWait; i++ {
		if err = umount(dir); err == nil {
			t.Logf("directory %v umounted", dir)
			return
		}

		time.Sleep(mountSleep)
	}

	t.Errorf("unable to umount dir %v, last error was: %v", dir, err)
}

func listSnapshots(t testing.TB, dir string) []string {
	snapshotsDir, err := os.Open(filepath.Join(dir, "snapshots"))
	rtest.OK(t, err)
	names, err := snapshotsDir.Readdirnames(-1)
	rtest.OK(t, err)
	rtest.OK(t, snapshotsDir.Close())
	return names
}

func checkSnapshots(t testing.TB, global GlobalOptions, repo *repository.Repository, mountpoint, repodir string, snapshotIDs restic.IDs, expectedSnapshotsInFuseDir int) {
	t.Logf("checking for %d snapshots: %v", len(snapshotIDs), snapshotIDs)

	var wg sync.WaitGroup
	wg.Add(1)
	go testRunMount(t, global, mountpoint, &wg)
	waitForMount(t, mountpoint)
	defer wg.Wait()
	defer testRunUmount(t, global, mountpoint)

	if !snapshotsDirExists(t, mountpoint) {
		t.Fatal(`virtual directory "snapshots" doesn't exist`)
	}

	ids := listSnapshots(t, repodir)
	t.Logf("found %v snapshots in repo: %v", len(ids), ids)

	namesInSnapshots := listSnapshots(t, mountpoint)
	t.Logf("found %v snapshots in fuse mount: %v", len(namesInSnapshots), namesInSnapshots)
	rtest.Assert(t,
		expectedSnapshotsInFuseDir == len(namesInSnapshots),
		"Invalid number of snapshots: expected %d, got %d", expectedSnapshotsInFuseDir, len(namesInSnapshots))

	namesMap := make(map[string]bool)
	for _, name := range namesInSnapshots {
		namesMap[name] = false
	}

	// Is "latest" present?
	if len(namesMap) != 0 {
		_, ok := namesMap["latest"]
		if !ok {
			t.Errorf("Symlink latest isn't present in fuse dir")
		} else {
			namesMap["latest"] = true
		}
	}

	for _, id := range snapshotIDs {
		snapshot, err := restic.LoadSnapshot(context.TODO(), repo, id)
		rtest.OK(t, err)

		ts := snapshot.Time.Format(time.RFC3339)
		present, ok := namesMap[ts]
		if !ok {
			t.Errorf("Snapshot %v (%q) isn't present in fuse dir", id.Str(), ts)
		}

		for i := 1; present; i++ {
			ts = fmt.Sprintf("%s-%d", snapshot.Time.Format(time.RFC3339), i)
			present, ok = namesMap[ts]
			if !ok {
				t.Errorf("Snapshot %v (%q) isn't present in fuse dir", id.Str(), ts)
			}

			if !present {
				break
			}
		}

		namesMap[ts] = true
	}

	for name, present := range namesMap {
		rtest.Assert(t, present, "Directory %s is present in fuse dir but is not a snapshot", name)
	}
}

func TestMount(t *testing.T) {
	if !rtest.RunFuseTest {
		t.Skip("Skipping fuse tests")
	}

	env, cleanup := withTestEnvironment(t)
	// must list snapshots more than once
	env.gopts.backendTestHook = nil
	defer cleanup()

	testRunInit(t, env.gopts)

	repo, err := OpenRepository(context.TODO(), env.gopts)
	rtest.OK(t, err)

	checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, []restic.ID{}, 0)

	rtest.SetupTarTestFixture(t, env.testdata, filepath.Join("testdata", "backup-data.tar.gz"))

	// first backup
	testRunBackup(t, "", []string{env.testdata}, BackupOptions{}, env.gopts)
	snapshotIDs := testRunList(t, "snapshots", env.gopts)
	rtest.Assert(t, len(snapshotIDs) == 1,
		"expected one snapshot, got %v", snapshotIDs)

	checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs, 2)

	// second backup, implicit incremental
	testRunBackup(t, "", []string{env.testdata}, BackupOptions{}, env.gopts)
	snapshotIDs = testRunList(t, "snapshots", env.gopts)
	rtest.Assert(t, len(snapshotIDs) == 2,
		"expected two snapshots, got %v", snapshotIDs)

	checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs, 3)

	// third backup, explicit incremental
	bopts := BackupOptions{Parent: snapshotIDs[0].String()}
	testRunBackup(t, "", []string{env.testdata}, bopts, env.gopts)
	snapshotIDs = testRunList(t, "snapshots", env.gopts)
	rtest.Assert(t, len(snapshotIDs) == 3,
		"expected three snapshots, got %v", snapshotIDs)

	checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs, 4)
}

func TestMountSameTimestamps(t *testing.T) {
	if !rtest.RunFuseTest {
		t.Skip("Skipping fuse tests")
	}

	env, cleanup := withTestEnvironment(t)
	// must list snapshots more than once
	env.gopts.backendTestHook = nil
	defer cleanup()

	rtest.SetupTarTestFixture(t, env.base, filepath.Join("testdata", "repo-same-timestamps.tar.gz"))

	repo, err := OpenRepository(context.TODO(), env.gopts)
	rtest.OK(t, err)

	ids := []restic.ID{
		restic.TestParseID("280303689e5027328889a06d718b729e96a1ce6ae9ef8290bff550459ae611ee"),
		restic.TestParseID("75ad6cdc0868e082f2596d5ab8705e9f7d87316f5bf5690385eeff8dbe49d9f5"),
		restic.TestParseID("5fd0d8b2ef0fa5d23e58f1e460188abb0f525c0f0c4af8365a1280c807a80a1b"),
	}

	checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, ids, 4)
}