1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

Merge pull request #1816 from marquiz/devel/gc-test-assert-msg

tests: better assertion message in nfd-gc unit tests
This commit is contained in:
Kubernetes Prow Robot 2024-10-31 19:33:27 +00:00 committed by GitHub
commit 1c6ce897f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,6 +18,7 @@ package nfdgarbagecollector
import (
"context"
"fmt"
"testing"
"time"
@ -41,7 +42,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)
@ -52,7 +53,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)
@ -67,7 +68,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"})
@ -83,7 +84,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")
})
}
@ -132,22 +133,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)
nrtNames := sets.NewString()
for _, meta := range rsp.Items {
nrtNames.Insert(meta.Name)
if err != nil {
return fmt.Sprintf("failed to list: %v", err)
}
if nrtNames.Equal(nameSet) {
return true
actual = sets.New[string]()
for _, meta := range rsp.Items {
actual.Insert(meta.Name)
}
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))
}