add tests for netstat and netlimit, fix error checking

This commit is contained in:
TheMenko 2022-02-16 17:24:26 +01:00 committed by Aayush
parent 1a0dd76ed1
commit 0d437f732d
2 changed files with 79 additions and 57 deletions

View File

@ -768,6 +768,11 @@ 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

View File

@ -3,6 +3,7 @@ package itests
import (
"context"
"fmt"
"testing"
"github.com/filecoin-project/lotus/api"
@ -10,6 +11,7 @@ import (
"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"
)
@ -20,9 +22,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_ID_001
secondNodeID, err := secondNode.ID(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
connState := getConnState(ctx, t, firstNode, secondNodeID)
@ -32,9 +32,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_ADDRS_LISTEN_001
addrInfo, err := secondNode.NetAddrsListen(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_CONNECT_001
err = firstNode.NetConnect(ctx, addrInfo)
@ -44,14 +42,11 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_PEER_INFO_001
netPeerInfo, err := firstNode.NetPeerInfo(ctx, secondNodeID)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_AGENT_VERSION_001
agent, err := firstNode.NetAgentVersion(ctx, secondNodeID)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if netPeerInfo.Agent != agent {
t.Errorf("agents not matching. %s", err.Error())
@ -59,15 +54,11 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_BANDWIDTH_STATS_001
bandwidth, err := secondNode.NetBandwidthStats(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BANDWIDTH_STATS_BY_PEER_001
peerBandwidths, err := firstNode.NetBandwidthStatsByPeer(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if peerBandwidths[secondNodeID.String()] != bandwidth {
t.Errorf("bandwidths differ")
@ -75,9 +66,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_FIND_PEER_001
secondNodePeer, err := firstNode.NetFindPeer(ctx, secondNodeID)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if secondNodePeer.ID != addrInfo.ID {
t.Errorf("peer id doesn't match with listen address.")
@ -91,9 +80,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_PEERS_001
peers, err := firstNode.NetPeers(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(peers) > 0 && peers[0].ID != addrInfo.ID {
t.Errorf("connected peer does not exist in network")
@ -113,9 +100,7 @@ func TestNetConn(t *testing.T) {
//stm: @NETWORK_COMMON_PEERS_001
peers, err = firstNode.NetPeers(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(peers) > 0 {
t.Errorf("there should be no peers in network after disconnecting node")
@ -123,6 +108,58 @@ func TestNetConn(t *testing.T) {
}
func TestNetStat(t *testing.T) {
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
ctx := context.Background()
sId, err := secondNode.ID(ctx)
require.NoError(t, err)
withScope := func(api interface{}, scope string) func(t *testing.T) {
return func(t *testing.T) {
stat, err := firstNode.NetStat(ctx, scope)
require.NoError(t, err)
switch scope {
case "all":
assert.NotNil(t, stat.System)
assert.NotNil(t, stat.Transient)
case "system":
assert.NotNil(t, stat.System)
case "transient":
assert.NotNil(t, stat.Transient)
}
}
}
t.Run("all", withScope(t, "all"))
t.Run("system", withScope(t, "system"))
t.Run("transient", withScope(t, "transient"))
t.Run("peer", withScope(t, fmt.Sprintf("peer:%s", sId)))
}
func TestNetLimit(t *testing.T) {
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
ctx := context.Background()
sId, err := secondNode.ID(ctx)
require.NoError(t, err)
withScope := func(api interface{}, scope string) func(t *testing.T) {
return func(t *testing.T) {
_, err := firstNode.NetLimit(ctx, scope)
require.NoError(t, err)
}
}
t.Run("system", withScope(t, "system"))
t.Run("transient", withScope(t, "transient"))
t.Run("peer", withScope(t, fmt.Sprintf("peer:%s", sId)))
}
func TestNetBlockPeer(t *testing.T) {
ctx := context.Background()
@ -131,21 +168,15 @@ func TestNetBlockPeer(t *testing.T) {
//stm: @NETWORK_COMMON_ID_001
secondNodeID, err := secondNode.ID(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BLOCK_ADD_001
err = firstNode.NetBlockAdd(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BLOCK_LIST_001
list, err := firstNode.NetBlockList(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(list.Peers) == 0 || list.Peers[0] != secondNodeID {
t.Errorf("blocked peer not in blocked peer list")
@ -153,15 +184,11 @@ func TestNetBlockPeer(t *testing.T) {
//stm: @NETWORK_COMMON_BLOCK_REMOVE_001
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BLOCK_LIST_001
list, err = firstNode.NetBlockList(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(list.Peers) > 0 {
t.Errorf("failed to remove blocked peer from blocked peer list")
@ -190,15 +217,11 @@ func TestNetBlockIPAddr(t *testing.T) {
//stm: @NETWORK_COMMON_BLOCK_ADD_001
err := firstNode.NetBlockAdd(ctx, api.NetBlockList{IPAddrs: secondNodeIPs})
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BLOCK_LIST_001
list, err := firstNode.NetBlockList(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(list.IPAddrs) == 0 || list.IPAddrs[0] != secondNodeIPs[0] {
t.Errorf("blocked ip not in blocked ip list")
@ -206,15 +229,11 @@ func TestNetBlockIPAddr(t *testing.T) {
//stm: @NETWORK_COMMON_BLOCK_REMOVE_001
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{IPAddrs: secondNodeIPs})
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
//stm: @NETWORK_COMMON_BLOCK_LIST_001
list, err = firstNode.NetBlockList(ctx)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
if len(list.IPAddrs) > 0 {
t.Errorf("failed to remove blocked ip from blocked ip list")
@ -225,9 +244,7 @@ func TestNetBlockIPAddr(t *testing.T) {
func getConnState(ctx context.Context, t *testing.T, node *kit.TestFullNode, peer peer.ID) network.Connectedness {
//stm: @NETWORK_COMMON_CONNECTEDNESS_001
connState, err := node.NetConnectedness(ctx, peer)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
return connState
}