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

Enable envtest-based unit tests for webhooks #5721

Closed
sbueringer opened this issue Nov 23, 2021 · 8 comments
Closed

Enable envtest-based unit tests for webhooks #5721

sbueringer opened this issue Nov 23, 2021 · 8 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@sbueringer
Copy link
Member

sbueringer commented Nov 23, 2021

User Story

As a developer I would like to be able to write unit tests with envtest for our webhooks.

Note: In case of webhooks the fakeclient unit tests have (at least) the following limitations:

  • The webhooks cannot use indices (as that's not supported by fakeclient)
  • There is no marshalling/unmarshalling of Go types and no OpenAPI schema validation of the kube-apiserver as we directly call validate/default funcs with our structs. Especially with ClusterClass variables this can lead to cases where the unit test is successful but the apiserver would reject the create/update call without even asking the webhook

Detailed Description

I would suggest implementing it the same way we currently add indices to our testenv:

  • create helper functions to register webhooks
  • Add a SetupWebhooks func to RunInput (used in internal/envtest.Run)

This solution would enable envtest-based unit test for all our webhook packages except top-level api/v1beta1, because this would lead to a circular dependency between api/v1beta and webhooks. But we already have this limitation today and if we want to unit test one of the webhooks in the api/v1beta1 package with envtest we can simply move it to the webhooks package.

/kind feature

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Nov 23, 2021
@sbueringer
Copy link
Member Author

Note: #5719 depends on this issue

@sbueringer
Copy link
Member Author

@fabriziopandini WDYT?

@sbueringer
Copy link
Member Author

Just realized that the idea might sound good in theory, but that we have way too many api packages. So we still would have to call 6 setup funcs whenever we use SetupWebhooks. I'll come up with an alternative approach

@sbueringer
Copy link
Member Author

sbueringer commented Nov 23, 2021

PR #5722 represents a potential solution by refactoring the envtest package.

I've discussed the issue with Fabrizio and we would simply implement the webhooks integration test in a package like internal/webhooks/integration instead of refactoring envtest like proposed in the PR.

The problem with the changes proposed in the PR is that they would distribute the webhook setup all over the place vs. currently webhooks will just work out of the box when using envtest.

@sbueringer
Copy link
Member Author

We're now implementing the first integration tests in internal/webhooks/test.

I'm not sure if we should keep this issue around for further discussions or close it for now.

@fabriziopandini @killianmuldoon WDYT?

@killianmuldoon
Copy link
Contributor

I think we close this discussion for now - if it comes up again we can open a new issue, but for the webhook case we have something that works for now.

@sbueringer
Copy link
Member Author

/close

@k8s-ci-robot
Copy link
Contributor

@sbueringer: Closing this issue.

In response to this:

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants