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:
commit
1c6ce897f2
1 changed files with 22 additions and 14 deletions
|
@ -18,6 +18,7 @@ package nfdgarbagecollector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ func TestNRTGC(t *testing.T) {
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
go func() { errChan <- gc.Run() }()
|
go func() { errChan <- gc.Run() }()
|
||||||
|
|
||||||
So(waitForNRT(gc.client), ShouldBeTrue)
|
So(gc.client, shouldEventuallyHaveNRTs)
|
||||||
|
|
||||||
gc.Stop()
|
gc.Stop()
|
||||||
So(<-errChan, ShouldBeNil)
|
So(<-errChan, ShouldBeNil)
|
||||||
|
@ -52,7 +53,7 @@ func TestNRTGC(t *testing.T) {
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
go func() { errChan <- gc.Run() }()
|
go func() { errChan <- gc.Run() }()
|
||||||
|
|
||||||
So(waitForNRT(gc.client, "node1"), ShouldBeTrue)
|
So(gc.client, shouldEventuallyHaveNRTs, "node1")
|
||||||
|
|
||||||
gc.Stop()
|
gc.Stop()
|
||||||
So(<-errChan, ShouldBeNil)
|
So(<-errChan, ShouldBeNil)
|
||||||
|
@ -67,7 +68,7 @@ func TestNRTGC(t *testing.T) {
|
||||||
err := gc.client.Resource(gvr).Delete(context.TODO(), "node1", metav1.DeleteOptions{})
|
err := gc.client.Resource(gvr).Delete(context.TODO(), "node1", metav1.DeleteOptions{})
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
So(waitForNRT(gc.client, "node2"), ShouldBeTrue)
|
So(gc.client, shouldEventuallyHaveNRTs, "node2")
|
||||||
})
|
})
|
||||||
Convey("periodic GC should remove obsolete NRT", t, func() {
|
Convey("periodic GC should remove obsolete NRT", t, func() {
|
||||||
gc := newMockGC([]string{"node1", "node2"}, []string{"node1", "node2"})
|
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{})
|
_, err := gc.client.Resource(gvr).(fake.MetadataClient).CreateFake(nrt, metav1.CreateOptions{})
|
||||||
So(err, ShouldBeNil)
|
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
|
client metadataclient.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForNRT(cli metadataclient.Interface, names ...string) bool {
|
func shouldEventuallyHaveNRTs(actualI interface{}, expectedI ...interface{}) string {
|
||||||
nameSet := sets.NewString(names...)
|
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")
|
gvr := topologyv1alpha2.SchemeGroupVersion.WithResource("noderesourcetopologies")
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
rsp, err := cli.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
|
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()
|
|
||||||
for _, meta := range rsp.Items {
|
|
||||||
nrtNames.Insert(meta.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if nrtNames.Equal(nameSet) {
|
actual = sets.New[string]()
|
||||||
return true
|
for _, meta := range rsp.Items {
|
||||||
|
actual.Insert(meta.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if actual.Equal(expected) {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
return false
|
return fmt.Sprintf("Expected: %v\nActual: %v", sets.List(expected), sets.List(actual))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue