34
34
testEntryValue = []byte {1 , 2 , 3 }
35
35
)
36
36
37
+ // expectedResult represents the expected result of a migration test.
38
+ type expectedResult struct {
39
+ kvEntries fn.Option [[]* kvEntry ]
40
+ }
41
+
37
42
// TestFirewallDBMigration tests the migration of firewalldb from a bolt
38
43
// backend to a SQL database. Note that this test does not attempt to be a
39
44
// complete migration test.
@@ -72,10 +77,10 @@ func TestFirewallDBMigration(t *testing.T) {
72
77
return store , genericExecutor
73
78
}
74
79
75
- // The assertMigrationResults function will currently assert that
80
+ // The assertKvStoreMigrationResults function will currently assert that
76
81
// the migrated kv stores entries in the SQLDB match the original kv
77
82
// stores entries in the BoltDB.
78
- assertMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
83
+ assertKvStoreMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
79
84
kvEntries []* kvEntry ) {
80
85
81
86
var (
@@ -213,6 +218,20 @@ func TestFirewallDBMigration(t *testing.T) {
213
218
}
214
219
}
215
220
221
+ // The assertMigrationResults function will currently assert that
222
+ // the migrated kv stores records in the SQLDB match the original kv
223
+ // stores records in the BoltDB.
224
+ assertMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
225
+ expRes * expectedResult ) {
226
+
227
+ // If the expected result contains kv records, then we
228
+ // assert that the kv store migration results match
229
+ // the expected results.
230
+ expRes .kvEntries .WhenSome (func (kvEntries []* kvEntry ) {
231
+ assertKvStoreMigrationResults (t , sqlStore , kvEntries )
232
+ })
233
+ }
234
+
216
235
// The tests slice contains all the tests that we will run for the
217
236
// migration of the firewalldb from a BoltDB to a SQLDB.
218
237
// Note that the tests currently only test the migration of the KV
@@ -221,16 +240,22 @@ func TestFirewallDBMigration(t *testing.T) {
221
240
tests := []struct {
222
241
name string
223
242
populateDB func (t * testing.T , ctx context.Context ,
224
- boltDB * BoltDB , sessionStore session.Store ) [] * kvEntry
243
+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult
225
244
}{
226
245
{
227
246
name : "empty" ,
228
247
populateDB : func (t * testing.T , ctx context.Context ,
229
248
boltDB * BoltDB ,
230
- sessionStore session.Store ) []* kvEntry {
249
+ sessionStore session.Store ) * expectedResult {
250
+
251
+ // Don't populate the DB, and return empty kv
252
+ // records and privacy pairs.
231
253
232
- // Don't populate the DB.
233
- return make ([]* kvEntry , 0 )
254
+ return & expectedResult {
255
+ kvEntries : fn .Some (
256
+ []* kvEntry {},
257
+ ),
258
+ }
234
259
},
235
260
},
236
261
{
@@ -315,7 +340,7 @@ func TestFirewallDBMigration(t *testing.T) {
315
340
// globalEntries populates the kv store with one global entry for the temp
316
341
// store, and one for the perm store.
317
342
func globalEntries (t * testing.T , ctx context.Context , boltDB * BoltDB ,
318
- _ session.Store ) [] * kvEntry {
343
+ _ session.Store ) * expectedResult {
319
344
320
345
return insertTempAndPermEntry (
321
346
t , ctx , boltDB , testRuleName , fn .None [[]byte ](),
@@ -327,7 +352,7 @@ func globalEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
327
352
// entry for the local temp store, and one session specific entry for the perm
328
353
// local store.
329
354
func sessionSpecificEntries (t * testing.T , ctx context.Context , boltDB * BoltDB ,
330
- sessionStore session.Store ) [] * kvEntry {
355
+ sessionStore session.Store ) * expectedResult {
331
356
332
357
groupAlias := getNewSessionAlias (t , ctx , sessionStore )
333
358
@@ -341,7 +366,7 @@ func sessionSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
341
366
// entry for the local temp store, and one feature specific entry for the perm
342
367
// local store.
343
368
func featureSpecificEntries (t * testing.T , ctx context.Context , boltDB * BoltDB ,
344
- sessionStore session.Store ) [] * kvEntry {
369
+ sessionStore session.Store ) * expectedResult {
345
370
346
371
groupAlias := getNewSessionAlias (t , ctx , sessionStore )
347
372
@@ -359,11 +384,11 @@ func featureSpecificEntries(t *testing.T, ctx context.Context, boltDB *BoltDB,
359
384
// any entries when the entry set is more complex than just a single entry at
360
385
// each level.
361
386
func allEntryCombinations (t * testing.T , ctx context.Context , boltDB * BoltDB ,
362
- sessionStore session.Store ) [] * kvEntry {
387
+ sessionStore session.Store ) * expectedResult {
363
388
364
389
var result []* kvEntry
365
- add := func (entry [] * kvEntry ) {
366
- result = append (result , entry ... )
390
+ add := func (entry * expectedResult ) {
391
+ result = append (result , entry .kvEntries . UnwrapOrFail ( t ) . .. )
367
392
}
368
393
369
394
// First lets create standard entries at all levels, which represents
@@ -444,7 +469,9 @@ func allEntryCombinations(t *testing.T, ctx context.Context, boltDB *BoltDB,
444
469
fn .Some (testFeatureName ), testEntryKey4 , emptyValue ,
445
470
))
446
471
447
- return result
472
+ return & expectedResult {
473
+ kvEntries : fn .Some (result ),
474
+ }
448
475
}
449
476
450
477
func getNewSessionAlias (t * testing.T , ctx context.Context ,
@@ -465,7 +492,7 @@ func getNewSessionAlias(t *testing.T, ctx context.Context,
465
492
func insertTempAndPermEntry (t * testing.T , ctx context.Context ,
466
493
boltDB * BoltDB , ruleName string , groupAlias fn.Option [[]byte ],
467
494
featureNameOpt fn.Option [string ], entryKey string ,
468
- entryValue []byte ) [] * kvEntry {
495
+ entryValue []byte ) * expectedResult {
469
496
470
497
tempKvEntry := & kvEntry {
471
498
ruleName : ruleName ,
@@ -489,7 +516,9 @@ func insertTempAndPermEntry(t *testing.T, ctx context.Context,
489
516
490
517
insertKvEntry (t , ctx , boltDB , permKvEntry )
491
518
492
- return []* kvEntry {tempKvEntry , permKvEntry }
519
+ return & expectedResult {
520
+ kvEntries : fn .Some ([]* kvEntry {tempKvEntry , permKvEntry }),
521
+ }
493
522
}
494
523
495
524
// insertKvEntry populates the kv store with passed entry, and asserts that the
@@ -538,7 +567,7 @@ func insertKvEntry(t *testing.T, ctx context.Context,
538
567
// across all possible combinations of different levels of entries in the kv
539
568
// store. All values and different bucket names are randomly generated.
540
569
func randomKVEntries (t * testing.T , ctx context.Context ,
541
- boltDB * BoltDB , sessionStore session.Store ) [] * kvEntry {
570
+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
542
571
543
572
var (
544
573
// We set the number of entries to insert to 1000, as that
@@ -649,7 +678,9 @@ func randomKVEntries(t *testing.T, ctx context.Context,
649
678
insertedEntries = append (insertedEntries , entry )
650
679
}
651
680
652
- return insertedEntries
681
+ return & expectedResult {
682
+ kvEntries : fn .Some (insertedEntries ),
683
+ }
653
684
}
654
685
655
686
// randomString generates a random string of the passed length n.
0 commit comments