-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate_test.go
More file actions
379 lines (339 loc) · 8.14 KB
/
migrate_test.go
File metadata and controls
379 lines (339 loc) · 8.14 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package seiconfig
import (
"strings"
"testing"
)
func TestMigrationRegistry_Empty(t *testing.T) {
r, err := NewMigrationRegistry()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if r.MaxVersion() != 0 {
t.Errorf("empty registry max version: got %d, want 0", r.MaxVersion())
}
}
func TestMigrationRegistry_SingleMigration(t *testing.T) {
r, err := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "test migration",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if r.MaxVersion() != 2 {
t.Errorf("max version: got %d, want 2", r.MaxVersion())
}
}
func TestMigrationRegistry_ChainedMigrations(t *testing.T) {
r, err := NewMigrationRegistry(
Migration{
FromVersion: 1,
ToVersion: 2,
Description: "v1 to v2",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
},
Migration{
FromVersion: 2,
ToVersion: 3,
Description: "v2 to v3",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 3
return nil
},
},
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if r.MaxVersion() != 3 {
t.Errorf("max version: got %d, want 3", r.MaxVersion())
}
}
func TestMigrationRegistry_RejectsBadToVersion(t *testing.T) {
_, err := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 3, // should be 2
Description: "bad",
Migrate: func(cfg *SeiConfig) error { return nil },
})
if err == nil {
t.Fatal("expected error for non-sequential ToVersion")
}
}
func TestMigrationRegistry_RejectsDuplicate(t *testing.T) {
_, err := NewMigrationRegistry(
Migration{
FromVersion: 1,
ToVersion: 2,
Description: "first",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
},
Migration{
FromVersion: 1,
ToVersion: 2,
Description: "duplicate",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
},
)
if err == nil {
t.Fatal("expected error for duplicate FromVersion")
}
}
func TestMigrationRegistry_RejectsGap(t *testing.T) {
_, err := NewMigrationRegistry(
Migration{
FromVersion: 1,
ToVersion: 2,
Description: "v1 to v2",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
},
Migration{
FromVersion: 3, // gap: missing v2->v3
ToVersion: 4,
Description: "v3 to v4",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 4
return nil
},
},
)
if err == nil {
t.Fatal("expected error for chain gap")
}
}
func TestMigrateConfig_NoOp(t *testing.T) {
r, _ := NewMigrationRegistry()
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
result, err := r.MigrateConfig(cfg, 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Applied) != 0 {
t.Errorf("expected no applied migrations, got %d", len(result.Applied))
}
}
func TestMigrateConfig_SingleStep(t *testing.T) {
r, _ := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "add new default",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
})
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
result, err := r.MigrateConfig(cfg, 2)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Version != 2 {
t.Errorf("config version: got %d, want 2", cfg.Version)
}
if len(result.Applied) != 1 {
t.Fatalf("applied migrations: got %d, want 1", len(result.Applied))
}
if result.Applied[0].Description != "add new default" {
t.Errorf("description: got %q", result.Applied[0].Description)
}
if result.FromVersion != 1 || result.ToVersion != 2 {
t.Errorf("result: from=%d to=%d", result.FromVersion, result.ToVersion)
}
}
func TestMigrateConfig_MultiStep(t *testing.T) {
r, _ := NewMigrationRegistry(
Migration{
FromVersion: 1,
ToVersion: 2,
Description: "step 1",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
},
Migration{
FromVersion: 2,
ToVersion: 3,
Description: "step 2",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 3
return nil
},
},
Migration{
FromVersion: 3,
ToVersion: 4,
Description: "step 3",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 4
return nil
},
},
)
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
result, err := r.MigrateConfig(cfg, 4)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Version != 4 {
t.Errorf("config version: got %d, want 4", cfg.Version)
}
if len(result.Applied) != 3 {
t.Errorf("applied migrations: got %d, want 3", len(result.Applied))
}
}
func TestMigrateConfig_RejectsDowngrade(t *testing.T) {
r, _ := NewMigrationRegistry()
cfg := Default()
cfg.Version = 3
cfg.Mode = ModeValidator
_, err := r.MigrateConfig(cfg, 2)
if err == nil {
t.Fatal("expected error for downgrade")
}
if !strings.Contains(err.Error(), "downgrade") {
t.Errorf("error should mention downgrade: %v", err)
}
}
func TestMigrateConfig_MissingMigration(t *testing.T) {
r, _ := NewMigrationRegistry()
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
_, err := r.MigrateConfig(cfg, 2)
if err == nil {
t.Fatal("expected error for missing migration")
}
}
func TestMigrateConfig_MigrationFuncError(t *testing.T) {
r, _ := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "fails",
Migrate: func(_ *SeiConfig) error {
return &migrationTestError{}
},
})
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
_, err := r.MigrateConfig(cfg, 2)
if err == nil {
t.Fatal("expected error from failing migration")
}
}
type migrationTestError struct{}
func (e *migrationTestError) Error() string { return "intentional test error" }
func TestMigrateConfig_ValidationAfterMigration(t *testing.T) {
r, _ := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "sets invalid mode",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
cfg.Mode = "bogus"
return nil
},
})
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
_, err := r.MigrateConfig(cfg, 2)
if err == nil {
t.Fatal("expected validation error after migration produces invalid config")
}
if !strings.Contains(err.Error(), "validation") {
t.Errorf("error should mention validation: %v", err)
}
}
func TestMigrateConfig_VersionNotUpdated(t *testing.T) {
r, _ := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "forgets to update version",
Migrate: func(_ *SeiConfig) error {
// intentionally does not set cfg.Version = 2
return nil
},
})
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
_, err := r.MigrateConfig(cfg, 2)
if err == nil {
t.Fatal("expected error when migration doesn't update version")
}
if !strings.Contains(err.Error(), "did not update") {
t.Errorf("error should mention version not updated: %v", err)
}
}
func TestMigrateConfig_PreservesFieldValues(t *testing.T) {
r, _ := NewMigrationRegistry(Migration{
FromVersion: 1,
ToVersion: 2,
Description: "schema-only change",
Migrate: func(cfg *SeiConfig) error {
cfg.Version = 2
return nil
},
})
cfg := Default()
cfg.Version = 1
cfg.Mode = ModeValidator
cfg.Chain.Moniker = "my-custom-node"
cfg.EVM.HTTPPort = 9999
result, err := r.MigrateConfig(cfg, 2)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Chain.Moniker != "my-custom-node" {
t.Errorf("moniker was lost: got %q", cfg.Chain.Moniker)
}
if cfg.EVM.HTTPPort != 9999 {
t.Errorf("evm port was lost: got %d", cfg.EVM.HTTPPort)
}
if result.ToVersion != 2 {
t.Errorf("result ToVersion: got %d", result.ToVersion)
}
}
func TestNeedsUpgrade(t *testing.T) {
r, _ := NewMigrationRegistry()
if !r.NeedsUpgrade(1, 2) {
t.Error("1 → 2 should need upgrade")
}
if r.NeedsUpgrade(2, 2) {
t.Error("2 → 2 should not need upgrade")
}
if r.NeedsUpgrade(3, 2) {
t.Error("3 → 2 should not need upgrade")
}
}
func TestDefaultMigrations_Valid(t *testing.T) {
_, err := NewMigrationRegistry(DefaultMigrations()...)
if err != nil {
t.Fatalf("DefaultMigrations failed to register: %v", err)
}
}