github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/nodeovercommitment/reserve_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package nodeovercommitment
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/kubernetes/pkg/scheduler/framework"
    28  
    29  	"github.com/kubewharf/katalyst-core/pkg/scheduler/plugins/nodeovercommitment/cache"
    30  )
    31  
    32  func TestReserve(t *testing.T) {
    33  	testCases := []struct {
    34  		name      string
    35  		pods      []*v1.Pod
    36  		nodeName  string
    37  		expectRes int
    38  	}{
    39  		{
    40  			name:     "case 1",
    41  			nodeName: "testNode",
    42  			pods: []*v1.Pod{
    43  				{
    44  					ObjectMeta: v12.ObjectMeta{
    45  						Name: "pod1",
    46  						UID:  "poduid1",
    47  					},
    48  					Spec: v1.PodSpec{
    49  						NodeName: "testNode",
    50  						Containers: []v1.Container{
    51  							{
    52  								Name: "testContainer",
    53  							},
    54  						},
    55  					},
    56  				},
    57  				{
    58  					ObjectMeta: v12.ObjectMeta{
    59  						Name: "pod2",
    60  						UID:  "poduid2",
    61  					},
    62  					Spec: v1.PodSpec{
    63  						NodeName: "testNode",
    64  						Containers: []v1.Container{
    65  							{
    66  								Name: "testContainer",
    67  								Resources: v1.ResourceRequirements{
    68  									Requests: map[v1.ResourceName]resource.Quantity{
    69  										v1.ResourceCPU:    resource.MustParse("4"),
    70  										v1.ResourceMemory: resource.MustParse("8Gi"),
    71  									},
    72  								},
    73  							},
    74  							{
    75  								Name: "testContainer2",
    76  								Resources: v1.ResourceRequirements{
    77  									Requests: map[v1.ResourceName]resource.Quantity{
    78  										v1.ResourceCPU:    resource.MustParse("2"),
    79  										v1.ResourceMemory: resource.MustParse("4Gi"),
    80  									},
    81  									Limits: map[v1.ResourceName]resource.Quantity{
    82  										v1.ResourceCPU:    resource.MustParse("2"),
    83  										v1.ResourceMemory: resource.MustParse("4Gi"),
    84  									},
    85  								},
    86  							},
    87  						},
    88  					},
    89  				},
    90  				{
    91  					ObjectMeta: v12.ObjectMeta{
    92  						Name: "pod3",
    93  						UID:  "poduid3",
    94  					},
    95  					Spec: v1.PodSpec{
    96  						NodeName: "testNode",
    97  						Containers: []v1.Container{
    98  							{
    99  								Name: "testContainer",
   100  								Resources: v1.ResourceRequirements{
   101  									Requests: map[v1.ResourceName]resource.Quantity{
   102  										v1.ResourceCPU:    resource.MustParse("4"),
   103  										v1.ResourceMemory: resource.MustParse("8Gi"),
   104  									},
   105  									Limits: map[v1.ResourceName]resource.Quantity{
   106  										v1.ResourceCPU:    resource.MustParse("4"),
   107  										v1.ResourceMemory: resource.MustParse("8Gi"),
   108  									},
   109  								},
   110  							},
   111  						},
   112  					},
   113  				},
   114  				{
   115  					ObjectMeta: v12.ObjectMeta{
   116  						Name: "pod4",
   117  						UID:  "poduid4",
   118  					},
   119  					Spec: v1.PodSpec{
   120  						NodeName: "testNode",
   121  						InitContainers: []v1.Container{
   122  							{
   123  								Name: "initContainer",
   124  								Resources: v1.ResourceRequirements{
   125  									Requests: map[v1.ResourceName]resource.Quantity{
   126  										v1.ResourceCPU:    resource.MustParse("1"),
   127  										v1.ResourceMemory: resource.MustParse("2Gi"),
   128  									},
   129  									Limits: map[v1.ResourceName]resource.Quantity{
   130  										v1.ResourceCPU:    resource.MustParse("1"),
   131  										v1.ResourceMemory: resource.MustParse("2Gi"),
   132  									},
   133  								},
   134  							},
   135  						},
   136  						Containers: []v1.Container{
   137  							{
   138  								Name: "testContainer",
   139  								Resources: v1.ResourceRequirements{
   140  									Requests: map[v1.ResourceName]resource.Quantity{
   141  										v1.ResourceCPU:    resource.MustParse("4"),
   142  										v1.ResourceMemory: resource.MustParse("8Gi"),
   143  									},
   144  									Limits: map[v1.ResourceName]resource.Quantity{
   145  										v1.ResourceCPU:    resource.MustParse("4"),
   146  										v1.ResourceMemory: resource.MustParse("8Gi"),
   147  									},
   148  								},
   149  							},
   150  						},
   151  					},
   152  				},
   153  			},
   154  			expectRes: 8,
   155  		},
   156  	}
   157  
   158  	for _, tc := range testCases {
   159  		t.Run(tc.name, func(t *testing.T) {
   160  			n := &NodeOvercommitment{}
   161  			cs := framework.NewCycleState()
   162  
   163  			for _, pod := range tc.pods {
   164  				n.Reserve(context.TODO(), cs, pod, tc.nodeName)
   165  			}
   166  
   167  			nodeCache, err := cache.GetCache().GetNode(tc.nodeName)
   168  			assert.NoError(t, err)
   169  			assert.Equal(t, tc.expectRes, nodeCache.GetGuaranteedCPUs())
   170  
   171  			for _, pod := range tc.pods {
   172  				err = cache.GetCache().AddPod(pod)
   173  				assert.NoError(t, err)
   174  			}
   175  
   176  			assert.Equal(t, tc.expectRes, nodeCache.GetGuaranteedCPUs())
   177  			for _, pod := range tc.pods {
   178  				err = cache.GetCache().RemovePod(pod)
   179  				assert.NoError(t, err)
   180  			}
   181  
   182  			for _, pod := range tc.pods {
   183  				n.Reserve(context.TODO(), cs, pod, tc.nodeName)
   184  				n.Unreserve(context.TODO(), cs, pod, tc.nodeName)
   185  			}
   186  			assert.Equal(t, 0, nodeCache.GetGuaranteedCPUs())
   187  		})
   188  	}
   189  }