Skip to content

Commit

Permalink
Refactor to use EventPolicy helper functions from reconciler/testing …
Browse files Browse the repository at this point in the history
…package
  • Loading branch information
creydr committed Jun 20, 2024
1 parent ed231fd commit 86885c3
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 49 deletions.
46 changes: 46 additions & 0 deletions pkg/auth/event_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,49 @@ func TestSubjectContained(t *testing.T) {
})
}
}

/*func TestGetApplyingResourcesOfEventPolicyForGK(t *testing.T) {
tests := []struct {
name string
eventPolicy *v1alpha1.EventPolicy
existingObjects []runtime.Object
want []string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, _ := reconcilertesting.SetupFakeContext(t)
ls := testingv1.NewListers(tt.existingObjects)
ls.GetEventPolicyLister()
ls.
eventpolicyinformerfake.Get(ctx).Informer().GetIndexer()
for i := range tt.existingPolicies {
err := eventpolicyinformerfake.Get(ctx).Informer().GetStore().Add(&tt.existingPolicies[i])
if err != nil {
t.Fatalf("error adding policies: %v", err)
}
}
brokerGVK := eventingv1.SchemeGroupVersion.WithKind("Broker")
got, err := GetEventPoliciesForResource(eventpolicyinformerfake.Get(ctx).Lister(), brokerGVK, tt.resourceObjectMeta)
if (err != nil) != tt.wantErr {
t.Errorf("GetEventPoliciesForResource() error = %v, wantErr %v", err, tt.wantErr)
return
}
got, err := GetApplyingResourcesOfEventPolicyForGK(tt.eventPolicy, tt.args.gk, tt.args.gvkIndexer)
if (err != nil) != tt.wantErr {
t.Errorf("GetApplyingResourcesOfEventPolicyForGK() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetApplyingResourcesOfEventPolicyForGK() got = %v, want %v", got, tt.want)
}
})
}
}*/
81 changes: 36 additions & 45 deletions pkg/reconciler/inmemorychannel/controller/inmemorychannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,14 @@ func TestAllCases(t *testing.T) {
WithInMemoryChannelGeneration(imcGeneration),
),
makeChannelService(NewInMemoryChannel(imcName, testNS)),
makeReadyEventPolicy(),
NewEventPolicy(readyEventPolicyName, testNS,
WithReadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "InMemoryChannel",
Name: imcName,
}),
),
},
WantErr: false,
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Expand All @@ -673,7 +680,7 @@ func TestAllCases(t *testing.T) {
WithDeadLetterSink(imcDest),
WithInMemoryChannelStatusDLS(dlsStatus),
WithInMemoryChannelEventPoliciesReady(),
WithInMemoryChannelEventPoliciesListed(makeReadyEventPolicy()),
WithInMemoryChannelEventPoliciesListed(readyEventPolicyName),
),
}},
}, {
Expand All @@ -689,7 +696,14 @@ func TestAllCases(t *testing.T) {
WithInMemoryChannelGeneration(imcGeneration),
),
makeChannelService(NewInMemoryChannel(imcName, testNS)),
makeUnreadyEventPolicy(),
NewEventPolicy(unreadyEventPolicyName, testNS,
WithUnreadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "InMemoryChannel",
Name: imcName,
}),
),
},
WantErr: false,
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Expand All @@ -704,7 +718,7 @@ func TestAllCases(t *testing.T) {
WithInMemoryChannelAddress(channelServiceAddress),
WithDeadLetterSink(imcDest),
WithInMemoryChannelStatusDLS(dlsStatus),
WithInMemoryChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", makeUnreadyEventPolicy().Name)),
WithInMemoryChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", unreadyEventPolicyName)),
),
}},
}, {
Expand All @@ -720,8 +734,22 @@ func TestAllCases(t *testing.T) {
WithInMemoryChannelGeneration(imcGeneration),
),
makeChannelService(NewInMemoryChannel(imcName, testNS)),
makeReadyEventPolicy(),
makeUnreadyEventPolicy(),
NewEventPolicy(readyEventPolicyName, testNS,
WithReadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "InMemoryChannel",
Name: imcName,
}),
),
NewEventPolicy(unreadyEventPolicyName, testNS,
WithUnreadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "InMemoryChannel",
Name: imcName,
}),
),
},
WantErr: false,
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Expand All @@ -736,8 +764,8 @@ func TestAllCases(t *testing.T) {
WithInMemoryChannelAddress(channelServiceAddress),
WithDeadLetterSink(imcDest),
WithInMemoryChannelStatusDLS(dlsStatus),
WithInMemoryChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", makeUnreadyEventPolicy().Name)),
WithInMemoryChannelEventPoliciesListed(makeReadyEventPolicy()),
WithInMemoryChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", unreadyEventPolicyName)),
WithInMemoryChannelEventPoliciesListed(readyEventPolicyName),
),
}},
}}
Expand Down Expand Up @@ -922,43 +950,6 @@ func makeUnknownDeployment() *appsv1.Deployment {
return d
}

