Skip to content

Commit b6891b4

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 381d7d1 commit b6891b4

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ type LightningTerminal struct {
216216
middleware *mid.Manager
217217
middlewareStarted bool
218218

219-
accountsStore *accounts.BoltStore
219+
accountsStore accounts.Store
220220
accountService *accounts.InterceptorService
221221
accountServiceStarted bool
222222

@@ -415,10 +415,14 @@ func (g *LightningTerminal) start(ctx context.Context) error {
415415
)
416416
}
417417

418-
g.accountsStore, err = accounts.NewBoltStore(
419-
filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename,
420-
clock.NewDefaultClock(),
421-
)
418+
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
419+
err = makeDirectories(networkDir)
420+
if err != nil {
421+
return fmt.Errorf("could not create network directory: %v", err)
422+
}
423+
424+
clock := clock.NewDefaultClock()
425+
g.accountsStore, err = NewAccountStore(g.cfg, clock)
422426
if err != nil {
423427
return fmt.Errorf("error creating accounts store: %w", err)
424428
}
@@ -445,10 +449,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
445449
g.ruleMgrs = rules.NewRuleManagerSet()
446450

447451
// Create an instance of the local Terminal Connect session store DB.
448-
networkDir := filepath.Join(g.cfg.LitDir, g.cfg.Network)
449-
g.sessionDB, err = session.NewDB(
450-
networkDir, session.DBFilename, clock.NewDefaultClock(),
451-
)
452+
g.sessionDB, err = session.NewDB(networkDir, session.DBFilename, clock)
452453
if err != nil {
453454
return fmt.Errorf("error creating session DB: %v", err)
454455
}

0 commit comments

Comments
 (0)