Skip to content

Commit

Permalink
Fixup itest net tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Jul 10, 2022
1 parent 49f0741 commit 9662c14
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -768,11 +768,6 @@ workflows:
suite: itest-api
target: "./itests/api_test.go"

- test:
name: test-itest-net
suite: itest-net
target: "./itests/net_test.go"

- test:
name: test-itest-batch_deal
suite: itest-batch_deal
Expand Down Expand Up @@ -893,6 +888,11 @@ workflows:
suite: itest-multisig
target: "./itests/multisig_test.go"

- test:
name: test-itest-net
suite: itest-net
target: "./itests/net_test.go"

- test:
name: test-itest-nonce
suite: itest-nonce
Expand Down
15 changes: 13 additions & 2 deletions itests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -98,11 +99,21 @@ func (ts *apiSuite) testConnectTwo(t *testing.T) {

peers, err := one.NetPeers(ctx)
require.NoError(t, err)
require.Lenf(t, peers, 2, "node one doesn't have 2 peers")

countPeerIDs := func(peers []peer.AddrInfo) int {
peerIDs := make(map[peer.ID]struct{})
for _, p := range peers {
peerIDs[p.ID] = struct{}{}
}

return len(peerIDs)
}

require.Equal(t, countPeerIDs(peers), 2, "node one doesn't have 2 peers")

peers, err = two.NetPeers(ctx)
require.NoError(t, err)
require.Lenf(t, peers, 2, "node two doesn't have 2 peers")
require.Equal(t, countPeerIDs(peers), 2, "node one doesn't have 2 peers")
}