func makeEventPolicy(eventPolicyName string) *v1alpha1.EventPolicy {
return &v1alpha1.EventPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "eventing.knative.dev/v1alpha1",
Kind: "EventPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: testNS,
Name: eventPolicyName,
},
Spec: v1alpha1.EventPolicySpec{
To: []v1alpha1.EventPolicySpecTo{
{
Ref: &v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "InMemoryChannel",
Name: imcName,
},
},
},
},
Status: v1alpha1.EventPolicyStatus{},
}
}

func makeReadyEventPolicy() *v1alpha1.EventPolicy {
policy := makeEventPolicy(readyEventPolicyName)
policy.Status.Conditions = []apis.Condition{{Type: v1alpha1.EventPolicyConditionReady, Status: corev1.ConditionTrue}}
return policy
}

func makeUnreadyEventPolicy() *v1alpha1.EventPolicy {
policy := makeEventPolicy(unreadyEventPolicyName)
policy.Status.Conditions = []apis.Condition{{Type: v1alpha1.EventPolicyConditionReady, Status: corev1.ConditionFalse}}
return policy
}

func makeService() *corev1.Service {
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Expand Down
104 changes: 104 additions & 0 deletions pkg/reconciler/testing/v1/eventpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*

Check failure on line 1 in pkg/reconciler/testing/v1/eventpolicy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Auto-format and Check

Please run goimports. diff --git a/pkg/reconciler/testing/v1/eventpolicy.go b/pkg/reconciler/testing/v1/eventpolicy.go index 95c4344..ee20e7c 100644 --- a/pkg/reconciler/testing/v1/eventpolicy.go +++ b/pkg/reconciler/testing/v1/eventpolicy.go @@ -18,6 +18,7 @@ package testing import ( "context" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
Copyright 2024 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testing

import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/pkg/apis"
)

// EventPolicyOption enables further configuration of an EventPolicy.
type EventPolicyOption func(*v1alpha1.EventPolicy)

// NewEventPolicy creates a EventPolicy with EventPolicyOptions.
func NewEventPolicy(name, namespace string, o ...EventPolicyOption) *v1alpha1.EventPolicy {
ep := &v1alpha1.EventPolicy{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
for _, opt := range o {
opt(ep)
}
ep.SetDefaults(context.Background())

return ep
}

func WithInitEventPolicyConditions(et *v1alpha1.EventPolicy) {
et.Status.InitializeConditions()
}

func WithReadyEventPolicyCondition(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = []apis.Condition{
{
Type: v1alpha1.EventPolicyConditionReady,
Status: corev1.ConditionTrue,
},
}
}

func WithUnreadyEventPolicyCondition(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = []apis.Condition{
{
Type: v1alpha1.EventPolicyConditionReady,
Status: corev1.ConditionFalse,
},
}
}

func WithEventPolicyTo(tos ...v1alpha1.EventPolicySpecTo) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
for _, to := range tos {

Check failure on line 70 in pkg/reconciler/testing/v1/eventpolicy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

S1011: should replace loop with `ep.Spec.To = append(ep.Spec.To, tos...)` (gosimple)
ep.Spec.To = append(ep.Spec.To, to)
}
}
}

func WithEventPolicyToRef(ref v1alpha1.EventPolicyToReference) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
ep.Spec.To = append(ep.Spec.To, v1alpha1.EventPolicySpecTo{
Ref: &ref,
})
}
}

func WithEventPolicyFrom(froms ...v1alpha1.EventPolicySpecFrom) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
for _, from := range froms {

Check failure on line 86 in pkg/reconciler/testing/v1/eventpolicy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

S1011: should replace loop with `ep.Spec.From = append(ep.Spec.From, froms...)` (gosimple)
ep.Spec.From = append(ep.Spec.From, from)
}
}
}

func WithEventPolicyLabels(labels map[string]string) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
ep.ObjectMeta.Labels = labels
}
}

func WithEventPolicyOwnerReference(ownerRef metav1.OwnerReference) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
ep.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
ownerRef,
}
}
}
8 changes: 4 additions & 4 deletions pkg/reconciler/testing/v1/inmemorychannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ func WithInMemoryChannelEventPoliciesNotReady(reason, message string) InMemoryCh
}
}

func WithInMemoryChannelEventPoliciesListed(policy ...*v1alpha1.EventPolicy) InMemoryChannelOption {
func WithInMemoryChannelEventPoliciesListed(policyNames ...string) InMemoryChannelOption {
return func(imc *v1.InMemoryChannel) {
for _, p := range policy {
for _, names := range policyNames {
imc.Status.Policies = append(imc.Status.Policies, eventingduckv1.AppliedEventPolicyRef{
APIVersion: p.APIVersion,
Name: p.Name,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Name: names,
})
}
}
Expand Down

0 comments on commit 86885c3

Please sign in to comment.