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 23, 2024
1 parent 819fd1c commit ee068e5
Show file tree
Hide file tree
Showing 21 changed files with 6,056 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.1
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
golang.org/x/sys v0.12.0
golang.org/x/term v0.12.0
Expand Down Expand Up @@ -139,6 +140,7 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/projectcalico/go-json v0.0.0-20161128004156-6219dc7339ba // indirect
github.com/projectcalico/go-yaml-wrapper v0.0.0-20191112210931-090425220c54 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
Expand Down
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)
}
}
89 changes: 89 additions & 0 deletions vendor/github.com/go-errors/errors/cover.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ee068e5

Please sign in to comment.