Skip to content

Commit

Permalink
Merge pull request #108276 from AllenZMC/improve_util_test_coverage
Browse files Browse the repository at this point in the history
improve test coverage
  • Loading branch information
k8s-ci-robot authored Mar 2, 2022
2 parents 604ab4f + f31bf3f commit 2de37aa
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions pkg/util/taints/taints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,192 @@ func TestValidateTaint(t *testing.T) {
}
}
}

func TestTaintSetDiff(t *testing.T) {
cases := []struct {
name string
t1 []v1.Taint
t2 []v1.Taint
expectedTaintsToAdd []*v1.Taint
expectedTaintsToRemove []*v1.Taint
}{
{
name: "two_taints_are_nil",
expectedTaintsToAdd: nil,
expectedTaintsToRemove: nil,
},
{
name: "one_taint_is_nil_and_the_other_is_not_nil",
t1: []v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
expectedTaintsToAdd: []*v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
expectedTaintsToRemove: nil,
},
{
name: "shared_taints_with_the_same_key_value_effect",
t1: []v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
t2: []v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
expectedTaintsToAdd: []*v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
},
expectedTaintsToRemove: []*v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
},
},
{
name: "shared_taints_with_the_same_key_effect_different_value",
t1: []v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "different-value",
Effect: v1.TaintEffectNoSchedule,
},
},
t2: []v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
expectedTaintsToAdd: []*v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
},
expectedTaintsToRemove: []*v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
},
},
{
name: "shared_taints_with_the_same_key_different_value_effect",
t1: []v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "different-value",
Effect: v1.TaintEffectNoExecute,
},
},
t2: []v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
expectedTaintsToAdd: []*v1.Taint{
{
Key: "foo_1",
Value: "bar_1",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "different-value",
Effect: v1.TaintEffectNoExecute,
},
},
expectedTaintsToRemove: []*v1.Taint{
{
Key: "foo_3",
Value: "bar_3",
Effect: v1.TaintEffectNoExecute,
},
{
Key: "foo_2",
Value: "bar_2",
Effect: v1.TaintEffectNoSchedule,
},
},
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
add, remove := TaintSetDiff(tt.t1, tt.t2)
if !reflect.DeepEqual(add, tt.expectedTaintsToAdd) {
t.Errorf("taintsToAdd: %v should equal %v, but get unexpected results", add, tt.expectedTaintsToAdd)
}
if !reflect.DeepEqual(remove, tt.expectedTaintsToRemove) {
t.Errorf("taintsToRemove: %v should equal %v, but get unexpected results", remove, tt.expectedTaintsToRemove)
}
})
}
}

0 comments on commit 2de37aa

Please sign in to comment.