Skip to content

Commit 46abdd1

Browse files
author
xiaozhuang
committed
chore: webhook add testing
Signed-off-by: xiaozhuang <xiaozhuang@minimaxi.com>
1 parent a773e3b commit 46abdd1

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

pkg/webhook/pod_webhook_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package webhook
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"gomodules.xyz/jsonpatch/v2"
7+
"testing"
8+
9+
admissionv1 "k8s.io/api/admission/v1"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
13+
"k8s.io/apimachinery/pkg/runtime"
14+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
15+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
16+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
17+
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func TestPodAntiAffinityMutate_Handle(t *testing.T) {
22+
scheme := runtime.NewScheme()
23+
_ = corev1.AddToScheme(scheme)
24+
25+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
26+
decoder := admission.NewDecoder(scheme)
27+
logger := zap.New(zap.WriteTo(nil))
28+
29+
mutator := NewPodAffiniytMutate(fakeClient, decoder, logger)
30+
31+
tests := []struct {
32+
name string
33+
pod *corev1.Pod
34+
expectedPatches []jsonpatch.JsonPatchOperation
35+
}{
36+
{
37+
name: "Should mutate pod with anti-affinity",
38+
pod: &corev1.Pod{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: "redis-leader-0",
41+
Namespace: "default",
42+
Annotations: map[string]string{
43+
annotationKeyEnablePodAntiAffinity: "true",
44+
podAnnotationsRedisClusterApp: "db-01",
45+
},
46+
Labels: map[string]string{
47+
podLabelsRedisType: "redis-cluster",
48+
},
49+
},
50+
Spec: corev1.PodSpec{},
51+
},
52+
expectedPatches: []jsonpatch.JsonPatchOperation{
53+
{
54+
Operation: "add",
55+
Path: "/spec/affinity",
56+
Value: map[string]interface{}{
57+
"podAntiAffinity": map[string]interface{}{
58+
"requiredDuringSchedulingIgnoredDuringExecution": []interface{}{
59+
map[string]interface{}{
60+
"labelSelector": map[string]interface{}{
61+
"matchExpressions": []interface{}{
62+
map[string]interface{}{
63+
"key": "statefulset.kubernetes.io/pod-name",
64+
"operator": "In",
65+
"values": []interface{}{"redis-follower-0"},
66+
},
67+
},
68+
},
69+
"topologyKey": "kubernetes.io/hostname",
70+
},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
},
77+
{
78+
name: "Should not mutate pod without proper annotations",
79+
pod: &corev1.Pod{
80+
ObjectMeta: metav1.ObjectMeta{
81+
Name: "redis-follower-0",
82+
Namespace: "default",
83+
Annotations: map[string]string{
84+
podAnnotationsRedisClusterApp: "db-01",
85+
},
86+
Labels: map[string]string{
87+
podLabelsRedisType: "redis-cluster",
88+
},
89+
},
90+
Spec: corev1.PodSpec{},
91+
},
92+
expectedPatches: nil,
93+
},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
podBytes, err := json.Marshal(tt.pod)
99+
assert.NoError(t, err)
100+
101+
req := admission.Request{
102+
AdmissionRequest: admissionv1.AdmissionRequest{
103+
Namespace: "default",
104+
Object: runtime.RawExtension{
105+
Raw: podBytes,
106+
},
107+
},
108+
}
109+
110+
resp := mutator.Handle(context.Background(), req)
111+
assert.True(t, resp.Allowed)
112+
assert.Equal(t, tt.expectedPatches, resp.Patches)
113+
})
114+
}
115+
}

0 commit comments

Comments
 (0)