This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
pubsub.go
125 lines (107 loc) · 4.53 KB
/
pubsub.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
/*
Copyright 2020 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 lib
import (
"net/http"
"testing"
"time"
reconcilertestingv1 "github.com/google/knative-gcp/pkg/reconciler/testing/v1"
reconcilertestingv1beta1 "github.com/google/knative-gcp/pkg/reconciler/testing/v1beta1"
v1 "k8s.io/api/core/v1"
schemasv1 "github.com/google/knative-gcp/pkg/schemas/v1"
"github.com/google/knative-gcp/test/lib/metrics"
"github.com/google/knative-gcp/test/lib/resources"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgmetrics "knative.dev/pkg/metrics"
)
type PubSubConfig struct {
SinkGVK metav1.GroupVersionKind
PubSubName string
SinkName string
TopicName string
ServiceAccountName string
}
func MakePubSubOrDie(client *Client, config PubSubConfig) {
client.T.Helper()
MakePubSub(client, config)
client.Core.WaitForResourceReadyOrFail(config.PubSubName, CloudPubSubSourceV1TypeMeta)
}
func MakePubSub(client *Client, config PubSubConfig) {
client.T.Helper()
so := make([]reconcilertestingv1.CloudPubSubSourceOption, 0)
so = append(so, reconcilertestingv1.WithCloudPubSubSourceSink(config.SinkGVK, config.SinkName))
so = append(so, reconcilertestingv1.WithCloudPubSubSourceTopic(config.TopicName))
so = append(so, reconcilertestingv1.WithCloudPubSubSourceServiceAccount(config.ServiceAccountName))
eventsPubSub := reconcilertestingv1.NewCloudPubSubSource(config.PubSubName, client.Namespace, so...)
client.CreatePubSubOrFail(eventsPubSub)
}
func MakePubSubV1beta1OrDie(client *Client, config PubSubConfig) {
client.T.Helper()
so := make([]reconcilertestingv1beta1.CloudPubSubSourceOption, 0)
so = append(so, reconcilertestingv1beta1.WithCloudPubSubSourceSink(config.SinkGVK, config.SinkName))
so = append(so, reconcilertestingv1beta1.WithCloudPubSubSourceTopic(config.TopicName))
so = append(so, reconcilertestingv1beta1.WithCloudPubSubSourceServiceAccount(config.ServiceAccountName))
eventsPubSub := reconcilertestingv1beta1.NewCloudPubSubSource(config.PubSubName, client.Namespace, so...)
client.CreatePubSubV1beta1OrFail(eventsPubSub)
client.Core.WaitForResourceReadyOrFail(config.PubSubName, CloudPubSubSourceV1beta1TypeMeta)
}
func MakePubSubTargetJobOrDie(client *Client, source, targetName, eventType string, schema string) {
client.T.Helper()
job := resources.PubSubTargetJob(targetName, []v1.EnvVar{
{
Name: "TYPE",
Value: eventType,
},
{
Name: "SOURCE",
Value: source,
}, {
Name: "TIME",
Value: "6m",
}, {
Name: "SCHEMA",
Value: schema,
},
})
client.CreateJobOrFail(job, WithServiceForJob(targetName))
}
func AssertMetrics(t *testing.T, client *Client, topicName, psName string) {
t.Helper()
sleepTime := 1 * time.Minute
t.Logf("Sleeping %s to make sure metrics were pushed to stackdriver", sleepTime.String())
time.Sleep(sleepTime)
// If we reach this point, the projectID should have been set.
projectID := GetEnvOrFail(t, ProwProjectKey)
f := map[string]interface{}{
"metric.type": EventCountMetricType,
"resource.type": GlobalMetricResourceType,
"metric.label.resource_group": PubsubResourceGroup,
"metric.label.event_type": schemasv1.CloudPubSubMessagePublishedEventType,
"metric.label.event_source": schemasv1.CloudPubSubEventSource(projectID, topicName),
"metric.label.namespace_name": client.Namespace,
"metric.label.name": psName,
// We exit the target image before sending a response, thus check for 500.
"metric.label.response_code": http.StatusInternalServerError,
"metric.label.response_code_class": pkgmetrics.ResponseCodeClass(http.StatusInternalServerError),
}
filter := metrics.StringifyStackDriverFilter(f)
t.Logf("Filter expression: %s", filter)
actualCount, err := client.StackDriverEventCountMetricFor(client.Namespace, projectID, filter)
if err != nil {
t.Fatalf("failed to get stackdriver event count metric: %v", err)
}
expectedCount := int64(1)
if actualCount != expectedCount {
t.Errorf("Actual count different than expected count, actual: %d, expected: %d", actualCount, expectedCount)
}
}