Skip to content

Commit 343ae96

Browse files
committed
lntest: add a new basedir flag to keep test data
1 parent 0d20e96 commit 343ae96

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lntest/node/config.go

Lines changed: 30 additions & 5 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

@@ -225,18 +235,33 @@ func (cfg *BaseNodeConfig) BaseConfig() *BaseNodeConfig {
225235

226236
// GenBaseDir creates a base dir that's used for the test.
227237
func (cfg *BaseNodeConfig) GenBaseDir() error {
228-
if cfg.BaseDir == "" {
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 == "" {
229249
var err error
230250

231-
// Create a temporary directory for the node's data and logs.
232-
// Use dash suffix as a separator between base name and random
233-
// suffix.
234-
dirBaseName := fmt.Sprintf("lndtest-node-%s-", cfg.Name)
235251
cfg.BaseDir, err = os.MkdirTemp("", dirBaseName)
236252

237253
return err
238254
}
239255

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+
240265
return nil
241266
}
242267

lntest/node/harness_node.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,13 @@ func (hn *HarnessNode) Shutdown() error {
794794
if err := hn.Stop(); err != nil {
795795
return err
796796
}
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+
797804
if err := hn.cleanup(); err != nil {
798805
return err
799806
}

0 commit comments

Comments
 (0)