lnwallet: use T.TempDir to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
4 files changed
+21
-62
No files matched your search
@@ -252,8 +252,7 @@ func serializeTxWitness(txWitness wire.TxWitness) ([]byte, error) {
|
|||||||
|
|
||||||
// TestSignPsbt tests the PSBT signing functionality.
|
// TestSignPsbt tests the PSBT signing functionality.
|
||||||
func TestSignPsbt(t *testing.T) {
|
func TestSignPsbt(t *testing.T) {
|
||||||
w, _, cleanup := newTestWallet(t, netParams, seedBytes)
|
w, _ := newTestWallet(t, netParams, seedBytes)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package btcwallet
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
"math"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -164,8 +162,7 @@ var (
|
|||||||
// BIP32 key path correctly.
|
// BIP32 key path correctly.
|
||||||
func TestBip32KeyDerivation(t *testing.T) {
|
func TestBip32KeyDerivation(t *testing.T) {
|
||||||
netParams := &chaincfg.RegressionNetParams
|
netParams := &chaincfg.RegressionNetParams
|
||||||
w, _, cleanup := newTestWallet(t, netParams, seedBytes)
|
w, _ := newTestWallet(t, netParams, seedBytes)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
// This is just a sanity check that the wallet was initialized
|
// This is just a sanity check that the wallet was initialized
|
||||||
// correctly. We make sure the first derived address is the expected
|
// 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.
|
// with a proof to make sure the resulting addresses match up.
|
||||||
func TestScriptImport(t *testing.T) {
|
func TestScriptImport(t *testing.T) {
|
||||||
netParams := &chaincfg.RegressionNetParams
|
netParams := &chaincfg.RegressionNetParams
|
||||||
w, miner, cleanup := newTestWallet(t, netParams, seedBytes)
|
w, miner := newTestWallet(t, netParams, seedBytes)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
firstDerivedAddr, err := w.NewAddress(
|
firstDerivedAddr, err := w.NewAddress(
|
||||||
lnwallet.TaprootPubkey, false, lnwallet.DefaultAccountName,
|
lnwallet.TaprootPubkey, false, lnwallet.DefaultAccountName,
|
||||||
@@ -286,21 +282,12 @@ func TestScriptImport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newTestWallet(t *testing.T, netParams *chaincfg.Params,
|
func newTestWallet(t *testing.T, netParams *chaincfg.Params,
|
||||||
seedBytes []byte) (*BtcWallet, *rpctest.Harness, func()) {
|
seedBytes []byte) (*BtcWallet, *rpctest.Harness) {
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "lnwallet")
|
|
||||||
if err != nil {
|
|
||||||
_ = os.RemoveAll(tempDir)
|
|
||||||
t.Fatalf("creating temp dir failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
chainBackend, miner, backendCleanup := getChainBackend(t, netParams)
|
chainBackend, miner, backendCleanup := getChainBackend(t, netParams)
|
||||||
cleanup := func() {
|
t.Cleanup(backendCleanup)
|
||||||
_ = os.RemoveAll(tempDir)
|
|
||||||
backendCleanup()
|
|
||||||
}
|
|
||||||
|
|
||||||
loaderOpt := LoaderWithLocalWalletDB(tempDir, false, time.Minute)
|
loaderOpt := LoaderWithLocalWalletDB(t.TempDir(), false, time.Minute)
|
||||||
config := Config{
|
config := Config{
|
||||||
PrivatePass: []byte("some-pass"),
|
PrivatePass: []byte("some-pass"),
|
||||||
HdSeed: seedBytes,
|
HdSeed: seedBytes,
|
||||||
@@ -314,17 +301,15 @@ func newTestWallet(t *testing.T, netParams *chaincfg.Params,
|
|||||||
blockCache := blockcache.NewBlockCache(10000)
|
blockCache := blockcache.NewBlockCache(10000)
|
||||||
w, err := New(config, blockCache)
|
w, err := New(config, blockCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup()
|
|
||||||
t.Fatalf("creating wallet failed: %v", err)
|
t.Fatalf("creating wallet failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = w.Start()
|
err = w.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup()
|
|
||||||
t.Fatalf("starting wallet failed: %v", err)
|
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.
|
// getChainBackend returns a simple btcd based chain backend to back the wallet.
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -3040,8 +3038,7 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) {
|
|||||||
|
|
||||||
rpcConfig := miningNode.RPCConfig()
|
rpcConfig := miningNode.RPCConfig()
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "channeldb")
|
tempDir := t.TempDir()
|
||||||
require.NoError(t, err, "unable to create temp dir")
|
|
||||||
db, err := channeldb.Open(tempDir)
|
db, err := channeldb.Open(tempDir)
|
||||||
require.NoError(t, err, "unable to create db")
|
require.NoError(t, err, "unable to create db")
|
||||||
testCfg := chainntnfs.CacheConfig{
|
testCfg := chainntnfs.CacheConfig{
|
||||||
@@ -3093,15 +3090,12 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
|||||||
|
|
||||||
aliceWalletController lnwallet.WalletController
|
aliceWalletController lnwallet.WalletController
|
||||||
bobWalletController lnwallet.WalletController
|
bobWalletController lnwallet.WalletController
|
||||||
|
|
||||||
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
tempTestDirAlice, err := ioutil.TempDir("", "lnwallet")
|
tempTestDirAlice := t.TempDir()
|
||||||
require.NoError(t, err, "unable to create temp directory")
|
tempTestDirBob := t.TempDir()
|
||||||
defer os.RemoveAll(tempTestDirAlice)
|
|
||||||
|
|
||||||
tempTestDirBob, err := ioutil.TempDir("", "lnwallet")
|
|
||||||
require.NoError(t, err, "unable to create temp directory")
|
|
||||||
defer os.RemoveAll(tempTestDirBob)
|
|
||||||
|
|
||||||
blockCache := blockcache.NewBlockCache(10000)
|
blockCache := blockcache.NewBlockCache(10000)
|
||||||
|
|
||||||
@@ -3191,13 +3185,9 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
|||||||
|
|
||||||
case "bitcoind":
|
case "bitcoind":
|
||||||
// Start a bitcoind instance.
|
// Start a bitcoind instance.
|
||||||
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
|
tempBitcoindDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create temp directory: %v", err)
|
|
||||||
}
|
|
||||||
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
|
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
|
||||||
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
|
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
|
||||||
defer os.RemoveAll(tempBitcoindDir)
|
|
||||||
rpcPort := getFreePort()
|
rpcPort := getFreePort()
|
||||||
bitcoind := exec.Command(
|
bitcoind := exec.Command(
|
||||||
"bitcoind",
|
"bitcoind",
|
||||||
@@ -3269,11 +3259,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
|||||||
|
|
||||||
case "bitcoind-rpc-polling":
|
case "bitcoind-rpc-polling":
|
||||||
// Start a bitcoind instance.
|
// Start a bitcoind instance.
|
||||||
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
|
tempBitcoindDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create temp directory: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempBitcoindDir)
|
|
||||||
rpcPort := getFreePort()
|
rpcPort := getFreePort()
|
||||||
bitcoind := exec.Command(
|
bitcoind := exec.Command(
|
||||||
"bitcoind",
|
"bitcoind",
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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
|
// Set up a test channel on which the test commitment transaction is
|
||||||
// going to be produced.
|
// going to be produced.
|
||||||
remoteChannel, localChannel, cleanUp := createTestChannelsForVectors(
|
remoteChannel, localChannel := createTestChannelsForVectors(
|
||||||
tc,
|
tc,
|
||||||
chanType, test.FeePerKw,
|
chanType, test.FeePerKw,
|
||||||
remoteBalance.ToSatoshis(),
|
remoteBalance.ToSatoshis(),
|
||||||
localBalance.ToSatoshis(),
|
localBalance.ToSatoshis(),
|
||||||
)
|
)
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// Add htlcs (if any) to the update logs of both sides and save a hash
|
// 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
|
// 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.
|
// test channel that is used to verify the test vectors.
|
||||||
func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelType,
|
func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelType,
|
||||||
feeRate btcutil.Amount, remoteBalance, localBalance btcutil.Amount) (
|
feeRate btcutil.Amount, remoteBalance, localBalance btcutil.Amount) (
|
||||||
*LightningChannel, *LightningChannel, func()) {
|
*LightningChannel, *LightningChannel) {
|
||||||
|
|
||||||
t := tc.t
|
t := tc.t
|
||||||
|
|
||||||
@@ -846,16 +844,10 @@ func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelTyp
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Create temporary databases.
|
// Create temporary databases.
|
||||||
remotePath, err := ioutil.TempDir("", "remotedb")
|
dbRemote, err := channeldb.Open(t.TempDir())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
dbRemote, err := channeldb.Open(remotePath)
|
dbLocal, err := channeldb.Open(t.TempDir())
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
localPath, err := ioutil.TempDir("", "localdb")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
dbLocal, err := channeldb.Open(localPath)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Create the initial commitment transactions for the channel.
|
// 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
|
// Return a clean up function that stops goroutines and removes the test
|
||||||
// databases.
|
// databases.
|
||||||
cleanUpFunc := func() {
|
t.Cleanup(func() {
|
||||||
dbLocal.Close()
|
dbLocal.Close()
|
||||||
dbRemote.Close()
|
dbRemote.Close()
|
||||||
|
|
||||||
os.RemoveAll(localPath)
|
|
||||||
os.RemoveAll(remotePath)
|
|
||||||
|
|
||||||
require.NoError(t, remotePool.Stop())
|
require.NoError(t, remotePool.Stop())
|
||||||
require.NoError(t, localPool.Stop())
|
require.NoError(t, localPool.Stop())
|
||||||
}
|
})
|
||||||
|
|
||||||
return channelRemote, channelLocal, cleanUpFunc
|
return channelRemote, channelLocal
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user