Skip to content

add tests for webhook/admission pkg #123

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

Merged
merged 2 commits into from
Aug 29, 2018

Conversation

mengqiy
Copy link
Member

@mengqiy mengqiy commented Aug 28, 2018

Fix several nil pointer issues when testing the unhappy path in the unit test.

@mengqiy mengqiy requested review from droot and Liujingfang1 August 28, 2018 20:17
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 28, 2018
Copy link
Contributor

@Liujingfang1 Liujingfang1 left a comment

Choose a reason for hiding this comment

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

Very nice and clean tests. A few comments are listed below.

@@ -130,37 +135,44 @@ func (w *Webhook) kvMapToContext(ctx context.Context) (context.Context, error) {
}

func (w *Webhook) handleMutating(ctx context.Context, req Request) Response {
var resp Response
var patches []jsonpatch.JsonPatchOperation
r := Response{
Copy link
Contributor

Choose a reason for hiding this comment

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

You can move this declaration to line 155

Expect(w.response).To(Equal(expected))
Expect(alwaysDeny.invoked).To(BeTrue())
Expect(alwaysDeny.valueFromContext).To(Equal("bar"))
Expect(alwaysAllow.invoked).To(BeFalse())
Copy link
Contributor

Choose a reason for hiding this comment

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

So the handlers are executed with respect to their order in the Handlers slice. Is that something users need to be aware of? Do you need to add some doc for that?

}
wh := &Webhook{
Type: types.WebhookTypeMutating,
Handlers: []Handler{patcher1, patcher2},
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you check conflict among different handlers?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. I can't find a easy way to do that.
I added a comment in the struct saying that user should ensure that their handlers should not mutate same field and generate conflicting JSON patches.

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

Looks good. Only suggestion is to see if we can use httptest pkg.

})
})

type fakeResponseWriter struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you look at httptest pkg https://golang.org/pkg/net/http/httptest/ It provides some nice utilities for testing http handlers without running the server.

logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

func TestSource(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

TestSource or we can call it something like TestAdmissionWebhook() ?

var err error
decoder, err = NewDecoder(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expect(decoder).NotTo(BeNil())
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to create decoder in BeforeEach ? I think it can be shared for the tests below. WDYT ?

w := &fakeResponseWriter{}
expected := []byte(`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
`)
It("should return a response with an error", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

should return an error with bad-request status code ?

w := &fakeResponseWriter{}
expected := []byte(`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expect application/json","code":400}}}
`)
It("should return a response with an error", func() {
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.

@mengqiy
Copy link
Member Author

mengqiy commented Aug 28, 2018

PTAL
I will squash commits before merging.

@Liujingfang1
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 29, 2018
@droot droot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 29, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: mengqiy

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

@k8s-ci-robot k8s-ci-robot merged commit 3bf2b5e into kubernetes-sigs:admissionwebhook Aug 29, 2018
@mengqiy mengqiy deleted the add_tests branch August 29, 2018 03:35
DirectXMan12 pushed a commit that referenced this pull request Jan 31, 2020
Make order of generated yaml from kubebuilder create config --crds deterministic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants