-
Notifications
You must be signed in to change notification settings - Fork 604
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
Add source status conformance test and run it for ApiServerSource #3605
Merged
knative-prow-robot
merged 6 commits into
knative:master
from
devguyio:3117-src-status-conf
Jul 27, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de837c1
Initial dirty WIP code
devguyio c516910
Add proper ApiServerSource initializer and finish status conf test
devguyio faa59af
Remove an early recordevent cleanup
devguyio 9e21638
Update deps with update-codegen.sh result
devguyio 37e16e6
Run e2e-tests and conformance against v1beta1 ApiServerSource
devguyio 22128cc
Add missing e2e build tag
devguyio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
test/conformance/helpers/sources/source_status_test_helper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sources | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"knative.dev/eventing/test/lib/duck" | ||
"knative.dev/eventing/test/lib/resources" | ||
"knative.dev/pkg/apis" | ||
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" | ||
"strings" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
testlib "knative.dev/eventing/test/lib" | ||
) | ||
|
||
// SourceStatusTestHelperWithComponentsTestRunner runs the Source status | ||
// conformance tests for all sources in the ComponentsTestRunner. This test | ||
// needs an instance created of each source which should be initialized via | ||
// ComponentsTestRunner.AddComponentSetupClientOption. | ||
// | ||
// Note: The source object name must be the lower case Kind name (e.g. | ||
// apiserversource for the Kind: ApiServerSource source) | ||
func SourceStatusTestHelperWithComponentsTestRunner( | ||
t *testing.T, | ||
componentsTestRunner testlib.ComponentsTestRunner, | ||
options ...testlib.SetupClientOption, | ||
) { | ||
table := [] struct { | ||
name string | ||
feature testlib.Feature | ||
// Sources report success via either a Ready condition or a Succeeded condition | ||
want apis.ConditionType | ||
} { | ||
{ | ||
"Long living sources have Ready status condition", | ||
testlib.FeatureLongLiving, | ||
apis.ConditionReady, | ||
}, | ||
{ | ||
"Batch sources have Succeeded status condition", | ||
testlib.FeatureBatch, | ||
apis.ConditionSucceeded, | ||
}, | ||
|
||
} | ||
for _, tc := range table { | ||
n := tc.name | ||
f := tc.feature | ||
w := tc.want | ||
t.Run(n, func(t *testing.T) { | ||
componentsTestRunner.RunTestsWithComponentOptions(t, f, true, | ||
func(st *testing.T, source metav1.TypeMeta, | ||
componentOptions ...testlib.SetupClientOption) { | ||
st.Log("About to setup client") | ||
options = append(options, componentOptions...) | ||
client := testlib.Setup(st, true, options...) | ||
defer testlib.TearDown(client) | ||
validateSourceStatus(st, client, source, w, options...) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func validateSourceStatus(st *testing.T, client *testlib.Client, source metav1.TypeMeta, successCondition apis.ConditionType, options ...testlib.SetupClientOption) { | ||
const( | ||
sourceName = "source-req-status" | ||
) | ||
|
||
st.Logf("Running source status conformance test with source %q", source) | ||
|
||
v1beta1Src, err := getSourceAsV1Beta1Source(client, source) | ||
if err != nil { | ||
st.Fatalf("unable to get source %q with v1beta1 duck type: %v", source, err) | ||
} | ||
|
||
// SPEC: Sources MUST implement conditions with a Ready condition for long lived sources, and Succeeded for batch style sources. | ||
if ! hasCondition(v1beta1Src, successCondition){ | ||
st.Fatalf("%q does not have %q", source, successCondition) | ||
} | ||
|
||
// SPEC: Sources MUST propagate the sinkUri to their status to signal to the cluster where their events are being sent. | ||
if v1beta1Src.Status.SinkURI.Host == "" { | ||
st.Fatalf("sinkUri was not propagated for source %q", source) | ||
} | ||
} | ||
|
||
func getSourceAsV1Beta1Source(client *testlib.Client, | ||
source metav1.TypeMeta) (*duckv1beta1.Source, error){ | ||
srcName := strings.ToLower(fmt.Sprintf("%s", source.Kind)) | ||
metaResource := resources.NewMetaResource(srcName, client.Namespace, | ||
&source) | ||
obj, err := duck.GetGenericObject(client.Dynamic, metaResource, | ||
&duckv1beta1.Source{}) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "unable to get the source as v1beta1 Source duck type: %q", source) | ||
} | ||
srcObj, ok := obj.(*duckv1beta1.Source) | ||
if !ok { | ||
return nil, errors.Errorf("unable to cast source %q to v1beta1 Source" + | ||
" duck type", source) | ||
} | ||
return srcObj, nil | ||
} | ||
|
||
func hasCondition(src *duckv1beta1.Source, t apis.ConditionType) bool{ | ||
if src.Status.GetCondition(t) == nil{ | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build e2e | ||
|
||
/* | ||
Copyright 2020 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package conformance | ||
|
||
import ( | ||
srchelpers "knative.dev/eventing/test/conformance/helpers/sources" | ||
testlib "knative.dev/eventing/test/lib" | ||
"testing" | ||
) | ||
|
||
func TestSourceStatus(t *testing.T) { | ||
srchelpers.SourceStatusTestHelperWithComponentsTestRunner(t, | ||
sourcesTestRunner, testlib.SetupClientOptionNoop) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If from a project outside this repo (eg eventing-contrib) i want to run these conformance tests, i need to create this "main" file and populate that
ComponentFeatureMap
manually right?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.
Correct.
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.
kn conformance run ...
? Anyone?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.
Would it be possible to restructure this so that something like this:
https://github.com/knative/eventing/pull/3531/files#diff-9cd17936c8cf7a4c529c64712ffa54abR37
Since these are conformance tests it would be nice if the user doesn't have to manually modify the test code to test their source for conformance. Ideally the creation of the resource would be separate from the tests and for tests, all you'd need to do is to give a namespace/name to test and the test would run against it. I've been advocating for this pattern for all of the conformance tests to make them easier to test. I further believe this to be even more important for sources as there are going to be more sources than channels / brokers.
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.
So it is my understanding that the "reusable" conformance tests package should not create any sources and should be "reusable" by other repos that want to run them against their sources, and that is the role of
conformance/helpers
package. Which is the case in this PR.The repo specific tests that reuse the helpers (in our case
test/conformance/main_tests.go & source_crd_metadata_test.go
) are the ones that inject the source creation tests. Ineventing-contrib
we'll do the same, inject the initializer that creates theKafkaSource
etc and just reuse the conformance helper function.@vaikas is the above different from what you want?
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.
Well, Ideally I guess I'd like, just like today I can run E2E tests against my own cluster so I can push custom bits into it. I create my cluster, then ko apply my bits into it and then execute:
go test ./test/... -tags=e2e
I do not have to change any of the e2e tests to point to different cluster, nor have the tests have any knowledge how my bits got to the cluster.
Ideally what I think I'd like is the following experience for a user:
create their sources however they want (kubectl apply for example)
take the NS/Name of the above
then run something like:
go test ./test/... -tags=source-conformance -sourceNamespace=myns -sourceName=mysource
(or some shell script that executes that)
So, to run the tests I wouldn't have to modify the code at all. Maybe I'm missing why the helpers need to create these instead of having tests that take the namespace / name and execute the tests against it instead of creating the "units under test" in our repo being a requirement.
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.
As agreed I'll do the cmd args in a separate PR