Skip to content

Commit

Permalink
test: add testcase for schedule plugin
Browse files Browse the repository at this point in the history
Signed-off-by: qiuming520 <[email protected]>
  • Loading branch information
qiuming520 committed Sep 22, 2024
1 parent 996b6de commit 9b9d051
Show file tree
Hide file tree
Showing 5 changed files with 1,281 additions and 0 deletions.
139 changes: 139 additions & 0 deletions pkg/scheduler/lifted/helpers/tainttoleration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package helpers

import (
"testing"

v1 "k8s.io/api/core/v1"
)

func TestHasLeafNodeTaint(t *testing.T) {
nodeWithTaint := &v1.Node{
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "kosmos.io/node",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
},
},
}

nodeWithoutTaint := &v1.Node{
Spec: v1.NodeSpec{
Taints: []v1.Taint{},
},
}

if !HasLeafNodeTaint(nodeWithTaint) {
t.Errorf("Expected node to have LeafNodeTaint")
}

if HasLeafNodeTaint(nodeWithoutTaint) {
t.Errorf("Expected node to not have LeafNodeTaint")
}
}

func TestTolerationsTolerateTaint(t *testing.T) {
taint := &v1.Taint{
Key: "kosmos.io/node",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
}

toleration := v1.Toleration{
Key: "kosmos.io/node",
Operator: v1.TolerationOpEqual,
Value: "true",
Effect: v1.TaintEffectNoSchedule,
}

tolerations := []v1.Toleration{toleration}

if !TolerationsTolerateTaint(tolerations, taint) {
t.Errorf("Expected tolerations to tolerate the taint")
}

untoleratedTaint := &v1.Taint{
Key: "kosmos.io/node-test",
Value: "false",
Effect: v1.TaintEffectNoSchedule,
}

if TolerationsTolerateTaint(tolerations, untoleratedTaint) {
t.Errorf("Expected tolerations to not tolerate the untolerated taint")
}
}

func TestFindMatchingUntoleratedTaint(t *testing.T) {
taints := []v1.Taint{
{
Key: "kosmos.io/node",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
{
Key: "example.io/another-taint",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
}

tolerations := []v1.Toleration{
{
Key: "example.io/another-taint",
Operator: v1.TolerationOpEqual,
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
}

taint, found := FindMatchingUntoleratedTaint(taints, tolerations, nil)
if found {
t.Errorf("Expected to find an untolerated taint")
} else if taint.Key != "" && taint.Key != "kosmos.io/node" {
t.Errorf("Expected untolerated taint to be 'kosmos.io/node', got '%s'", taint.Key)
}
}

func TestGetFilteredTaints(t *testing.T) {
taints := []v1.Taint{
{
Key: "kosmos.io/node",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
{
Key: "example.io/another-taint",
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
}

filterFunc := func(t *v1.Taint) bool {
return t.Key == "kosmos.io/node"
}

filtered := getFilteredTaints(taints, filterFunc)
if len(filtered) != 1 {
t.Errorf("Expected 1 filtered taint, got %d", len(filtered))
} else if filtered[0].Key != "kosmos.io/node" {
t.Errorf("Expected filtered taint to be 'kosmos.io/node', got '%s'", filtered[0].Key)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package leafnodedistribution

import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
)

func TestMatchDistributionPolicy(t *testing.T) {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "default",
Labels: map[string]string{"app": "test"},
},
}

dp := &kosmosv1alpha1.DistributionPolicy{
DistributionSpec: kosmosv1alpha1.DistributionSpec{
ResourceSelectors: []kosmosv1alpha1.ResourceSelector{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test"},
},
PolicyName: "test-policy",
},
},
},
}

policyName, priority, _ := matchDistributionPolicy(pod, dp)
if policyName == "test-policy" {
t.Errorf("Expected policy name to be 'test-policy', got '%s'", policyName)
}
if priority == LabelSelectorInNsScope {
t.Errorf("Expected priority to be LabelSelectorInNsScope, got %d", priority)
}
}
Loading

0 comments on commit 9b9d051

Please sign in to comment.