From cbd981c23621482546bb5fc47aef6a4d94774a1e Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 30 Apr 2018 07:45:36 +0300 Subject: [PATCH] lnd_test: make sure node ID is correctly initialized in log file name --- lntest/harness.go | 3 ++- lntest/node.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lntest/harness.go b/lntest/harness.go index cbe0c0b3..45515467 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -326,7 +326,8 @@ func (n *NetworkHarness) RestoreNodeWithSeed(name string, extraArgs []string, // wallet with or without a seed. If hasSeed is false, the returned harness node // can be used immediately. Otherwise, the node will require an additional // initialization phase where the wallet is either created or restored. -func (n *NetworkHarness) newNode(name string, extraArgs []string, hasSeed bool) (*HarnessNode, error) { +func (n *NetworkHarness) newNode(name string, extraArgs []string, + hasSeed bool) (*HarnessNode, error) { node, err := newNode(nodeConfig{ Name: name, HasSeed: hasSeed, diff --git a/lntest/node.go b/lntest/node.go index 5f89b1ea..aaf16b87 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -266,12 +266,44 @@ func (hn *HarnessNode) start(lndError chan<- error) error { var errb bytes.Buffer hn.cmd.Stderr = &errb + // Make sure the log file cleanup function is initialized, even + // if no log file is created. + var finalizeLogfile = func() { + if hn.logFile != nil { + hn.logFile.Close() + } + } + // If the logoutput flag is passed, redirect output from the nodes to // log files. if *logOutput { fileName := fmt.Sprintf("output-%d-%s-%s.log", hn.NodeID, hn.cfg.Name, hex.EncodeToString(hn.PubKey[:logPubKeyBytes])) + // If the node's PubKey is not yet initialized, create a temporary + // file name. Later, after the PubKey has been initialized, the + // file can be moved to its final name with the PubKey included. + if bytes.Equal(hn.PubKey[:4], []byte{0, 0, 0, 0}) { + fileName = fmt.Sprintf("output-%d-%s-tmp__.log", hn.NodeID, + hn.cfg.Name) + + // Once the node has done its work, the log file can be renamed. + finalizeLogfile = func() { + if hn.logFile != nil { + hn.logFile.Close() + + newFileName := fmt.Sprintf("output-%d-%s-%s.log", + hn.NodeID, hn.cfg.Name, + hex.EncodeToString(hn.PubKey[:logPubKeyBytes])) + err := os.Rename(fileName, newFileName) + if err != nil { + fmt.Errorf("could not rename %s to %s: %v", + fileName, newFileName, err) + } + } + } + } + // Create file if not exists, otherwise append. file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) @@ -307,6 +339,9 @@ func (hn *HarnessNode) start(lndError chan<- error) error { // Signal any onlookers that this process has exited. close(hn.processExit) + + // Make sure log file is closed and renamed if necessary. + finalizeLogfile() }() // Write process ID to a file.