This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
test_pull_subscription.go
170 lines (141 loc) · 4.23 KB
/
test_pull_subscription.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright 2019 Google LLC
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 e2e
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/google/uuid"
"cloud.google.com/go/pubsub"
"k8s.io/apimachinery/pkg/runtime/schema"
// The following line to load the gcp plugin (only required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
// SmokePullSubscriptionTestImpl tests we can create a pull subscription to ready state.
func SmokePullSubscriptionTestImpl(t *testing.T) {
topic, deleteTopic := makeTopicOrDie(t)
defer deleteTopic()
psName := topic + "-sub"
client := Setup(t, true)
defer TearDown(client)
installer := NewInstaller(client.Dynamic, map[string]string{
"namespace": client.Namespace,
"topic": topic,
"subscription": psName,
}, EndToEndConfigYaml([]string{"pull_subscription_test", "istio", "event_display"})...)
// Create the resources for the test.
if err := installer.Do("create"); err != nil {
t.Errorf("failed to create, %s", err)
return
}
// Delete deferred.
defer func() {
if err := installer.Do("delete"); err != nil {
t.Errorf("failed to create, %s", err)
}
// Just chill for tick.
time.Sleep(10 * time.Second)
}()
if err := client.WaitForResourceReady(client.Namespace, psName, schema.GroupVersionResource{
Group: "pubsub.cloud.google.com",
Version: "v1alpha1",
Resource: "pullsubscriptions",
}); err != nil {
t.Error(err)
}
}
// PullSubscriptionWithTargetTestImpl tests we can receive an event from a PullSubscription.
func PullSubscriptionWithTargetTestImpl(t *testing.T, packages map[string]string) {
topicName, deleteTopic := makeTopicOrDie(t)
defer deleteTopic()
psName := topicName + "-sub"
targetName := topicName + "-target"
client := Setup(t, true)
defer TearDown(client)
config := map[string]string{
"namespace": client.Namespace,
"topic": topicName,
"subscription": psName,
"targetName": targetName,
"targetUID": uuid.New().String(),
}
for k, v := range packages {
config[k] = v
}
installer := NewInstaller(client.Dynamic, config,
EndToEndConfigYaml([]string{"pull_subscription_target", "istio"})...)
// Create the resources for the test.
if err := installer.Do("create"); err != nil {
t.Errorf("failed to create, %s", err)
return
}
// Delete deferred.
defer func() {
if err := installer.Do("delete"); err != nil {
t.Errorf("failed to create, %s", err)
}
// Just chill for tick.
time.Sleep(10 * time.Second)
}()
gvr := schema.GroupVersionResource{
Group: "pubsub.cloud.google.com",
Version: "v1alpha1",
Resource: "pullsubscriptions",
}
jobGVR := schema.GroupVersionResource{
Group: "batch",
Version: "v1",
Resource: "jobs",
}
if err := client.WaitForResourceReady(client.Namespace, psName, gvr); err != nil {
t.Error(err)
}
topic := getTopic(t, topicName)
r := topic.Publish(context.TODO(), &pubsub.Message{
Attributes: map[string]string{
"target": "falldown",
},
Data: []byte(`{"foo":bar}`),
})
_, err := r.Get(context.TODO())
if err != nil {
t.Logf("%s", err)
}
msg, err := client.WaitUntilJobDone(client.Namespace, targetName)
if err != nil {
t.Error(err)
}
t.Logf("Last term message => %s", msg)
if msg != "" {
out := &TargetOutput{}
if err := json.Unmarshal([]byte(msg), out); err != nil {
t.Error(err)
}
if !out.Success {
// Log the output pull subscription pods.
if logs, err := client.LogsFor(client.Namespace, psName, gvr); err != nil {
t.Error(err)
} else {
t.Logf("pullsubscription: %+v", logs)
}
// Log the output of the target job pods.
if logs, err := client.LogsFor(client.Namespace, targetName, jobGVR); err != nil {
t.Error(err)
} else {
t.Logf("job: %s\n", logs)
}
t.Fail()
}
}
}