lnwallet: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-08-15 21:07:28 +08:00
parent ca5c695526
commit 9acd53a5de
No known key found for this signature in database
GPG key ID: DAEBBD2E34C111E6
4 changed files with 21 additions and 62 deletions

View file

@ -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

View file

@ -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.

View file

@ -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",

View file

@ -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
}