Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UT for workstatus.go and taint.go and rule.go #3395

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 355 additions & 0 deletions pkg/util/helper/taint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package helper

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/util/gclient"
)

Expand Down Expand Up @@ -98,6 +102,16 @@ func TestUpdateClusterControllerTaint(t *testing.T) {
wantTaints: []corev1.Taint{*notReadyTaintTemplate},
wantErr: false,
},
{
name: "clusterTaintsToAdd is nil and clusterTaintsToRemove is nil",
args: args{
taints: []corev1.Taint{*unreachableTaintTemplate},
taintsToAdd: []*corev1.Taint{unreachableTaintTemplate.DeepCopy()},
taintsToRemove: []*corev1.Taint{notReadyTaintTemplate.DeepCopy()},
},
wantTaints: []corev1.Taint{*unreachableTaintTemplate},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -266,6 +280,31 @@ func TestTolerationExists(t *testing.T) {
}
}

func TestAddTolerations(t *testing.T) {
placement := &policyv1alpha1.Placement{
ClusterTolerations: []corev1.Toleration{},
}

toleration1 := &corev1.Toleration{
Key: "key1",
Operator: corev1.TolerationOpEqual,
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
}
toleration2 := &corev1.Toleration{
Key: "key2",
Operator: corev1.TolerationOpEqual,
Value: "value2",
Effect: corev1.TaintEffectNoSchedule,
}

AddTolerations(placement, toleration1, toleration2)

assert.Equal(t, 2, len(placement.ClusterTolerations))
assert.Equal(t, *toleration1, placement.ClusterTolerations[0])
assert.Equal(t, *toleration2, placement.ClusterTolerations[1])
}

func TestHasNoExecuteTaints(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -357,3 +396,319 @@ func TestGetNoExecuteTaints(t *testing.T) {
})
}
}

func TestGetMinTolerationTime(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some paths may not be tested. Can you add more test items?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

tests := []struct {
name string
noExecuteTaints []corev1.Taint
usedTolerantion []corev1.Toleration
wantResult time.Duration
}{
{
name: "no noExecuteTaints",
noExecuteTaints: []corev1.Taint{},
usedTolerantion: []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{60}[0],
},
},
wantResult: -1,
},
{
name: "no usedTolerations",
noExecuteTaints: []corev1.Taint{
{
Key: "key",
Value: "value",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{Time: time.Now()},
},
},
usedTolerantion: []corev1.Toleration{},
wantResult: 0,
},
{
name: "with noExecuteTaints and usedTolerations",
noExecuteTaints: []corev1.Taint{
{
Key: "key",
Value: "value",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{Time: time.Now()},
},
},
usedTolerantion: []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{60}[0],
},
},
wantResult: 60,
},
{
name: "usedTolerantion.TolerationSeconds is nil",
noExecuteTaints: []corev1.Taint{
{
Key: "key",
Value: "value",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{Time: time.Now()},
},
},
usedTolerantion: []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: nil,
},
},
wantResult: -1,
},
{
name: "noExecuteTaints.TimeAdded is nil",
noExecuteTaints: []corev1.Taint{
{
Key: "key",
Value: "value",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: nil,
},
},
usedTolerantion: []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{60}[0],
},
},
wantResult: -1,
},
{
name: "find the latest trigger time",
noExecuteTaints: []corev1.Taint{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{Time: time.Now()},
},
{
Key: "key2",
Value: "value2",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{Time: time.Now()},
},
},
usedTolerantion: []corev1.Toleration{
{
Key: "key1",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{120}[0],
},
{
Key: "key2",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{60}[0],
},
},
wantResult: 60,
},
{
name: "trigger time is up",
noExecuteTaints: []corev1.Taint{
{
Key: "key",
Value: "value",
Effect: corev1.TaintEffectNoExecute,
TimeAdded: &metav1.Time{
Time: time.Now().Add(-time.Hour),
},
},
},
usedTolerantion: []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &[]int64{60}[0],
},
},
wantResult: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetMinTolerationTime(tt.noExecuteTaints, tt.usedTolerantion)
fmt.Printf("%+v", result)
if result > 0 {
if result > (tt.wantResult+1)*time.Second || result < (tt.wantResult-1)*time.Second {
t.Errorf("GetMinTolerationTime() = %v, want %v", result, tt.wantResult)
}
} else if result != tt.wantResult {
t.Errorf("GetMinTolerationTime() = %v, want %v", result, tt.wantResult)
}
})
}
}

