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

Apiserversource webhook test #5327

Closed
wants to merge 10 commits into from

Conversation

aliok
Copy link
Member

@aliok aliok commented Apr 29, 2021

Part of #4921

Proposed Changes

  • 🧹 Add a test that makes sure webhook kicks in to validate ApiServerSource updates

Pre-review Checklist

  • At least 80% unit test coverage
  • E2E tests for any new behavior
  • Docs PR for any user-facing impact
  • Spec PR for any new API feature
  • Conformance test for any change to the spec

Release Note


Docs

@knative-prow-robot knative-prow-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Apr 29, 2021
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please ask for approval from aliok after the PR has been reviewed.

The full list of commands accepted by this bot can be found 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

@knative-prow-robot knative-prow-robot added the area/test-and-release Test infrastructure, tests or release label Apr 29, 2021
@google-cla google-cla bot added the cla: yes Indicates the PR's author has signed the CLA. label Apr 29, 2021
@codecov
Copy link

codecov bot commented Apr 29, 2021

Codecov Report

Merging #5327 (adb332e) into main (c2d6649) will decrease coverage by 0.59%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5327      +/-   ##
==========================================
- Coverage   82.95%   82.36%   -0.60%     
==========================================
  Files         211      189      -22     
  Lines        6291     5902     -389     
==========================================
- Hits         5219     4861     -358     
+ Misses        742      720      -22     
+ Partials      330      321       -9     
Impacted Files Coverage Δ
pkg/apis/sources/v1beta1/apiserver_conversion.go
pkg/apis/sources/v1beta1/sinkbinding_lifecycle.go
pkg/apis/sources/v1beta1/apiserver_types.go
pkg/apis/sources/v1beta1/container_lifecycle.go
pkg/apis/sources/v1beta1/ping_lifecycle.go
pkg/apis/sources/v1beta1/ping_validation.go
pkg/apis/sources/v1beta1/sinkbinding_defaults.go
pkg/apis/sources/v1beta1/apiserver_lifecycle.go
pkg/apis/sources/v1beta1/apiserver_validation.go
pkg/apis/sources/v1beta1/container_types.go
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c2d6649...adb332e. Read the comment docs.

@aliok aliok changed the title Apiserversource webhook test [WIP] Apiserversource webhook test Apr 29, 2021
@knative-prow-robot knative-prow-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 29, 2021
@aliok
Copy link
Member Author

aliok commented Apr 29, 2021

@vaikas Following up on #5304

I've added a test for ApiServerSource to make sure webhook is triggered and it does the validation correctly.
It doesn't do the entire validation checks for ApiServerSource. There's only 1 case that checks webhook rejects invalid spec. I guess that serves our purpose with the original problem: making sure webhook validation is configured correctly.

I didn't want to go beyond this point w/o checking with you first.

I wrote the test as if it is a conformance test but there is no spec for ApiServerSource in https://github.com/knative/specs/tree/main/specs/eventing. I see there's a task for that here: #4933.
I would rather do this same PR for other resources instead of the context switch to writing a spec. So, let's solve all the problems in this PR and I can write tests that check webhook validation configuration for other resources.

Question: The tests in apiserversource_validation_test.go checks all the validations. Should we retest these rules in the conformance tests?

…d to refetch the resource. ...and, we do that already!
Copy link
Contributor

@vaikas vaikas left a comment

Choose a reason for hiding this comment

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

Thanks! Couple of tiny things since still in wip mode :)

test/rekt/apiserversource_test.go Outdated Show resolved Hide resolved
test/rekt/resources/apiserversource/apiserversource.go Outdated Show resolved Hide resolved

sinkRef := &duckv1.KReference{
Kind: "sinkkind",
Namespace: "sinknamespace",
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if above this we might want to make the comment about namespace being not used? Or above where you skipped it? Cause here when you create it with this namespace, it might not be clear to the user why L136 is not sinknamespace?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, the namespace shouldn't be skipped here as ApiServerSource allows cross namespace sink. I was think it doesn't. So, I will cancel the skipping and also fix the test code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I pushed a commit for not skipping the namespace.

Comment on lines +62 to +64
// TODO: skip for now
//"resourceNames": resource.ResourceNames,
//"nonResourceURLs": resource.NonResourceURLs,
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't want to add these for now in order not to waste time and focus on getting feedback on the general idea of the PR.

Would we ever need these? Happy to add code that handles these too, if necessary.

Comment on lines +34 to +38
{{ end }}

{{ if .rules }}
rules:
{{ range $_, $rule := .rules }}
Copy link
Member Author

@aliok aliok Apr 29, 2021

Choose a reason for hiding this comment

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

I wanted to extend and reuse the existing account_role here.
In the future if we need to add more functionality to this template, I would create a separate template; but it is manageable for now.

@aliok aliok changed the title [WIP] Apiserversource webhook test Apiserversource webhook test Apr 29, 2021
@knative-prow-robot knative-prow-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 29, 2021
sourceName := "myapiserversource"

// Install and wait for a Ready ApiServerSource.
env.Prerequisite(ctx, t, apiserversource.GoesReady(sourceName))
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect the source to have an install step or have the name of the resource passed in but I am not seeing either.

// wait until the ApiServerSource is ready or fail.
// otherwise, some race happens and Kubernetes tells us to refetch the object as it was updated, although
// we just refetched the object a couple of lines above.
apiserversource.IsReady(apiServerSource.Name)(ctx, t)
Copy link
Contributor

Choose a reason for hiding this comment

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

this will race with the update, likely you will test IsReady before the reconciler acts on the above Update and never see the resource go unready and then ready again.


f.Stable("EventMode").
Must("ApiServerSource MUST allow retrieving the event payloads of ObjectReference or ResourceEvent",
eventMode()).
Copy link
Contributor

Choose a reason for hiding this comment

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

this test does not seem to test what the spec is saying. The test confirms we can set the value, but the conformance line is talking about the shape of the event payload I believe, so your test will need to get more involved and see the event on the data plane side.


sacmName := feature.MakeRandomK8sName("apiserversource")
f.Setup("Create Service Account for ApiServerSource",
account_role.Install(sacmName,
Copy link
Contributor

Choose a reason for hiding this comment

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

If you look at the broker/channel changes we found that this pattern of combining Install with GoesReady was not idea for test composition (my bad). I would recommend this being split into two methods: GoesReady which tests that the thing you pass is does go ready (and we have some helpers for that. and Install which installs the ApiServerSource resource.

@@ -196,3 +197,76 @@ func Example_addressableResolver() {
// name: addressable-resolver-collector-foo
// apiGroup: rbac.authorization.k8s.io
}

func Example_withRoleAndRules() {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I like this testing pattern

@aliok
Copy link
Member Author

aliok commented Apr 29, 2021

/close

I will close this one. I will write a separate test that checks the webhook configuration.

@knative-prow-robot
Copy link
Contributor

@aliok: Closed this PR.

In response to this:

/close

I will close this one. I will write a separate test that checks the webhook configuration.

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.

@aliok aliok deleted the apiserversource-webhook-test branch February 16, 2022 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/test-and-release Test infrastructure, tests or release cla: yes Indicates the PR's author has signed the CLA. 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.

5 participants