multi: move many t.Fatalf calls to require.NoError

This commit is contained in:
Tommy Volk
2022-05-05 20:11:50 +00:00
parent 9e6f0ef46b
commit 9a10c80bcb
92 changed files with 1905 additions and 5565 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/stretchr/testify/require"
)
type mockChannelSource struct {
@@ -86,13 +87,9 @@ func TestFetchBackupForChan(t *testing.T) {
// First, we'll make two channels, only one of them will have all the
// information we need to construct set of backups for them.
randomChan1, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to generate chan: %v", err)
}
require.NoError(t, err, "unable to generate chan")
randomChan2, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to generate chan: %v", err)
}
require.NoError(t, err, "unable to generate chan")
chanSource := newMockChannelSource()
chanSource.chans[randomChan1.FundingOutpoint] = randomChan1
@@ -152,13 +149,9 @@ func TestFetchStaticChanBackups(t *testing.T) {
// channel source.
const numChans = 2
randomChan1, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to generate chan: %v", err)
}
require.NoError(t, err, "unable to generate chan")
randomChan2, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to generate chan: %v", err)
}
require.NoError(t, err, "unable to generate chan")
chanSource := newMockChannelSource()
chanSource.chans[randomChan1.FundingOutpoint] = randomChan1
@@ -170,9 +163,7 @@ func TestFetchStaticChanBackups(t *testing.T) {
// of backups for all the channels. This should succeed, as all items
// are populated within the channel source.
backups, err := FetchStaticChanBackups(chanSource, chanSource)
if err != nil {
t.Fatalf("unable to create chan back ups: %v", err)
}
require.NoError(t, err, "unable to create chan back ups")
if len(backups) != numChans {
t.Fatalf("expected %v chans, instead got %v", numChans,

View File

@@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func makeFakePackedMulti() (PackedMulti, error) {
@@ -25,9 +27,7 @@ func assertBackupMatches(t *testing.T, filePath string,
t.Helper()
packedBackup, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatalf("unable to test file: %v", err)
}
require.NoError(t, err, "unable to test file")
if !bytes.Equal(packedBackup, currentBackup) {
t.Fatalf("backups don't match after first swap: "+
@@ -53,9 +53,7 @@ func TestUpdateAndSwap(t *testing.T) {
t.Parallel()
tempTestDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to make temp dir: %v", err)
}
require.NoError(t, err, "unable to make temp dir")
defer os.Remove(tempTestDir)
testCases := []struct {
@@ -193,9 +191,7 @@ func TestExtractMulti(t *testing.T) {
// First, as prep, we'll create a single chan backup, then pack that
// fully into a multi backup.
channel, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to gen chan: %v", err)
}
require.NoError(t, err, "unable to gen chan")
singleBackup := NewSingle(channel, nil)
@@ -204,24 +200,18 @@ func TestExtractMulti(t *testing.T) {
StaticBackups: []Single{singleBackup},
}
err = unpackedMulti.PackToWriter(&b, keyRing)
if err != nil {
t.Fatalf("unable to pack to writer: %v", err)
}
require.NoError(t, err, "unable to pack to writer")
packedMulti := PackedMulti(b.Bytes())
// Finally, we'll make a new temporary file, then write out the packed
// multi directly to to it.
tempFile, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("unable to create temp file: %v", err)
}
require.NoError(t, err, "unable to create temp file")
defer os.Remove(tempFile.Name())
_, err = tempFile.Write(packedMulti)
if err != nil {
t.Fatalf("unable to write temp file: %v", err)
}
require.NoError(t, err, "unable to write temp file")
if err := tempFile.Sync(); err != nil {
t.Fatalf("unable to sync temp file: %v", err)
}

View File

@@ -4,6 +4,8 @@ import (
"bytes"
"net"
"testing"
"github.com/stretchr/testify/require"
)
// TestMultiPackUnpack...
@@ -126,9 +128,7 @@ func TestPackedMultiUnpack(t *testing.T) {
// First, we'll make a new unpacked multi with a random channel.
testChannel, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to gen random channel: %v", err)
}
require.NoError(t, err, "unable to gen random channel")
var multi Multi
multi.StaticBackups = append(
multi.StaticBackups, NewSingle(testChannel, nil),
@@ -143,9 +143,7 @@ func TestPackedMultiUnpack(t *testing.T) {
// We should be able to properly unpack this typed packed multi.
packedMulti := PackedMulti(b.Bytes())
unpackedMulti, err := packedMulti.Unpack(keyRing)
if err != nil {
t.Fatalf("unable to unpack multi: %v", err)
}
require.NoError(t, err, "unable to unpack multi")
// Finally, the versions should match, and the unpacked singles also
// identical.

View File

@@ -7,6 +7,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/require"
)
type mockSwapper struct {
@@ -157,9 +158,7 @@ func TestSubSwapperIdempotentStartStop(t *testing.T) {
swapper := newMockSwapper(keyRing)
subSwapper, err := NewSubSwapper(nil, &chanNotifier, keyRing, swapper)
if err != nil {
t.Fatalf("unable to init subSwapper: %v", err)
}
require.NoError(t, err, "unable to init subSwapper")
if err := subSwapper.Start(); err != nil {
t.Fatalf("unable to start swapper: %v", err)
@@ -226,9 +225,7 @@ func TestSubSwapperUpdater(t *testing.T) {
subSwapper, err := NewSubSwapper(
initialChanSet, chanNotifier, keyRing, swapper,
)
if err != nil {
t.Fatalf("unable to make swapper: %v", err)
}
require.NoError(t, err, "unable to make swapper")
if err := subSwapper.Start(); err != nil {
t.Fatalf("unable to start sub swapper: %v", err)
}
@@ -241,9 +238,7 @@ func TestSubSwapperUpdater(t *testing.T) {
// Now that the sub-swapper is active, we'll notify to add a brand new
// channel to the channel state.
newChannel, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to create new chan: %v", err)
}
require.NoError(t, err, "unable to create new chan")
// With the new channel created, we'll send a new update to the main
// goroutine telling it about this new channel.

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
)
type mockChannelRestorer struct {
@@ -108,9 +109,7 @@ func TestUnpackAndRecoverSingles(t *testing.T) {
err = UnpackAndRecoverSingles(
packedBackups, keyRing, &chanRestorer, &peerConnector,
)
if err != nil {
t.Fatalf("unable to recover chans: %v", err)
}
require.NoError(t, err, "unable to recover chans")
// Both the restorer, and connector should have been called 10 times,
// once for each backup.
@@ -204,9 +203,7 @@ func TestUnpackAndRecoverMulti(t *testing.T) {
err = UnpackAndRecoverMulti(
packedMulti, keyRing, &chanRestorer, &peerConnector,
)
if err != nil {
t.Fatalf("unable to recover chans: %v", err)
}
require.NoError(t, err, "unable to recover chans")
// Both the restorer, and connector should have been called 10 times,
// once for each backup.

View File

@@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/stretchr/testify/require"
)
var (
@@ -202,9 +203,7 @@ func TestSinglePackUnpack(t *testing.T) {
// contains all the information we need to create a static channel
// backup.
channel, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to gen open channel: %v", err)
}
require.NoError(t, err, "unable to gen open channel")
singleChanBackup := NewSingle(channel, []net.Addr{addr1, addr2})
@@ -340,9 +339,7 @@ func TestPackedSinglesUnpack(t *testing.T) {
// With all singles packed, we'll create the grouped type and attempt
// to Unpack all of them in a single go.
freshSingles, err := PackedSingles(packedSingles).Unpack(keyRing)
if err != nil {
t.Fatalf("unable to unpack singles: %v", err)
}
require.NoError(t, err, "unable to unpack singles")
// The set of freshly unpacked singles should exactly match the initial
// set of singles that we packed before.
@@ -386,9 +383,7 @@ func TestSinglePackStaticChanBackups(t *testing.T) {
// Now that we have all of our singles are created, we'll attempt to
// pack them all in a single batch.
packedSingleMap, err := PackStaticChanBackups(unpackedSingles, keyRing)
if err != nil {
t.Fatalf("unable to pack backups: %v", err)
}
require.NoError(t, err, "unable to pack backups")
// With our packed singles obtained, we'll ensure that each of them
// match their unpacked counterparts after they themselves have been
@@ -432,9 +427,7 @@ func TestSingleUnconfirmedChannel(t *testing.T) {
// we need to create a static channel backup but simulate an
// unconfirmed channel by setting the block height to 0.
channel, err := genRandomOpenChannelShell()
if err != nil {
t.Fatalf("unable to gen open channel: %v", err)
}
require.NoError(t, err, "unable to gen open channel")
channel.ShortChannelID.BlockHeight = 0
channel.FundingBroadcastHeight = fundingBroadcastHeight
@@ -450,9 +443,7 @@ func TestSingleUnconfirmedChannel(t *testing.T) {
}
var unpackedSingle Single
err = unpackedSingle.UnpackFromReader(&b, keyRing)
if err != nil {
t.Fatalf("unable to unpack single: %v", err)
}
require.NoError(t, err, "unable to unpack single")
if unpackedSingle.ShortChannelID.BlockHeight != fundingBroadcastHeight {
t.Fatalf("invalid block height. got %d expected %d.",
unpackedSingle.ShortChannelID.BlockHeight,