func TestGetMatchingTolerations(t *testing.T) {
tests := []struct {
name string
taints []corev1.Taint
tolerations []corev1.Toleration
wantActual bool
wantActualTolerations []corev1.Toleration
}{
{
name: "taints is nil",
taints: []corev1.Taint{},
tolerations: []corev1.Toleration{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
},
wantActual: true,
wantActualTolerations: []corev1.Toleration{},
},
{
name: "tolerations is nil",
taints: []corev1.Taint{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
},
tolerations: []corev1.Toleration{},
wantActual: false,
wantActualTolerations: []corev1.Toleration{},
},
{
name: "tolerated is true",
taints: []corev1.Taint{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "key2",
Value: "value2",
Effect: corev1.TaintEffectNoSchedule,
},
},
tolerations: []corev1.Toleration{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "key2",
Value: "value2",
Effect: corev1.TaintEffectNoSchedule,
},
},
wantActual: true,
wantActualTolerations: []corev1.Toleration{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "key2",
Value: "value2",
Effect: corev1.TaintEffectNoSchedule,
},
},
},
{
name: "tolerated is false",
taints: []corev1.Taint{
{
Key: "key1",
Value: "value1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "key2",
Value: "value2",
Effect: corev1.TaintEffectNoSchedule,
},
},
tolerations: []corev1.Toleration{
{
Key: "key1",
Value: "value_1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "key2",
Value: "value_2",
Effect: corev1.TaintEffectNoSchedule,
},
},
wantActual: false,
wantActualTolerations: []corev1.Toleration{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, actualTolerations := GetMatchingTolerations(tt.taints, tt.tolerations)
if actual != tt.wantActual || !reflect.DeepEqual(actualTolerations, tt.wantActualTolerations) {
t.Errorf("GetMatchingTolerations(%v, %v) = (%v, %v), expected (%v, %v)", tt.taints, tt.tolerations, actual, actualTolerations, tt.wantActual, tt.wantActualTolerations)
}
})
}
}

func TestNewNotReadyToleration(t *testing.T) {
expectedKey := clusterv1alpha1.TaintClusterNotReady
expectedOperator := corev1.TolerationOpExists
expectedEffect := corev1.TaintEffectNoExecute
expectedSeconds := int64(123)

toleration := NewNotReadyToleration(expectedSeconds)

if toleration.Key != expectedKey {
t.Errorf("Expected key %q but got %q", expectedKey, toleration.Key)
}
if toleration.Operator != expectedOperator {
t.Errorf("Expected operator %q but got %q", expectedOperator, toleration.Operator)
}
if toleration.Effect != expectedEffect {
t.Errorf("Expected effect %q but got %q", expectedEffect, toleration.Effect)
}
if *toleration.TolerationSeconds != expectedSeconds {
t.Errorf("Expected seconds %d but got %d", expectedSeconds, *toleration.TolerationSeconds)
}
}

func TestNewUnreachableToleration(t *testing.T) {
tolerationSeconds := int64(300)
expectedToleration := &corev1.Toleration{
Key: clusterv1alpha1.TaintClusterUnreachable,
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
TolerationSeconds: &tolerationSeconds,
}

actualToleration := NewUnreachableToleration(tolerationSeconds)

if !reflect.DeepEqual(actualToleration, expectedToleration) {
t.Errorf("NewUnreachableToleration() = %v, want %v", actualToleration, expectedToleration)
}
}
Loading