chanbackup+rpcserver+server: return number of recovered channels
This commit is contained in:
4 files changed
+70
-79
No files matched your search
+47
-61
@@ -2,7 +2,7 @@ package chanbackup
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
@@ -11,6 +11,12 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
errRestoreFail = errors.New("restore fail")
|
||||
|
||||
errConnectFail = errors.New("connect fail")
|
||||
)
|
||||
|
||||
type mockChannelRestorer struct {
|
||||
fail bool
|
||||
|
||||
@@ -19,7 +25,7 @@ type mockChannelRestorer struct {
|
||||
|
||||
func (m *mockChannelRestorer) RestoreChansFromSingles(...Single) error {
|
||||
if m.fail {
|
||||
return fmt.Errorf("fail")
|
||||
return errRestoreFail
|
||||
}
|
||||
|
||||
m.callCount++
|
||||
@@ -33,11 +39,11 @@ type mockPeerConnector struct {
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (m *mockPeerConnector) ConnectPeer(node *btcec.PublicKey,
|
||||
addrs []net.Addr) error {
|
||||
func (m *mockPeerConnector) ConnectPeer(_ *btcec.PublicKey,
|
||||
_ []net.Addr) error {
|
||||
|
||||
if m.fail {
|
||||
return fmt.Errorf("fail")
|
||||
return errConnectFail
|
||||
}
|
||||
|
||||
m.callCount++
|
||||
@@ -59,16 +65,13 @@ func TestUnpackAndRecoverSingles(t *testing.T) {
|
||||
var packedBackups PackedSingles
|
||||
for i := 0; i < numSingles; i++ {
|
||||
channel, err := genRandomOpenChannelShell()
|
||||
if err != nil {
|
||||
t.Fatalf("unable make channel: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
single := NewSingle(channel, nil)
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := single.PackToWriter(&b, keyRing); err != nil {
|
||||
t.Fatalf("unable to pack single: %v", err)
|
||||
}
|
||||
err = single.PackToWriter(&b, keyRing)
|
||||
require.NoError(t, err)
|
||||
|
||||
backups = append(backups, single)
|
||||
packedBackups = append(packedBackups, b.Bytes())
|
||||
@@ -83,54 +86,47 @@ func TestUnpackAndRecoverSingles(t *testing.T) {
|
||||
// If we make the channel restore fail, then the entire method should
|
||||
// as well
|
||||
chanRestorer.fail = true
|
||||
err := UnpackAndRecoverSingles(
|
||||
_, err := UnpackAndRecoverSingles(
|
||||
packedBackups, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("restoration should have failed")
|
||||
}
|
||||
require.ErrorIs(t, err, errRestoreFail)
|
||||
|
||||
chanRestorer.fail = false
|
||||
|
||||
// If we make the peer connector fail, then the entire method should as
|
||||
// well
|
||||
peerConnector.fail = true
|
||||
err = UnpackAndRecoverSingles(
|
||||
_, err = UnpackAndRecoverSingles(
|
||||
packedBackups, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("restoration should have failed")
|
||||
}
|
||||
require.ErrorIs(t, err, errConnectFail)
|
||||
|
||||
chanRestorer.callCount--
|
||||
peerConnector.fail = false
|
||||
|
||||
// Next, we'll ensure that if all the interfaces function as expected,
|
||||
// then the channels will properly be unpacked and restored.
|
||||
err = UnpackAndRecoverSingles(
|
||||
numRestored, err := UnpackAndRecoverSingles(
|
||||
packedBackups, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
require.NoError(t, err, "unable to recover chans")
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, numSingles, numRestored)
|
||||
|
||||
// Both the restorer, and connector should have been called 10 times,
|
||||
// once for each backup.
|
||||
if chanRestorer.callCount != numSingles {
|
||||
t.Fatalf("expected %v calls, instead got %v",
|
||||
numSingles, chanRestorer.callCount)
|
||||
}
|
||||
if peerConnector.callCount != numSingles {
|
||||
t.Fatalf("expected %v calls, instead got %v",
|
||||
numSingles, peerConnector.callCount)
|
||||
}
|
||||
require.EqualValues(
|
||||
t, numSingles, chanRestorer.callCount, "restorer call count",
|
||||
)
|
||||
require.EqualValues(
|
||||
t, numSingles, peerConnector.callCount, "peer call count",
|
||||
)
|
||||
|
||||
// If we modify the keyRing, then unpacking should fail.
|
||||
keyRing.Fail = true
|
||||
err = UnpackAndRecoverSingles(
|
||||
_, err = UnpackAndRecoverSingles(
|
||||
packedBackups, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("unpacking should have failed")
|
||||
}
|
||||
require.ErrorContains(t, err, "fail")
|
||||
|
||||
// TODO(roasbeef): verify proper call args
|
||||
}
|
||||
@@ -148,9 +144,7 @@ func TestUnpackAndRecoverMulti(t *testing.T) {
|
||||
backups := make([]Single, 0, numSingles)
|
||||
for i := 0; i < numSingles; i++ {
|
||||
channel, err := genRandomOpenChannelShell()
|
||||
if err != nil {
|
||||
t.Fatalf("unable make channel: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
single := NewSingle(channel, nil)
|
||||
|
||||
@@ -162,9 +156,8 @@ func TestUnpackAndRecoverMulti(t *testing.T) {
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := multi.PackToWriter(&b, keyRing); err != nil {
|
||||
t.Fatalf("unable to pack multi: %v", err)
|
||||
}
|
||||
err := multi.PackToWriter(&b, keyRing)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Next, we'll pack the set of singles into a packed multi, and also
|
||||
// create the set of interfaces we need to carry out the remainder of
|
||||
@@ -177,54 +170,47 @@ func TestUnpackAndRecoverMulti(t *testing.T) {
|
||||
// If we make the channel restore fail, then the entire method should
|
||||
// as well
|
||||
chanRestorer.fail = true
|
||||
err := UnpackAndRecoverMulti(
|
||||
_, err = UnpackAndRecoverMulti(
|
||||
packedMulti, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("restoration should have failed")
|
||||
}
|
||||
require.ErrorIs(t, err, errRestoreFail)
|
||||
|
||||
chanRestorer.fail = false
|
||||
|
||||
// If we make the peer connector fail, then the entire method should as
|
||||
// well
|
||||
peerConnector.fail = true
|
||||
err = UnpackAndRecoverMulti(
|
||||
_, err = UnpackAndRecoverMulti(
|
||||
packedMulti, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("restoration should have failed")
|
||||
}
|
||||
require.ErrorIs(t, err, errConnectFail)
|
||||
|
||||
chanRestorer.callCount--
|
||||
peerConnector.fail = false
|
||||
|
||||
// Next, we'll ensure that if all the interfaces function as expected,
|
||||
// then the channels will properly be unpacked and restored.
|
||||
err = UnpackAndRecoverMulti(
|
||||
numRestored, err := UnpackAndRecoverMulti(
|
||||
packedMulti, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
require.NoError(t, err, "unable to recover chans")
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, numSingles, numRestored)
|
||||
|
||||
// Both the restorer, and connector should have been called 10 times,
|
||||
// once for each backup.
|
||||
if chanRestorer.callCount != numSingles {
|
||||
t.Fatalf("expected %v calls, instead got %v",
|
||||
numSingles, chanRestorer.callCount)
|
||||
}
|
||||
if peerConnector.callCount != numSingles {
|
||||
t.Fatalf("expected %v calls, instead got %v",
|
||||
numSingles, peerConnector.callCount)
|
||||
}
|
||||
require.EqualValues(
|
||||
t, numSingles, chanRestorer.callCount, "restorer call count",
|
||||
)
|
||||
require.EqualValues(
|
||||
t, numSingles, peerConnector.callCount, "peer call count",
|
||||
)
|
||||
|
||||
// If we modify the keyRing, then unpacking should fail.
|
||||
keyRing.Fail = true
|
||||
err = UnpackAndRecoverMulti(
|
||||
_, err = UnpackAndRecoverMulti(
|
||||
packedMulti, keyRing, &chanRestorer, &peerConnector,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatalf("unpacking should have failed")
|
||||
}
|
||||
require.ErrorContains(t, err, "fail")
|
||||
|
||||
// TODO(roasbeef): verify proper call args
|
||||
}
|
||||
Reference in new issue
Block a user