-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathcheckpoint_test.go
More file actions
323 lines (249 loc) · 9.19 KB
/
Copy pathcheckpoint_test.go
File metadata and controls
323 lines (249 loc) · 9.19 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package storage
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs"
dbAPI "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/pathbadger"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/node"
)
const testVersion uint64 = 10
var testNs common.Namespace = common.NewTestNamespaceFromSeed([]byte("test namespace"), 0)
func TestCreateCheckpoints(t *testing.T) {
t.Run("fails on non-empty output dir", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
ndb, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer ndb.Close()
stateRoot := commitRoot(ctx, t, ndb, ns, testVersion, node.RootTypeState, map[string]string{
"key": "value",
})
require.NoError(t, ndb.Finalize([]node.Root{stateRoot}))
outDir := filepath.Join(t.TempDir(), "checkpoints")
require.NoError(t, os.Mkdir(outDir, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(outDir, "existing"), []byte{}, 0o600))
err = createCheckpoints(ctx, ndb, ns, testVersion, outDir)
require.ErrorContains(t, err, "output directory is not empty")
})
t.Run("fails on empty node DB", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
ndb, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer ndb.Close()
cpDir := filepath.Join(t.TempDir(), "checkpoint")
err = createCheckpoints(ctx, ndb, ns, testVersion, cpDir)
require.ErrorContains(t, err, "empty state DB")
})
t.Run("fails when version is before earliest", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
ndb, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer ndb.Close()
stateRoot := commitRoot(ctx, t, ndb, ns, testVersion, node.RootTypeState, map[string]string{
"key": "value",
})
require.NoError(t, ndb.Finalize([]node.Root{stateRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
err = createCheckpoints(ctx, ndb, ns, testVersion-1, cpDir)
require.ErrorContains(t, err, "version not found")
})
t.Run("fails when version is after latest", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
ndb, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer ndb.Close()
stateRoot := commitRoot(ctx, t, ndb, ns, testVersion, node.RootTypeState, map[string]string{
"key": "value",
})
require.NoError(t, ndb.Finalize([]node.Root{stateRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
err = createCheckpoints(ctx, ndb, ns, testVersion+1, cpDir)
require.ErrorContains(t, err, "version not found")
})
}
func TestImportCheckpoints(t *testing.T) {
t.Run("fails on non-empty destination DB", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
srcNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer srcNDB.Close()
stateRoot := commitRoot(ctx, t, srcNDB, ns, testVersion, node.RootTypeState, map[string]string{
"key": "value",
})
require.NoError(t, srcNDB.Finalize([]node.Root{stateRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
require.NoError(t, createCheckpoints(ctx, srcNDB, ns, testVersion, cpDir))
dstNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer dstNDB.Close()
// NodeDB allows importing checkpoint even if the target NodeDB is not empty,
// as long as the checkpoint is younger than the the last finalized version.
// We don't want that, thus finalizing testVersion-1 to ensure createCheckpoints
// guard is tested and that the test does not rely on mkvs version already finalized.
err = dstNDB.Finalize([]node.Root{emptyRoot(ns, testVersion-1, node.RootTypeState)})
require.NoError(t, err)
err = importCheckpoints(ctx, dstNDB, ns, cpDir)
require.ErrorContains(t, err, "state db not empty")
})
t.Run("fails on empty input dir", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
ndb, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer ndb.Close()
err = importCheckpoints(ctx, ndb, ns, t.TempDir())
require.ErrorContains(t, err, "input directory is empty")
})
}
func TestCreateImportRoundtrip(t *testing.T) {
t.Run("single non-empty checkpoint", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
srcNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer srcNDB.Close()
stateRoot := commitRoot(ctx, t, srcNDB, ns, testVersion, node.RootTypeState, map[string]string{
"key": "value",
})
require.NoError(t, srcNDB.Finalize([]node.Root{stateRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
require.NoError(t, createCheckpoints(ctx, srcNDB, ns, testVersion, cpDir))
dstNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer dstNDB.Close()
require.NoError(t, importCheckpoints(ctx, dstNDB, ns, cpDir))
roots, err := dstNDB.GetRootsForVersion(testVersion)
require.NoError(t, err)
require.Equal(t, []node.Root{stateRoot}, roots)
require.True(t, dstNDB.HasRoot(stateRoot))
})
t.Run("empty root is imported", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
ns := common.Namespace{}
stateRoot := emptyRoot(ns, testVersion, node.RootTypeState)
srcNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer srcNDB.Close()
require.NoError(t, srcNDB.Finalize([]node.Root{stateRoot}))
latest, ok := srcNDB.GetLatestVersion()
require.True(t, ok)
require.EqualValues(t, testVersion, latest)
// Empty roots are implicit yet GetRootForVersion does not return them.
roots, err := srcNDB.GetRootsForVersion(testVersion)
require.NoError(t, err)
require.Empty(t, roots)
require.True(t, srcNDB.HasRoot(stateRoot))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
require.NoError(t, createCheckpoints(ctx, srcNDB, ns, testVersion, cpDir))
dstNDB, err := newTestNodeDB(t, ns)
require.NoError(t, err)
defer dstNDB.Close()
require.NoError(t, importCheckpoints(ctx, dstNDB, ns, cpDir))
latest, ok = dstNDB.GetLatestVersion()
require.True(t, ok)
require.EqualValues(t, testVersion, latest)
roots, err = dstNDB.GetRootsForVersion(testVersion)
require.NoError(t, err)
// Via checkpoint roundtrip, empty state can be materialized as an explicit root,
// which technically makes checkpoint restoration not 1:1 match. Same issue would
// happen with checkpointer if this case would be ever triggered.
require.Contains(t, roots, stateRoot)
require.True(t, dstNDB.HasRoot(stateRoot))
})
t.Run("two non-empty roots (state and io)", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
srcNDB, err := newTestNodeDB(t, testNs)
require.NoError(t, err)
defer srcNDB.Close()
stateRoot := commitRoot(ctx, t, srcNDB, testNs, testVersion, node.RootTypeState, map[string]string{
"state-key": "state-value",
})
ioRoot := commitRoot(ctx, t, srcNDB, testNs, testVersion, node.RootTypeIO, map[string]string{
"io-key": "io-value",
})
require.NoError(t, srcNDB.Finalize([]node.Root{stateRoot, ioRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
require.NoError(t, createCheckpoints(ctx, srcNDB, testNs, testVersion, cpDir))
dstNDB, err := newTestNodeDB(t, testNs)
require.NoError(t, err)
defer dstNDB.Close()
require.NoError(t, importCheckpoints(ctx, dstNDB, testNs, cpDir))
roots, err := dstNDB.GetRootsForVersion(testVersion)
require.NoError(t, err)
require.ElementsMatch(t, []node.Root{stateRoot, ioRoot}, roots)
})
t.Run("non-empty state root and empty io root", func(t *testing.T) {
t.Parallel()
ctx := t.Context()
srcNDB, err := newTestNodeDB(t, testNs)
require.NoError(t, err)
defer srcNDB.Close()
stateRoot := commitRoot(ctx, t, srcNDB, testNs, testVersion, node.RootTypeState, map[string]string{
"state-key": "state-value",
})
ioRoot := emptyRoot(testNs, testVersion, node.RootTypeIO)
require.NoError(t, srcNDB.Finalize([]node.Root{stateRoot, ioRoot}))
cpDir := filepath.Join(t.TempDir(), "checkpoint")
require.NoError(t, createCheckpoints(ctx, srcNDB, testNs, testVersion, cpDir))
dstNDB, err := newTestNodeDB(t, testNs)
require.NoError(t, err)
defer dstNDB.Close()
require.NoError(t, importCheckpoints(ctx, dstNDB, testNs, cpDir))
roots, err := dstNDB.GetRootsForVersion(testVersion)
require.NoError(t, err)
require.Contains(t, roots, stateRoot)
require.True(t, dstNDB.HasRoot(stateRoot))
require.True(t, dstNDB.HasRoot(ioRoot))
})
}
func newTestNodeDB(t *testing.T, ns common.Namespace) (dbAPI.NodeDB, error) {
t.Helper()
return pathbadger.New(&dbAPI.Config{
DB: filepath.Join(t.TempDir()),
Namespace: ns,
MemoryOnly: true,
})
}
func commitRoot(
ctx context.Context,
t *testing.T,
ndb dbAPI.NodeDB,
ns common.Namespace,
version uint64,
rootType node.RootType,
data map[string]string,
) node.Root {
t.Helper()
tree := mkvs.New(nil, ndb, rootType)
defer tree.Close()
for k, v := range data {
err := tree.Insert(ctx, []byte(k), []byte(v))
require.NoError(t, err)
}
_, rootHash, err := tree.Commit(ctx, ns, version)
require.NoError(t, err)
return node.Root{
Namespace: ns,
Version: version,
Type: rootType,
Hash: rootHash,
}
}