Skip to content

Commit ea40479

Browse files
test: unit tests for sentry_dsn_data struct (#75)
* create test for findObject * add unit tests for DsnClientMapping methods * add unit tests for sentry_dsn_data
1 parent a7a426c commit ea40479

File tree

2 files changed

+311
-0
lines changed

2 files changed

+311
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ notes.txt
3636

3737
# dotenv files
3838
.env
39+
40+
# vscode
41+
.vscode/

sentry_dsn_data_test.go

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/getsentry/sentry-go"
9+
v1 "k8s.io/api/apps/v1"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/client-go/kubernetes/fake"
13+
)
14+
15+
func TestNewDsnClientMapping(t *testing.T) {
16+
17+
// Set the custom dsn flag as true
18+
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "TRUE")
19+
clientMapping := NewDsnClientMapping()
20+
if clientMapping.clientMap == nil {
21+
t.Errorf("Failed to initialize client mapping")
22+
}
23+
if !clientMapping.customDsnFlag {
24+
t.Errorf("Failed to read custom dsn flag as true")
25+
}
26+
27+
// set the custom dsn flag as false
28+
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "")
29+
clientMapping = NewDsnClientMapping()
30+
if clientMapping.customDsnFlag {
31+
t.Errorf("Failed to read custom dsn flag as false")
32+
}
33+
34+
}
35+
36+
func TestAddClientToMap(t *testing.T) {
37+
38+
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
39+
clientOptions := sentry.ClientOptions{
40+
Dsn: fakeDsn,
41+
}
42+
clientMapping := NewDsnClientMapping()
43+
44+
// Add the dsn for the first time
45+
clientMapping.AddClientToMap(clientOptions)
46+
firstClient, ok := clientMapping.clientMap[fakeDsn]
47+
if !ok {
48+
t.Errorf("Failed to add the fake dsn to the client map")
49+
}
50+
if firstClient.Options().Dsn != fakeDsn {
51+
t.Errorf("The DSN in the added client is incorrect")
52+
}
53+
54+
// Try to add the same dsn again, which should create a new client
55+
clientMapping.AddClientToMap(clientOptions)
56+
secondClient, ok := clientMapping.clientMap[fakeDsn]
57+
if !ok {
58+
t.Errorf("Failed to add client if dsn already exists in the map")
59+
}
60+
if firstClient == secondClient {
61+
t.Errorf("Failed to create new client if the dsn already exists")
62+
}
63+
}
64+
65+
func TestGetClientFromMap(t *testing.T) {
66+
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
67+
clientOptions := sentry.ClientOptions{
68+
Dsn: fakeDsn,
69+
}
70+
clientMapping := NewDsnClientMapping()
71+
72+
// Add the DSN and its corresponding client
73+
clientMapping.AddClientToMap(clientOptions)
74+
75+
// Test function to retrieve the client with DSN
76+
client, ok := clientMapping.GetClientFromMap(fakeDsn)
77+
if !ok {
78+
t.Errorf("Failed to retrieve a client")
79+
}
80+
if client.Options().Dsn != fakeDsn {
81+
t.Errorf("Failed to retrieve client with correct DSN")
82+
}
83+
}
84+
85+
func TestGetClientFromObject(t *testing.T) {
86+
87+
// Set the custom dsn flag as true
88+
t.Setenv("SENTRY_K8S_CUSTOM_DSNS", "TRUE")
89+
clientMapping := NewDsnClientMapping()
90+
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
91+
92+
// Create empty context
93+
ctx := context.Background()
94+
// Create simple fake client
95+
fakeClientset := fake.NewSimpleClientset()
96+
97+
// Create annotation map that includes the DSN
98+
annotations := make(map[string]string)
99+
annotations["k8s.sentry.io/dsn"] = fakeDsn
100+
101+
// Create replicaset object with DSN annotation
102+
var replicas int32 = 3
103+
replicasetObj := &v1.ReplicaSet{
104+
ObjectMeta: metav1.ObjectMeta{
105+
Name: "TestSearchDsnReplicaset",
106+
Namespace: "TestSearchDsnNamespace",
107+
Annotations: annotations,
108+
},
109+
Spec: v1.ReplicaSetSpec{
110+
Replicas: &replicas,
111+
},
112+
Status: v1.ReplicaSetStatus{
113+
Replicas: replicas,
114+
},
115+
}
116+
117+
// Add the replicaset object to the fake clientset
118+
_, err := fakeClientset.AppsV1().ReplicaSets("TestSearchDsnNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
119+
if err != nil {
120+
t.Fatalf("Error injecting replicaset add: %v", err)
121+
}
122+
ctx = setClientsetOnContext(ctx, fakeClientset)
123+
124+
// Create client options with DSN
125+
clientOptions := sentry.ClientOptions{
126+
Dsn: fakeDsn,
127+
}
128+
129+
// Test function to retrieve client from object using DSN
130+
firstClient, ok := clientMapping.GetClientFromObject(ctx, replicasetObj, clientOptions)
131+
if !ok {
132+
t.Errorf("The function failed to create and retrieve new client from object")
133+
}
134+
if firstClient.Options().Dsn != fakeDsn {
135+
t.Errorf("The retrieved client has the incorrect DSN")
136+
}
137+
secondClient, ok := clientMapping.GetClientFromObject(ctx, replicasetObj, clientOptions)
138+
if !ok {
139+
t.Errorf("The function failed to retrieve existing client from object")
140+
}
141+
if !reflect.DeepEqual(firstClient, secondClient) {
142+
t.Errorf("The function failed to retrieve the client it originally created")
143+
}
144+
145+
}
146+
147+
func TestSearchDsn(t *testing.T) {
148+
149+
// Create empty context
150+
ctx := context.Background()
151+
// Create simple fake client
152+
fakeClientset := fake.NewSimpleClientset()
153+
154+
fakeDsn := "https://c6f9a148ee0775891414b50b9af35959@o4506191942320128.ingest.sentry.io/1234567890"
155+
156+
// Create annotation map that includes the DSN
157+
annotations := make(map[string]string)
158+
annotations["k8s.sentry.io/dsn"] = fakeDsn
159+
160+
// Create pod object with replicaset as owning reference
161+
podObj := &corev1.Pod{
162+
ObjectMeta: metav1.ObjectMeta{
163+
Name: "TestSearchDsnPod",
164+
Namespace: "TestSearchDsnNamespace",
165+
OwnerReferences: []metav1.OwnerReference{
166+
{
167+
Kind: "ReplicaSet",
168+
Name: "TestSearchDsnReplicaset",
169+
},
170+
},
171+
},
172+
Spec: corev1.PodSpec{
173+
NodeName: "TestSearchDsnNode",
174+
},
175+
}
176+
177+
var replicas int32 = 3
178+
replicasetObj := &v1.ReplicaSet{
179+
ObjectMeta: metav1.ObjectMeta{
180+
Name: "TestSearchDsnReplicaset",
181+
Namespace: "TestSearchDsnNamespace",
182+
Annotations: annotations,
183+
},
184+
Spec: v1.ReplicaSetSpec{
185+
Replicas: &replicas,
186+
},
187+
Status: v1.ReplicaSetStatus{
188+
Replicas: replicas,
189+
},
190+
}
191+
192+
_, err := fakeClientset.CoreV1().Pods("TestSearchDsnNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
193+
if err != nil {
194+
t.Fatalf("Error injecting pod add: %v", err)
195+
}
196+
_, err = fakeClientset.AppsV1().ReplicaSets("TestSearchDsnNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
197+
if err != nil {
198+
t.Fatalf("Error injecting replicaset add: %v", err)
199+
}
200+
201+
ctx = setClientsetOnContext(ctx, fakeClientset)
202+
203+
// The replicaset contains the annotation for the DSN
204+
replicasetDsn, err := searchDsn(ctx, replicasetObj)
205+
if err != nil {
206+
t.Error(err)
207+
t.Errorf("Failed to find replicaset's owning object's DSN")
208+
}
209+
if replicasetDsn != fakeDsn {
210+
t.Errorf("DSN expected: %s, actual: %s", fakeDsn, replicasetDsn)
211+
}
212+
213+
// The pod object does not itself contain the DSN annotation
214+
// but its owning object (replicaset) contains the DSN
215+
podDsn, err := searchDsn(ctx, podObj)
216+
if err != nil {
217+
t.Error(err)
218+
t.Errorf("Failed to find pod's owning object's DSN")
219+
}
220+
if podDsn != fakeDsn {
221+
t.Errorf("DSN expected: %s, actual: %s", fakeDsn, podDsn)
222+
}
223+
224+
}
225+
226+
func TestFindObject(t *testing.T) {
227+
228+
// Create empty context
229+
ctx := context.Background()
230+
// Create simple fake client
231+
fakeClientset := fake.NewSimpleClientset()
232+
233+
// Create pod object with an error status
234+
podObj := &corev1.Pod{
235+
ObjectMeta: metav1.ObjectMeta{
236+
Name: "TestFindObjectPod",
237+
Namespace: "TestFindObjectNamespace",
238+
},
239+
Spec: corev1.PodSpec{
240+
NodeName: "TestFindObjectNode",
241+
},
242+
Status: corev1.PodStatus{
243+
ContainerStatuses: []corev1.ContainerStatus{
244+
{
245+
Name: "FakeDnsLabel",
246+
State: corev1.ContainerState{
247+
Terminated: &corev1.ContainerStateTerminated{
248+
ExitCode: 1,
249+
Reason: "Fake Reason: TestFindObjectEvent",
250+
Message: "Fake Message: TestFindObjectEvent",
251+
},
252+
},
253+
},
254+
},
255+
},
256+
}
257+
258+
var replicas int32 = 3
259+
replicasetObj := &v1.ReplicaSet{
260+
ObjectMeta: metav1.ObjectMeta{
261+
Name: "TestFindObjectReplicaset",
262+
Namespace: "TestFindObjectNamespace",
263+
},
264+
Spec: v1.ReplicaSetSpec{
265+
Replicas: &replicas,
266+
},
267+
Status: v1.ReplicaSetStatus{
268+
Replicas: replicas,
269+
},
270+
}
271+
272+
_, err := fakeClientset.CoreV1().Pods("TestFindObjectNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
273+
if err != nil {
274+
t.Fatalf("Error injecting pod add: %v", err)
275+
}
276+
_, err = fakeClientset.AppsV1().ReplicaSets("TestFindObjectNamespace").Create(context.TODO(), replicasetObj, metav1.CreateOptions{})
277+
if err != nil {
278+
t.Fatalf("Error injecting replicaset add: %v", err)
279+
}
280+
281+
ctx = setClientsetOnContext(ctx, fakeClientset)
282+
283+
// test function to find the pod object
284+
retObj, ok := findObject(ctx, KindPod, "TestFindObjectNamespace", "TestFindObjectPod")
285+
if !ok {
286+
t.Errorf("The function findObject was not able to find the pod object")
287+
}
288+
retPod, ok := retObj.(*corev1.Pod)
289+
if !ok {
290+
t.Errorf("The object returned is not of the Pod type")
291+
}
292+
if !reflect.DeepEqual(podObj, retPod) {
293+
t.Errorf("The returned pod is not equal to the original pod")
294+
}
295+
296+
// test function to find the replicaset object
297+
retObj, ok = findObject(ctx, KindReplicaset, "TestFindObjectNamespace", "TestFindObjectReplicaset")
298+
if !ok {
299+
t.Errorf("The function findObject was not able to find the pod object")
300+
}
301+
retReplicaset, ok := retObj.(*v1.ReplicaSet)
302+
if !ok {
303+
t.Errorf("The object returned is not of the replicaset type")
304+
}
305+
if !reflect.DeepEqual(replicasetObj, retReplicaset) {
306+
t.Errorf("The returned replicaset is not equal to the original replicaset")
307+
}
308+
}

0 commit comments

Comments
 (0)