test: improve engine test (#198)

This commit is contained in:
Mikhail Bragin 2022-01-18 17:52:55 +01:00 committed by GitHub
parent 5db130a12e
commit 23f028e65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,8 +57,9 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
}, cancel, ctx) }, cancel, ctx)
type testCase struct { type testCase struct {
idx int name string
networkMap *mgmtProto.NetworkMap networkMap *mgmtProto.NetworkMap
expectedLen int expectedLen int
expectedPeers []string expectedPeers []string
expectedSerial uint64 expectedSerial uint64
@ -79,9 +80,8 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
AllowedIps: []string{"100.64.0.12/24"}, AllowedIps: []string{"100.64.0.12/24"},
} }
// 1st case - new peer and network map has Serial grater than local => apply the update
case1 := testCase{ case1 := testCase{
idx: 1, name: "input with a new peer to add",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{
Serial: 1, Serial: 1,
PeerConfig: nil, PeerConfig: nil,
@ -97,7 +97,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
// 2nd case - one extra peer added and network map has Serial grater than local => apply the update // 2nd case - one extra peer added and network map has Serial grater than local => apply the update
case2 := testCase{ case2 := testCase{
idx: 2, name: "input with an old peer and a new peer to add",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{
Serial: 2, Serial: 2,
PeerConfig: nil, PeerConfig: nil,
@ -111,9 +111,8 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
expectedSerial: 2, expectedSerial: 2,
} }
// 3rd case - an update with 3 peers and Serial lower than the current serial of the engine => ignore the update
case3 := testCase{ case3 := testCase{
idx: 3, name: "input with outdated (old) update to ignore",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{
Serial: 0, Serial: 0,
PeerConfig: nil, PeerConfig: nil,
@ -127,9 +126,8 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
expectedSerial: 2, expectedSerial: 2,
} }
// 4th case - an update with 2 peers (1 new and 1 old) => apply the update removing old peer and adding a new one
case4 := testCase{ case4 := testCase{
idx: 3, name: "input with one peer to remove and one new to add",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{
Serial: 4, Serial: 4,
PeerConfig: nil, PeerConfig: nil,
@ -143,9 +141,8 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
expectedSerial: 4, expectedSerial: 4,
} }
// 5th case - an update with all peers to be removed
case5 := testCase{ case5 := testCase{
idx: 3, name: "input with all peers to remove",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{
Serial: 5, Serial: 5,
PeerConfig: nil, PeerConfig: nil,
@ -158,25 +155,29 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
} }
for _, c := range []testCase{case1, case2, case3, case4, case5} { for _, c := range []testCase{case1, case2, case3, case4, case5} {
err = engine.updateNetworkMap(c.networkMap)
if err != nil {
t.Fatal(err)
return
}
if len(engine.peerConns) != c.expectedLen { t.Run(c.name, func(t *testing.T) {
t.Errorf("case %d expecting Engine.peerConns to be of size %d, got %d", c.idx, c.expectedLen, len(engine.peerConns)) err = engine.updateNetworkMap(c.networkMap)
} if err != nil {
t.Fatal(err)
if engine.networkSerial != c.expectedSerial { return
t.Errorf("case %d expecting Engine.networkSerial to be equal to %d, actual %d", c.idx, c.expectedSerial, engine.networkSerial)
}
for _, p := range c.expectedPeers {
if _, ok := engine.peerConns[p]; !ok {
t.Errorf("case %d expecting Engine.peerConns to contain peer %s", c.idx, p)
} }
}
if len(engine.peerConns) != c.expectedLen {
t.Errorf("expecting Engine.peerConns to be of size %d, got %d", c.expectedLen, len(engine.peerConns))
}
if engine.networkSerial != c.expectedSerial {
t.Errorf("expecting Engine.networkSerial to be equal to %d, actual %d", c.expectedSerial, engine.networkSerial)
}
for _, p := range c.expectedPeers {
if _, ok := engine.peerConns[p]; !ok {
t.Errorf("expecting Engine.peerConns to contain peer %s", p)
}
}
})
} }
} }