Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: update TestRunEnhancers #82

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file added images/example_crons_monitor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes