-
Notifications
You must be signed in to change notification settings - Fork 996
/
gcp_stackdriver_client_test.go
159 lines (153 loc) · 4.07 KB
/
gcp_stackdriver_client_test.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
package gcp
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildMQLQuery(t *testing.T) {
for _, tc := range []struct {
name string
resourceType string
metric string
resourceName string
aggregation string
expected string
isError bool
}{
{
"topic with aggregation",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "count",
"fetch pubsub_topic | metric 'pubsub.googleapis.com/topic/x' | filter (resource.project_id == 'myproject' && resource.topic_id == 'mytopic')" +
" | within 5m | align delta(3m) | every 3m | group_by [], count(value)",
false,
},
{
"topic without aggregation",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "",
"fetch pubsub_topic | metric 'pubsub.googleapis.com/topic/x' | filter (resource.project_id == 'myproject' && resource.topic_id == 'mytopic')" +
" | within 2m",
false,
},
{
"subscription with aggregation",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "percentile99",
"fetch pubsub_subscription | metric 'pubsub.googleapis.com/subscription/x' | filter (resource.project_id == 'myproject' && resource.subscription_id == 'mysubscription')" +
" | within 5m | align delta(3m) | every 3m | group_by [], percentile(value, 99)",
false,
},
{
"subscription without aggregation",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "",
"fetch pubsub_subscription | metric 'pubsub.googleapis.com/subscription/x' | filter (resource.project_id == 'myproject' && resource.subscription_id == 'mysubscription')" +
" | within 2m",
false,
},
{
"invalid percentile",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "percentile101",
"invalid percentile value: 101",
true,
},
{
"unsupported aggregation function",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "max",
"unsupported aggregation function: max",
true,
},
} {
s := &StackDriverClient{}
t.Run(tc.name, func(t *testing.T) {
q, err := s.BuildMQLQuery("myproject", tc.resourceType, tc.metric, tc.resourceName, tc.aggregation)
if tc.isError {
assert.Error(t, err)
assert.Equal(t, tc.expected, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, q)
}
})
}
}
func TestGetActualProjectID(t *testing.T) {
// There are three ways to get projectID
// This is ordered from highest priority to lowest priority
pidFromMetadata := "myproject0"
pidFromClient := "myproject1"
pidFromClientCreds := "myproject2"
for _, tc := range []struct {
name string
projectID string
client *StackDriverClient
expected string
}{
{
"all projectID present",
pidFromMetadata,
&StackDriverClient{
projectID: pidFromClient,
credentials: GoogleApplicationCredentials{
ProjectID: pidFromClientCreds,
},
},
pidFromMetadata,
},
{
"both projectID from metadata and client present",
pidFromMetadata,
&StackDriverClient{
projectID: pidFromClient,
},
pidFromMetadata,
},
{
"both projectID from metadata and client credentials present",
pidFromMetadata,
&StackDriverClient{
credentials: GoogleApplicationCredentials{
ProjectID: pidFromClientCreds,
},
},
pidFromMetadata,
},
{
"both projectID from client and client credentials present",
"",
&StackDriverClient{
projectID: pidFromClient,
credentials: GoogleApplicationCredentials{
ProjectID: pidFromClientCreds,
},
},
pidFromClient,
},
{
"projectID from metadata only",
pidFromMetadata,
&StackDriverClient{},
pidFromMetadata,
},
{
"projectID from client only",
"",
&StackDriverClient{
projectID: pidFromClient,
},
pidFromClient,
},
{
"projectID from client credentials only",
"",
&StackDriverClient{
projectID: "",
credentials: GoogleApplicationCredentials{
ProjectID: pidFromClientCreds,
},
},
pidFromClientCreds,
},
} {
t.Run(tc.name, func(t *testing.T) {
pid := getActualProjectID(tc.client, tc.projectID)
assert.Equal(t, pid, tc.expected)
})
}
}