lntest: add log dir flag

This commit is contained in:
Oliver Gugger 2020-11-04 11:03:30 +01:00
parent 9bbf134237
commit f358a4474d
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
4 changed files with 52 additions and 27 deletions

@ -15,8 +15,8 @@ import (
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
) )
// logDir is the name of the temporary log directory. // logDirPattern is the pattern of the name of the temporary log directory.
const logDir = "./.backendlogs" const logDirPattern = "%s/.backendlogs"
// BitcoindBackendConfig is an implementation of the BackendConfig interface // BitcoindBackendConfig is an implementation of the BackendConfig interface
// backed by a Bitcoind node. // backed by a Bitcoind node.
@ -73,15 +73,16 @@ func (b BitcoindBackendConfig) Name() string {
func newBackend(miner string, netParams *chaincfg.Params, extraArgs []string) ( func newBackend(miner string, netParams *chaincfg.Params, extraArgs []string) (
*BitcoindBackendConfig, func() error, error) { *BitcoindBackendConfig, func() error, error) {
baseLogDir := fmt.Sprintf(logDirPattern, GetLogDir())
if netParams != &chaincfg.RegressionNetParams { if netParams != &chaincfg.RegressionNetParams {
return nil, nil, fmt.Errorf("only regtest supported") return nil, nil, fmt.Errorf("only regtest supported")
} }
if err := os.MkdirAll(logDir, 0700); err != nil { if err := os.MkdirAll(baseLogDir, 0700); err != nil {
return nil, nil, err return nil, nil, err
} }
logFile, err := filepath.Abs(logDir + "/bitcoind.log") logFile, err := filepath.Abs(baseLogDir + "/bitcoind.log")
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -128,13 +129,16 @@ func newBackend(miner string, netParams *chaincfg.Params, extraArgs []string) (
var errStr string var errStr string
// After shutting down the chain backend, we'll make a copy of // After shutting down the chain backend, we'll make a copy of
// the log file before deleting the temporary log dir. // the log file before deleting the temporary log dir.
err := CopyFile("./output_bitcoind_chainbackend.log", logFile) logDestination := fmt.Sprintf(
"%s/output_bitcoind_chainbackend.log", GetLogDir(),
)
err := CopyFile(logDestination, logFile)
if err != nil { if err != nil {
errStr += fmt.Sprintf("unable to copy file: %v\n", err) errStr += fmt.Sprintf("unable to copy file: %v\n", err)
} }
if err = os.RemoveAll(logDir); err != nil { if err = os.RemoveAll(baseLogDir); err != nil {
errStr += fmt.Sprintf( errStr += fmt.Sprintf(
"cannot remove dir %s: %v\n", logDir, err, "cannot remove dir %s: %v\n", baseLogDir, err,
) )
} }
if err := os.RemoveAll(tempBitcoindDir); err != nil { if err := os.RemoveAll(tempBitcoindDir); err != nil {

@ -14,8 +14,8 @@ import (
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
) )
// logDir is the name of the temporary log directory. // logDirPattern is the pattern of the name of the temporary log directory.
const logDir = "./.backendlogs" const logDirPattern = "%s/.backendlogs"
// temp is used to signal we want to establish a temporary connection using the // temp is used to signal we want to establish a temporary connection using the
// btcd Node API. // btcd Node API.
@ -75,12 +75,13 @@ func (b BtcdBackendConfig) Name() string {
func NewBackend(miner string, netParams *chaincfg.Params) ( func NewBackend(miner string, netParams *chaincfg.Params) (
*BtcdBackendConfig, func() error, error) { *BtcdBackendConfig, func() error, error) {
baseLogDir := fmt.Sprintf(logDirPattern, GetLogDir())
args := []string{ args := []string{
"--rejectnonstd", "--rejectnonstd",
"--txindex", "--txindex",
"--trickleinterval=100ms", "--trickleinterval=100ms",
"--debuglevel=debug", "--debuglevel=debug",
"--logdir=" + logDir, "--logdir=" + baseLogDir,
"--nowinservice", "--nowinservice",
// The miner will get banned and disconnected from the node if // The miner will get banned and disconnected from the node if
// its requested data are not found. We add a nobanning flag to // its requested data are not found. We add a nobanning flag to
@ -110,14 +111,17 @@ func NewBackend(miner string, netParams *chaincfg.Params) (
// After shutting down the chain backend, we'll make a copy of // After shutting down the chain backend, we'll make a copy of
// the log file before deleting the temporary log dir. // the log file before deleting the temporary log dir.
logFile := logDir + "/" + netParams.Name + "/btcd.log" logFile := baseLogDir + "/" + netParams.Name + "/btcd.log"
err := CopyFile("./output_btcd_chainbackend.log", logFile) logDestination := fmt.Sprintf(
"%s/output_btcd_chainbackend.log", GetLogDir(),
)
err := CopyFile(logDestination, logFile)
if err != nil { if err != nil {
errStr += fmt.Sprintf("unable to copy file: %v\n", err) errStr += fmt.Sprintf("unable to copy file: %v\n", err)
} }
if err = os.RemoveAll(logDir); err != nil { if err = os.RemoveAll(baseLogDir); err != nil {
errStr += fmt.Sprintf( errStr += fmt.Sprintf(
"cannot remove dir %s: %v\n", logDir, err, "cannot remove dir %s: %v\n", baseLogDir, err,
) )
} }
if errStr != "" { if errStr != "" {

@ -2435,7 +2435,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
) )
// Set up a new miner that we can use to cause a reorg. // Set up a new miner that we can use to cause a reorg.
tempLogDir := "./.tempminerlogs" tempLogDir := fmt.Sprintf("%s/.tempminerlogs", lntest.GetLogDir())
logFilename := "output-open_channel_reorg-temp_miner.log" logFilename := "output-open_channel_reorg-temp_miner.log"
tempMiner, tempMinerCleanUp, err := lntest.NewMiner( tempMiner, tempMinerCleanUp, err := lntest.NewMiner(
tempLogDir, logFilename, tempLogDir, logFilename,
@ -14158,6 +14158,8 @@ func TestLightningNetworkDaemon(t *testing.T) {
} }
// Parse testing flags that influence our test execution. // Parse testing flags that influence our test execution.
logDir := lntest.GetLogDir()
require.NoError(t, os.MkdirAll(logDir, 0700))
testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche() testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche()
lntest.ApplyPortOffset(uint32(trancheIndex) * 1000) lntest.ApplyPortOffset(uint32(trancheIndex) * 1000)
@ -14176,7 +14178,7 @@ func TestLightningNetworkDaemon(t *testing.T) {
// guarantees of getting included in to blocks. // guarantees of getting included in to blocks.
// //
// We will also connect it to our chain backend. // We will also connect it to our chain backend.
minerLogDir := "./.minerlogs" minerLogDir := fmt.Sprintf("%s/.minerlogs", logDir)
miner, minerCleanUp, err := lntest.NewMiner( miner, minerCleanUp, err := lntest.NewMiner(
minerLogDir, "output_btcd_miner.log", minerLogDir, "output_btcd_miner.log",
harnessNetParams, &rpcclient.NotificationHandlers{}, harnessNetParams, &rpcclient.NotificationHandlers{},

@ -70,6 +70,10 @@ var (
logOutput = flag.Bool("logoutput", false, logOutput = flag.Bool("logoutput", false,
"log output from node n to file output-n.log") "log output from node n to file output-n.log")
// logSubDir is the default directory where the logs are written to if
// logOutput is true.
logSubDir = flag.String("logdir", ".", "default dir to write logs to")
// goroutineDump is a flag that can be set to dump the active // goroutineDump is a flag that can be set to dump the active
// goroutines of test nodes on failure. // goroutines of test nodes on failure.
goroutineDump = flag.Bool("goroutinedump", false, goroutineDump = flag.Bool("goroutinedump", false,
@ -110,6 +114,15 @@ func ApplyPortOffset(offset uint32) {
_ = atomic.AddUint32(&lastPort, offset) _ = atomic.AddUint32(&lastPort, offset)
} }
// GetLogDir returns the passed --logdir flag or the default value if it wasn't
// set.
func GetLogDir() string {
if logSubDir != nil && *logSubDir != "" {
return *logSubDir
}
return "."
}
// generateListeningPorts returns four ints representing ports to listen on // generateListeningPorts returns four ints representing ports to listen on
// designated for the current lightning network test. This returns the next // designated for the current lightning network test. This returns the next
// available ports for the p2p, rpc, rest and profiling services. // available ports for the p2p, rpc, rest and profiling services.
@ -392,11 +405,9 @@ func NewMiner(logDir, logFilename string, netParams *chaincfg.Params,
// After shutting down the miner, we'll make a copy of the log // After shutting down the miner, we'll make a copy of the log
// file before deleting the temporary log dir. // file before deleting the temporary log dir.
logFile := fmt.Sprintf( logFile := fmt.Sprintf("%s/%s/btcd.log", logDir, netParams.Name)
"%s/%s/btcd.log", logDir, netParams.Name, copyPath := fmt.Sprintf("%s/../%s", logDir, logFilename)
) err := CopyFile(filepath.Clean(copyPath), logFile)
copyPath := fmt.Sprintf("./%s", logFilename)
err := CopyFile(copyPath, logFile)
if err != nil { if err != nil {
return fmt.Errorf("unable to copy file: %v", err) return fmt.Errorf("unable to copy file: %v", err)
} }
@ -481,24 +492,28 @@ func (hn *HarnessNode) start(lndBinary string, lndError chan<- error) error {
// If the logoutput flag is passed, redirect output from the nodes to // If the logoutput flag is passed, redirect output from the nodes to
// log files. // log files.
if *logOutput { if *logOutput {
fileName := fmt.Sprintf("output-%d-%s-%s.log", hn.NodeID, dir := GetLogDir()
fileName := fmt.Sprintf("%s/output-%d-%s-%s.log", dir, hn.NodeID,
hn.Cfg.Name, hex.EncodeToString(hn.PubKey[:logPubKeyBytes])) hn.Cfg.Name, hex.EncodeToString(hn.PubKey[:logPubKeyBytes]))
// If the node's PubKey is not yet initialized, create a temporary // If the node's PubKey is not yet initialized, create a temporary
// file name. Later, after the PubKey has been initialized, the // file name. Later, after the PubKey has been initialized, the
// file can be moved to its final name with the PubKey included. // file can be moved to its final name with the PubKey included.
if bytes.Equal(hn.PubKey[:4], []byte{0, 0, 0, 0}) { if bytes.Equal(hn.PubKey[:4], []byte{0, 0, 0, 0}) {
fileName = fmt.Sprintf("output-%d-%s-tmp__.log", hn.NodeID, fileName = fmt.Sprintf("%s/output-%d-%s-tmp__.log",
hn.Cfg.Name) dir, hn.NodeID, hn.Cfg.Name)
// Once the node has done its work, the log file can be renamed. // Once the node has done its work, the log file can be renamed.
finalizeLogfile = func() { finalizeLogfile = func() {
if hn.logFile != nil { if hn.logFile != nil {
hn.logFile.Close() hn.logFile.Close()
newFileName := fmt.Sprintf("output-%d-%s-%s.log", pubKeyHex := hex.EncodeToString(
hn.NodeID, hn.Cfg.Name, hn.PubKey[:logPubKeyBytes],
hex.EncodeToString(hn.PubKey[:logPubKeyBytes])) )
newFileName := fmt.Sprintf("%s/output"+
"-%d-%s-%s.log", dir, hn.NodeID,
hn.Cfg.Name, pubKeyHex)
err := os.Rename(fileName, newFileName) err := os.Rename(fileName, newFileName)
if err != nil { if err != nil {
fmt.Printf("could not rename "+ fmt.Printf("could not rename "+