From 9acd53a5deab208e25b6bbb60f335ed093872158 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Mon, 15 Aug 2022 21:07:28 +0800 Subject: [PATCH] lnwallet: use `T.TempDir` to create temporary test directory Signed-off-by: Eng Zer Jun --- lnwallet/btcwallet/psbt_test.go | 3 +-- lnwallet/btcwallet/signer_test.go | 27 ++++++--------------------- lnwallet/test/test_interface.go | 28 +++++++--------------------- lnwallet/transactions_test.go | 25 +++++++------------------ 4 files changed, 21 insertions(+), 62 deletions(-) diff --git a/lnwallet/btcwallet/psbt_test.go b/lnwallet/btcwallet/psbt_test.go index 6bb657d8b..245ea682c 100644 --- a/lnwallet/btcwallet/psbt_test.go +++ b/lnwallet/btcwallet/psbt_test.go @@ -252,8 +252,7 @@ func serializeTxWitness(txWitness wire.TxWitness) ([]byte, error) { // TestSignPsbt tests the PSBT signing functionality. func TestSignPsbt(t *testing.T) { - w, _, cleanup := newTestWallet(t, netParams, seedBytes) - defer cleanup() + w, _ := newTestWallet(t, netParams, seedBytes) testCases := []struct { name string diff --git a/lnwallet/btcwallet/signer_test.go b/lnwallet/btcwallet/signer_test.go index 4b36accc7..25e62b4df 100644 --- a/lnwallet/btcwallet/signer_test.go +++ b/lnwallet/btcwallet/signer_test.go @@ -3,9 +3,7 @@ package btcwallet import ( "encoding/hex" "fmt" - "io/ioutil" "math" - "os" "testing" "time" @@ -164,8 +162,7 @@ var ( // BIP32 key path correctly. func TestBip32KeyDerivation(t *testing.T) { netParams := &chaincfg.RegressionNetParams - w, _, cleanup := newTestWallet(t, netParams, seedBytes) - defer cleanup() + w, _ := newTestWallet(t, netParams, seedBytes) // This is just a sanity check that the wallet was initialized // correctly. We make sure the first derived address is the expected @@ -203,8 +200,7 @@ func TestBip32KeyDerivation(t *testing.T) { // with a proof to make sure the resulting addresses match up. func TestScriptImport(t *testing.T) { netParams := &chaincfg.RegressionNetParams - w, miner, cleanup := newTestWallet(t, netParams, seedBytes) - defer cleanup() + w, miner := newTestWallet(t, netParams, seedBytes) firstDerivedAddr, err := w.NewAddress( lnwallet.TaprootPubkey, false, lnwallet.DefaultAccountName, @@ -286,21 +282,12 @@ func TestScriptImport(t *testing.T) { } func newTestWallet(t *testing.T, netParams *chaincfg.Params, - seedBytes []byte) (*BtcWallet, *rpctest.Harness, func()) { - - tempDir, err := ioutil.TempDir("", "lnwallet") - if err != nil { - _ = os.RemoveAll(tempDir) - t.Fatalf("creating temp dir failed: %v", err) - } + seedBytes []byte) (*BtcWallet, *rpctest.Harness) { chainBackend, miner, backendCleanup := getChainBackend(t, netParams) - cleanup := func() { - _ = os.RemoveAll(tempDir) - backendCleanup() - } + t.Cleanup(backendCleanup) - loaderOpt := LoaderWithLocalWalletDB(tempDir, false, time.Minute) + loaderOpt := LoaderWithLocalWalletDB(t.TempDir(), false, time.Minute) config := Config{ PrivatePass: []byte("some-pass"), HdSeed: seedBytes, @@ -314,17 +301,15 @@ func newTestWallet(t *testing.T, netParams *chaincfg.Params, blockCache := blockcache.NewBlockCache(10000) w, err := New(config, blockCache) if err != nil { - cleanup() t.Fatalf("creating wallet failed: %v", err) } err = w.Start() if err != nil { - cleanup() t.Fatalf("starting wallet failed: %v", err) } - return w, miner, cleanup + return w, miner } // getChainBackend returns a simple btcd based chain backend to back the wallet. diff --git a/lnwallet/test/test_interface.go b/lnwallet/test/test_interface.go index ebaf91e8d..88a9a3501 100644 --- a/lnwallet/test/test_interface.go +++ b/lnwallet/test/test_interface.go @@ -5,9 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "fmt" - "io/ioutil" "net" - "os" "os/exec" "path/filepath" "reflect" @@ -3040,8 +3038,7 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) { rpcConfig := miningNode.RPCConfig() - tempDir, err := ioutil.TempDir("", "channeldb") - require.NoError(t, err, "unable to create temp dir") + tempDir := t.TempDir() db, err := channeldb.Open(tempDir) require.NoError(t, err, "unable to create db") testCfg := chainntnfs.CacheConfig{ @@ -3093,15 +3090,12 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver, aliceWalletController lnwallet.WalletController bobWalletController lnwallet.WalletController + + err error ) - tempTestDirAlice, err := ioutil.TempDir("", "lnwallet") - require.NoError(t, err, "unable to create temp directory") - defer os.RemoveAll(tempTestDirAlice) - - tempTestDirBob, err := ioutil.TempDir("", "lnwallet") - require.NoError(t, err, "unable to create temp directory") - defer os.RemoveAll(tempTestDirBob) + tempTestDirAlice := t.TempDir() + tempTestDirBob := t.TempDir() blockCache := blockcache.NewBlockCache(10000) @@ -3191,13 +3185,9 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver, case "bitcoind": // Start a bitcoind instance. - tempBitcoindDir, err := ioutil.TempDir("", "bitcoind") - if err != nil { - t.Fatalf("unable to create temp directory: %v", err) - } + tempBitcoindDir := t.TempDir() zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket" zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket" - defer os.RemoveAll(tempBitcoindDir) rpcPort := getFreePort() bitcoind := exec.Command( "bitcoind", @@ -3269,11 +3259,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver, case "bitcoind-rpc-polling": // Start a bitcoind instance. - tempBitcoindDir, err := ioutil.TempDir("", "bitcoind") - if err != nil { - t.Fatalf("unable to create temp directory: %v", err) - } - defer os.RemoveAll(tempBitcoindDir) + tempBitcoindDir := t.TempDir() rpcPort := getFreePort() bitcoind := exec.Command( "bitcoind", diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index 2bb14573c..b525d8a59 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -10,7 +10,6 @@ import ( "io" "io/ioutil" "net" - "os" "sort" "testing" "time" @@ -269,13 +268,12 @@ func testVectors(t *testing.T, chanType channeldb.ChannelType, test testCase) { // Set up a test channel on which the test commitment transaction is // going to be produced. - remoteChannel, localChannel, cleanUp := createTestChannelsForVectors( + remoteChannel, localChannel := createTestChannelsForVectors( tc, chanType, test.FeePerKw, remoteBalance.ToSatoshis(), localBalance.ToSatoshis(), ) - defer cleanUp() // Add htlcs (if any) to the update logs of both sides and save a hash // map that allows us to identify the htlcs in the scripts later on and @@ -747,7 +745,7 @@ func (p *mockProducer) Encode(w io.Writer) error { // test channel that is used to verify the test vectors. func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelType, feeRate btcutil.Amount, remoteBalance, localBalance btcutil.Amount) ( - *LightningChannel, *LightningChannel, func()) { + *LightningChannel, *LightningChannel) { t := tc.t @@ -846,16 +844,10 @@ func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelTyp ) // Create temporary databases. - remotePath, err := ioutil.TempDir("", "remotedb") + dbRemote, err := channeldb.Open(t.TempDir()) require.NoError(t, err) - dbRemote, err := channeldb.Open(remotePath) - require.NoError(t, err) - - localPath, err := ioutil.TempDir("", "localdb") - require.NoError(t, err) - - dbLocal, err := channeldb.Open(localPath) + dbLocal, err := channeldb.Open(t.TempDir()) require.NoError(t, err) // Create the initial commitment transactions for the channel. @@ -1005,16 +997,13 @@ func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelTyp // Return a clean up function that stops goroutines and removes the test // databases. - cleanUpFunc := func() { + t.Cleanup(func() { dbLocal.Close() dbRemote.Close() - os.RemoveAll(localPath) - os.RemoveAll(remotePath) - require.NoError(t, remotePool.Stop()) require.NoError(t, localPool.Stop()) - } + }) - return channelRemote, channelLocal, cleanUpFunc + return channelRemote, channelLocal }