mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 03:08:15 +00:00
Use field.Error(s) in scheduler plugin args validation
Signed-off-by: Ling Samuel <lingsamuelgrace@gmail.com>
This commit is contained in:
@@ -21,9 +21,12 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/kubernetes/pkg/scheduler/apis/config"
|
||||
"k8s.io/kubernetes/pkg/scheduler/framework"
|
||||
"k8s.io/kubernetes/pkg/scheduler/framework/runtime"
|
||||
@@ -101,7 +104,7 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
|
||||
pods []*v1.Pod
|
||||
nodes []*v1.Node
|
||||
args config.NodeResourcesLeastAllocatedArgs
|
||||
wantErr string
|
||||
wantErr error
|
||||
expectedList framework.NodeScoreList
|
||||
name string
|
||||
}{
|
||||
@@ -260,27 +263,42 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
|
||||
},
|
||||
{
|
||||
// resource with negtive weight is not allowed
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: -1}, {Name: "cpu", Weight: 1}}},
|
||||
wantErr: "resource Weight of memory should be a positive value, got -1",
|
||||
name: "resource with negtive weight",
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: -1}, {Name: "cpu", Weight: 1}}},
|
||||
wantErr: field.ErrorList{
|
||||
&field.Error{
|
||||
Type: field.ErrorTypeInvalid,
|
||||
Field: "resources[0].weight",
|
||||
},
|
||||
}.ToAggregate(),
|
||||
name: "resource with negtive weight",
|
||||
},
|
||||
{
|
||||
// resource with zero weight is not allowed
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}, {Name: "cpu", Weight: 0}}},
|
||||
wantErr: "resource Weight of cpu should be a positive value, got 0",
|
||||
name: "resource with zero weight",
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 1}, {Name: "cpu", Weight: 0}}},
|
||||
wantErr: field.ErrorList{
|
||||
&field.Error{
|
||||
Type: field.ErrorTypeInvalid,
|
||||
Field: "resources[1].weight",
|
||||
},
|
||||
}.ToAggregate(),
|
||||
name: "resource with zero weight",
|
||||
},
|
||||
{
|
||||
// resource weight should be less than MaxNodeScore
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 120}}},
|
||||
wantErr: "resource Weight of memory should be less than 100, got 120",
|
||||
name: "resource weight larger than MaxNodeScore",
|
||||
pod: &v1.Pod{Spec: cpuAndMemory},
|
||||
nodes: []*v1.Node{makeNode("machine", 4000, 10000)},
|
||||
args: config.NodeResourcesLeastAllocatedArgs{Resources: []config.ResourceSpec{{Name: "memory", Weight: 120}}},
|
||||
wantErr: field.ErrorList{
|
||||
&field.Error{
|
||||
Type: field.ErrorTypeInvalid,
|
||||
Field: "resources[0].weight",
|
||||
},
|
||||
}.ToAggregate(),
|
||||
name: "resource weight larger than MaxNodeScore",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -290,16 +308,19 @@ func TestNodeResourcesLeastAllocated(t *testing.T) {
|
||||
fh, _ := runtime.NewFramework(nil, nil, nil, runtime.WithSnapshotSharedLister(snapshot))
|
||||
p, err := NewLeastAllocated(&test.args, fh)
|
||||
|
||||
if len(test.wantErr) != 0 {
|
||||
if err != nil && test.wantErr != err.Error() {
|
||||
t.Fatalf("got err %v, want %v", err.Error(), test.wantErr)
|
||||
} else if err == nil {
|
||||
if test.wantErr != nil {
|
||||
if err != nil {
|
||||
diff := cmp.Diff(test.wantErr, err, cmpopts.IgnoreFields(field.Error{}, "BadValue", "Detail"))
|
||||
if diff != "" {
|
||||
t.Fatalf("got err (-want,+got):\n%s", diff)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("no error produced, wanted %v", test.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil && len(test.wantErr) == 0 {
|
||||
if err != nil && test.wantErr == nil {
|
||||
t.Fatalf("failed to initialize plugin NodeResourcesLeastAllocated, got error: %v", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user