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

Channel conformance tests for spec.subscriber #3050

Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions test/conformance/channel_spec_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 (
"testing"

"knative.dev/eventing/test/conformance/helpers"
"knative.dev/eventing/test/lib"
)

func TestChannelSpec(t *testing.T) {
helpers.ChannelSpecTestHelperWithChannelTestRunner(t, channelTestRunner, lib.SetupClientOptionNoop)
}
12 changes: 12 additions & 0 deletions test/conformance/helpers/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ const (
SubscribableAnnotationKey = "messaging.knative.dev/subscribable"
)

var (
channelv1alpha1 = metav1.TypeMeta{
Kind: "Channel",
APIVersion: "messaging.knative.dev/v1alpha1",
}

channelv1beta1 = metav1.TypeMeta{
Kind: "Channel",
APIVersion: "messaging.knative.dev/v1beta1",
}
)

aliok marked this conversation as resolved.
Show resolved Hide resolved
func getChannelDuckTypeSupportVersion(channelName string, client *lib.Client, channel *metav1.TypeMeta) (string, error) {
metaResource := resources.NewMetaResource(channelName, client.Namespace, channel)
obj, err := duck.GetGenericObject(client.Dynamic, metaResource, &eventingduckv1beta1.Channelable{})
Expand Down
135 changes: 135 additions & 0 deletions test/conformance/helpers/channel_spec_test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
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 helpers

import (
"testing"

"encoding/json"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apiserver/pkg/storage/names"

eventingduckv1alpha1 "knative.dev/eventing/pkg/apis/duck/v1alpha1"
eventingduckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1"
"knative.dev/eventing/test/lib"
"knative.dev/pkg/apis"
)

func ChannelSpecTestHelperWithChannelTestRunner(
t *testing.T,
channelTestRunner lib.ChannelTestRunner,
options ...lib.SetupClientOption,
) {

channelTestRunner.RunTests(t, lib.FeatureBasic, func(st *testing.T, channel metav1.TypeMeta) {
client := lib.Setup(st, true, options...)
defer lib.TearDown(client)

t.Run("Channel spec allows subscribers", func(t *testing.T) {
if channel == channelv1alpha1 || channel == channelv1beta1 {
t.Skip("Not running spec.subscribers array test for generic Channel")
}
channelSpecAllowsSubscribersArray(st, client, channel)
})
})
}

func channelSpecAllowsSubscribersArray(st *testing.T, client *lib.Client, channel metav1.TypeMeta, options ...lib.SetupClientOption) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious about the reason behind this st variable name when other tests seem to unanimously use t.

Copy link
Member

Choose a reason for hiding this comment

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

single or specific test perhaps? I've seen tt vs t used in other spots as well iirc, I'm not sure we need to bike shed the variable name too much?

Copy link
Contributor

Choose a reason for hiding this comment

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

Like I said, just nits

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've seen that's setup that way in other conformance tests and used it that way.

But I think it could make sense.

st is defined here in the test runner call: https://github.com/knative/eventing/pull/3050/files#diff-5d560f68ffc10b7bccae5ab980f0ffddR41

Let's ignore that here in this PR and if we would like to change that var name, let's do that in a separate PR for all other tests as well.

st.Logf("Running channel spec conformance test with channel %q", channel)

dtsv, err := getChannelDuckTypeSupportVersionFromTypeMeta(client, channel)
if err != nil {
st.Fatalf("Unable to check Channel duck type support version for %q: %q", channel, err)
}

channelName := names.SimpleNameGenerator.GenerateName("channel-spec-subscribers-")
client.T.Logf("Creating channel %+v-%s", channel, channelName)
client.CreateChannelOrFail(channelName, &channel)
client.WaitForResourceReadyOrFail(channelName, &channel)

sampleUrl, _ := apis.ParseURL("http://example.com")
gvr, _ := meta.UnsafeGuessKindToResource(channel.GroupVersionKind())
Copy link
Contributor

@antoineco antoineco May 14, 2020

Choose a reason for hiding this comment

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

import "knative.dev/eventing/pkg/apis/messaging"

// ...
    gvr := messaging.ChannelsResource.String()

Copy link
Member Author

Choose a reason for hiding this comment

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

channel here is actually passed by the test runner and it is different in each execution of the test.

  • IMC v1alpha1
  • IMC v1beta1
  • Channel v1alpha1

Copy link
Contributor

Choose a reason for hiding this comment

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

OK makes sense now


var ch interface{}

if dtsv == "" || dtsv == "v1alpha1" {
// treat missing annotation value as v1alpha1, as written in the spec
channelable, err := getChannelAsV1Alpha1Channelable(channelName, client, channel)
if err != nil {
st.Fatalf("Unable to get channel %q to v1alpha1 duck type: %q", channel, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

The error doesn't need quotes I believe. (comment applies to other occurrences as well)

Copy link
Member Author

Choose a reason for hiding this comment

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

Converted all of them now to %s

}

// SPEC: each channel CRD MUST contain an array of subscribers: spec.subscribable.subscribers
channelable.Spec.Subscribable = &eventingduckv1alpha1.Subscribable{
Subscribers: []eventingduckv1alpha1.SubscriberSpec{
{
UID: "1234",
ReplyURI: sampleUrl,
},
Comment on lines +82 to +85
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it even necessary to add an element to the array for this assertion?

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 think it is actually better to have an element inside the array instead of setting an empty array

},
}

ch = channelable

} else if dtsv == "v1beta1" {
channelable, err := getChannelAsV1Beta1Channelable(channelName, client, channel)
if err != nil {
st.Fatalf("Unable to get channel %q to v1beta1 duck type: %q", channel, err)
}

// SPEC: each channel CRD MUST contain an array of subscribers: spec.subscribers
channelable.Spec.Subscribers = []eventingduckv1beta1.SubscriberSpec{
Copy link
Member

Choose a reason for hiding this comment

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

@lionelvillard the spec.subscribers are only populated by the Subscription, right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Some new info: #3051 (comment)

{
UID: "1234",
ReplyURI: sampleUrl,
},
}

ch = channelable
} else {
st.Fatalf("Channel doesn't support v1alpha1 nor v1beta1 Channel duck types: %q", channel)
}

raw, err := json.Marshal(ch)
if err != nil {
st.Fatalf("Error marshaling %q: %q", channel, err)
}
u := &unstructured.Unstructured{}
err = json.Unmarshal(raw, u)
if err != nil {
st.Fatalf("Error unmarshaling %q: %q", u, err)
}

_, err = client.Dynamic.Resource(gvr).Namespace(client.Namespace).Update(u, metav1.UpdateOptions{})
if err != nil {
st.Fatalf("Error updating %q with subscribers in spec: %q", channel, err)
}
}

func getChannelDuckTypeSupportVersionFromTypeMeta(client *lib.Client, channel metav1.TypeMeta) (string, error) {
// the only way is to create one and see
channelName := names.SimpleNameGenerator.GenerateName("channel-tmp-")

client.T.Logf("Creating channel %+v-%s", channel, channelName)
client.CreateChannelOrFail(channelName, &channel)
client.WaitForResourceReadyOrFail(channelName, &channel)

return getChannelDuckTypeSupportVersion(channelName, client, &channel)
}