44 "context"
55 "database/sql"
66 "fmt"
7- "github.com/lightningnetwork/lnd/fn"
87 "testing"
98 "time"
109
@@ -13,6 +12,7 @@ import (
1312 "github.com/lightninglabs/lightning-terminal/db/sqlc"
1413 "github.com/lightninglabs/lightning-terminal/session"
1514 "github.com/lightningnetwork/lnd/clock"
15+ "github.com/lightningnetwork/lnd/fn"
1616 "github.com/lightningnetwork/lnd/sqldb"
1717 "github.com/stretchr/testify/require"
1818 "golang.org/x/exp/rand"
@@ -29,6 +29,10 @@ type kvStoreRecord struct {
2929 Value []byte
3030}
3131
32+ type expectedResult struct {
33+ kvRecords fn.Option [[]kvStoreRecord ]
34+ }
35+
3236// TestFirewallDBMigration tests the migration of firewalldb from a bolt
3337// backed to a SQL database. Note that this test does not attempt to be a
3438// complete migration test.
@@ -67,10 +71,7 @@ func TestFirewallDBMigration(t *testing.T) {
6771 return store , genericExecutor
6872 }
6973
70- // The assertMigrationResults function will currently assert that
71- // the migrated kv stores records in the SQLDB match the original kv
72- // stores records in the BoltDB.
73- assertMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
74+ assertKvStoreMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
7475 kvRecords []kvStoreRecord ) {
7576
7677 var (
@@ -188,6 +189,20 @@ func TestFirewallDBMigration(t *testing.T) {
188189 }
189190 }
190191
192+ // The assertMigrationResults function will currently assert that
193+ // the migrated kv stores records in the SQLDB match the original kv
194+ // stores records in the BoltDB.
195+ assertMigrationResults := func (t * testing.T , sqlStore * SQLDB ,
196+ expRes * expectedResult ) {
197+
198+ // If the expected result contains kv records, then we
199+ // assert that the kv store migration results match
200+ // the expected results.
201+ expRes .kvRecords .WhenSome (func (kvRecords []kvStoreRecord ) {
202+ assertKvStoreMigrationResults (t , sqlStore , kvRecords )
203+ })
204+ }
205+
191206 // The tests slice contains all the tests that we will run for the
192207 // migration of the firewalldb from a BoltDB to a SQLDB.
193208 // Note that the tests currently only test the migration of the KV
@@ -197,16 +212,22 @@ func TestFirewallDBMigration(t *testing.T) {
197212 name string
198213 populateDB func (t * testing.T , ctx context.Context ,
199214 boltDB * BoltDB ,
200- sessionStore session.Store ) [] kvStoreRecord
215+ sessionStore session.Store ) * expectedResult
201216 }{
202217 {
203218 name : "empty" ,
204219 populateDB : func (t * testing.T , ctx context.Context ,
205220 boltDB * BoltDB ,
206- sessionStore session.Store ) []kvStoreRecord {
221+ sessionStore session.Store ) * expectedResult {
222+
223+ // Don't populate the DB, and return empty kv
224+ // records and privacy pairs.
207225
208- // Don't populate the DB.
209- return make ([]kvStoreRecord , 0 )
226+ return & expectedResult {
227+ kvRecords : fn .Some (
228+ []kvStoreRecord {},
229+ ),
230+ }
210231 },
211232 },
212233 {
@@ -291,7 +312,7 @@ func TestFirewallDBMigration(t *testing.T) {
291312// globalRecords populates the kv store with one global record for the temp
292313// store, and one for the perm store.
293314func globalRecords (t * testing.T , ctx context.Context ,
294- boltDB * BoltDB , sessionStore session.Store ) [] kvStoreRecord {
315+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
295316
296317 return insertTestKVRecords (
297318 t , ctx , boltDB , sessionStore , true , fn .None [string ](),
@@ -302,7 +323,7 @@ func globalRecords(t *testing.T, ctx context.Context,
302323// record for the local temp store, and one session specific record for the perm
303324// local store.
304325func sessionSpecificRecords (t * testing.T , ctx context.Context ,
305- boltDB * BoltDB , sessionStore session.Store ) [] kvStoreRecord {
326+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
306327
307328 return insertTestKVRecords (
308329 t , ctx , boltDB , sessionStore , false , fn .None [string ](),
@@ -313,7 +334,7 @@ func sessionSpecificRecords(t *testing.T, ctx context.Context,
313334// record for the local temp store, and one feature specific record for the perm
314335// local store.
315336func featureSpecificRecords (t * testing.T , ctx context.Context ,
316- boltDB * BoltDB , sessionStore session.Store ) [] kvStoreRecord {
337+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
317338
318339 return insertTestKVRecords (
319340 t , ctx , boltDB , sessionStore , false , fn .Some ("test-feature" ),
@@ -324,13 +345,25 @@ func featureSpecificRecords(t *testing.T, ctx context.Context,
324345// by utilizing all the other helper functions that populates the kvstores at
325346// different levels.
326347func recordsAtAllLevels (t * testing.T , ctx context.Context ,
327- boltDB * BoltDB , sessionStore session.Store ) []kvStoreRecord {
348+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
349+
350+ gRecords := globalRecords (
351+ t , ctx , boltDB , sessionStore ,
352+ ).kvRecords .UnwrapOrFail (t )
328353
329- gRecords := globalRecords ( t , ctx , boltDB , sessionStore )
330- sRecords := sessionSpecificRecords ( t , ctx , boltDB , sessionStore )
331- fRecords := featureSpecificRecords ( t , ctx , boltDB , sessionStore )
354+ sRecords := sessionSpecificRecords (
355+ t , ctx , boltDB , sessionStore ,
356+ ). kvRecords . UnwrapOrFail ( t )
332357
333- return append (gRecords , append (sRecords , fRecords ... )... )
358+ fRecords := featureSpecificRecords (
359+ t , ctx , boltDB , sessionStore ,
360+ ).kvRecords .UnwrapOrFail (t )
361+
362+ allRecords := append (gRecords , append (sRecords , fRecords ... )... )
363+
364+ return & expectedResult {
365+ kvRecords : fn .Some (allRecords ),
366+ }
334367}
335368
336369// insertTestKVRecords populates the kv store with one record for the local temp
@@ -341,7 +374,7 @@ func recordsAtAllLevels(t *testing.T, ctx context.Context,
341374// ruleName, entryKey and entryVal.
342375func insertTestKVRecords (t * testing.T , ctx context.Context ,
343376 boltDB * BoltDB , sessionStore session.Store , global bool ,
344- featureNameOpt fn.Option [string ]) [] kvStoreRecord {
377+ featureNameOpt fn.Option [string ]) * expectedResult {
345378
346379 var (
347380 ruleName = "test-rule"
@@ -380,7 +413,9 @@ func insertTestKVRecords(t *testing.T, ctx context.Context,
380413
381414 insertKvRecord (t , ctx , boltDB , permKvRecord )
382415
383- return []kvStoreRecord {tempKvRecord , permKvRecord }
416+ return & expectedResult {
417+ kvRecords : fn .Some ([]kvStoreRecord {tempKvRecord , permKvRecord }),
418+ }
384419}
385420
386421// insertTestKVRecords populates the kv store with passed record, and asserts
@@ -431,7 +466,7 @@ func insertKvRecord(t *testing.T, ctx context.Context,
431466// across all possible combinations of different levels of records in the kv
432467// store. All values and different bucket names are randomly generated.
433468func randomKVRecords (t * testing.T , ctx context.Context ,
434- boltDB * BoltDB , sessionStore session.Store ) [] kvStoreRecord {
469+ boltDB * BoltDB , sessionStore session.Store ) * expectedResult {
435470
436471 var (
437472 // We set the number of records to insert to 1000, as that
@@ -522,7 +557,9 @@ func randomKVRecords(t *testing.T, ctx context.Context,
522557 insertedRecords = append (insertedRecords , kvEntry )
523558 }
524559
525- return insertedRecords
560+ return & expectedResult {
561+ kvRecords : fn .Some (insertedRecords ),
562+ }
526563}
527564
528565// randomString generates a random string of the passed length n.
0 commit comments