-
Notifications
You must be signed in to change notification settings - Fork 70
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +10 |
||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 🤷