Silin-Chen's picture
CrossSWE-Bench: current 171-instance release
0e239c6
Raw History Blame Contribute Delete
4.98 kB
package crossrepo_native_gate_test
import (
"context"
"reflect"
"strings"
"testing"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
eventsv1 "k8s.io/api/events/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/events"
)
type annotatedRecorder interface {
AnnotatedEventf(runtime.Object, runtime.Object, map[string]string, string, string, string, string, ...interface{})
}
type eventSink struct{ received chan *eventsv1.Event }
func (s *eventSink) Create(_ context.Context, event *eventsv1.Event) (*eventsv1.Event, error) {
s.received <- event.DeepCopy()
return event.DeepCopy(), nil
}
func (s *eventSink) Update(_ context.Context, event *eventsv1.Event) (*eventsv1.Event, error) {
s.received <- event.DeepCopy()
return event.DeepCopy(), nil
}
func (s *eventSink) Patch(_ context.Context, event *eventsv1.Event, _ []byte) (*eventsv1.Event, error) {
s.received <- event.DeepCopy()
return event.DeepCopy(), nil
}
func newRecorder(t *testing.T) (events.EventRecorderLogger, *eventSink) {
t.Helper()
sink := &eventSink{received: make(chan *eventsv1.Event, 16)}
broadcaster := events.NewBroadcaster(sink)
ctx, cancel := context.WithCancel(context.Background())
if err := broadcaster.StartRecordingToSinkWithContext(ctx); err != nil {
cancel()
t.Fatalf("start actual client-go broadcaster: %v", err)
}
t.Cleanup(func() { cancel(); broadcaster.Shutdown() })
return broadcaster.NewRecorder(scheme.Scheme, "crossrepo.example/recorder"), sink
}
func pod(name, uid string) *corev1.Pod {
return &corev1.Pod{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}, ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "contract-ns", UID: types.UID(uid)}}
}
func receive(t *testing.T, sink *eventSink) *eventsv1.Event {
t.Helper()
select {
case event := <-sink.received:
return event
case <-time.After(8 * time.Second):
t.Fatal("actual client-go broadcaster emitted no sink payload")
return nil
}
}
func checkEvent(t *testing.T, event *eventsv1.Event, reason, eventType string, annotations map[string]string, related bool) {
t.Helper()
if len(annotations) == 0 {
if len(event.Annotations) != 0 {
t.Fatalf("unexpected annotations: %#v", event.Annotations)
}
} else if !reflect.DeepEqual(event.Annotations, annotations) {
t.Fatalf("annotations lost or changed: got %#v, want %#v", event.Annotations, annotations)
}
if event.Type != eventType || event.Reason != reason || event.Action != "ContractAction" || event.Note != "emitted value 17" {
t.Fatalf("ordinary event fields changed: %#v", event)
}
if event.Namespace != "contract-ns" || event.Regarding.Kind != "Pod" || event.Regarding.APIVersion != "v1" || event.Regarding.Name != "subject" || event.Regarding.UID != "subject-uid" {
t.Fatalf("regarding reference or namespace changed: %#v", event)
}
if event.ReportingController != "crossrepo.example/recorder" || !strings.HasPrefix(event.ReportingInstance, "crossrepo.example/recorder-") || event.EventTime.IsZero() {
t.Fatalf("reporter or event timestamp missing: %#v", event)
}
if related {
if event.Related == nil || event.Related.Name != "related" || event.Related.UID != "related-uid" || event.Related.Kind != "Pod" {
t.Fatalf("related reference lost: %#v", event.Related)
}
} else if event.Related != nil {
t.Fatalf("unexpected related reference: %#v", event.Related)
}
}
func TestCrossRepoNativeAnnotatedClientGo(t *testing.T) {
for _, logged := range []bool{false, true} {
name := "default"
if logged {
name = "WithLogger"
}
t.Run(name, func(t *testing.T) {
recorder, sink := newRecorder(t)
if logged {
recorder = recorder.WithLogger(logr.Discard())
}
annotated, ok := interface{}(recorder).(annotatedRecorder)
if !ok {
t.Fatal("actual client-go recorder lacks callable AnnotatedEventf support")
}
annotations := map[string]string{"crossrepo.example/source": "contract", "details": "punctuation: ✓"}
annotated.AnnotatedEventf(pod("subject", "subject-uid"), pod("related", "related-uid"), annotations, corev1.EventTypeNormal, "AnnotatedPayload", "ContractAction", "emitted %s %d", "value", 17)
checkEvent(t, receive(t, sink), "AnnotatedPayload", corev1.EventTypeNormal, annotations, true)
recorder.Eventf(pod("subject", "subject-uid"), nil, corev1.EventTypeWarning, "OrdinaryAfterAnnotated", "ContractAction", "emitted %s %d", "value", 17)
checkEvent(t, receive(t, sink), "OrdinaryAfterAnnotated", corev1.EventTypeWarning, nil, false)
})
}
}
func TestCrossRepoNativeClientGoEventfP2P(t *testing.T) {
recorder, sink := newRecorder(t)
recorder.Eventf(pod("subject", "subject-uid"), pod("related", "related-uid"), corev1.EventTypeNormal, "OrdinaryBaseline", "ContractAction", "emitted %s %d", "value", 17)
checkEvent(t, receive(t, sink), "OrdinaryBaseline", corev1.EventTypeNormal, nil, true)
}