mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
libnetwork: refactor networkdb test implementation
Leverage higher-order functions to DRY the polling checks in TestNetworkDBNodeJoinLeaveIteration. Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
parent
49f021ebf0
commit
1213881712
1 changed files with 31 additions and 40 deletions
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/docker/docker/pkg/stringid"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/go-events"
|
"github.com/docker/go-events"
|
||||||
"github.com/hashicorp/memberlist"
|
"github.com/hashicorp/memberlist"
|
||||||
"github.com/hashicorp/serf/serf"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
|
@ -480,47 +479,43 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) {
|
||||||
func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
|
func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
|
||||||
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
||||||
|
|
||||||
var (
|
dbChangeWitness := func(db *NetworkDB) func(network string, expectNodeCount int) {
|
||||||
dbIndex int32
|
staleNetworkTime := db.networkClock.Time()
|
||||||
staleNetworkTime [2]serf.LamportTime
|
return func(network string, expectNodeCount int) {
|
||||||
expectNodeCount int
|
check := func(t poll.LogT) poll.Result {
|
||||||
network = "network1"
|
networkTime := db.networkClock.Time()
|
||||||
)
|
if networkTime <= staleNetworkTime {
|
||||||
dbChangeWitness := func(t poll.LogT) poll.Result {
|
return poll.Continue("network time is stale, no change registered yet.")
|
||||||
db := dbs[dbIndex]
|
}
|
||||||
networkTime := db.networkClock.Time()
|
count := -1
|
||||||
if networkTime <= staleNetworkTime[dbIndex] {
|
db.Lock()
|
||||||
return poll.Continue("network time is stale, no change registered yet.")
|
if nodes, ok := db.networkNodes[network]; ok {
|
||||||
|
count = len(nodes)
|
||||||
|
}
|
||||||
|
db.Unlock()
|
||||||
|
if count != expectNodeCount {
|
||||||
|
return poll.Continue("current number of nodes is %d, expect %d.", count, expectNodeCount)
|
||||||
|
}
|
||||||
|
return poll.Success()
|
||||||
|
}
|
||||||
|
t.Helper()
|
||||||
|
poll.WaitOn(t, check, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
||||||
}
|
}
|
||||||
count := -1
|
|
||||||
db.Lock()
|
|
||||||
if nodes, ok := db.networkNodes[network]; ok {
|
|
||||||
count = len(nodes)
|
|
||||||
}
|
|
||||||
db.Unlock()
|
|
||||||
if count != expectNodeCount {
|
|
||||||
return poll.Continue("current number of nodes is %d, expect %d.", count, expectNodeCount)
|
|
||||||
}
|
|
||||||
return poll.Success()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single node Join/Leave
|
// Single node Join/Leave
|
||||||
staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
|
witness0 := dbChangeWitness(dbs[0])
|
||||||
err := dbs[0].JoinNetwork("network1")
|
err := dbs[0].JoinNetwork("network1")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
witness0("network1", 1)
|
||||||
|
|
||||||
dbIndex, expectNodeCount = 0, 1
|
witness0 = dbChangeWitness(dbs[0])
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
|
|
||||||
staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
|
|
||||||
err = dbs[0].LeaveNetwork("network1")
|
err = dbs[0].LeaveNetwork("network1")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
witness0("network1", 0)
|
||||||
dbIndex, expectNodeCount = 0, 0
|
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
|
|
||||||
// Multiple nodes Join/Leave
|
// Multiple nodes Join/Leave
|
||||||
staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
|
witness0, witness1 := dbChangeWitness(dbs[0]), dbChangeWitness(dbs[1])
|
||||||
err = dbs[0].JoinNetwork("network1")
|
err = dbs[0].JoinNetwork("network1")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
@ -529,34 +524,30 @@ func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
|
||||||
|
|
||||||
// Wait for the propagation on db[0]
|
// Wait for the propagation on db[0]
|
||||||
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
||||||
dbIndex, expectNodeCount = 0, 2
|
witness0("network1", 2)
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
if n, ok := dbs[0].networks[dbs[0].config.NodeID]["network1"]; !ok || n.leaving {
|
if n, ok := dbs[0].networks[dbs[0].config.NodeID]["network1"]; !ok || n.leaving {
|
||||||
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the propagation on db[1]
|
// Wait for the propagation on db[1]
|
||||||
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
||||||
dbIndex, expectNodeCount = 1, 2
|
witness1("network1", 2)
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
if n, ok := dbs[1].networks[dbs[1].config.NodeID]["network1"]; !ok || n.leaving {
|
if n, ok := dbs[1].networks[dbs[1].config.NodeID]["network1"]; !ok || n.leaving {
|
||||||
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try a quick leave/join
|
// Try a quick leave/join
|
||||||
staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
|
witness0, witness1 = dbChangeWitness(dbs[0]), dbChangeWitness(dbs[1])
|
||||||
err = dbs[0].LeaveNetwork("network1")
|
err = dbs[0].LeaveNetwork("network1")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
err = dbs[0].JoinNetwork("network1")
|
err = dbs[0].JoinNetwork("network1")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
||||||
dbIndex, expectNodeCount = 0, 2
|
witness0("network1", 2)
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
|
|
||||||
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
||||||
dbIndex, expectNodeCount = 1, 2
|
witness1("network1", 2)
|
||||||
poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
|
||||||
|
|
||||||
closeNetworkDBInstances(t, dbs)
|
closeNetworkDBInstances(t, dbs)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue