github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/qos_resources_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 native
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	"github.com/kubewharf/katalyst-api/pkg/consts"
    28  )
    29  
    30  var makeQoSResourcePod = func(name string, container, initContainer, overhead v1.ResourceList) *v1.Pod {
    31  	pod := &v1.Pod{
    32  		ObjectMeta: metav1.ObjectMeta{Name: name},
    33  		Spec: v1.PodSpec{
    34  			Containers: []v1.Container{
    35  				{
    36  					Name: "c1",
    37  					Resources: v1.ResourceRequirements{
    38  						Limits:   container,
    39  						Requests: container,
    40  					},
    41  				},
    42  				{
    43  					Name: "c2",
    44  					Resources: v1.ResourceRequirements{
    45  						Limits:   container,
    46  						Requests: container,
    47  					},
    48  				},
    49  			},
    50  			InitContainers: []v1.Container{
    51  				{
    52  					Name: "c1",
    53  					Resources: v1.ResourceRequirements{
    54  						Limits:   initContainer,
    55  						Requests: initContainer,
    56  					},
    57  				},
    58  			},
    59  			Overhead: overhead,
    60  		},
    61  	}
    62  	return pod
    63  }
    64  
    65  func Test_CalculateQoSResource(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	for _, tc := range []struct {
    69  		name string
    70  		pod  *v1.Pod
    71  
    72  		res     QoSResource
    73  		non0CPU int64
    74  		non0Mem int64
    75  	}{
    76  		{
    77  			name: "test with resource set",
    78  			pod: makeQoSResourcePod("pod-1", map[v1.ResourceName]resource.Quantity{
    79  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(2000, resource.DecimalSI),
    80  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(2*1024*0o124*1024, resource.DecimalSI),
    81  			}, map[v1.ResourceName]resource.Quantity{
    82  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(3000, resource.DecimalSI),
    83  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(3*1024*0o124*1024, resource.DecimalSI),
    84  			}, map[v1.ResourceName]resource.Quantity{
    85  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(1000, resource.DecimalSI),
    86  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(1*1024*0o124*1024, resource.DecimalSI),
    87  			}),
    88  			res:     QoSResource{5000, 5 * 1024 * 0o124 * 1024},
    89  			non0CPU: 5000,
    90  			non0Mem: 5 * 1024 * 0o124 * 1024,
    91  		},
    92  		{
    93  			name: "test with resource init",
    94  			pod: makeQoSResourcePod("pod-1", map[v1.ResourceName]resource.Quantity{
    95  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(2000, resource.DecimalSI),
    96  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(2*1024*0o124*1024, resource.DecimalSI),
    97  			}, map[v1.ResourceName]resource.Quantity{
    98  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(5000, resource.DecimalSI),
    99  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(5*1024*0o124*1024, resource.DecimalSI),
   100  			}, map[v1.ResourceName]resource.Quantity{
   101  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(1000, resource.DecimalSI),
   102  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(1*1024*0o124*1024, resource.DecimalSI),
   103  			}),
   104  			res:     QoSResource{6000, 6 * 1024 * 0o124 * 1024},
   105  			non0CPU: 6000,
   106  			non0Mem: 6 * 1024 * 0o124 * 1024,
   107  		},
   108  		{
   109  			name: "test with resource missing",
   110  			pod: makeQoSResourcePod("pod-1", map[v1.ResourceName]resource.Quantity{
   111  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(2000, resource.DecimalSI),
   112  			}, map[v1.ResourceName]resource.Quantity{
   113  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(3000, resource.DecimalSI),
   114  			}, map[v1.ResourceName]resource.Quantity{
   115  				consts.ReclaimedResourceMilliCPU: *resource.NewQuantity(1000, resource.DecimalSI),
   116  				consts.ReclaimedResourceMemory:   *resource.NewQuantity(1*1024*0o124*1024, resource.DecimalSI),
   117  			}),
   118  			res:     QoSResource{5000, 1 * 1024 * 0o124 * 1024},
   119  			non0CPU: 5000,
   120  			non0Mem: 1*1024*0o124*1024 + DefaultReclaimedMemoryRequest*2,
   121  		},
   122  	} {
   123  		tc := tc
   124  		t.Run(tc.name, func(t *testing.T) {
   125  			t.Parallel()
   126  			t.Logf("case: %v", tc.name)
   127  			res, non0CPU, non0Mem := CalculateQoSResource(tc.pod)
   128  			assert.Equal(t, tc.res, res)
   129  			assert.Equal(t, tc.non0CPU, non0CPU)
   130  			assert.Equal(t, tc.non0Mem, non0Mem)
   131  		})
   132  	}
   133  }