mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			153 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
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 (
 | 
						|
	"reflect"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"k8s.io/api/core/v1"
 | 
						|
	"k8s.io/apimachinery/pkg/api/resource"
 | 
						|
	//metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						|
	schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
 | 
						|
	schedulercache "k8s.io/kubernetes/pkg/scheduler/cache"
 | 
						|
)
 | 
						|
 | 
						|
func TestResourceLimistPriority(t *testing.T) {
 | 
						|
	noResources := v1.PodSpec{
 | 
						|
		Containers: []v1.Container{},
 | 
						|
	}
 | 
						|
 | 
						|
	cpuOnly := v1.PodSpec{
 | 
						|
		NodeName: "machine1",
 | 
						|
		Containers: []v1.Container{
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("1000m"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("0"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("2000m"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("0"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	memOnly := v1.PodSpec{
 | 
						|
		NodeName: "machine2",
 | 
						|
		Containers: []v1.Container{
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("0"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("2000"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("0"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("3000"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	cpuAndMemory := v1.PodSpec{
 | 
						|
		NodeName: "machine2",
 | 
						|
		Containers: []v1.Container{
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("1000m"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("2000"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			{
 | 
						|
				Resources: v1.ResourceRequirements{
 | 
						|
					Limits: v1.ResourceList{
 | 
						|
						v1.ResourceCPU:    resource.MustParse("2000m"),
 | 
						|
						v1.ResourceMemory: resource.MustParse("3000"),
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	tests := []struct {
 | 
						|
		// input pod
 | 
						|
		pod          *v1.Pod
 | 
						|
		nodes        []*v1.Node
 | 
						|
		expectedList schedulerapi.HostPriorityList
 | 
						|
		name         string
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			pod:          &v1.Pod{Spec: noResources},
 | 
						|
			nodes:        []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 0), makeNode("machine3", 0, 10000), makeNode("machine4", 0, 0)},
 | 
						|
			expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}, {Host: "machine4", Score: 0}},
 | 
						|
			name:         "pod does not specify its resource limits",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			pod:          &v1.Pod{Spec: cpuOnly},
 | 
						|
			nodes:        []*v1.Node{makeNode("machine1", 3000, 10000), makeNode("machine2", 2000, 10000)},
 | 
						|
			expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 0}},
 | 
						|
			name:         "pod only specifies  cpu limits",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			pod:          &v1.Pod{Spec: memOnly},
 | 
						|
			nodes:        []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
 | 
						|
			expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 1}},
 | 
						|
			name:         "pod only specifies  mem limits",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			pod:          &v1.Pod{Spec: cpuAndMemory},
 | 
						|
			nodes:        []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
 | 
						|
			expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 1}},
 | 
						|
			name:         "pod specifies both cpu and  mem limits",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			pod:          &v1.Pod{Spec: cpuAndMemory},
 | 
						|
			nodes:        []*v1.Node{makeNode("machine1", 0, 0)},
 | 
						|
			expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}},
 | 
						|
			name:         "node does not advertise its allocatables",
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, test := range tests {
 | 
						|
		t.Run(test.name, func(t *testing.T) {
 | 
						|
			nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
 | 
						|
			list, err := priorityFunction(ResourceLimitsPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
 | 
						|
			if err != nil {
 | 
						|
				t.Errorf("unexpected error: %v", err)
 | 
						|
			}
 | 
						|
			if !reflect.DeepEqual(test.expectedList, list) {
 | 
						|
				t.Errorf("expected %#v, got %#v", test.expectedList, list)
 | 
						|
			}
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |