Skip to content

Commit e9a7154

Browse files
committed
libnetwork/networkdb: improve TestCRUDTableEntries
Log more details when assertions fail to provide a more complete picture of what went wrong when TestCRUDTableEntries fails. Log the state of each NetworkDB instance at various points in TestCRUDTableEntries to provide an even more complete picture. Increase the global logger verbosity in tests so warnings and debug logs are printed to the test log. Signed-off-by: Cory Snider <[email protected]>
1 parent dbb0d88 commit e9a7154

1 file changed

Lines changed: 52 additions & 17 deletions

File tree

libnetwork/networkdb/networkdb_test.go

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"net"
77
"os"
88
"strconv"
9+
"strings"
910
"sync/atomic"
1011
"testing"
12+
"text/tabwriter"
1113
"time"
1214

15+
cerrdefs "github.com/containerd/errdefs"
1316
"github.com/containerd/log"
1417
"github.com/docker/docker/pkg/stringid"
1518
"github.com/docker/go-events"
@@ -27,7 +30,7 @@ func init() {
2730

2831
func TestMain(m *testing.M) {
2932
os.WriteFile("/proc/sys/net/ipv6/conf/lo/disable_ipv6", []byte{'0', '\n'}, 0o644)
30-
log.SetLevel("error")
33+
log.SetLevel("debug")
3134
os.Exit(m.Run())
3235
}
3336

@@ -85,18 +88,14 @@ func (nDB *NetworkDB) verifyNodeExistence(t *testing.T, node string, present boo
8588
nDB.RLock()
8689
_, ok := nDB.nodes[node]
8790
nDB.RUnlock()
88-
if present && ok {
89-
return
90-
}
91-
92-
if !present && !ok {
91+
if present == ok {
9392
return
9493
}
9594

9695
time.Sleep(50 * time.Millisecond)
9796
}
9897

99-
t.Errorf("%v(%v): Node existence verification for node %s failed", nDB.config.Hostname, nDB.config.NodeID, node)
98+
t.Errorf("%v(%v): expected node %s existence in the cluster = %v, got %v", nDB.config.Hostname, nDB.config.NodeID, node, present, !present)
10099
}
101100

102101
func (nDB *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id string, present bool) {
@@ -109,6 +108,7 @@ func (nDB *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id strin
109108
} else {
110109
maxRetries = 80
111110
}
111+
var ok, leaving bool
112112
for i := int64(0); i < maxRetries; i++ {
113113
nDB.RLock()
114114
var vn *network
@@ -123,35 +123,49 @@ func (nDB *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id strin
123123
}
124124
}
125125
}
126-
exists := vn != nil && !vn.leaving
126+
ok = vn != nil
127+
leaving = ok && vn.leaving
127128
nDB.RUnlock()
128129

129-
if present == exists {
130+
if present == (ok && !leaving) {
130131
return
131132
}
132133

133134
time.Sleep(sleepInterval)
134135
}
135136

136-
t.Error("Network existence verification failed")
137+
if present {
138+
t.Errorf("%v(%v): want node %v to be a member of network %q, got that it is not a member (ok=%v, leaving=%v)",
139+
nDB.config.Hostname, nDB.config.NodeID, node, id, ok, leaving)
140+
} else {
141+
t.Errorf("%v(%v): want node %v to not be a member of network %q, got that it is a member (ok=%v, leaving=%v)",
142+
nDB.config.Hostname, nDB.config.NodeID, node, id, ok, leaving)
143+
}
137144
}
138145

