-
Notifications
You must be signed in to change notification settings - Fork 586
/
channel_spec_test_helper.go
139 lines (116 loc) · 4.92 KB
/
channel_spec_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
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 (
"context"
"fmt"
"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"
"k8s.io/client-go/util/retry"
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
"knative.dev/pkg/apis"
)
func ChannelSpecTestHelperWithChannelTestRunner(
t *testing.T,
channelTestRunner testlib.ComponentsTestRunner,
options ...testlib.SetupClientOption,
) {
channelTestRunner.RunTests(t, testlib.FeatureBasic, func(t *testing.T, channel metav1.TypeMeta) {
t.Run("Channel spec allows subscribers", func(t *testing.T) {
client := testlib.Setup(t, true, options...)
defer testlib.TearDown(client)
if channel == channelv1 {
t.Skip("Not running spec.subscribers array test for generic Channel")
}
channelSpecAllowsSubscribersArray(t, client, channel)
})
})
}
func channelSpecAllowsSubscribersArray(t *testing.T, client *testlib.Client, channel metav1.TypeMeta, options ...testlib.SetupClientOption) {
t.Logf("Running channel spec conformance test with channel %s", channel)
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
dtsv, err := getChannelDuckTypeSupportVersionFromTypeMeta(client, channel)
if err != nil {
t.Fatalf("Unable to check Channel duck type support version for %s: %s", channel, err)
}
if dtsv != "v1" {
t.Fatalf("Unexpected duck type version, wanted [v1] got: %s", dtsv)
}
channelName := names.SimpleNameGenerator.GenerateName("channel-spec-subscribers-")
t.Logf("Creating channel %+v-%s", channel, channelName)
client.CreateChannelOrFail(channelName, &channel)
client.WaitForResourceReadyOrFail(channelName, &channel)
sampleUrl := apis.HTTP("example.com")
gvr, _ := meta.UnsafeGuessKindToResource(channel.GroupVersionKind())
var ch interface{}
channelable, err := getChannelAsChannelable(channelName, client, channel)
if err != nil {
t.Fatalf("Unable to get channel %s to v1 duck type: %s", channel, err)
}
// SPEC: each channel CRD MUST contain an array of subscribers: spec.subscribers
channelable.Spec.Subscribers = []eventingduckv1.SubscriberSpec{
{
UID: "1234",
ReplyURI: sampleUrl,
},
}
ch = channelable
raw, err := json.Marshal(ch)
if err != nil {
t.Fatalf("Error marshaling %s: %s", channel, err)
}
u := &unstructured.Unstructured{}
err = json.Unmarshal(raw, u)
if err != nil {
t.Fatalf("Error unmarshaling %s: %s", u, err)
}
// the admission webhook should deny attempts to modify spec.subscribers directly from anything other than eventing-controller
err = client.RetryWebhookErrors(func(attempt int) error {
_, e := client.Dynamic.Resource(gvr).Namespace(client.Namespace).Update(context.Background(), u, metav1.UpdateOptions{})
if e != nil {
t.Logf("Failed to update channel spec at attempt %d %q %q: %v", attempt, channel.Kind, channelName, e)
}
return e
})
if err == nil {
return fmt.Errorf("channel validation should prevent direct updates to spec.subcribers except when made by eventing-controller")
}
subscriptionName := names.SimpleNameGenerator.GenerateName("channel-spec-subscribers-")
subscriberUrl, _ := apis.ParseURL("http://localhost")
client.CreateSubscriptionOrFail(subscriptionName, channelName, &channel, resources.WithURIForSubscription(subscriberUrl))
client.WaitForResourceReadyOrFail(subscriptionName, testlib.SubscriptionTypeMeta)
client.WaitForResourceReadyOrFail(channelName, &channel)
channelable, err = getChannelAsChannelable(channelName, client, channel)
if len(channelable.Spec.Subscribers) != 1 {
return fmt.Errorf("channel.Spec.Subscribers was not updated by the subscription reconciler")
}
return err
})
if err != nil {
t.Fatalf("Error updating %s with subscribers in spec: %s", channel, err)
}
}
func getChannelDuckTypeSupportVersionFromTypeMeta(client *testlib.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)
}