github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/memory/dynamicpolicy/util_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 dynamicpolicy
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  )
    25  
    26  func TestGetFullyDropCacheBytes(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	type args struct {
    30  		container *v1.Container
    31  	}
    32  	tests := []struct {
    33  		name string
    34  		args args
    35  		want int64
    36  	}{
    37  		{
    38  			name: "contaienr with both request and limit",
    39  			args: args{
    40  				container: &v1.Container{
    41  					Name: "c1",
    42  					Resources: v1.ResourceRequirements{
    43  						Limits: map[v1.ResourceName]resource.Quantity{
    44  							v1.ResourceMemory: resource.MustParse("3Gi"),
    45  						},
    46  						Requests: map[v1.ResourceName]resource.Quantity{
    47  							v1.ResourceMemory: resource.MustParse("2Gi"),
    48  						},
    49  					},
    50  				},
    51  			},
    52  			want: 3221225472,
    53  		},
    54  		{
    55  			name: "contaienr only with request",
    56  			args: args{
    57  				container: &v1.Container{
    58  					Name: "c1",
    59  					Resources: v1.ResourceRequirements{
    60  						Requests: map[v1.ResourceName]resource.Quantity{
    61  							v1.ResourceMemory: resource.MustParse("2Gi"),
    62  						},
    63  					},
    64  				},
    65  			},
    66  			want: 2147483648,
    67  		},
    68  		{
    69  			name: "nil container",
    70  			args: args{},
    71  			want: 0,
    72  		},
    73  	}
    74  	for _, tt := range tests {
    75  		tt := tt
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			t.Parallel()
    78  			if got := GetFullyDropCacheBytes(tt.args.container); got != tt.want {
    79  				t.Errorf("GetFullyDropCacheBytes() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }