Skip to content

Commit 1c6ce89

Browse files
authored
Merge pull request #1816 from marquiz/devel/gc-test-assert-msg
tests: better assertion message in nfd-gc unit tests
2 parents d13729b + d6c1a7e commit 1c6ce89

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

pkg/nfd-gc/nfd-gc_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package nfdgarbagecollector
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"testing"
2223
"time"
2324

@@ -41,7 +42,7 @@ func TestNRTGC(t *testing.T) {
4142
errChan := make(chan error)
4243
go func() { errChan <- gc.Run() }()
4344

44-
So(waitForNRT(gc.client), ShouldBeTrue)
45+
So(gc.client, shouldEventuallyHaveNRTs)
4546

4647
gc.Stop()
4748
So(<-errChan, ShouldBeNil)
@@ -52,7 +53,7 @@ func TestNRTGC(t *testing.T) {
5253
errChan := make(chan error)
5354
go func() { errChan <- gc.Run() }()
5455

55-
So(waitForNRT(gc.client, "node1"), ShouldBeTrue)
56+
So(gc.client, shouldEventuallyHaveNRTs, "node1")
5657

5758
gc.Stop()
5859
So(<-errChan, ShouldBeNil)
@@ -67,7 +68,7 @@ func TestNRTGC(t *testing.T) {
6768
err := gc.client.Resource(gvr).Delete(context.TODO(), "node1", metav1.DeleteOptions{})
6869
So(err, ShouldBeNil)
6970

70-
So(waitForNRT(gc.client, "node2"), ShouldBeTrue)
71+
So(gc.client, shouldEventuallyHaveNRTs, "node2")
7172
})
7273
Convey("periodic GC should remove obsolete NRT", t, func() {
7374
gc := newMockGC([]string{"node1", "node2"}, []string{"node1", "node2"})
@@ -83,7 +84,7 @@ func TestNRTGC(t *testing.T) {
8384
_, err := gc.client.Resource(gvr).(fake.MetadataClient).CreateFake(nrt, metav1.CreateOptions{})
8485
So(err, ShouldBeNil)
8586

86-
So(waitForNRT(gc.client, "node1", "node2"), ShouldBeTrue)
87+
So(gc.client, shouldEventuallyHaveNRTs, "node1", "node2")
8788
})
8889
}
8990

@@ -132,22 +133,29 @@ type mockGC struct {
132133
client metadataclient.Interface
133134
}
134135

135-
func waitForNRT(cli metadataclient.Interface, names ...string) bool {
136-
nameSet := sets.NewString(names...)
136+
func shouldEventuallyHaveNRTs(actualI interface{}, expectedI ...interface{}) string {
137+
cli := actualI.(metadataclient.Interface)
138+
expected := sets.Set[string]{}
139+
for _, e := range expectedI {
140+
expected.Insert(e.(string))
141+
}
142+
actual := sets.Set[string]{}
137143
gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies")
138144
for i := 0; i < 2; i++ {
139145
rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
140-
So(err, ShouldBeNil)
146+
if err != nil {
147+
return fmt.Sprintf("failed to list: %v", err)
148+
}
141149

142-
nrtNames := sets.NewString()
150+
actual = sets.New[string]()
143151
for _, meta := range rsp.Items {
144-
nrtNames.Insert(meta.Name)
152+
actual.Insert(meta.Name)
145153
}
146154

147-
if nrtNames.Equal(nameSet) {
148-
return true
155+
if actual.Equal(expected) {
156+
return ""
149157
}
150158
time.Sleep(1 * time.Second)
151159
}
152-
return false
160+
return fmt.Sprintf("Expected: %v\nActual: %v", sets.List(expected), sets.List(actual))
153161
}

0 commit comments

Comments
 (0)