~bigbes/sr-ht-dolt

ref: 51a5acf935a1f7fbcf879fb9f748331e5a27a559 sr-ht-dolt/storage/storage_test.go -rw-r--r-- 7.6 KiB
51a5acf9 — Eugene Blikh style(web): restyle the beads parade in the todo.sr.ht idiom 30 days ago
                                                                                
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package storage

import (
	"context"
	"os"
	"path/filepath"
	"testing"

	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
	"github.com/dolthub/dolt/go/libraries/utils/earl"
	"github.com/dolthub/dolt/go/libraries/utils/filesys"
	"github.com/dolthub/dolt/go/store/types"
)

// nbfVer is the NBS version string matching the format InitStore writes with.
var nbfVer = types.Format_DOLT.VersionString()

func TestRepoDiskPath(t *testing.T) {
	got := RepoDiskPath("/var/lib/dolt", "alice", "widgets")
	want := filepath.Join("/var/lib/dolt", "~alice", "widgets")
	if got != want {
		t.Fatalf("RepoDiskPath = %q, want %q", got, want)
	}
}

func TestInitStoreCreatesValidStore(t *testing.T) {
	ctx := context.Background()
	root := t.TempDir()
	absPath := RepoDiskPath(root, "alice", "widgets")

	if err := InitStore(ctx, absPath, "alice", "alice@example.test"); err != nil {
		t.Fatalf("InitStore: %v", err)
	}

	// Re-open the bare store read-only and confirm it is a real repo with a
	// "main" branch and a resolvable initial commit.
	fileURL := earl.FileUrlFromPath(absPath, os.PathSeparator)
	ddb, err := doltdb.LoadDoltDB(ctx, types.Format_DOLT, fileURL, filesys.LocalFS)
	if err != nil {
		t.Fatalf("re-open LoadDoltDB(%s): %v", fileURL, err)
	}
	// NOTE: this read-only verification handle is intentionally not closed.
	// Closing a DoltDB after resolving a commit trips an nbs table-index
	// ref-count panic upstream (store/nbs/table_index.go). The handle is
	// reclaimed at process exit and t.TempDir removes the files; this test only
	// needs to confirm the store is a valid, readable repo.

	if _, has, err := ddb.HasBranch(ctx, "main"); err != nil {
		t.Fatalf("HasBranch(main): %v", err)
	} else if !has {
		t.Fatalf("re-opened store has no 'main' branch")
	}

	commit, err := ddb.ResolveCommitRef(ctx, ref.NewBranchRef("main"))
	if err != nil {
		t.Fatalf("ResolveCommitRef(main): %v", err)
	}
	if commit == nil {
		t.Fatalf("ResolveCommitRef(main) returned nil commit")
	}
	if _, err := commit.HashOf(); err != nil {
		t.Fatalf("initial commit HashOf: %v", err)
	}
}

func TestInitStoreRejectsRelativePath(t *testing.T) {
	if err := InitStore(context.Background(), "relative/store", "alice", "a@b.test"); err == nil {
		t.Fatalf("InitStore accepted a relative path")
	}
}

func TestInitStorePartialFailureCleanup(t *testing.T) {
	if os.Geteuid() == 0 {
		t.Skip("running as root: permission-based failure cannot be induced")
	}
	ctx := context.Background()
	parent := t.TempDir()

	// A read-only intermediate directory makes MkdirAll fail so InitStore
	// cannot even create the store dir; assert nothing is left behind.
	ro := filepath.Join(parent, "ro")
	if err := os.Mkdir(ro, 0o555); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { os.Chmod(ro, 0o755) })

	absPath := filepath.Join(ro, "sub", "store")
	if err := InitStore(ctx, absPath, "alice", "a@b.test"); err == nil {
		t.Fatalf("InitStore succeeded under a read-only parent")
	}
	if _, err := os.Stat(absPath); !os.IsNotExist(err) {
		t.Fatalf("expected no leftover store dir at %q, stat err = %v", absPath, err)
	}
	if _, err := os.Stat(filepath.Join(ro, "sub")); !os.IsNotExist(err) {
		t.Fatalf("expected no leftover intermediate dir, stat err = %v", err)
	}
}

func TestDeleteStoreRemovesStore(t *testing.T) {
	ctx := context.Background()
	root := t.TempDir()
	absPath := RepoDiskPath(root, "alice", "widgets")
	if err := InitStore(ctx, absPath, "alice", "a@b.test"); err != nil {
		t.Fatalf("InitStore: %v", err)
	}

	if err := DeleteStore(ctx, root, absPath); err != nil {
		t.Fatalf("DeleteStore: %v", err)
	}
	if _, err := os.Stat(absPath); !os.IsNotExist(err) {
		t.Fatalf("store dir still present after DeleteStore, stat err = %v", err)
	}
}

