Skip to content

Commit

Permalink
Add integration test for listing policies
Browse files Browse the repository at this point in the history
  • Loading branch information
creydr committed Jun 21, 2024
1 parent 74db296 commit 4b2cc5c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 8 deletions.
178 changes: 171 additions & 7 deletions pkg/reconciler/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"context"
"errors"
"fmt"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
v1 "knative.dev/eventing/pkg/apis/messaging/v1"
"testing"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -48,6 +50,9 @@ import (
const (
testNS = "test-namespace"
channelName = "test-channel"

readyEventPolicyName = "test-event-policy-ready"
unreadyEventPolicyName = "test-event-policy-unready"
)

var (
Expand Down Expand Up @@ -145,7 +150,8 @@ func TestReconcile(t *testing.T) {
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelDLSUnknown(),
WithChannelAddress(&backingChannelAddressable)),
WithChannelAddress(&backingChannelAddressable),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
}},
}, {
Name: "Already reconciled",
Expand All @@ -157,7 +163,8 @@ func TestReconcile(t *testing.T) {
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelAddress(&backingChannelAddressable),
WithChannelDLSUnknown()),
WithChannelDLSUnknown(),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
NewInMemoryChannel(channelName, testNS,
WithInitInMemoryChannelConditions,
WithInMemoryChannelDeploymentReady(),
Expand Down Expand Up @@ -190,7 +197,8 @@ func TestReconcile(t *testing.T) {
WithBackingChannelObjRef(backingChannelObjRef()),
WithChannelNoAddress(),
WithChannelDLSUnknown(),
WithBackingChannelUnknown("BackingChannelNotConfigured", "BackingChannel has not yet been reconciled.")),
WithBackingChannelUnknown("BackingChannelNotConfigured", "BackingChannel has not yet been reconciled."),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
}},
}, {
Name: "Backing channel created with delivery",
Expand Down Expand Up @@ -240,7 +248,8 @@ func TestReconcile(t *testing.T) {
WithChannelNoAddress(),
WithChannelDelivery(deliverySpec),
WithChannelDLSUnknown(),
WithBackingChannelUnknown("BackingChannelNotConfigured", "BackingChannel has not yet been reconciled.")),
WithBackingChannelUnknown("BackingChannelNotConfigured", "BackingChannel has not yet been reconciled."),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
}},
}, {
Name: "Generation Bump",
Expand Down Expand Up @@ -274,7 +283,8 @@ func TestReconcile(t *testing.T) {
WithChannelAddress(&backingChannelAddressable),
WithChannelGeneration(42),
// Updates
WithChannelObservedGeneration(42)),
WithChannelObservedGeneration(42),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
}},
}, {
Name: "Updating subscribers statuses",
Expand All @@ -285,7 +295,8 @@ func TestReconcile(t *testing.T) {
WithInitChannelConditions,
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelAddress(&backingChannelAddressable)),
WithChannelAddress(&backingChannelAddressable),
WithChannelEventPoliciesReady()),
NewInMemoryChannel(channelName, testNS,
WithInitInMemoryChannelConditions,
WithInMemoryChannelDuckAnnotationV1Beta1,
Expand All @@ -307,7 +318,123 @@ func TestReconcile(t *testing.T) {
WithBackingChannelReady,
WithChannelAddress(&backingChannelAddressable),
WithChannelSubscriberStatuses(subscriberStatuses()),
WithChannelDLSUnknown()),
WithChannelDLSUnknown(),
WithChannelEventPoliciesReadyBecauseOIDCDisabled()),
}},
}, {
Name: "Should provision applying EventPolicies",
Key: testKey,
Objects: []runtime.Object{
NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions),
NewInMemoryChannel(channelName, testNS,
WithInitInMemoryChannelConditions,
WithInMemoryChannelDeploymentReady(),
WithInMemoryChannelServiceReady(),
WithInMemoryChannelEndpointsReady(),
WithInMemoryChannelChannelServiceReady(),
WithInMemoryChannelAddress(backingChannelAddressable),
WithInMemoryChannelDLSUnknown(),
WithInMemoryChannelEventPoliciesReady()),
NewEventPolicy(readyEventPolicyName, testNS,
WithReadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "Channel",
Name: channelName,
}),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions,
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelDLSUnknown(),
WithChannelAddress(&backingChannelAddressable),
WithChannelEventPoliciesReady(),
WithChannelEventPoliciesListed(readyEventPolicyName)),
}},
}, {
Name: "Should mark as NotReady on unready EventPolicies",
Key: testKey,
Objects: []runtime.Object{
NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions),
NewInMemoryChannel(channelName, testNS,
WithInitInMemoryChannelConditions,
WithInMemoryChannelDeploymentReady(),
WithInMemoryChannelServiceReady(),
WithInMemoryChannelEndpointsReady(),
WithInMemoryChannelChannelServiceReady(),
WithInMemoryChannelAddress(backingChannelAddressable),
WithInMemoryChannelDLSUnknown(),
WithInMemoryChannelEventPoliciesReady()),
NewEventPolicy(unreadyEventPolicyName, testNS,
WithUnreadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "Channel",
Name: channelName,
}),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions,
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelDLSUnknown(),
WithChannelAddress(&backingChannelAddressable),
WithChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", unreadyEventPolicyName))),
}},
}, {
Name: "should list only Ready EventPolicies",
Key: testKey,
Objects: []runtime.Object{
NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions),
NewInMemoryChannel(channelName, testNS,
WithInitInMemoryChannelConditions,
WithInMemoryChannelDeploymentReady(),
WithInMemoryChannelServiceReady(),
WithInMemoryChannelEndpointsReady(),
WithInMemoryChannelChannelServiceReady(),
WithInMemoryChannelAddress(backingChannelAddressable),
WithInMemoryChannelDLSUnknown(),
WithInMemoryChannelEventPoliciesReady()),
NewEventPolicy(readyEventPolicyName, testNS,
WithReadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "Channel",
Name: channelName,
}),
),
NewEventPolicy(unreadyEventPolicyName, testNS,
WithUnreadyEventPolicyCondition,
WithEventPolicyToRef(v1alpha1.EventPolicyToReference{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "Channel",
Name: channelName,
}),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewChannel(channelName, testNS,
WithChannelTemplate(channelCRD()),
WithInitChannelConditions,
WithBackingChannelObjRef(backingChannelObjRef()),
WithBackingChannelReady,
WithChannelDLSUnknown(),
WithChannelAddress(&backingChannelAddressable),
WithChannelEventPoliciesNotReady("EventPoliciesNotReady", fmt.Sprintf("event policies %s are not ready", unreadyEventPolicyName)),
WithChannelEventPoliciesListed(readyEventPolicyName)),
}},
}}

Expand Down Expand Up @@ -479,3 +606,40 @@ func createChannel(namespace, name string, ready bool) *unstructured.Unstructure
},
}
}

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: "Channel",
Name: channelName,
},
},
},
},
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
}
10 changes: 9 additions & 1 deletion pkg/reconciler/channel/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package channel

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/feature"
"testing"

"knative.dev/pkg/configmap"
Expand All @@ -34,7 +37,12 @@ import (
func TestNew(t *testing.T) {
ctx, _ := SetupFakeContext(t)

c := NewController(ctx, configmap.NewStaticWatcher())
c := NewController(ctx, configmap.NewStaticWatcher(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: feature.FlagsConfigName,
Namespace: "knative-eventing",
},
}))

if c == nil {
t.Fatal("Expected NewController to return a non-nil value")
Expand Down
31 changes: 31 additions & 0 deletions pkg/reconciler/testing/v1/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package testing
import (
"context"
"fmt"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/eventing/pkg/apis/feature"
"time"

"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -165,6 +167,35 @@ func WithChannelEventPoliciesReady() ChannelOption {
}
}

func WithChannelEventPoliciesNotReady(reason, message string) ChannelOption {
return func(c *eventingv1.Channel) {
c.Status.MarkEventPoliciesFailed(reason, message)
}
}

func WithChannelEventPoliciesListed(policyNames ...string) ChannelOption {
return func(c *eventingv1.Channel) {
for _, name := range policyNames {
c.Status.Policies = append(c.Status.Policies, eventingduckv1.AppliedEventPolicyRef{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Name: name,
})
}
}
}

func WithChannelEventPoliciesReadyBecauseOIDCDisabled() ChannelOption {
return func(c *eventingv1.Channel) {
c.Status.MarkEventPoliciesTrueWithReason("OIDCDisabled", "Feature %q must be enabled to support Authorization", feature.OIDCAuthentication)
}
}

func WithChannelEventPoliciesReadyBecauseNoPolicyAndOIDCEnabled() ChannelOption {
return func(c *eventingv1.Channel) {
c.Status.MarkEventPoliciesTrueWithReason("DefaultAuthorizationMode", "Default authz mode is %q", feature.AuthorizationAllowSameNamespace)
}
}

func WithChannelDLSResolvedFailed() ChannelOption {
return func(c *eventingv1.Channel) {
c.Status.MarkDeadLetterSinkResolvedFailed(
Expand Down

0 comments on commit 4b2cc5c

Please sign in to comment.