Skip to content

Commit

Permalink
Simplify WithEventPolicyToRef
Browse files Browse the repository at this point in the history
  • Loading branch information
creydr committed Jun 24, 2024
1 parent 693438c commit 56a01a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
32 changes: 10 additions & 22 deletions pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 15 additions & 10 deletions pkg/reconciler/testing/v1/eventpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
}
}

Expand Down

0 comments on commit 56a01a7

Please sign in to comment.