Skip to content

Commit

Permalink
Add channel metadata conformance tests (#2963)
Browse files Browse the repository at this point in the history
* Add channel metadata conformance tests

* Add channel metadata conformance tests

* Add channel metadata conformance tests

* Add channel metadata conformance tests

* Add channel metadata conformance tests

* Rename things properly to state that they are CRD tests

* Check channel CRD has label duck.knative.dev/addressable

* Use apiextensionsv1beta1

* update-codegen
aliok authored Apr 22, 2020

Verified

This commit was signed with the committer’s verified signature.
1 parent 5353f17 commit 7a2e16a
Showing 4 changed files with 162 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions test/conformance/channel_crd_metadata_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 TestChannelCRDMetadata(t *testing.T) {
helpers.ChannelCRDMetadataTestHelperWithChannelTestRunner(t, channelTestRunner, lib.SetupClientOptionNoop)
}
120 changes: 120 additions & 0 deletions test/conformance/helpers/channel_crd_metadata_test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
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"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/test/lib"
)

// ChannelCRDMetadataTestHelperWithChannelTestRunner runs the Channel CRD metadata tests for all
// Channel resources in the ChannelTestRunner.
func ChannelCRDMetadataTestHelperWithChannelTestRunner(
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 is namespaced", func(t *testing.T) {
channelIsNamespaced(st, client, channel)
})
t.Run("Channel CRD has required label", func(t *testing.T) {
channelCRDHasRequiredLabels(st, client, channel)
})
t.Run("Channel CRD has required label", func(t *testing.T) {
channelCRDHasProperCategory(st, client, channel)
})
})
}

func channelIsNamespaced(st *testing.T, client *lib.Client, channel metav1.TypeMeta) {
// From spec: Each channel is namespaced

apiResource, err := getApiResource(client, channel)
if err != nil {
client.T.Fatalf("Error finding server resource for %q: %v", channel, err)
}
if !apiResource.Namespaced {
client.T.Fatalf("%q is not namespace scoped: %v", channel, err)
}
}

func channelCRDHasRequiredLabels(st *testing.T, client *lib.Client, channel metav1.TypeMeta) {
// From spec:
// Each channel MUST have the following:
// label of messaging.knative.dev/subscribable: "true"
// label of duck.knative.dev/addressable: "true"

gvr, _ := meta.UnsafeGuessKindToResource(channel.GroupVersionKind())
crdName := gvr.Resource + "." + gvr.Group

crd, err := client.Apiextensions.CustomResourceDefinitions().Get(crdName, metav1.GetOptions{
TypeMeta: metav1.TypeMeta{},
})
if err != nil {
client.T.Fatalf("Unable to find CRD for %q: %v", channel, err)
}
if crd.Labels["messaging.knative.dev/subscribable"] != "true" {
client.T.Fatalf("Channel CRD doesn't have the label 'messaging.knative.dev/subscribable=true' %q: %v", channel, err)
}
if crd.Labels["duck.knative.dev/addressable"] != "true" {
client.T.Fatalf("Channel CRD doesn't have the label 'duck.knative.dev/addressable=true' %q: %v", channel, err)
}
}

func channelCRDHasProperCategory(st *testing.T, client *lib.Client, channel metav1.TypeMeta) {
// From spec:
// Each channel MUST have the following: the category channel

apiResource, err := getApiResource(client, channel)
if err != nil {
client.T.Fatalf("Error finding server resource for %q: %v", channel, err)
}
found := false
for _, cat := range apiResource.Categories {
if cat == "channel" {
found = true
break
}
}
if !found {
client.T.Fatalf("Channel CRD %q does not have the category 'channel': %v", channel, err)
}
}

func getApiResource(client *lib.Client, typeMeta metav1.TypeMeta) (*metav1.APIResource, error) {
gvr, _ := meta.UnsafeGuessKindToResource(typeMeta.GroupVersionKind())
apiResourceList, err := client.Kube.Kube.Discovery().ServerResourcesForGroupVersion(gvr.GroupVersion().String())
if err != nil {
return nil, errors.Wrapf(err, "Unable to list server resources for groupVersion of %q: %v", typeMeta, err)
}

for _, apiResource := range apiResourceList.APIResources {
if apiResource.Kind == typeMeta.Kind {
return &apiResource, nil
}
}
return nil, errors.Errorf("Unable to find server resource for %q: %v", typeMeta, err)
}
15 changes: 11 additions & 4 deletions test/lib/client.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ package lib
import (
"testing"

apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"knative.dev/pkg/test"
@@ -31,10 +32,11 @@ import (

// Client holds instances of interfaces for making requests to Knative.
type Client struct {
Kube *test.KubeClient
Eventing *eventing.Clientset
Dynamic dynamic.Interface
Config *rest.Config
Kube *test.KubeClient
Eventing *eventing.Clientset
Apiextensions *apiextensionsv1beta1.ApiextensionsV1beta1Client
Dynamic dynamic.Interface
Config *rest.Config

Namespace string
T *testing.T
@@ -63,6 +65,11 @@ func NewClient(configPath string, clusterName string, namespace string, t *testi
return nil, err
}

client.Apiextensions, err = apiextensionsv1beta1.NewForConfig(client.Config)
if err != nil {
return nil, err
}

client.Dynamic, err = dynamic.NewForConfig(client.Config)
if err != nil {
return nil, err

0 comments on commit 7a2e16a

Please sign in to comment.