From cd1afb4af1271752b794bdc0466601fbbd59ae47 Mon Sep 17 00:00:00 2001 From: Qing Hao Date: Thu, 24 Mar 2022 11:07:05 +0800 Subject: [PATCH] add more UT for taints/tolerations (#66) Signed-off-by: haoqing0110 --- .../tainttoleration/taint_toleration_test.go | 583 +++++++++++++++--- 1 file changed, 503 insertions(+), 80 deletions(-) diff --git a/pkg/plugins/tainttoleration/taint_toleration_test.go b/pkg/plugins/tainttoleration/taint_toleration_test.go index 80fc699ac..ff1880dca 100644 --- a/pkg/plugins/tainttoleration/taint_toleration_test.go +++ b/pkg/plugins/tainttoleration/taint_toleration_test.go @@ -2,6 +2,7 @@ package tainttoleration import ( "context" + "errors" "strings" "testing" "time" @@ -32,7 +33,9 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { initObjs []runtime.Object expectedClusterNames []string expectedRequeueResult plugins.PluginRequeueResult + expectedErr error }{ + // taint.Effect is NoSelect, testing tolerations with different operator and key/value. { name: "taint.Effect is NoSelect and tolerations is empty", placement: testinghelpers.NewPlacement("test", "test").Build(), @@ -44,26 +47,13 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( - &clusterapiv1.Taint{ - Key: "key1", - Value: "value1", - Effect: clusterapiv1.TaintEffectNoSelect, - TimeAdded: metav1.Time{}, - }).WithTaint( - &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", - Effect: clusterapiv1.TaintEffectNoSelect, - TimeAdded: metav1.Time{}, - }).Build(), }, initObjs: []runtime.Object{}, expectedClusterNames: []string{}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, { - name: "taint.Effect is NoSelect and tolerations.Operator is Equal", + name: "taint.Effect is NoSelect and tolerations.Operator is Equal, key/value matches", placement: testinghelpers.NewPlacement("test", "test").AddToleration( &clusterapiv1beta1.Toleration{ Key: "key1", @@ -78,26 +68,82 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( - &clusterapiv1.Taint{ - Key: "key1", - Value: "value1", - Effect: clusterapiv1.TaintEffectNoSelect, - TimeAdded: metav1.Time{}, - }).WithTaint( - &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", - Effect: clusterapiv1.TaintEffectNoSelect, - TimeAdded: metav1.Time{}, - }).Build(), }, initObjs: []runtime.Object{}, expectedClusterNames: []string{"cluster1"}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, { - name: "taint.Effect is NoSelect and tolerations.Operator is Exist", + name: "taint.Effect is NoSelect and tolerations.Operator is Equal, toleration has empty key", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + expectedErr: errors.New("If the key is empty, operator must be Exists.\n"), + }, + { + name: "taint.Effect is NoSelect and tolerations.Operator is Equal, toleration has empty value", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelect and tolerations.Operator is Equal, key/value doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value2", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + testinghelpers.NewManagedCluster("cluster2").WithTaint( + &clusterapiv1.Taint{ + Key: "key2", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelect and tolerations.Operator is Exist, key matches", placement: testinghelpers.NewPlacement("test", "test").AddToleration( &clusterapiv1beta1.Toleration{ Key: "key1", @@ -111,16 +157,46 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelect and tolerations.Operator is Exist, toleration has empty key", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ Key: "key1", Value: "value1", Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, - }).WithTaint( + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + expectedErr: errors.New( + "If the operator is Exists, the value should be empty.\n", + ), + }, + { + name: "taint.Effect is NoSelect and tolerations.Operator is Exist, toleration has empty value", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", + Key: "key1", + Value: "value1", Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), @@ -130,39 +206,63 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { expectedRequeueResult: plugins.PluginRequeueResult{}, }, { - name: "taint.Effect is NoSelectIfNew and tolerations is empty", + name: "taint.Effect is NoSelect and tolerations.Operator is Exist, toleration key doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key2", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + // taint.Effect is NoSelectIfNew, testing tolerations with differet key/value, and cluster is/not in decision + { + name: "taint.Effect is NoSelectIfNew and tolerations is empty, cluster is not in decision", placement: testinghelpers.NewPlacement("test", "test").Build(), clusters: []*clusterapiv1.ManagedCluster{ testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ Key: "key1", - Value: "value1", Effect: clusterapiv1.TaintEffectNoSelectIfNew, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew and tolerations is empty, cluster is in decision", + placement: testinghelpers.NewPlacement("test", "test").Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", - Effect: clusterapiv1.TaintEffectNoSelectIfNew, - TimeAdded: metav1.Time{}, - }).Build(), - testinghelpers.NewManagedCluster("cluster3").WithTaint( - &clusterapiv1.Taint{ - Key: "key3", - Value: "value3", + Key: "key1", Effect: clusterapiv1.TaintEffectNoSelectIfNew, TimeAdded: metav1.Time{}, }).Build(), }, initObjs: []runtime.Object{ - testinghelpers.NewPlacementDecision("test", "test").WithLabel(placementLabel, "test").WithDecisions("cluster2").Build(), + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), }, - expectedClusterNames: []string{"cluster2"}, + expectedClusterNames: []string{"cluster1"}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, { - name: "taint.Effect is NoSelectIfNew and tolerations is Exist", + name: "taint.Effect is NoSelectIfNew and tolerations matches, cluster is not in decision", placement: testinghelpers.NewPlacement("test", "test").AddToleration( &clusterapiv1beta1.Toleration{ Key: "key1", @@ -172,31 +272,82 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ Key: "key1", - Value: "value1", Effect: clusterapiv1.TaintEffectNoSelectIfNew, TimeAdded: metav1.Time{}, }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew and tolerations matches, cluster is in decision", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", - Effect: clusterapiv1.TaintEffectNoSelectIfNew, - TimeAdded: metav1.Time{}, - }).Build(), - testinghelpers.NewManagedCluster("cluster3").WithTaint( - &clusterapiv1.Taint{ - Key: "key3", - Value: "value3", + Key: "key1", Effect: clusterapiv1.TaintEffectNoSelectIfNew, TimeAdded: metav1.Time{}, }).Build(), }, initObjs: []runtime.Object{ - testinghelpers.NewPlacementDecision("test", "test").WithLabel(placementLabel, "test").WithDecisions("cluster2").Build(), + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), }, - expectedClusterNames: []string{"cluster1", "cluster2"}, + expectedClusterNames: []string{"cluster1"}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, + { + name: "taint.Effect is NoSelectIfNew and tolerations doesn't match, cluster is not in decision", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key2", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew and tolerations doesn't match, cluster is in decision", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key2", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{ + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), + }, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + // taint.Effect is PreferNoSelect, testing tolerations with differet key/value { name: "taint.Effect is PreferNoSelect and tolerations is Empty", placement: testinghelpers.NewPlacement("test", "test").Build(), @@ -204,24 +355,16 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ Key: "key1", - Value: "value1", - Effect: clusterapiv1.TaintEffectPreferNoSelect, - TimeAdded: metav1.Time{}, - }).Build(), - testinghelpers.NewManagedCluster("cluster2").WithTaint( - &clusterapiv1.Taint{ - Key: "key1", - Value: "value2", Effect: clusterapiv1.TaintEffectPreferNoSelect, TimeAdded: metav1.Time{}, }).Build(), }, initObjs: []runtime.Object{}, - expectedClusterNames: []string{"cluster1", "cluster2"}, + expectedClusterNames: []string{"cluster1"}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, { - name: "taint.Effect is PreferNoSelect and tolerations is Exist", + name: "taint.Effect is PreferNoSelect and tolerations matches", placement: testinghelpers.NewPlacement("test", "test").AddToleration( &clusterapiv1beta1.Toleration{ Key: "key1", @@ -230,16 +373,70 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { clusters: []*clusterapiv1.ManagedCluster{ testinghelpers.NewManagedCluster("cluster1").WithTaint( &clusterapiv1.Taint{ - Key: "key2", - Value: "value2", + Key: "key1", Effect: clusterapiv1.TaintEffectPreferNoSelect, TimeAdded: metav1.Time{}, }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is PreferNoSelect and tolerations doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key2", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Effect: clusterapiv1.TaintEffectPreferNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + // multiple taints and tolerations + { + name: "cluster has multiple taints and placement has multiple tolerations, all the taints are matched", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key2", + Operator: clusterapiv1beta1.TolerationOpExists, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).WithTaint( + &clusterapiv1.Taint{ + Key: "key2", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), testinghelpers.NewManagedCluster("cluster2").WithTaint( &clusterapiv1.Taint{ - Key: "key3", - Value: "value3", - Effect: clusterapiv1.TaintEffectPreferNoSelect, + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).WithTaint( + &clusterapiv1.Taint{ + Key: "key2", + Effect: clusterapiv1.TaintEffectNoSelect, TimeAdded: metav1.Time{}, }).Build(), }, @@ -247,6 +444,218 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { expectedClusterNames: []string{"cluster1", "cluster2"}, expectedRequeueResult: plugins.PluginRequeueResult{}, }, + // testing effect matches + { + name: "taint.Effect is NoSelect, key/value matches, toleration effect is empty", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelect, key/value matches, toleration effect doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelect, key/value matches, toleration effect matches", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelect, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelect, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, key/value matches, toleration effect is empty", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, key/value matches, toleration effect matches", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value1", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, key/value matches, toleration effect doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value2", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{}, + expectedClusterNames: []string{}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, cluster is in decision, toleration effect is empty", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value2", + Operator: clusterapiv1beta1.TolerationOpEqual, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{ + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), + }, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, cluster is in decision, toleration effect matches", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value2", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{ + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), + }, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + { + name: "taint.Effect is NoSelectIfNew, cluster is in decision, toleration effect doesn't match", + placement: testinghelpers.NewPlacement("test", "test").AddToleration( + &clusterapiv1beta1.Toleration{ + Key: "key1", + Value: "value2", + Operator: clusterapiv1beta1.TolerationOpEqual, + Effect: clusterapiv1.TaintEffectNoSelect, + }).Build(), + clusters: []*clusterapiv1.ManagedCluster{ + testinghelpers.NewManagedCluster("cluster1").WithTaint( + &clusterapiv1.Taint{ + Key: "key1", + Value: "value1", + Effect: clusterapiv1.TaintEffectNoSelectIfNew, + TimeAdded: metav1.Time{}, + }).Build(), + }, + initObjs: []runtime.Object{ + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1"). + Build(), + }, + expectedClusterNames: []string{"cluster1"}, + expectedRequeueResult: plugins.PluginRequeueResult{}, + }, + // testing TolerationSeconds { name: "tanits match tolerations by toleration.TolerationSeconds", placement: testinghelpers.NewPlacement("test", "test").AddToleration( @@ -314,7 +723,10 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { }).Build(), }, initObjs: []runtime.Object{ - testinghelpers.NewPlacementDecision("test", "test").WithLabel(placementLabel, "test").WithDecisions("cluster1", "cluster2").Build(), + testinghelpers.NewPlacementDecision("test", "test"). + WithLabel(placementLabel, "test"). + WithDecisions("cluster1", "cluster2"). + Build(), }, expectedClusterNames: []string{"cluster1", "cluster2"}, expectedRequeueResult: plugins.PluginRequeueResult{ @@ -337,19 +749,26 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { clusters := result.Filtered err := result.Err - if err != nil { + if err != nil && err.Error() != c.expectedErr.Error() { t.Errorf("unexpected err: %v", err) } expectedClusterNames := sets.NewString(c.expectedClusterNames...) if len(clusters) != expectedClusterNames.Len() { - t.Errorf("expected %d clusters but got %d", expectedClusterNames.Len(), len(clusters)) + t.Errorf( + "expected %d clusters but got %d", + expectedClusterNames.Len(), + len(clusters), + ) } for _, cluster := range clusters { expectedClusterNames.Delete(cluster.Name) } if expectedClusterNames.Len() > 0 { - t.Errorf("expected clusters not selected: %s", strings.Join(expectedClusterNames.List(), ",")) + t.Errorf( + "expected clusters not selected: %s", + strings.Join(expectedClusterNames.List(), ","), + ) } requeueResult := p.RequeueAfter(context.TODO(), c.placement) @@ -357,7 +776,11 @@ func TestMatchWithClusterTaintToleration(t *testing.T) { actualRequeueTime := requeueResult.RequeueTime if !((expectedRequeueTime == nil && actualRequeueTime == nil) || (expectedRequeueTime != nil && actualRequeueTime != nil && expectedRequeueTime.Equal(*actualRequeueTime))) { - t.Errorf("expected clusters requeued at: %s, but actual requeue at %s", expectedRequeueTime, actualRequeueTime) + t.Errorf( + "expected clusters requeued at: %s, but actual requeue at %s", + expectedRequeueTime, + actualRequeueTime, + ) } }) }