From 53550a825cbab31820ec67b70298e56e876a0d52 Mon Sep 17 00:00:00 2001 From: AllenZMC Date: Sun, 14 Aug 2022 23:49:15 +0800 Subject: [PATCH] improve ut for taint Signed-off-by: AllenZMC --- pkg/util/helper/taint_test.go | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/pkg/util/helper/taint_test.go b/pkg/util/helper/taint_test.go index 977c58d46..8f5d4a0c8 100644 --- a/pkg/util/helper/taint_test.go +++ b/pkg/util/helper/taint_test.go @@ -2,6 +2,7 @@ package helper import ( "context" + "reflect" "testing" corev1 "k8s.io/api/core/v1" @@ -264,3 +265,95 @@ func TestTolerationExists(t *testing.T) { }) } } + +func TestHasNoExecuteTaints(t *testing.T) { + tests := []struct { + name string + taints []corev1.Taint + want bool + }{ + { + name: "has NoExecute taints", + taints: []corev1.Taint{ + { + Key: clusterv1alpha1.TaintClusterUnreachable, + Effect: corev1.TaintEffectNoExecute, + }, + { + Key: clusterv1alpha1.TaintClusterNotReady, + Effect: corev1.TaintEffectNoSchedule, + }, + }, + want: true, + }, + { + name: "no NoExecute taints", + taints: []corev1.Taint{ + { + Key: clusterv1alpha1.TaintClusterUnreachable, + Effect: corev1.TaintEffectPreferNoSchedule, + }, + { + Key: clusterv1alpha1.TaintClusterNotReady, + Effect: corev1.TaintEffectNoSchedule, + }, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := HasNoExecuteTaints(tt.taints); got != tt.want { + t.Errorf("HasNoExecuteTaints() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetNoExecuteTaints(t *testing.T) { + tests := []struct { + name string + taints []corev1.Taint + want []corev1.Taint + }{ + { + name: "has NoExecute taints", + taints: []corev1.Taint{ + { + Key: clusterv1alpha1.TaintClusterUnreachable, + Effect: corev1.TaintEffectNoExecute, + }, + { + Key: clusterv1alpha1.TaintClusterNotReady, + Effect: corev1.TaintEffectNoSchedule, + }, + }, + want: []corev1.Taint{ + { + Key: clusterv1alpha1.TaintClusterUnreachable, + Effect: corev1.TaintEffectNoExecute, + }, + }, + }, + { + name: "no NoExecute taints", + taints: []corev1.Taint{ + { + Key: clusterv1alpha1.TaintClusterUnreachable, + Effect: corev1.TaintEffectPreferNoSchedule, + }, + { + Key: clusterv1alpha1.TaintClusterNotReady, + Effect: corev1.TaintEffectNoSchedule, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetNoExecuteTaints(tt.taints); !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetNoExecuteTaints() = %v, want %v", got, tt.want) + } + }) + } +}