1 Star 0 Fork 0

blocklake/lxcfs-admission-webhook

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
lxcfs_test.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
denverdino 提交于 2019-09-15 08:04 . Initial support
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))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/blocklake/lxcfs-admission-webhook.git
git@gitee.com:blocklake/lxcfs-admission-webhook.git
blocklake
lxcfs-admission-webhook
lxcfs-admission-webhook
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385