-
Notifications
You must be signed in to change notification settings - Fork 581
/
channel_crd_metadata_test_helper.go
117 lines (99 loc) · 3.81 KB
/
channel_crd_metadata_test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
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/pkg/apis/messaging"
testlib "knative.dev/eventing/test/lib"
"knative.dev/pkg/apis/duck"
)
var channelLabels = map[string]string{
messaging.SubscribableDuckVersionAnnotation: "true",
duck.AddressableDuckVersionLabel: "true",
}
// ChannelCRDMetadataTestHelperWithChannelTestRunner runs the Channel CRD metadata tests for all
// Channel resources in the ComponentsTestRunner.
// Deprecated, use reconciler-test based tests.
func ChannelCRDMetadataTestHelperWithChannelTestRunner(
t *testing.T,
channelTestRunner testlib.ComponentsTestRunner,
options ...testlib.SetupClientOption,
) {
channelTestRunner.RunTests(t, testlib.FeatureBasic, func(st *testing.T, channel metav1.TypeMeta) {
client := testlib.Setup(st, true, options...)
defer testlib.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(client, channel)
})
t.Run("Channel CRD has required label", func(t *testing.T) {
channelCRDHasProperCategory(st, client, channel)
})
})
}
// Deprecated, use reconciler-test based tests.
func channelIsNamespaced(st *testing.T, client *testlib.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)
}
}
// Deprecated, use reconciler-test based tests.
func channelCRDHasRequiredLabels(client *testlib.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"
ValidateRequiredLabels(client, channel, channelLabels)
}
// Deprecated, use reconciler-test based tests.
func channelCRDHasProperCategory(st *testing.T, client *testlib.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 *testlib.Client, typeMeta metav1.TypeMeta) (*metav1.APIResource, error) {
gvr, _ := meta.UnsafeGuessKindToResource(typeMeta.GroupVersionKind())
apiResourceList, err := client.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)
}