func TestDeleteStoreRootEscapeGuard(t *testing.T) {
	ctx := context.Background()
	root := t.TempDir()
	sibling := t.TempDir() // absolute, definitely outside root

	// A canary the guard must protect.
	canary := filepath.Join(sibling, "keep")
	if err := os.WriteFile(canary, []byte("x"), 0o644); err != nil {
		t.Fatal(err)
	}

	cases := map[string]string{
		"sibling dir":       sibling,
		"traversal escape":  filepath.Join(root, "..", filepath.Base(sibling)),
		"the root itself":   root,
		"prefix-not-subdir": root + "-evil",
	}
	for name, target := range cases {
		t.Run(name, func(t *testing.T) {
			if err := DeleteStore(ctx, root, target); err == nil {
				t.Fatalf("DeleteStore(%q) should have been refused", target)
			}
		})
	}

	if _, err := os.Stat(canary); err != nil {
		t.Fatalf("canary disturbed by a refused DeleteStore: %v", err)
	}
}

func TestDeleteStoreRequiresAbsolute(t *testing.T) {
	ctx := context.Background()
	if err := DeleteStore(ctx, "rel/root", "/tmp/x"); err == nil {
		t.Fatalf("DeleteStore accepted a relative root")
	}
	if err := DeleteStore(ctx, "/tmp", "rel/path"); err == nil {
		t.Fatalf("DeleteStore accepted a relative path")
	}
}

func TestNewCacheRejectsNilLookup(t *testing.T) {
	defer func() {
		if recover() == nil {
			t.Fatalf("NewCache(nil) did not panic")
		}
	}()
	NewCache(nil)
}

func TestCacheGetHitAndMemoize(t *testing.T) {
	ctx := context.Background()
	root := t.TempDir()
	absPath := RepoDiskPath(root, "alice", "widgets")
	if err := InitStore(ctx, absPath, "alice", "a@b.test"); err != nil {
		t.Fatalf("InitStore: %v", err)
	}

	var lookups int
	cache := NewCache(func(_ context.Context, owner, name string) (string, error) {
		lookups++
		if owner == "alice" && name == "widgets" {
			return absPath, nil
		}
		return "", os.ErrNotExist
	})
	defer cache.Close()

	cs1, err := cache.Get(ctx, "~alice/widgets", nbfVer)
	if err != nil {
		t.Fatalf("Get (first): %v", err)
	}
	cs2, err := cache.Get(ctx, "alice/widgets", nbfVer)
	if err != nil {
		t.Fatalf("Get (second): %v", err)
	}
	if cs1 != cs2 {
		t.Fatalf("expected memoized store to be identical across calls")
	}
	if lookups != 2 {
		t.Fatalf("expected 2 lookups (memoization keys on disk path, not the call), got %d", lookups)
	}
}

func TestCacheGetMiss(t *testing.T) {
	ctx := context.Background()
	cache := NewCache(func(_ context.Context, owner, name string) (string, error) {
		return "", os.ErrNotExist
	})
	defer cache.Close()

	if _, err := cache.Get(ctx, "~ghost/missing", nbfVer); err == nil {
		t.Fatalf("Get for an unknown repo should error")
	}
}

func TestCacheGetRejectsBadPath(t *testing.T) {
	ctx := context.Background()
	called := false
	cache := NewCache(func(_ context.Context, owner, name string) (string, error) {
		called = true
		return "", nil
	})
	defer cache.Close()

	if _, err := cache.Get(ctx, "no-slash", nbfVer); err == nil {
		t.Fatalf("Get with a malformed path should error before lookup")
	}
	if called {
		t.Fatalf("RepoLookup was consulted for a structurally invalid path")
	}
}

func TestCacheEvictReopens(t *testing.T) {
	ctx := context.Background()
	root := t.TempDir()
	absPath := RepoDiskPath(root, "alice", "widgets")
	if err := InitStore(ctx, absPath, "alice", "a@b.test"); err != nil {
		t.Fatalf("InitStore: %v", err)
	}

	cache := NewCache(func(_ context.Context, owner, name string) (string, error) {
		return absPath, nil
	})
	defer cache.Close()

	cs1, err := cache.Get(ctx, "~alice/widgets", nbfVer)
	if err != nil {
		t.Fatalf("Get (first): %v", err)
	}

	if err := cache.Evict(absPath); err != nil {
		t.Fatalf("Evict: %v", err)
	}
	// Evicting an absent path is a no-op.
	if err := cache.Evict(absPath); err != nil {
		t.Fatalf("Evict (absent) should be a no-op, got %v", err)
	}

	cs2, err := cache.Get(ctx, "~alice/widgets", nbfVer)
	if err != nil {
		t.Fatalf("Get after evict: %v", err)
	}
	if cs1 == cs2 {
		t.Fatalf("expected a freshly opened store after eviction, got the evicted handle")
	}
}