Skip to content

Commit 386fb4a

Browse files
committed
itest: run itests against chosen backend
Add the ability to specify the db backend to run use for accounts when running itests. With this commit, you can run something like: `make itest dbbackend=sqlite`.
1 parent e2b8e8b commit 386fb4a

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

itest/litd_node.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/hex"
99
"encoding/json"
1010
"errors"
11+
"flag"
1112
"fmt"
1213
"io"
1314
"io/ioutil"
@@ -25,6 +26,7 @@ import (
2526
"github.com/btcsuite/btcd/wire"
2627
"github.com/lightninglabs/faraday/frdrpc"
2728
terminal "github.com/lightninglabs/lightning-terminal"
29+
"github.com/lightninglabs/lightning-terminal/db"
2830
"github.com/lightninglabs/lightning-terminal/litrpc"
2931
"github.com/lightninglabs/lightning-terminal/subservers"
3032
"github.com/lightninglabs/loop/looprpc"
@@ -60,7 +62,12 @@ var (
6062
numActiveNodes = 0
6163
numActiveNodesMtx sync.Mutex
6264

63-
defaultLndPassphrase = []byte("default-wallet-password")
65+
// litDBBackend is a command line flag for specifying the database
66+
// backend to use when starting a LiT daemon.
67+
litDBBackend = flag.String(
68+
"litdbbackend", terminal.DatabaseBackendBbolt, "Set the "+
69+
"database backend to use when starting a LiT daemon.",
70+
)
6471
)
6572

6673
type LitNodeConfig struct {
@@ -80,6 +87,9 @@ type LitNodeConfig struct {
8087
LitTLSCertPath string
8188
LitMacPath string
8289

90+
DBBackend string
91+
PostgresConfig *db.PostgresConfig
92+
8393
UIPassword string
8494
LitDir string
8595
FaradayDir string
@@ -220,8 +230,20 @@ func (cfg *LitNodeConfig) defaultLitdArgs() *litArgs {
220230
"restcors": "*",
221231
"lnd.debuglevel": "trace,GRPC=error,PEER=info",
222232
"lndconnectinterval": "200ms",
233+
"databasebackend": cfg.DBBackend,
223234
}
224235
)
236+
237+
if cfg.DBBackend == terminal.DatabaseBackendPostgres {
238+
args["postgres.host"] = cfg.PostgresConfig.Host
239+
args["postgres.port"] = fmt.Sprintf(
240+
"%d", cfg.PostgresConfig.Port,
241+
)
242+
args["postgres.user"] = cfg.PostgresConfig.User
243+
args["postgres.password"] = cfg.PostgresConfig.Password
244+
args["postgres.dbname"] = cfg.PostgresConfig.DBName
245+
}
246+
225247
for _, arg := range cfg.LitArgs {
226248
parts := strings.Split(arg, "=")
227249
option := strings.TrimLeft(parts[0], "--")
@@ -417,6 +439,28 @@ func NewNode(t *testing.T, cfg *LitNodeConfig,
417439
cfg.LitTLSCertPath = filepath.Join(cfg.LitDir, "tls.cert")
418440
cfg.GenerateListeningPorts()
419441

442+
// Decide which DB backend to use.
443+
switch *litDBBackend {
444+
case terminal.DatabaseBackendSqlite:
445+
cfg.DBBackend = terminal.DatabaseBackendSqlite
446+
447+
case terminal.DatabaseBackendPostgres:
448+
fixture := db.NewTestPgFixture(
449+
t, db.DefaultPostgresFixtureLifetime, true,
450+
)
451+
t.Cleanup(func() {
452+
fixture.TearDown(t)
453+
})
454+
455+
cfg.DBBackend = terminal.DatabaseBackendPostgres
456+
cfg.PostgresConfig = fixture.GetConfig()
457+
458+
default:
459+
cfg.DBBackend = terminal.DatabaseBackendBbolt
460+
}
461+
462+
t.Logf("Using %v database backend", cfg.DBBackend)
463+
420464
// Generate a random UI password by reading 16 random bytes and base64
421465
// encoding them.
422466
var randomBytes [16]byte

make/testing_flags.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
include make/compile_flags.mk
22

3-
ITEST_FLAGS =
43
TEST_FLAGS =
54
DEV_TAGS = dev
65

@@ -9,6 +8,11 @@ ifneq ($(icase),)
98
ITEST_FLAGS += -test.run="TestLightningTerminal/$(icase)"
109
endif
1110

11+
# Run itests with specified db backend.
12+
ifneq ($(dbbackend),)
13+
ITEST_FLAGS += -litdbbackend=$(dbbackend)
14+
endif
15+
1216
# If a specific unit test case is being targeted, construct test.run filter.
1317
ifneq ($(case),)
1418
TEST_FLAGS += -test.run=$(case)

terminal.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/lightningnetwork/lnd"
3939
"github.com/lightningnetwork/lnd/build"
4040
"github.com/lightningnetwork/lnd/chainreg"
41-
"github.com/lightningnetwork/lnd/clock"
4241
"github.com/lightningnetwork/lnd/fn"
4342
"github.com/lightningnetwork/lnd/funding"
4443
"github.com/lightningnetwork/lnd/htlcswitch"
@@ -215,7 +214,7 @@ type LightningTerminal struct {
215214
middleware *mid.Manager
216215
middlewareStarted bool
217216

218-
accountsStore *accounts.BoltStore
217+
accountsStore accounts.Store
219218
accountService *accounts.InterceptorService
220219
accountServiceStarted bool
221220

@@ -414,10 +413,13 @@ func (g *LightningTerminal) start(ctx context.Context) error {
414413
)
415414
}
416415

417-
g.accountsStore, err = accounts.NewBoltStore(
418-
filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename,
419-
clock.NewDefaultClock(),
420-
)
416+
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
417+
err = makeDirectories(networkDir)
418+
if err != nil {
419+
return fmt.Errorf("could not create network directory: %v", err)
420+
}
421+
422+
g.accountsStore, err = NewAccountStore(g.cfg)
421423
if err != nil {
422424
return fmt.Errorf("error creating accounts store: %w", err)
423425
}
@@ -445,7 +447,6 @@ func (g *LightningTerminal) start(ctx context.Context) error {
445447
g.ruleMgrs = rules.NewRuleManagerSet()
446448

447449
// Create an instance of the local Terminal Connect session store DB.
448-
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
449450
g.sessionDB, err = session.NewDB(networkDir, session.DBFilename)
450451
if err != nil {
451452
return fmt.Errorf("error creating session DB: %v", err)

0 commit comments

Comments
 (0)