From 310f87e2717b0e4bc3a78365c50411fecf005a54 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 30 Sep 2020 06:15:05 +0800 Subject: [PATCH] itest: save temp miner's logs --- lntest/itest/lnd_test.go | 66 ++++++++++++---------------------------- lntest/node.go | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index 6a7dfbcb..ee064bfa 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -2339,28 +2339,25 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) { ) // Set up a new miner that we can use to cause a reorg. - args := []string{ - "--rejectnonstd", - "--txindex", - "--nowinservice", - "--nobanning", - } - tempMiner, err := rpctest.New( - harnessNetParams, &rpcclient.NotificationHandlers{}, args, + tempLogDir := "./.tempminerlogs" + logFilename := "output-open_channel_reorg-temp_miner.log" + tempMiner, tempMinerCleanUp, err := lntest.NewMiner( + tempLogDir, logFilename, + harnessNetParams, &rpcclient.NotificationHandlers{}, ) - if err != nil { - t.Fatalf("unable to create mining node: %v", err) - } - if err := tempMiner.SetUp(false, 0); err != nil { - t.Fatalf("unable to set up mining node: %v", err) - } + require.NoError(t.t, err, "failed to create temp miner") defer func() { require.NoError( - t.t, tempMiner.TearDown(), - "failed to tear down temp miner", + t.t, tempMinerCleanUp(), + "failed to clean up temp miner", ) }() + // Setup the temp miner + require.NoError( + t.t, tempMiner.SetUp(false, 0), "unable to set up mining node", + ) + // We start by connecting the new miner to our original miner, // such that it will sync to our original chain. err = net.Miner.Node.Node( @@ -14060,43 +14057,18 @@ func TestLightningNetworkDaemon(t *testing.T) { // // We will also connect it to our chain backend. minerLogDir := "./.minerlogs" - args := []string{ - "--rejectnonstd", - "--txindex", - "--debuglevel=debug", - "--logdir=" + minerLogDir, - "--trickleinterval=100ms", - "--nowinservice", - "--nobanning", - } handlers := &rpcclient.NotificationHandlers{ OnTxAccepted: func(hash *chainhash.Hash, amt btcutil.Amount) { lndHarness.OnTxAccepted(hash) }, } - - miner, err := rpctest.New(harnessNetParams, handlers, args) - if err != nil { - ht.Fatalf("unable to create mining node: %v", err) - } + miner, minerCleanUp, err := lntest.NewMiner( + minerLogDir, "output_btcd_miner.log", + harnessNetParams, handlers, + ) + require.NoError(t, err, "failed to create new miner") defer func() { - require.NoError( - t, miner.TearDown(), "failed to tear down miner", - ) - - // After shutting down the miner, we'll make a copy of the log - // file before deleting the temporary log dir. - logFile := fmt.Sprintf( - "%s/%s/btcd.log", minerLogDir, harnessNetParams.Name, - ) - err := lntest.CopyFile("./output_btcd_miner.log", logFile) - if err != nil { - fmt.Printf("unable to copy file: %v\n", err) - } - if err = os.RemoveAll(minerLogDir); err != nil { - fmt.Printf("Cannot remove dir %s: %v\n", - minerLogDir, err) - } + require.NoError(t, minerCleanUp(), "failed to clean up miner") }() // Start a chain backend. diff --git a/lntest/node.go b/lntest/node.go index eeaf120c..cdf0be03 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -19,6 +19,8 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/integration/rpctest" + "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/go-errors/errors" @@ -351,6 +353,59 @@ func newNode(cfg NodeConfig) (*HarnessNode, error) { }, nil } +// NewMiner creates a new miner using btcd backend. The logDir specifies the +// miner node's log dir. When tests finished, during clean up, its logs are +// copied to a file specified as logFilename. +func NewMiner(logDir, logFilename string, netParams *chaincfg.Params, + handler *rpcclient.NotificationHandlers) (*rpctest.Harness, + func() error, error) { + + args := []string{ + "--rejectnonstd", + "--txindex", + "--nowinservice", + "--nobanning", + "--debuglevel=debug", + "--logdir=" + logDir, + "--trickleinterval=100ms", + } + + miner, err := rpctest.New(netParams, handler, args) + if err != nil { + return nil, nil, fmt.Errorf( + "unable to create mining node: %v", err, + ) + } + + cleanUp := func() error { + if err := miner.TearDown(); err != nil { + return fmt.Errorf( + "failed to tear down miner, got error: %s", err, + ) + } + + // After shutting down the miner, we'll make a copy of the log + // file before deleting the temporary log dir. + logFile := fmt.Sprintf( + "%s/%s/btcd.log", logDir, netParams.Name, + ) + copyPath := fmt.Sprintf("./%s", logFilename) + err := CopyFile(copyPath, logFile) + if err != nil { + return fmt.Errorf("unable to copy file: %v", err) + } + + if err = os.RemoveAll(logDir); err != nil { + return fmt.Errorf( + "cannot remove dir %s: %v", logDir, err, + ) + } + return nil + } + + return miner, cleanUp, nil +} + // DBPath returns the filepath to the channeldb database file for this node. func (hn *HarnessNode) DBPath() string { return hn.Cfg.DBPath()