-
Notifications
You must be signed in to change notification settings - Fork 64
/
enhancers_test.go
247 lines (225 loc) · 6.71 KB
/
enhancers_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
package main
import (
"context"
"reflect"
"sort"
"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 TestRunEnhancers(t *testing.T) {
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
var replicas int32 = 3
replicasetObj := &v1.ReplicaSet{
TypeMeta: metav1.TypeMeta{
Kind: "ReplicaSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "TestRunPodEnhancerReplicaset",
Namespace: "TestRunPodEnhancerNamespace",
UID: "218fc5a9-a5f1-4b54-aa05-46717d0ab26d",
},
Spec: v1.ReplicaSetSpec{
Replicas: &replicas,
},
Status: v1.ReplicaSetStatus{
Replicas: replicas,
},
}
// Create pod object with an error status
podObj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "TestRunPodEnhancerPod",
Namespace: "TestRunPodEnhancerNamespace",
UID: "123g4e1p-a591-5b50-aa28-12345d0ab26f",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "TestRunPodEnhancerReplicaset",
UID: "218fc5a9-a5f1-4b54-aa05-46717d0ab26d",
},
},
},
Spec: corev1.PodSpec{
NodeName: "TestRunPodEnhancerNode",
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "FakeDnsLabel",
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Fake Reason: TestRunPodEnhancerEvent",
Message: "Fake Message: TestRunPodEnhancerEvent",
},
},
},
},
},
}
_, err := fakeClientset.AppsV1().ReplicaSets("TestRunPodEnhancerNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting replicaset add: %v", err)
}
_, err = fakeClientset.CoreV1().Pods("TestRunPodEnhancerNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error injecting pod add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
// Create empty scope
scope := sentry.NewScope()
// Create empty event
event := sentry.NewEvent()
// Add event message
event.Message = "This event is for TestRunPodEnhancer"
// Call pod enhancer to modify scope and event
err = runEnhancers(ctx, nil, KindPod, podObj, scope, event)
if err != nil {
t.Errorf("runEnhancers returned an error: %v", err)
}
// Apply the scope to the event
// so we can check the tags
scope.ApplyToEvent(event, nil)
expectedTags := map[string]string{
"node_name": "TestRunPodEnhancerNode",
}
// The test fails if any tag key, value pair does not match
for key, val := range expectedTags {
if event.Tags[key] != expectedTags[key] {
t.Errorf("For Sentry tag with key [%s], received \"%s\", wanted \"%s\"", key, event.Tags[key], val)
}
}
expectedFingerprints := []string{
"This event is for TestRunPodEnhancer",
"ReplicaSet",
"TestRunPodEnhancerReplicaset",
}
// This helps ensure that the enhancer has all the necessary fingerprint
// strings (it is important that only replicaset's kind and name is included
// and not the pod's so all events from the replicaset are grouped together)
sort.Strings(expectedFingerprints)
sort.Strings(event.Fingerprint)
if !reflect.DeepEqual(expectedFingerprints, event.Fingerprint) {
t.Errorf("The fingerprint is incorrect")
}
// Check message is changed to include pod name
expectedMessage := "TestRunPodEnhancerPod: This event is for TestRunPodEnhancer"
if event.Message != expectedMessage {
t.Errorf("For event message, received \"%s\", wanted \"%s\"", event.Message, expectedMessage)
}
}
func TestFindRootOwner(t *testing.T) {
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
// Create pod object with no owning references
podObj := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "TestFindRootOwnerPod",
Namespace: "TestFindRootOwnerNamespace",
},
Spec: corev1.PodSpec{
NodeName: "TestFindRootOwnerNode",
},
}
_, err := fakeClientset.CoreV1().Pods("TestFindRootOwnerNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting pod add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
// Check the findRootOwners function does not return a slice
// with pod which is the object passed in since it has no
// owning references
rootOwners, err := findRootOwners(ctx, &KindObjectPair{
kind: "Pod",
object: podObj,
})
if err != nil {
t.Errorf("Function returned error: %#v", err)
}
if len(rootOwners) != 0 {
t.Errorf("Function did not return empty slice as expected")
}
}
func TestOwnerRefDFS(t *testing.T) {
// Create empty context
ctx := context.Background()
// Create simple fake client
fakeClientset := fake.NewSimpleClientset()
// Create pod object with replicaset as owning reference
podObj := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "TestOwnerRefDFSPod",
Namespace: "TestOwnerRefDFSNamespace",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "TestOwnerRefDFSReplicaset",
},
},
},
Spec: corev1.PodSpec{
NodeName: "TestOwnerRefDFSNode",
},
}
var replicas int32 = 3
replicasetObj := &v1.ReplicaSet{
TypeMeta: metav1.TypeMeta{
Kind: "ReplicaSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: "TestOwnerRefDFSReplicaset",
Namespace: "TestOwnerRefDFSNamespace",
},
Spec: v1.ReplicaSetSpec{
Replicas: &replicas,
},
Status: v1.ReplicaSetStatus{
Replicas: replicas,
},
}
_, err := fakeClientset.CoreV1().Pods("TestOwnerRefDFSNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting pod add: %v", err)
}
_, err = fakeClientset.AppsV1().ReplicaSets("TestOwnerRefDFSNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error injecting replicaset add: %v", err)
}
ctx = setClientsetOnContext(ctx, fakeClientset)
rootOwners, err := ownerRefDFS(ctx, &KindObjectPair{
kind: podObj.Kind,
object: podObj,
})
if err != nil {
t.Errorf("the DFS produced an error: %#v", err)
}
if rootOwners == nil {
t.Errorf("Failed to return a slice of root owners: %#v", err)
return
}
if len(rootOwners) != 1 {
t.Errorf("Failed to produce correct number of root owners")
return
}
if rootOwners[0].kind != replicasetObj.Kind {
t.Errorf("The root owner's kind is incorrect")
}
if rootOwners[0].object.GetName() != replicasetObj.Name {
t.Errorf("The root owner's object is incorrect")
}
}