-
Notifications
You must be signed in to change notification settings - Fork 64
/
sentry_dsn_data_test.go
308 lines (276 loc) · 8.91 KB
/
sentry_dsn_data_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"context"
"reflect"
"testing"
"github.com/getsentry/sentry-go"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
func TestNewDsnClientMapping(t *testing.T) {
// Set the custom dsn flag as true
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "TRUE")
clientMapping := NewDsnClientMapping()
if clientMapping.clientMap == nil {
t.Errorf("Failed to initialize client mapping")
}
if !clientMapping.customDsnFlag {
t.Errorf("Failed to read custom dsn flag as true")
}
// set the custom dsn flag as false
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "")
clientMapping = NewDsnClientMapping()
if clientMapping.customDsnFlag {
t.Errorf("Failed to read custom dsn flag as false")
}
}
func TestAddClientToMap(t *testing.T) {
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
clientOptions := sentry.ClientOptions{
Dsn: fakeDsn,
}
clientMapping := NewDsnClientMapping()
// Add the dsn for the first time
_, err := clientMapping.AddClientToMap(clientOptions)
if err != nil {
t.Errorf("Failed to add client to map")
}
firstClient, ok := clientMapping.clientMap[fakeDsn]
if !ok {
t.Errorf("Failed to add the fake dsn to the client map")
}
if firstClient.Options().Dsn != fakeDsn {
t.Errorf("The DSN in the added client is incorrect")
}
// Try to add the same dsn again, which should create a new client
_, err = clientMapping.AddClientToMap(clientOptions)
if err != nil {
t.Errorf("Failed to add client to map")
}
secondClient, ok := clientMapping.clientMap[fakeDsn]
if !ok {
t.Errorf("Failed to add client if dsn already exists in the map")
}
if firstClient == secondClient {
t.Errorf("Failed to create new client if the dsn already exists")
}
}
func TestGetClientFromMap(t *testing.T) {
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
clientOptions := sentry.ClientOptions{
Dsn: fakeDsn,
}
clientMapping := NewDsnClientMapping()
// Add the DSN and its corresponding client
_, err := clientMapping.AddClientToMap(clientOptions)
if err != nil {
t.Errorf("Failed to add client to map")
}
// Test function to retrieve the client with DSN
client, ok := clientMapping.GetClientFromMap(fakeDsn)
if !ok {
t.Errorf("Failed to retrieve a client")
}
if client.Options().Dsn != fakeDsn {
t.Errorf("Failed to retrieve client with correct DSN")
}
}
func TestGetClientFromObject(t *testing.T) {
// Set the custom dsn flag as true
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "TRUE")
clientMapping := NewDsnClientMapping()
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
// Create annotation map that includes the DSN
annotations := make(map[string]string)
annotations["k8s.sentry.io/dsn"] = fakeDsn
// Create replicaset object with DSN annotation
var replicas int32 = 3
replicasetObj := &v1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "TestSearchDsnReplicaset",
Namespace: "TestSearchDsnNamespace",
Annotations: annotations,
},
Spec: v1.ReplicaSetSpec{
Replicas: &replicas,
},
Status: v1.ReplicaSetStatus{
Replicas: replicas,
},
}
// Add the replicaset object to the fake clientset
_, err := fakeClientset.AppsV1().ReplicaSets("TestSearchDsnNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting replicaset add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
// Create client options with DSN
clientOptions := sentry.ClientOptions{
Dsn: fakeDsn,
}
// Test function to retrieve client from object using DSN
firstClient, ok := clientMapping.GetClientFromObject(ctx, replicasetObj, clientOptions)
if !ok {
t.Errorf("The function failed to create and retrieve new client from object")
}
if firstClient.Options().Dsn != fakeDsn {
t.Errorf("The retrieved client has the incorrect DSN")
}
secondClient, ok := clientMapping.GetClientFromObject(ctx, replicasetObj, clientOptions)
if !ok {
t.Errorf("The function failed to retrieve existing client from object")
}
if !reflect.DeepEqual(firstClient, secondClient) {
t.Errorf("The function failed to retrieve the client it originally created")
}
}
func TestSearchDsn(t *testing.T) {
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
// Create annotation map that includes the DSN
annotations := make(map[string]string)
annotations["k8s.sentry.io/dsn"] = fakeDsn
// Create pod object with replicaset as owning reference
podObj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "TestSearchDsnPod",
Namespace: "TestSearchDsnNamespace",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "TestSearchDsnReplicaset",
},
},
},
Spec: corev1.PodSpec{
NodeName: "TestSearchDsnNode",
},
}
var replicas int32 = 3
replicasetObj := &v1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "TestSearchDsnReplicaset",
Namespace: "TestSearchDsnNamespace",
Annotations: annotations,
},
Spec: v1.ReplicaSetSpec{
Replicas: &replicas,
},
Status: v1.ReplicaSetStatus{
Replicas: replicas,
},
}
_, err := fakeClientset.CoreV1().Pods("TestSearchDsnNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting pod add: %v", err)
}
_, err = fakeClientset.AppsV1().ReplicaSets("TestSearchDsnNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting replicaset add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
// The replicaset contains the annotation for the DSN
replicasetDsn, err := searchDsn(ctx, replicasetObj)
if err != nil {
t.Error(err)
t.Errorf("Failed to find replicaset's owning object's DSN")
}
if replicasetDsn != fakeDsn {
t.Errorf("DSN expected: %s, actual: %s", fakeDsn, replicasetDsn)
}
// The pod object does not itself contain the DSN annotation
// but its owning object (replicaset) contains the DSN
podDsn, err := searchDsn(ctx, podObj)
if err != nil {
t.Error(err)
t.Errorf("Failed to find pod's owning object's DSN")
}
if podDsn != fakeDsn {
t.Errorf("DSN expected: %s, actual: %s", fakeDsn, podDsn)
}
}
func TestFindObject(t *testing.T) {
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
// Create pod object with an error status
podObj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "TestFindObjectPod",
Namespace: "TestFindObjectNamespace",
},
Spec: corev1.PodSpec{
NodeName: "TestFindObjectNode",
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "FakeDnsLabel",
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Fake Reason: TestFindObjectEvent",
Message: "Fake Message: TestFindObjectEvent",
},
},
},
},
},
}
var replicas int32 = 3
replicasetObj := &v1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "TestFindObjectReplicaset",
Namespace: "TestFindObjectNamespace",
},
Spec: v1.ReplicaSetSpec{
Replicas: &replicas,
},
Status: v1.ReplicaSetStatus{
Replicas: replicas,
},
}
_, err := fakeClientset.CoreV1().Pods("TestFindObjectNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting pod add: %v", err)
}
_, err = fakeClientset.AppsV1().ReplicaSets("TestFindObjectNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting replicaset add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
// test function to find the pod object
retObj, ok := findObject(ctx, KindPod, "TestFindObjectNamespace", "TestFindObjectPod")
if !ok {
t.Errorf("The function findObject was not able to find the pod object")
}
retPod, ok := retObj.(*corev1.Pod)
if !ok {
t.Errorf("The object returned is not of the Pod type")
}
if !reflect.DeepEqual(podObj, retPod) {
t.Errorf("The returned pod is not equal to the original pod")
}
// test function to find the replicaset object
retObj, ok = findObject(ctx, KindReplicaset, "TestFindObjectNamespace", "TestFindObjectReplicaset")
if !ok {
t.Errorf("The function findObject was not able to find the pod object")
}
retReplicaset, ok := retObj.(*v1.ReplicaSet)
if !ok {
t.Errorf("The object returned is not of the replicaset type")
}
if !reflect.DeepEqual(replicasetObj, retReplicaset) {
t.Errorf("The returned replicaset is not equal to the original replicaset")
}
}