From 56a01a704c62b53ea30ab0f46caa6b6875cd0afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Mon, 24 Jun 2024 11:57:39 +0200 Subject: [PATCH] Simplify WithEventPolicyToRef --- .../controller/inmemorychannel_test.go | 32 ++++++------------- pkg/reconciler/testing/v1/eventpolicy.go | 25 +++++++++------ 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go b/pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go index 11c0aca30cd..f9f859444ba 100644 --- a/pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go +++ b/pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go @@ -22,8 +22,6 @@ import ( "strconv" "testing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/pointer" @@ -105,6 +103,12 @@ var ( URL: dlsURI, CACerts: nil, } + + imcV1GVK = metav1.GroupVersionKind{ + Group: "messaging.knative.dev", + Version: "v1", + Kind: "InMemoryChannel", + } ) func TestAllCases(t *testing.T) { @@ -659,11 +663,7 @@ func TestAllCases(t *testing.T) { makeChannelService(NewInMemoryChannel(imcName, testNS)), NewEventPolicy(readyEventPolicyName, testNS, WithReadyEventPolicyCondition, - WithEventPolicyToRef(v1alpha1.EventPolicyToReference{ - APIVersion: v1.SchemeGroupVersion.String(), - Kind: "InMemoryChannel", - Name: imcName, - }), + WithEventPolicyToRef(imcV1GVK, imcName), ), }, WantErr: false, @@ -698,11 +698,7 @@ func TestAllCases(t *testing.T) { makeChannelService(NewInMemoryChannel(imcName, testNS)), NewEventPolicy(unreadyEventPolicyName, testNS, WithUnreadyEventPolicyCondition, - WithEventPolicyToRef(v1alpha1.EventPolicyToReference{ - APIVersion: v1.SchemeGroupVersion.String(), - Kind: "InMemoryChannel", - Name: imcName, - }), + WithEventPolicyToRef(imcV1GVK, imcName), ), }, WantErr: false, @@ -736,19 +732,11 @@ func TestAllCases(t *testing.T) { makeChannelService(NewInMemoryChannel(imcName, testNS)), NewEventPolicy(readyEventPolicyName, testNS, WithReadyEventPolicyCondition, - WithEventPolicyToRef(v1alpha1.EventPolicyToReference{ - APIVersion: v1.SchemeGroupVersion.String(), - Kind: "InMemoryChannel", - Name: imcName, - }), + WithEventPolicyToRef(imcV1GVK, imcName), ), NewEventPolicy(unreadyEventPolicyName, testNS, WithUnreadyEventPolicyCondition, - WithEventPolicyToRef(v1alpha1.EventPolicyToReference{ - APIVersion: v1.SchemeGroupVersion.String(), - Kind: "InMemoryChannel", - Name: imcName, - }), + WithEventPolicyToRef(imcV1GVK, imcName), ), }, WantErr: false, diff --git a/pkg/reconciler/testing/v1/eventpolicy.go b/pkg/reconciler/testing/v1/eventpolicy.go index a54b07a03e0..28766921c71 100644 --- a/pkg/reconciler/testing/v1/eventpolicy.go +++ b/pkg/reconciler/testing/v1/eventpolicy.go @@ -66,23 +66,28 @@ func WithUnreadyEventPolicyCondition(ep *v1alpha1.EventPolicy) { } } -func WithEventPolicyTo(tos ...v1alpha1.EventPolicySpecTo) EventPolicyOption { - return func(ep *v1alpha1.EventPolicy) { - ep.Spec.To = append(ep.Spec.To, tos...) - } -} - -func WithEventPolicyToRef(ref v1alpha1.EventPolicyToReference) EventPolicyOption { +func WithEventPolicyToRef(gvk metav1.GroupVersionKind, name string) EventPolicyOption { return func(ep *v1alpha1.EventPolicy) { ep.Spec.To = append(ep.Spec.To, v1alpha1.EventPolicySpecTo{ - Ref: &ref, + Ref: &v1alpha1.EventPolicyToReference{ + APIVersion: apiVersion(gvk), + Kind: gvk.Kind, + Name: name, + }, }) } } -func WithEventPolicyFrom(froms ...v1alpha1.EventPolicySpecFrom) EventPolicyOption { +func WithEventPolicyFrom(gvk metav1.GroupVersionKind, name, namespace string) EventPolicyOption { return func(ep *v1alpha1.EventPolicy) { - ep.Spec.From = append(ep.Spec.From, froms...) + ep.Spec.From = append(ep.Spec.From, v1alpha1.EventPolicySpecFrom{ + Ref: &v1alpha1.EventPolicyFromReference{ + APIVersion: apiVersion(gvk), + Kind: gvk.Kind, + Name: name, + Namespace: namespace, + }, + }) } }