forked from denverdino/lxcfs-admission-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxcfs_test.go
98 lines (80 loc) · 1.94 KB
/
lxcfs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
jsonpatch "github.com/evanphx/json-patch"
corev1 "k8s.io/api/core/v1"
"testing"
)
func TestCreatePodPatch(t *testing.T) {
var pod corev1.Pod
pod.Name = "test"
pod.Namespace = "testNS"
pod.Annotations = map[string]string{}
pod.Annotations[admissionWebhookAnnotationStatusKey] = "testAnnotation"
pod.Spec.Containers = []corev1.Container{
{
Image: "test_image",
},
}
testCreatePodPatch(t, &pod)
}
func TestCreatePodPatch2(t *testing.T) {
var pod corev1.Pod
pod.Name = "test"
pod.Namespace = "testNS"
pod.Annotations = map[string]string{}
pod.Annotations[admissionWebhookAnnotationStatusKey] = "testAnnotation"
pod.Spec.Containers = []corev1.Container{
{
Image: "test_image",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test",
MountPath: "/etc/test",
},
},
},
}
pod.Spec.Volumes = []corev1.Volume{
{
Name: "test",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/test",
},
},
},
}
testCreatePodPatch(t, &pod)
}
func testCreatePodPatch(t *testing.T, pod *corev1.Pod) {
patchJSON, err := createPodPatch(pod)
if err != nil {
t.Errorf("error in createPodPatch: %v", err)
} else {
t.Logf("patch :\n%s", string(patchJSON))
}
patch, err := jsonpatch.DecodePatch(patchJSON)
if err != nil {
t.Errorf("error in createPodPatch: %v", err)
}
podJSON, err := json.Marshal(pod)
if err != nil {
t.Errorf("error in json.Marshal: %v", err)
}
modified, err := patch.Apply(podJSON)
if err != nil {
t.Errorf("error in createPodPatch: %v", err)
}
t.Logf("modified Pod:\n%s", string(modified))
var modifiedPod corev1.Pod
err = json.Unmarshal(modified, &modifiedPod)
if err != nil {
t.Errorf("error in createPodPatch: %v", err)
}
modifiedJSON, err := json.Marshal(modifiedPod)
if err != nil {
t.Errorf("error in json.Marshal: %v", err)
}
t.Logf("modified Pod:\n%s", string(modifiedJSON))
}