Skip to content

Commit

Permalink
test: update TestRunEnhancers (#82)
Browse files Browse the repository at this point in the history
* update README with new features

* update test to prevent regression

* linting

* change README back to avoid conflict
  • Loading branch information
Jiahui-Zhang-20 authored Dec 22, 2023
1 parent d94e48e commit 40471f1
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions enhancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"reflect"
"sort"
"testing"

"github.com/getsentry/sentry-go"
Expand All @@ -16,11 +18,38 @@ func TestRunEnhancers(t *testing.T) {
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",
Expand All @@ -40,7 +69,11 @@ func TestRunEnhancers(t *testing.T) {
},
},
}
_, err := fakeClientset.CoreV1().Pods("TestRunPodEnhancerNamespace").Create(context.TODO(), podObj, metav1.CreateOptions{})
_, 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)
}
Expand All @@ -65,29 +98,26 @@ func TestRunEnhancers(t *testing.T) {
expectedTags := map[string]string{
"node_name": "TestRunPodEnhancerNode",
}
// the test fails if any tag key, value pair does not match

// 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",
"TestRunPodEnhancerPod",
"ReplicaSet",
"TestRunPodEnhancerReplicaset",
}

// The test fails if any tag key, value pair does not match
var found bool
for _, expectedFingerprint := range expectedFingerprints {
found = false
for _, fingerprint := range event.Fingerprint {
if expectedFingerprint == fingerprint {
found = true
}
}
if !found {
t.Errorf("The fingerprint slice does not contain the expected fingerprint: %s", expectedFingerprint)
}
// 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
Expand Down

0 comments on commit 40471f1

Please sign in to comment.