From 5e3d5190c3ef118138b6254f7b7e00ac73daa9f4 Mon Sep 17 00:00:00 2001 From: Hu Shuai Date: Thu, 28 Jan 2021 05:56:14 +0800 Subject: [PATCH] Add unit test for pkg/healthcheck/sidecar.go (#5609) Signed-off-by: Hu Shuai --- pkg/healthcheck/sidecar_test.go | 122 ++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 pkg/healthcheck/sidecar_test.go diff --git a/pkg/healthcheck/sidecar_test.go b/pkg/healthcheck/sidecar_test.go new file mode 100644 index 000000000..2824b8ad3 --- /dev/null +++ b/pkg/healthcheck/sidecar_test.go @@ -0,0 +1,122 @@ +package healthcheck + +import ( + "reflect" + "testing" + + "github.com/linkerd/linkerd2/pkg/k8s" + corev1 "k8s.io/api/core/v1" +) + +func TestHasExistingSidecars(t *testing.T) { + for _, tc := range []struct { + podSpec *corev1.PodSpec + expected bool + }{ + { + podSpec: &corev1.PodSpec{}, + expected: false, + }, + { + podSpec: &corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "foo", + Image: "bar", + }, + }, + InitContainers: []corev1.Container{ + { + Name: "foo", + Image: "bar", + }, + }, + }, + expected: false, + }, + { + podSpec: &corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: k8s.ProxyContainerName, + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "istio-proxy", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + Containers: []corev1.Container{ + { + Image: "ghcr.io/linkerd/proxy:1.0.0", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + Containers: []corev1.Container{ + { + Image: "gcr.io/istio-release/proxyv2:1.0.0", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "linkerd-init", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "istio-init", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Image: "ghcr.io/linkerd/proxy-init:1.0.0", + }, + }, + }, + expected: true, + }, + { + podSpec: &corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Image: "gcr.io/istio-release/proxy_init:1.0.0", + }, + }, + }, + expected: true, + }, + } { + if !reflect.DeepEqual(HasExistingSidecars(tc.podSpec), tc.expected) { + t.Errorf("expected: %v, got: %v", tc.expected, HasExistingSidecars(tc.podSpec)) + } + } +}