Skip to content

Commit

Permalink
chore(dra): improve assert event for unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Sep 29, 2024
1 parent dad39bf commit ca5775b
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -29,10 +30,12 @@ import (
resourceapi "k8s.io/api/resource/v1alpha3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2/ktesting"
_ "k8s.io/klog/v2/ktesting/init"
)
Expand Down Expand Up @@ -126,6 +129,10 @@ func TestController(t *testing.T) {
schedulingCtx, expectedSchedulingCtx *resourceapi.PodSchedulingContext
claim, expectedClaim *resourceapi.ResourceClaim
expectedError string
// expectedEvent is a slice of strings representing expected events.
// Each string in the slice should follow the format: "EventType Reason Message".
// - "Warning Failed processing failed"
expectedEvent []string
}{
"invalid-key": {
key: "claim:x/y/z",
Expand Down Expand Up @@ -340,6 +347,10 @@ func TestController(t *testing.T) {
) {
t.Fatal("could not sync caches")
}
cc := ctrl.(*controller)
// We need to mock the event recorder to test the controller's event.
fakeRecorder := record.NewFakeRecorder(100)
cc.eventRecorder = fakeRecorder
_, err := ctrl.(*controller).syncKey(ctx, test.key)
if err != nil && test.expectedError == "" {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -365,10 +376,8 @@ func TestController(t *testing.T) {
expectedPodSchedulings = append(expectedPodSchedulings, *test.expectedSchedulingCtx)
}
assert.Equal(t, expectedPodSchedulings, podSchedulings.Items)

// TODO: add testing of events.
// Right now, client-go/tools/record/event.go:267 fails during unit testing with
// request namespace does not match object namespace, request: "" object: "default",
// Assert that the events are correct.
assertEqualEvents(t, test.expectedEvent, fakeRecorder.Events)
})
}
}
Expand Down Expand Up @@ -528,3 +537,28 @@ func fakeK8s(objs []runtime.Object) (kubernetes.Interface, informers.SharedInfor
informerFactory := informers.NewSharedInformerFactory(client, 0)
return client, informerFactory
}

func assertEqualEvents(t *testing.T, expected []string, actual <-chan string) {
t.Logf("Assert for events: %v", expected)
c := time.After(wait.ForeverTestTimeout)
for _, e := range expected {
select {
case a := <-actual:
if e != a {
t.Errorf("Expected event %q, got %q", e, a)
return
}
case <-c:
t.Errorf("Expected event %q, got nothing", e)
// continue iterating to print all expected events
}
}
for {
select {
case a := <-actual:
t.Errorf("Unexpected event: %q", a)
default:
return // No more events, as expected.
}
}
}

0 comments on commit ca5775b

Please sign in to comment.