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

Conversation

markusthoemmes
Copy link
Contributor

Currently, the webhook's objects are mocked through the decoder interface, but the webhooks don't actually get a request. This replaces the decoder with a generic encoder based on the scheme and instead passes a "proper" request to the webhooks under test.

Note: Newer versions of controller-runtime no longer have a decoder interface, so this is prep work to have a much smaller diff once bumping the version.

Currently, the webhook's objects are mocked through the decoder interface, but the webhooks don't actually get a request. This replaces the decoder with a generic encoder based on the scheme and instead passes a "proper" request to the webhooks under test.
ObjectMeta: metav1.ObjectMeta{
Name: "ke2",
},
dec, err := admission.NewDecoder(scheme.Scheme)
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.

It seems that the call will never produce an error:

// NewDecoder creates a Decoder given the runtime.Scheme
func NewDecoder(scheme *runtime.Scheme) (types.Decoder, error) {
	return decoder{codecs: serializer.NewCodecFactory(scheme)}, nil
}

In addition I think we could just have a pointer and check that value in tests and fail instead of panicking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh, great catch. Pushed a version that ditches the error handling. I moved it outside of tests since we never expect it to fail so I didn't want to pollute tests. Now it's even better.

Version: version,
// RequestFor generates an admission request for the given object.
func RequestFor(obj runtime.Object) (types.Request, error) {
b, err := json.Marshal(obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

s/b/bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bytes is a stdlib package name, so I'd rather not use it.

ObjectMeta: metav1.ObjectMeta{
Name: "ks2",
},
dec, err := admission.NewDecoder(scheme.Scheme)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above.

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 🤷

*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

@skonto
Copy link
Contributor

skonto commented Oct 27, 2020

/lgtm

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: markusthoemmes, skonto

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-robot openshift-merge-robot merged commit 146d8dd into openshift-knative:master Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants