1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 13:29:11 +01:00

Improves the validation logic in the plugins

Signed-off-by: googs1025 <googs1025@gmail.com>
This commit is contained in:
googs1025
2025-08-06 23:03:19 +08:00
parent f2211e1cef
commit 2cce141feb
17 changed files with 221 additions and 138 deletions

View File

@@ -16,44 +16,72 @@ limitations under the License.
package removepodsviolatingnodeaffinity
import "testing"
import (
"fmt"
"testing"
"sigs.k8s.io/descheduler/pkg/api"
)
func TestValidateRemovePodsViolatingNodeAffinityArgs(t *testing.T) {
testCases := []struct {
description string
args *RemovePodsViolatingNodeAffinityArgs
expectError bool
errInfo error
}{
{
description: "nil NodeAffinityType args, expects errors",
args: &RemovePodsViolatingNodeAffinityArgs{
NodeAffinityType: nil,
},
expectError: true,
errInfo: fmt.Errorf(`nodeAffinityType needs to be set`),
},
{
description: "empty NodeAffinityType args, expects errors",
args: &RemovePodsViolatingNodeAffinityArgs{
NodeAffinityType: []string{},
},
expectError: true,
errInfo: fmt.Errorf(`nodeAffinityType needs to be set`),
},
{
description: "valid NodeAffinityType args, no errors",
args: &RemovePodsViolatingNodeAffinityArgs{
NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"},
},
expectError: false,
},
{
description: "invalid namespaces args, expects error",
args: &RemovePodsViolatingNodeAffinityArgs{
NodeAffinityType: []string{"requiredDuringSchedulingIgnoredDuringExecution"},
Namespaces: &api.Namespaces{
Include: []string{"default"},
Exclude: []string{"kube-system"},
},
},
errInfo: fmt.Errorf(`only one of Include/Exclude namespaces can be set`),
},
{
description: "nil NodeAffinityType args and invalid namespaces args, expects error",
args: &RemovePodsViolatingNodeAffinityArgs{
NodeAffinityType: []string{},
Namespaces: &api.Namespaces{
Include: []string{"default"},
Exclude: []string{"kube-system"},
},
},
errInfo: fmt.Errorf(`[nodeAffinityType needs to be set, only one of Include/Exclude namespaces can be set]`),
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
err := ValidateRemovePodsViolatingNodeAffinityArgs(tc.args)
hasError := err != nil
if tc.expectError != hasError {
t.Error("unexpected arg validation behavior")
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
validateErr := ValidateRemovePodsViolatingNodeAffinityArgs(testCase.args)
if validateErr == nil || testCase.errInfo == nil {
if validateErr != testCase.errInfo {
t.Errorf("expected validity of plugin config: %q but got %q instead", testCase.errInfo, validateErr)
}
} else if validateErr.Error() != testCase.errInfo.Error() {
t.Errorf("expected validity of plugin config: %q but got %q instead", testCase.errInfo, validateErr)
}
})
}