139146
func (nDB *NetworkDB) verifyEntryExistence(t *testing.T, tname, nid, key, value string, present bool) {
140147
t.Helper()
141148
n := 80
149+
var v []byte
142150
for i := 0; i < n; i++ {
143-
v, err := nDB.GetEntry(tname, nid, key)
151+
var err error
152+
v, err = nDB.GetEntry(tname, nid, key)
144153
if present && err == nil && string(v) == value {
145154
return
146155
}
147-
if err != nil && !present {
156+
if cerrdefs.IsNotFound(err) && !present {
148157
return
149158
}
159+
if err != nil && !cerrdefs.IsNotFound(err) {
160+
t.Errorf("%v(%v): unexpected error while getting entry %v/%v in network %q: %v",
161+
nDB.config.Hostname, nDB.config.NodeID, tname, key, nid, err)
162+
}
150163

151164
time.Sleep(50 * time.Millisecond)
152165
}
153166

154-
t.Errorf("Entry existence verification test failed for %v(%v)", nDB.config.Hostname, nDB.config.NodeID)
167+
t.Errorf("%v(%v): want entry %v/%v in network %q to be (present=%v, value=%q), got (present=%v, value=%q)",
168+
nDB.config.Hostname, nDB.config.NodeID, tname, key, nid, present, value, !present, string(v))
155169
}
156170

157171
func testWatch(t *testing.T, ch chan events.Event, ev interface{}, tname, nid, key, value string) {
@@ -274,6 +288,19 @@ func TestNetworkDBCRUDTableEntry(t *testing.T) {
274288
closeNetworkDBInstances(t, dbs)
275289
}
276290

291+
func (nDB *NetworkDB) dumpTable(t *testing.T, tname string) {
292+
t.Helper()
293+
var b strings.Builder
294+
tw := tabwriter.NewWriter(&b, 10, 1, 1, ' ', 0)
295+
tw.Write([]byte("NetworkID\tKey\tValue\tFlags\n"))
296+
nDB.WalkTable(tname, func(nid, key string, value []byte, deleting bool) bool {
297+
fmt.Fprintf(tw, "%s\t%s\t%s\t%v\n", nid, key, string(value), map[bool]string{true: "D"}[deleting])
298+
return false
299+
})
300+
tw.Flush()
301+
t.Logf("%s(%s): Table %s:\n%s", nDB.config.Hostname, nDB.config.NodeID, tname, b.String())
302+
}
303+
277304
func TestNetworkDBCRUDTableEntries(t *testing.T) {
278305
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
279306

@@ -302,18 +329,24 @@ func TestNetworkDBCRUDTableEntries(t *testing.T) {
302329
assert.NilError(t, err)
303330
}
304331

332+
for n := range dbs {
333+
dbs[n].dumpTable(t, "test_table")
334+
}
335+
305336
for i := 1; i <= n; i++ {
306337
dbs[0].verifyEntryExistence(t, "test_table", "network1",
307338
fmt.Sprintf("test_key1%d", i),
308339
fmt.Sprintf("test_value1%d", i), true)
309-
assert.NilError(t, err)
310340
}
311341

312342
for i := 1; i <= n; i++ {
313343
dbs[1].verifyEntryExistence(t, "test_table", "network1",
314344
fmt.Sprintf("test_key0%d", i),
315345
fmt.Sprintf("test_value0%d", i), true)
316-
assert.NilError(t, err)
346+
}
347+
348+
for n := range dbs {
349+
dbs[n].dumpTable(t, "test_table")
317350
}
318351

319352
// Verify deletes
@@ -332,13 +365,15 @@ func TestNetworkDBCRUDTableEntries(t *testing.T) {
332365
for i := 1; i <= n; i++ {
333366
dbs[0].verifyEntryExistence(t, "test_table", "network1",
334367
fmt.Sprintf("test_key1%d", i), "", false)
335-
assert.NilError(t, err)
336368
}
337369

338370
for i := 1; i <= n; i++ {
339371
dbs[1].verifyEntryExistence(t, "test_table", "network1",
340372
fmt.Sprintf("test_key0%d", i), "", false)
341-
assert.NilError(t, err)
373+
}
374+
375+
for n := range dbs {
376+
dbs[n].dumpTable(t, "test_table")
342377
}
343378

344379
closeNetworkDBInstances(t, dbs)

0 commit comments

Comments
 (0)