Skip to content

Commit 09df589

Browse files
authored
Merge pull request #9947 from yyforyongyu/export-itest-db
lntest: add a flag to export data generated during the test
2 parents 5ebb293 + 3b84088 commit 09df589

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

lntest/node/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10+
"time"
1011

1112
"github.com/btcsuite/btcd/chaincfg"
1213
"github.com/btcsuite/btcd/integration/rpctest"
@@ -28,6 +29,10 @@ const (
2829
)
2930

3031
var (
32+
// baseDirFlag is the default directory where all the node's data are
33+
// saved. If not set, a temporary dir will be created.
34+
baseDirFlag = flag.String("basedir", "", "default dir to save data")
35+
3136
// logOutput is a flag that can be set to append the output from the
3237
// seed nodes to log files.
3338
logOutput = flag.Bool("logoutput", false,
@@ -162,6 +167,11 @@ type BaseNodeConfig struct {
162167
// compiled with all required itest flags.
163168
LndBinary string
164169

170+
// SkipCleanup specifies whether the harness will remove the base dir or
171+
// not when the test finishes. When using customized BaseDir, the
172+
// cleanup will be skipped.
173+
SkipCleanup bool
174+
165175
// backupDBDir is the path where a database backup is stored, if any.
166176
backupDBDir string
167177

@@ -223,6 +233,38 @@ func (cfg *BaseNodeConfig) BaseConfig() *BaseNodeConfig {
223233
return cfg
224234
}
225235

236+
// GenBaseDir creates a base dir that's used for the test.
237+
func (cfg *BaseNodeConfig) GenBaseDir() error {
238+
// Exit early if the BaseDir is already set.
239+
if cfg.BaseDir != "" {
240+
return nil
241+
}
242+
243+
dirBaseName := fmt.Sprintf("itest-%v-%v-%v-%v", cfg.LogFilenamePrefix,
244+
cfg.Name, cfg.NodeID, time.Now().Unix())
245+
246+
// Create a temporary directory for the node's data and logs. Use dash
247+
// suffix as a separator between base name and node ID.
248+
if *baseDirFlag == "" {
249+
var err error
250+
251+
cfg.BaseDir, err = os.MkdirTemp("", dirBaseName)
252+
253+
return err
254+
}
255+
256+
// Create the customized base dir.
257+
if err := os.MkdirAll(*baseDirFlag, 0700); err != nil {
258+
return err
259+
}
260+
261+
// Use customized base dir and skip the cleanups.
262+
cfg.BaseDir = filepath.Join(*baseDirFlag, dirBaseName)
263+
cfg.SkipCleanup = true
264+
265+
return nil
266+
}
267+
226268
// GenArgs generates a slice of command line arguments from the lightning node
227269
// config struct.
228270
func (cfg *BaseNodeConfig) GenArgs() []string {

lntest/node/harness_node.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,10 @@ type HarnessNode struct {
9393
// NewHarnessNode creates a new test lightning node instance from the passed
9494
// config.
9595
func NewHarnessNode(t *testing.T, cfg *BaseNodeConfig) (*HarnessNode, error) {
96-
if cfg.BaseDir == "" {
97-
var err error
98-
99-
// Create a temporary directory for the node's data and logs.
100-
// Use dash suffix as a separator between base name and random
101-
// suffix.
102-
dirBaseName := fmt.Sprintf("lndtest-node-%s-", cfg.Name)
103-
cfg.BaseDir, err = os.MkdirTemp("", dirBaseName)
104-
if err != nil {
105-
return nil, err
106-
}
96+
if err := cfg.GenBaseDir(); err != nil {
97+
return nil, err
10798
}
99+
108100
cfg.DataDir = filepath.Join(cfg.BaseDir, "data")
109101
cfg.LogDir = filepath.Join(cfg.BaseDir, "logs")
110102
cfg.TLSCertPath = filepath.Join(cfg.BaseDir, "tls.cert")
@@ -802,6 +794,13 @@ func (hn *HarnessNode) Shutdown() error {
802794
if err := hn.Stop(); err != nil {
803795
return err
804796
}
797+
798+
// Exit if we want to skip the cleanup, which happens when a customized
799+
// base dir is used.
800+
if hn.Cfg.SkipCleanup {
801+
return nil
802+
}
803+
805804
if err := hn.cleanup(); err != nil {
806805
return err
807806
}

make/testing_flags.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ ifneq ($(shuffleseed),)
3434
SHUFFLE_SEED = $(shuffleseed)
3535
endif
3636

37+
# Set the base dir if specified.
38+
ifneq ($(basedir),)
39+
ITEST_FLAGS += -basedir=$(basedir)
40+
endif
41+
3742
# Windows needs to append a .exe suffix to all executable files, otherwise it
3843
# won't run them.
3944
ifneq ($(windows),)

0 commit comments

Comments
 (0)