Skip to content

Commit

Permalink
add tests for root owners finding
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 20, 2023
1 parent 0b58f2c commit 16a14ae
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions enhancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"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"
Expand Down Expand Up @@ -96,3 +97,125 @@ func TestRunEnhancers(t *testing.T) {
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")
}

}

0 comments on commit 16a14ae

Please sign in to comment.