Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework webhook test method to actually send requests #640

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,66 +1,72 @@
package knativeeventing_test
package knativeeventing

import (
"context"
"os"
"testing"

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/apis"
. "github.com/openshift-knative/serverless-operator/knative-operator/pkg/webhook/knativeeventing"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/webhook/testutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
eventingv1alpha1 "knative.dev/operator/pkg/apis/operator/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)

var (
ke1 = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "ke1",
},
}
ke2 = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "ke2",
},
}

decoder types.Decoder
)

func init() {
apis.AddToScheme(scheme.Scheme)
}

var ke1 = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "ke1",
},
}
var ke2 = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "ke2",
},
decoder, _ = admission.NewDecoder(scheme.Scheme)
}

func TestInvalidNamespace(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_EVENTING_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{ke1})
result := validator.Handle(context.TODO(), types.Request{})
validator.InjectDecoder(decoder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also returns an error but is always nil, weird...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we usually just don't care in tests 🤷


req, err := testutil.RequestFor(ke1)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", ke1, err)
}

result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Error("The required namespace is wrong, but the request is allowed")
}
}

func TestLoneliness(t *testing.T) {
os.Clearenv()

validator := Validator{}
validator.InjectDecoder(&mockDecoder{ke1})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient(ke2))
result := validator.Handle(context.TODO(), types.Request{})
if result.Response.Allowed {
t.Errorf("Too many KnativeEventings: %v", result.Response)
}
}

type mockDecoder struct {
ke *eventingv1alpha1.KnativeEventing
}

var _ types.Decoder = (*mockDecoder)(nil)
req, err := testutil.RequestFor(ke1)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", ke1, err)
}

func (mock *mockDecoder) Decode(_ types.Request, obj runtime.Object) error {
if p, ok := obj.(*eventingv1alpha1.KnativeEventing); ok {
*p = *mock.ke
result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Errorf("Too many KnativeEventings: %v", result.Response)
Copy link
Contributor

@skonto skonto Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could aggregate response reasons and check them. Validation checks the namespace and the uniqueness of the namespace. Here since no required env var is set it will return true for the invalid namespace check and will return Allowed=false with reason text: "Only one KnativeEventing allowed per namespace".
It seems to me that if something changes in the implementation this test may pass for the wrong reason eg. fail the first check and pass the latter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Would you be fine if we did that in a separate PR? This refactor is on the blocking path for the version bumping, so I'd rather not conflate the concerns.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+10

}
return nil
}
195 changes: 110 additions & 85 deletions knative-operator/pkg/webhook/knativekafka/webhook_validating_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package knativekafka_test
package knativekafka

import (
"context"
Expand All @@ -7,92 +7,102 @@ import (

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/apis"
operatorv1alpha1 "github.com/openshift-knative/serverless-operator/knative-operator/pkg/apis/operator/v1alpha1"
. "github.com/openshift-knative/serverless-operator/knative-operator/pkg/webhook/knativekafka"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/webhook/testutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
eventingv1alpha1 "knative.dev/operator/pkg/apis/operator/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
)

func init() {
apis.AddToScheme(scheme.Scheme)
}

var defaultCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "defaultCR",
Namespace: "knative-eventing",
},
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
var (
defaultCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "defaultCR",
Namespace: "knative-eventing",
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
},
},
},
}
var duplicateCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "duplicateCR",
Namespace: "knative-eventing",
},
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
}
duplicateCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "duplicateCR",
Namespace: "knative-eventing",
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
},
},
},
}

var invalidNamespaceCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidNamespaceCR",
Namespace: "FOO",
},
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
}
invalidNamespaceCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidNamespaceCR",
Namespace: "FOO",
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
},
Channel: operatorv1alpha1.Channel{
Enabled: false,
},
},
},
}
var invalidShapeCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidShapeCR",
Namespace: "knative-eventing",
},
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
}
invalidShapeCR = &operatorv1alpha1.KnativeKafka{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidShapeCR",
Namespace: "knative-eventing",
},
Channel: operatorv1alpha1.Channel{
Enabled: true,
// need to have bootstrapServers defined here!
Spec: operatorv1alpha1.KnativeKafkaSpec{
Source: operatorv1alpha1.Source{
Enabled: false,
},
Channel: operatorv1alpha1.Channel{
Enabled: true,
// need to have bootstrapServers defined here!
},
},
},
}
}
validKnativeEventingCR = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "validKnativeEventing",
Namespace: "knative-eventing",
},
}

var validKnativeEventingCR = &eventingv1alpha1.KnativeEventing{
ObjectMeta: metav1.ObjectMeta{
Name: "validKnativeEventing",
Namespace: "knative-eventing",
},
decoder types.Decoder
)

func init() {
apis.AddToScheme(scheme.Scheme)
decoder, _ = admission.NewDecoder(scheme.Scheme)
}

func TestHappy(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_KAFKA_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{defaultCR})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient(validKnativeEventingCR))
result := validator.Handle(context.TODO(), types.Request{})

req, err := testutil.RequestFor(defaultCR)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", defaultCR, err)
}

result := validator.Handle(context.Background(), req)
if !result.Response.Allowed {
t.Error("The request is not allowed but should be")
}
Expand All @@ -101,10 +111,17 @@ func TestHappy(t *testing.T) {
func TestInvalidNamespace(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_KAFKA_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{invalidNamespaceCR})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient(validKnativeEventingCR))
result := validator.Handle(context.TODO(), types.Request{})

req, err := testutil.RequestFor(invalidNamespaceCR)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", invalidNamespaceCR, err)
}

result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Error("The required namespace is wrong, but the request is allowed")
}
Expand All @@ -113,10 +130,17 @@ func TestInvalidNamespace(t *testing.T) {
func TestLoneliness(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_KAFKA_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{defaultCR})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient(duplicateCR, validKnativeEventingCR))
result := validator.Handle(context.TODO(), types.Request{})

req, err := testutil.RequestFor(defaultCR)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", defaultCR, err)
}

result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Errorf("Too many KnativeKafkas: %v", result.Response)
}
Expand All @@ -125,10 +149,17 @@ func TestLoneliness(t *testing.T) {
func TestInvalidShape(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_KAFKA_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{invalidShapeCR})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient(validKnativeEventingCR))
result := validator.Handle(context.TODO(), types.Request{})

req, err := testutil.RequestFor(invalidShapeCR)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", invalidShapeCR, err)
}

result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Error("The shape is invalid, but the request is allowed")
}
Expand All @@ -137,24 +168,18 @@ func TestInvalidShape(t *testing.T) {
func TestValidateDeps(t *testing.T) {
os.Clearenv()
os.Setenv("REQUIRED_KAFKA_NAMESPACE", "knative-eventing")

validator := Validator{}
validator.InjectDecoder(&mockDecoder{defaultCR})
validator.InjectDecoder(decoder)
validator.InjectClient(fake.NewFakeClient())
result := validator.Handle(context.TODO(), types.Request{})
if result.Response.Allowed {
t.Error("No KnativeEventing instance install, but request allowed")
}
}

type mockDecoder struct {
ke *operatorv1alpha1.KnativeKafka
}

var _ types.Decoder = (*mockDecoder)(nil)
req, err := testutil.RequestFor(defaultCR)
if err != nil {
t.Fatalf("Failed to generate a request for %v: %v", defaultCR, err)
}

func (mock *mockDecoder) Decode(_ types.Request, obj runtime.Object) error {
if p, ok := obj.(*operatorv1alpha1.KnativeKafka); ok {
*p = *mock.ke
result := validator.Handle(context.Background(), req)
if result.Response.Allowed {
t.Error("No KnativeEventing instance install, but request allowed")
}
return nil
}
Loading