Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: better assertion message in nfd-gc unit tests #1816

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pkg/nfd-gc/nfd-gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package nfdgarbagecollector

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -42,7 +43,7 @@ func TestNRTGC(t *testing.T) {
errChan := make(chan error)
go func() { errChan <- gc.Run() }()

So(waitForNRT(gc.client), ShouldBeTrue)
So(gc.client, shouldEventuallyHaveNRTs)

gc.Stop()
So(<-errChan, ShouldBeNil)
Expand All @@ -53,7 +54,7 @@ func TestNRTGC(t *testing.T) {
errChan := make(chan error)
go func() { errChan <- gc.Run() }()

So(waitForNRT(gc.client, "node1"), ShouldBeTrue)
So(gc.client, shouldEventuallyHaveNRTs, "node1")

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

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

So(waitForNRT(gc.client, "node1", "node2"), ShouldBeTrue)
So(gc.client, shouldEventuallyHaveNRTs, "node1", "node2")
})
}

Expand Down Expand Up @@ -133,22 +134,29 @@ type mockGC struct {
client metadataclient.Interface
}

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

nrtNames := sets.NewString()
actual = sets.New[string]()
for _, meta := range rsp.Items {
nrtNames.Insert(meta.Name)
actual.Insert(meta.Name)
}

if nrtNames.Equal(nameSet) {
return true
if actual.Equal(expected) {
return ""
}
time.Sleep(1 * time.Second)
}
return false
return fmt.Sprintf("Expected: %v\nActual: %v", sets.List(expected), sets.List(actual))
}