From 724f6e0969330677ebf5e0fb78ba36086548af3f Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 3 Sep 2020 03:15:04 +0800 Subject: [PATCH] itest: require no error when cleaning up chainbackends --- lntest/bitcoind.go | 24 ++++++++++++++++++------ lntest/btcd.go | 20 +++++++++++++++----- lntest/itest/lnd_test.go | 6 +++++- lntest/neutrino.go | 4 ++-- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lntest/bitcoind.go b/lntest/bitcoind.go index 0a73c86e..86d59ef4 100644 --- a/lntest/bitcoind.go +++ b/lntest/bitcoind.go @@ -3,6 +3,7 @@ package lntest import ( + "errors" "fmt" "io/ioutil" "math/rand" @@ -71,7 +72,7 @@ func (b BitcoindBackendConfig) Name() string { // NewBackend starts a bitcoind node and returns a BitoindBackendConfig for // that node. func NewBackend(miner string, netParams *chaincfg.Params) ( - *BitcoindBackendConfig, func(), error) { + *BitcoindBackendConfig, func() error, error) { if netParams != &chaincfg.RegressionNetParams { return nil, nil, fmt.Errorf("only regtest supported") @@ -121,21 +122,32 @@ func NewBackend(miner string, netParams *chaincfg.Params) ( return nil, nil, fmt.Errorf("couldn't start bitcoind: %v", err) } - cleanUp := func() { + cleanUp := func() error { bitcoind.Process.Kill() bitcoind.Wait() + var errStr string // After shutting down the chain backend, we'll make a copy of // the log file before deleting the temporary log dir. err := CopyFile("./output_bitcoind_chainbackend.log", logFile) if err != nil { - fmt.Printf("unable to copy file: %v\n", err) + errStr += fmt.Sprintf("unable to copy file: %v\n", err) } if err = os.RemoveAll(logDir); err != nil { - fmt.Printf("Cannot remove dir %s: %v\n", logDir, err) + errStr += fmt.Sprintf( + "cannot remove dir %s: %v\n", logDir, err, + ) } - - os.RemoveAll(tempBitcoindDir) + if err := os.RemoveAll(tempBitcoindDir); err != nil { + errStr += fmt.Sprintf( + "cannot remove dir %s: %v\n", + tempBitcoindDir, err, + ) + } + if errStr != "" { + return errors.New(errStr) + } + return nil } // Allow process to start. diff --git a/lntest/btcd.go b/lntest/btcd.go index 2322d8ee..3b75b7bc 100644 --- a/lntest/btcd.go +++ b/lntest/btcd.go @@ -4,6 +4,7 @@ package lntest import ( "encoding/hex" + "errors" "fmt" "os" @@ -72,7 +73,7 @@ func (b BtcdBackendConfig) Name() string { // that node. miner should be set to the P2P address of the miner to connect // to. func NewBackend(miner string, netParams *chaincfg.Params) ( - *BtcdBackendConfig, func(), error) { + *BtcdBackendConfig, func() error, error) { args := []string{ "--rejectnonstd", @@ -98,19 +99,28 @@ func NewBackend(miner string, netParams *chaincfg.Params) ( minerAddr: miner, } - cleanUp := func() { - chainBackend.TearDown() + cleanUp := func() error { + var errStr string + if err := chainBackend.TearDown(); err != nil { + errStr += err.Error() + "\n" + } // After shutting down the chain backend, we'll make a copy of // the log file before deleting the temporary log dir. logFile := logDir + "/" + netParams.Name + "/btcd.log" err := CopyFile("./output_btcd_chainbackend.log", logFile) if err != nil { - fmt.Printf("unable to copy file: %v\n", err) + errStr += fmt.Sprintf("unable to copy file: %v\n", err) } if err = os.RemoveAll(logDir); err != nil { - fmt.Printf("Cannot remove dir %s: %v\n", logDir, err) + errStr += fmt.Sprintf( + "cannot remove dir %s: %v\n", logDir, err, + ) } + if errStr != "" { + return errors.New(errStr) + } + return nil } return bd, cleanUp, nil diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index 8a094b3b..a8589450 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -14445,7 +14445,11 @@ func TestLightningNetworkDaemon(t *testing.T) { if err != nil { ht.Fatalf("unable to start backend: %v", err) } - defer cleanUp() + defer func() { + require.NoError( + t, cleanUp(), "failed to clean up chain backend", + ) + }() if err := miner.SetUp(true, 50); err != nil { ht.Fatalf("unable to set up mining node: %v", err) diff --git a/lntest/neutrino.go b/lntest/neutrino.go index 1a5497b9..25f2af24 100644 --- a/lntest/neutrino.go +++ b/lntest/neutrino.go @@ -44,12 +44,12 @@ func (b NeutrinoBackendConfig) Name() string { // NewBackend starts and returns a NeutrinoBackendConfig for the node. func NewBackend(miner string, _ *chaincfg.Params) ( - *NeutrinoBackendConfig, func(), error) { + *NeutrinoBackendConfig, func() error, error) { bd := &NeutrinoBackendConfig{ minerAddr: miner, } - cleanUp := func() {} + cleanUp := func() error { return nil } return bd, cleanUp, nil }