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

Add source status conformance test and run it for ApiServerSource #3605

Merged
merged 6 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func channelCRDHasRequiredLabels(client *testlib.Client, channel metav1.TypeMeta
// label of messaging.knative.dev/subscribable: "true"
// label of duck.knative.dev/addressable: "true"

validateRequiredLabels(client, channel, channelLabels)
ValidateRequiredLabels(client, channel, channelLabels)
}

func channelCRDHasProperCategory(st *testing.T, client *testlib.Client, channel metav1.TypeMeta) {
Expand Down
2 changes: 1 addition & 1 deletion test/conformance/helpers/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
testlib "knative.dev/eventing/test/lib"
)

func validateRequiredLabels(client *testlib.Client, object metav1.TypeMeta, labels map[string]string) {
func ValidateRequiredLabels(client *testlib.Client, object metav1.TypeMeta, labels map[string]string) {
for k, v := range labels {
if !objectHasRequiredLabel(client, object, k, v) {
client.T.Fatalf("can't find label '%s=%s' in CRD %q", k, v, object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package helpers
package sources

import (
"knative.dev/eventing/test/conformance/helpers"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -41,8 +42,8 @@ func SourceCRDMetadataTestHelperWithChannelTestRunner(
// From spec:
// Each source MUST have the following:
// label of duck.knative.dev/source: "true"
t.Run("Source CRD has required label", func(t *testing.T) {
validateRequiredLabels(client, source, sourceLabels)
st.Run("Source CRD has required label", func(t *testing.T) {
helpers.ValidateRequiredLabels(client, source, sourceLabels)
})

})
Expand Down
128 changes: 128 additions & 0 deletions test/conformance/helpers/sources/source_status_test_helper.go
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
}
23 changes: 22 additions & 1 deletion test/conformance/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ limitations under the License.
package conformance

import (
"fmt"
"log"
"os"
"strings"
"testing"

"knative.dev/pkg/test/zipkin"

"knative.dev/eventing/test"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
"knative.dev/eventing/test/lib/setupclientoptions"
)
const (
roleName = "event-watcher-r"
serviceAccountName = "event-watcher-sa"
recordEventsPodName = "api-server-source-logger-pod"
)

var channelTestRunner testlib.ComponentsTestRunner
Expand All @@ -43,12 +51,14 @@ func TestMain(m *testing.M) {
ComponentsToTest: test.EventingFlags.Channels,
}
sourcesTestRunner = testlib.ComponentsTestRunner{
ComponentsToTest: test.EventingFlags.Sources,
ComponentFeatureMap: testlib.SourceFeatureMap,
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct.

Copy link
Member

Choose a reason for hiding this comment

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

kn conformance run ... ? Anyone?

Copy link
Contributor

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.

Copy link
Contributor Author

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. In eventing-contrib we'll do the same, inject the initializer that creates the KafkaSource etc and just reuse the conformance helper function.

@vaikas is the above different from what you want?

Copy link
Contributor

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.

Copy link
Contributor Author

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

ComponentsToTest: test.EventingFlags.Sources,
}
brokerClass = test.EventingFlags.BrokerClass
brokerName = test.EventingFlags.BrokerName
brokerNamespace = test.EventingFlags.BrokerNamespace

addSourcesInitializers()
// Any tests may SetupZipkinTracing, it will only actually be done once. This should be the ONLY
// place that cleans it up. If an individual test calls this instead, then it will break other
// tests that need the tracing in place.
Expand All @@ -58,3 +68,14 @@ func TestMain(m *testing.M) {
return m.Run()
}())
}

func addSourcesInitializers() {
name := strings.ToLower(fmt.Sprintf("%s",
testlib.ApiServerSourceTypeMeta.Kind))
sourcesTestRunner.AddComponentSetupClientOption(
devguyio marked this conversation as resolved.
Show resolved Hide resolved
testlib.ApiServerSourceTypeMeta,
setupclientoptions.ApiServerSourceClientSetupOption(name,
"Reference",
recordEventsPodName, roleName, serviceAccountName),
)
}
4 changes: 2 additions & 2 deletions test/conformance/source_crd_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package conformance
import (
"testing"

"knative.dev/eventing/test/conformance/helpers"
srchelpers "knative.dev/eventing/test/conformance/helpers/sources"
testlib "knative.dev/eventing/test/lib"
)

func TestSourceCRDMetadata(t *testing.T) {
helpers.SourceCRDMetadataTestHelperWithChannelTestRunner(t, sourcesTestRunner, testlib.SetupClientOptionNoop)
srchelpers.SourceCRDMetadataTestHelperWithChannelTestRunner(t, sourcesTestRunner, testlib.SetupClientOptionNoop)
}
30 changes: 30 additions & 0 deletions test/conformance/source_status_test.go
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)
}
2 changes: 1 addition & 1 deletion test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ install_sugar || fail_test "Could not install Sugar Controller"
unleash_duck || fail_test "Could not unleash the chaos duck"

echo "Running tests with Multi Tenant Channel Based Broker"
go_test_e2e -timeout=30m -parallel=12 ./test/e2e ./test/conformance -brokerclass=MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1alpha2:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1alpha2:PingSource || fail_test
go_test_e2e -timeout=30m -parallel=12 ./test/e2e ./test/conformance -brokerclass=MTChannelBasedBroker -channels=messaging.knative.dev/v1beta1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel,messaging.knative.dev/v1:Channel,messaging.knative.dev/v1:InMemoryChannel -sources=sources.knative.dev/v1beta1:ApiServerSource,sources.knative.dev/v1alpha2:ContainerSource,sources.knative.dev/v1alpha2:PingSource || fail_test

success
21 changes: 20 additions & 1 deletion test/lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,36 @@ var ChannelFeatureMap = map[metav1.TypeMeta][]Feature{
MessagingChannelTypeMeta: {FeatureBasic},
}

var ApiServerSourceTypeMeta = metav1.TypeMeta{
APIVersion: resources.SourcesAPIVersion,
Kind: resources.ApiServerSourceKind,
}

var PingSourceTypeMeta = metav1.TypeMeta{
APIVersion: resources.SourcesAPIVersion,
Kind: resources.PingSourceKind,
}

var SourceFeatureMap = map[metav1.TypeMeta][]Feature{
ApiServerSourceTypeMeta: {FeatureBasic, FeatureLongLiving},
PingSourceTypeMeta: {FeatureBasic, FeatureLongLiving},
}

// Feature is the feature supported by the channel.
type Feature string

const (
// FeatureBasic is the feature that should be supported by all channels.
// FeatureBasic is the feature that should be supported by all components.
FeatureBasic Feature = "basic"
// FeatureRedelivery means if downstream rejects an event, that request will be attempted again.
FeatureRedelivery Feature = "redelivery"
// FeaturePersistence means if channel's Pod goes down, all events already ACKed by the channel
// will persist and be retransmitted when the Pod restarts.
FeaturePersistence Feature = "persistence"
// A long living component
FeatureLongLiving Feature = "longliving"
// A batch style of components that run once and complete
FeatureBatch Feature = "batch"
)

const (
Expand Down
7 changes: 7 additions & 0 deletions test/lib/resources/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
MessagingAPIVersion = "messaging.knative.dev/v1beta1"
FlowsAPIVersion = "flows.knative.dev/v1beta1"
ServingAPIVersion = "serving.knative.dev/v1"
SourcesAPIVersion = "sources.knative.dev/v1beta1"
)

// Kind for Knative resources.
Expand Down Expand Up @@ -76,3 +77,9 @@ const (
FlowsSequenceKind string = "Sequence"
FlowsParallelKind string = "Parallel"
)

//Kind for sources resources that exist in Eventing core
const(
ApiServerSourceKind string = "ApiServerSource"
PingSourceKind string = "PingSource"
)
Loading