diff --git a/plugin/pkg/scheduler/algorithm/priorities/BUILD b/plugin/pkg/scheduler/algorithm/priorities/BUILD index f223c50ad5..2d68f1df5e 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/BUILD +++ b/plugin/pkg/scheduler/algorithm/priorities/BUILD @@ -48,6 +48,7 @@ go_test( "image_locality_test.go", "interpod_affinity_test.go", "least_requested_test.go", + "metadata_test.go", "most_requested_test.go", "node_affinity_test.go", "node_label_test.go", diff --git a/plugin/pkg/scheduler/algorithm/priorities/metadata_test.go b/plugin/pkg/scheduler/algorithm/priorities/metadata_test.go new file mode 100644 index 0000000000..1457b5f5f8 --- /dev/null +++ b/plugin/pkg/scheduler/algorithm/priorities/metadata_test.go @@ -0,0 +1,142 @@ +/* +Copyright 2017 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 priorities + +import ( + "encoding/json" + "reflect" + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/kubernetes/pkg/api/resource" + "k8s.io/kubernetes/pkg/api/v1" + priorityutil "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities/util" + "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache" +) + +func TestPriorityMetadata(t *testing.T) { + nonZeroReqs := &schedulercache.Resource{} + nonZeroReqs.MilliCPU = priorityutil.DefaultMilliCpuRequest + nonZeroReqs.Memory = priorityutil.DefaultMemoryRequest + + specifiedReqs := &schedulercache.Resource{} + specifiedReqs.MilliCPU = 200 + specifiedReqs.Memory = 2000 + + tolerations := []v1.Toleration{{ + Key: "foo", + Operator: v1.TolerationOpEqual, + Value: "bar", + Effect: v1.TaintEffectPreferNoSchedule, + }} + tolerationData, _ := json.Marshal(tolerations) + podAffinity := &v1.Affinity{ + PodAffinity: &v1.PodAffinity{ + PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{ + { + Weight: 5, + PodAffinityTerm: v1.PodAffinityTerm{ + LabelSelector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "security", + Operator: metav1.LabelSelectorOpIn, + Values: []string{"S1"}, + }, + }, + }, + TopologyKey: "region", + }, + }, + }, + }, + } + podWithTolerationsAndAffinity := &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + v1.TolerationsAnnotationKey: string(tolerationData), + }, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container", + Image: "image", + ImagePullPolicy: "Always", + }, + }, + Affinity: podAffinity, + }, + } + podWithTolerationsAndRequests := &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + v1.TolerationsAnnotationKey: string(tolerationData), + }, + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Name: "container", + Image: "image", + ImagePullPolicy: "Always", + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + "cpu": resource.MustParse("200m"), + "memory": resource.MustParse("2000"), + }, + }, + }, + }, + }, + } + tests := []struct { + pod *v1.Pod + test string + expected interface{} + }{ + { + pod: nil, + expected: nil, + test: "pod is nil , priorityMetadata is nil", + }, + { + pod: podWithTolerationsAndAffinity, + expected: &priorityMetadata{ + nonZeroRequest: nonZeroReqs, + podTolerations: tolerations, + affinity: podAffinity, + }, + test: "Produce a priorityMetadata with default requests", + }, + { + pod: podWithTolerationsAndRequests, + expected: &priorityMetadata{ + nonZeroRequest: specifiedReqs, + podTolerations: tolerations, + affinity: nil, + }, + test: "Produce a priorityMetadata with specified requests", + }, + } + for _, test := range tests { + ptData := PriorityMetadata(test.pod, nil) + if !reflect.DeepEqual(test.expected, ptData) { + t.Errorf("%s: expected %#v, got %#v", test.test, test.expected, ptData) + } + } +}