Skip to content

Commit

Permalink
test(injection): add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <maxwell92@126.com>
  • Loading branch information
mlycore committed Aug 6, 2022
1 parent 0072ec8 commit df61eb4
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pisa-controller/pkg/webhook/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func retrievePodFromAdmissionRequest(req *v1.AdmissionRequest) *corev1.Pod {
return pod
}

//TODO: fix the fields
func buildPatch(pod *corev1.Pod) string {
var pisaProxyDeployedName string

Expand All @@ -112,7 +113,7 @@ func buildPatch(pod *corev1.Pod) string {
pisaProxyDeployedName,
pisaProxyAdminListenHost,
pisaProxyAdminListenPort,
pisaProxyLoglevel,
pisaProxyAdminLoglevel,
)
}

Expand Down
232 changes: 232 additions & 0 deletions pisa-controller/pkg/webhook/injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
package webhook

import (
"encoding/json"
"errors"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func Test_GetPisaProxyDeployedNameFromPod(t *testing.T) {
Expand Down Expand Up @@ -48,3 +55,228 @@ func Test_GetPisaProxyDeployedNameFromPod(t *testing.T) {
assert.Equal(t, c.exp, getPisaProxyDeployedNameFromPod(c.kind, c.generatedName), c.msg)
}
}

func Test_injection(t *testing.T) {
cases := []struct {
rev *v1.AdmissionReview
msg string
err error
errString string
}{
{
rev: &v1.AdmissionReview{
Request: &v1.AdmissionRequest{
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "svc",
},
},
},
msg: "wrong gvr should be error",
err: errors.New("retrieve pod from admission request error"),
errString: "retrieve pod from admission request error",
},
{
rev: &v1.AdmissionReview{
Request: &v1.AdmissionRequest{
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
},
},
msg: "duplicated sidecar cause error",
err: errors.New("build pod patch response error"),
errString: "build pod patch response error",
},
}

for _, c := range cases {
assert.EqualError(t, c.err, c.errString, c.msg)
}
}

const pod = `{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"labels": {
"app": "nginx"
},
"name": "nginx",
"namespace": "temp"
},
"spec": {
"containers": [
{
"image": "nginx:1.14.2",
"imagePullPolicy": "IfNotPresent",
"name": "nginx",
"ports": [
{
"containerPort": 80,
"protocol": "TCP"
}
]
},
{
"image": "pisa-proxy: latest",
"name": "pisa-proxy",
"ports": [
{
"containerPort": 5591,
"protocol": "TCP"
}
]
}
]
}
}`

func Test_retrievePodFromAdmissionRequest(t *testing.T) {
cases := []struct {
req *v1.AdmissionRequest
msg string
}{
{
req: &v1.AdmissionRequest{
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
},
msg: "gvr should be equal",
},
{
req: &v1.AdmissionRequest{
Resource: metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
Object: runtime.RawExtension{
Raw: []byte(
pod,
),
},
},
msg: "decode should be succeed",
},
}

for _, c := range cases {
assert.NotNil(t, retrievePodFromAdmissionRequest(c.req), c.msg)
}
}

type patch struct {
OP string `json:"op"`
Path string `json:"path"`
Value *corev1.Container `json:"value"`
}

func Test_buildPatch(t *testing.T) {
cases := []struct {
pod *corev1.Pod
patch *patch
}{
{
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
GenerateName: "a-b-c-12345678-",
},
},
patch: &patch{
Value: &corev1.Container{
Env: []corev1.EnvVar{
{Name: EnvPisaControllerService, Value: DefaultPisaControllerService},
{Name: EnvPisaControllerNamespace, Value: DefaultPisaControllerNamespace},
{Name: EnvPisaProxyDeployedNamespace, ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.namespace",
},
}},
{Name: EnvPisaProxyDeployedName, Value: "pod"},
{Name: EnvPisaProxyAdminListenHost, Value: DefaultPisaProxyAdminListenHost},
{Name: EnvPisaProxyAdminListenPort, Value: strconv.Itoa(DefaultPisaProxyAdminListenPort)},
{Name: EnvPisaProxyAdminLoglevel, Value: DefaultPisaProxyAdminLoglevel},
},
},
},
},
{
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
},
},
GenerateName: "a-b-c-12345678-",
},
},
patch: &patch{
Value: &corev1.Container{
Env: []corev1.EnvVar{
{Name: EnvPisaControllerService, Value: DefaultPisaControllerService},
{Name: EnvPisaControllerNamespace, Value: DefaultPisaControllerNamespace},
{Name: EnvPisaProxyDeployedNamespace, ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.namespace",
},
}},
{Name: EnvPisaProxyDeployedName, Value: "a-b-c"},
{Name: EnvPisaProxyAdminListenHost, Value: DefaultPisaProxyAdminListenHost},
{Name: EnvPisaProxyAdminListenPort, Value: strconv.Itoa(DefaultPisaProxyAdminListenPort)},
{Name: EnvPisaProxyAdminLoglevel, Value: DefaultPisaProxyAdminLoglevel},
},
},
},
},
{
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "StatefulSet",
},
},
GenerateName: "a-b-",
},
},
patch: &patch{
Value: &corev1.Container{
Env: []corev1.EnvVar{
{Name: EnvPisaControllerService, Value: DefaultPisaControllerService},
{Name: EnvPisaControllerNamespace, Value: DefaultPisaControllerNamespace},
{Name: EnvPisaProxyDeployedNamespace, ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.namespace",
},
}},
{Name: EnvPisaProxyDeployedName, Value: "a-b"},
{Name: EnvPisaProxyAdminListenHost, Value: DefaultPisaProxyAdminListenHost},
{Name: EnvPisaProxyAdminListenPort, Value: strconv.Itoa(DefaultPisaProxyAdminListenPort)},
{Name: EnvPisaProxyAdminLoglevel, Value: DefaultPisaProxyAdminLoglevel},
},
},
},
},
}