func (ts *apiSuite) testSearchMsg(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions itests/kit/ensemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (n *Ensemble) Start() *Ensemble {
node.FullAPI(&full.FullNode, node.Lite(full.options.lite)),
node.Base(),
node.Repo(r),
node.MockHost(n.mn),
node.If(full.options.disableLibp2p, node.MockHost(n.mn)),
node.Test(),

// so that we subscribe to pubsub topics immediately
Expand Down Expand Up @@ -583,7 +583,7 @@ func (n *Ensemble) Start() *Ensemble {
node.Repo(r),
node.Test(),

node.If(!m.options.disableLibp2p, node.MockHost(n.mn)),
node.If(m.options.disableLibp2p, node.MockHost(n.mn)),

node.Override(new(v1api.FullNode), m.FullNode.FullNode),
node.Override(new(*lotusminer.Miner), lotusminer.NewTestMiner(mineBlock, m.ActorAddr)),
Expand Down
81 changes: 53 additions & 28 deletions itests/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"fmt"
"testing"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/itests/kit"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/itests/kit"
)

func TestNetConn(t *testing.T) {
Expand Down Expand Up @@ -52,18 +53,6 @@ func TestNetConn(t *testing.T) {
t.Errorf("agents not matching. %s", err.Error())
}

//stm: @NETWORK_COMMON_BANDWIDTH_STATS_001
bandwidth, err := secondNode.NetBandwidthStats(ctx)
require.NoError(t, err)

//stm: @NETWORK_COMMON_BANDWIDTH_STATS_BY_PEER_001
peerBandwidths, err := firstNode.NetBandwidthStatsByPeer(ctx)
require.NoError(t, err)

if peerBandwidths[secondNodeID.String()] != bandwidth {
t.Errorf("bandwidths differ")
}

//stm: @NETWORK_COMMON_FIND_PEER_001
secondNodePeer, err := firstNode.NetFindPeer(ctx, secondNodeID)
require.NoError(t, err)
Expand All @@ -81,10 +70,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_PEERS_001
addrs, err := firstNode.NetPeers(ctx)
require.NoError(t, err)

if len(addrs) > 0 {
t.Errorf("connected peer does not exist in network")
}
require.NotEqual(t, 0, len(addrs))

//stm: @NETWORK_COMMON_DISCONNECT_001
err = firstNode.NetDisconnect(ctx, secondNodeID)
Expand Down Expand Up @@ -170,13 +156,20 @@ func TestNetLimit(t *testing.T) {
func TestNetBlockPeer(t *testing.T) {
ctx := context.Background()

firstNode, secondNode, _, ens := kit.EnsembleTwoOne(t)
ens.InterconnectAll()
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)

//stm: @NETWORK_COMMON_ID_001
firstAddrInfo, _ := firstNode.NetAddrsListen(ctx)
firstNodeID, err := firstNode.ID(ctx)
require.NoError(t, err)
secondNodeID, err := secondNode.ID(ctx)
require.NoError(t, err)

// Sanity check that we're not already connected somehow
connectedness, err := secondNode.NetConnectedness(ctx, firstNodeID)
require.NoError(t, err, "failed to determine connectedness")
require.NotEqual(t, connectedness, network.Connected, "shouldn't already be connected")

//stm: @NETWORK_COMMON_BLOCK_ADD_001
err = firstNode.NetBlockAdd(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
require.NoError(t, err)
Expand All @@ -189,6 +182,11 @@ func TestNetBlockPeer(t *testing.T) {
t.Errorf("blocked peer not in blocked peer list")
}

require.Error(t, secondNode.NetConnect(ctx, firstAddrInfo), "shouldn't be able to connect to second node")
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
require.NoError(t, err, "failed to determine connectedness")
require.NotEqual(t, connectedness, network.Connected)

//stm: @NETWORK_COMMON_BLOCK_REMOVE_001
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
require.NoError(t, err)
Expand All @@ -201,39 +199,62 @@ func TestNetBlockPeer(t *testing.T) {
t.Errorf("failed to remove blocked peer from blocked peer list")
}

require.NoError(t, secondNode.NetConnect(ctx, firstAddrInfo), "failed to connect to second node")
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
require.NoError(t, err, "failed to determine connectedness")
require.Equal(t, connectedness, network.Connected)
}

func TestNetBlockIPAddr(t *testing.T) {
ctx := context.Background()

firstNode, secondNode, _, ens := kit.EnsembleTwoOne(t)
ens.InterconnectAll()
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)

//stm: @NETWORK_COMMON_ADDRS_LISTEN_001
addrInfo, _ := secondNode.NetAddrsListen(ctx)
firstAddrInfo, _ := firstNode.NetAddrsListen(ctx)
secondAddrInfo, _ := secondNode.NetAddrsListen(ctx)

var secondNodeIPs []string

for _, addr := range addrInfo.Addrs {
for _, addr := range secondAddrInfo.Addrs {
ip, err := manet.ToIP(addr)
if err != nil {
continue
}
secondNodeIPs = append(secondNodeIPs, ip.String())
}

// Sanity check that we're not already connected somehow
connectedness, err := secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
require.NoError(t, err, "failed to determine connectedness")
require.NotEqual(t, connectedness, network.Connected, "shouldn't already be connected")

//stm: @NETWORK_COMMON_BLOCK_ADD_001
err := firstNode.NetBlockAdd(ctx, api.NetBlockList{IPAddrs: secondNodeIPs})
require.NoError(t, err)
require.NoError(t, firstNode.NetBlockAdd(ctx, api.NetBlockList{
IPAddrs: secondNodeIPs}), "failed to add blocked IPs")

//stm: @NETWORK_COMMON_BLOCK_LIST_001
list, err := firstNode.NetBlockList(ctx)
require.NoError(t, err)

if len(list.IPAddrs) == 0 || list.IPAddrs[0] != secondNodeIPs[0] {
t.Errorf("blocked ip not in blocked ip list")
require.Equal(t, len(list.IPAddrs), len(secondNodeIPs), "expected %d blocked IPs", len(secondNodeIPs))
for _, blockedIP := range list.IPAddrs {
found := false
for _, secondNodeIP := range secondNodeIPs {
if blockedIP == secondNodeIP {
found = true
break
}
}

require.True(t, found, "blocked IP %s is not one of secondNodeIPs", blockedIP)
}

require.Error(t, secondNode.NetConnect(ctx, firstAddrInfo), "shouldn't be able to connect to second node")
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
require.NoError(t, err, "failed to determine connectedness")
require.NotEqual(t, connectedness, network.Connected)

//stm: @NETWORK_COMMON_BLOCK_REMOVE_001
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{IPAddrs: secondNodeIPs})
require.NoError(t, err)
Expand All @@ -246,6 +267,10 @@ func TestNetBlockIPAddr(t *testing.T) {
t.Errorf("failed to remove blocked ip from blocked ip list")
}

require.NoError(t, secondNode.NetConnect(ctx, firstAddrInfo), "failed to connect to second node")
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
require.NoError(t, err, "failed to determine connectedness")
require.Equal(t, connectedness, network.Connected)
}

func getConnState(ctx context.Context, t *testing.T, node *kit.TestFullNode, peer peer.ID) network.Connectedness {
Expand Down

0 comments on commit 9662c14

Please sign in to comment.