Skip to content

Commit

Permalink
add unit test webook TestGetPodFromRequest
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 committed Dec 26, 2023
1 parent f75df8e commit b135f47
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions pkg/webhook/mutating_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webhook
import (
"context"
gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -11,6 +12,7 @@ import (
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"testing"
)

Expand Down Expand Up @@ -135,3 +137,146 @@ func TestPatchContainers(t *testing.T) {
}
}
}

func TestGetPodFromRequest(t *testing.T) {
tests := []struct {
req admission.Request
pod *corev1.Pod
}{
// case 0
{
req: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
Object: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v2",
"name": "bar"
}
]
}
}`),
},
OldObject: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v1",
"name": "bar"
}
]
}
}`),
},
},
},
pod: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Image: "bar:v1", Name: "bar"},
},
},
},
},

// case 1
{
req: admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Operation: admissionv1.Update,
Object: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v2",
"name": "bar"
}
]
}
}`),
},
OldObject: runtime.RawExtension{
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "foo",
"namespace": "default"
},
"spec": {
"containers": [
{
"image": "bar:v1",
"name": "bar"
}
]
}
}`),
},
},
},
pod: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Image: "bar:v2", Name: "bar"},
},
},
},
},
}

decoder, err := admission.NewDecoder(runtime.NewScheme())
if err != nil {
t.Error(err)
}

for i, test := range tests {
actual, err := getPodFromRequest(test.req, decoder)
if err != nil {
t.Error(err)
}
expect := test.pod
if !reflect.DeepEqual(actual, expect) {
t.Errorf("case %d: expect pod %v, but actually got %v", i, expect, actual)
}
}
}

0 comments on commit b135f47

Please sign in to comment.