for _, c := range cases {
pl := []patch{{Value: &corev1.Container{}}}
err := json.Unmarshal([]byte(buildPatch(c.pod)), &pl)
assert.NoError(t, err, "unmarshal should not be error")
assert.ElementsMatch(t, c.patch.Value.Env, pl[0].Value.Env, "sidecar env should be equal[")
}
}
26 changes: 14 additions & 12 deletions pisa-controller/pkg/webhook/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
pisaProxyImage, pisaControllerService, pisaControllerNamespace, pisaProxyAdminListenHost, pisaProxyLoglevel string
pisaProxyAdminListenPort uint32
pisaProxyImage, pisaControllerService, pisaControllerNamespace, pisaProxyAdminListenHost, pisaProxyAdminLoglevel string
pisaProxyAdminListenPort uint32
)

func init() {
Expand All @@ -48,26 +48,28 @@ func init() {
pisaProxyAdminListenPort = uint32(port)
}

if lv := os.Getenv(EnvPisaProxyLoglevel); lv == "" {
pisaProxyLoglevel = DefaultPisaProxyLoglevel
if lv := os.Getenv(EnvPisaProxyAdminLoglevel); lv == "" {
pisaProxyAdminLoglevel = DefaultPisaProxyAdminLoglevel
} else {
pisaProxyLoglevel = lv
pisaProxyAdminLoglevel = lv
}
}

const (
pisaProxyContainerName = "pisa-proxy"

EnvPisaProxyAdminListenHost = "PISA_PROXY_ADMIN_LISTEN_HOST"
EnvPisaProxyAdminListenPort = "PISA_PROXY_ADMIN_LISTEN_PORT"
EnvPisaProxyLoglevel = "PISA_PROXY_ADMIN_LOG_LEVEL"
EnvPisaProxyImage = "PISA_PROXY_IMAGE"
EnvPisaControllerService = "PISA_CONTROLLER_SERVIE"
EnvPisaControllerNamespace = "PISA_CONTROLLER_NAMESPACE"
EnvPisaProxyAdminListenHost = "PISA_PROXY_ADMIN_LISTEN_HOST"
EnvPisaProxyAdminListenPort = "PISA_PROXY_ADMIN_LISTEN_PORT"
EnvPisaProxyAdminLoglevel = "PISA_PROXY_ADMIN_LOG_LEVEL"
EnvPisaProxyImage = "PISA_PROXY_IMAGE"
EnvPisaProxyDeployedNamespace = "PISA_DEPLOYED_NAMESPACE"
EnvPisaProxyDeployedName = "PISA_DEPLOYED_NAME"
EnvPisaControllerService = "PISA_CONTROLLER_SERVICE"
EnvPisaControllerNamespace = "PISA_CONTROLLER_NAMESPACE"

DefaultPisaProxyAdminListenHost = "0.0.0.0"
DefaultPisaProxyAdminListenPort = 5591
DefaultPisaProxyLoglevel = "INFO"
DefaultPisaProxyAdminLoglevel = "INFO"
DefaultPisaProxyImage = "pisanixio/proxy:latest"
DefaultPisaControllerService = "default"
DefaultPisaControllerNamespace = "default"
Expand Down

0 comments on commit df61eb4

Please sign in to comment.