github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/spd/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 spd
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/metrics/pkg/apis/metrics/v1beta1"
    27  
    28  	workloadapi "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1"
    29  	"github.com/kubewharf/katalyst-api/pkg/consts"
    30  )
    31  
    32  func TestGetContainerMemoryBandwidthRequest(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	type args struct {
    36  		profilingManager ServiceProfilingManager
    37  		podMeta          metav1.ObjectMeta
    38  		cpuRequest       int
    39  	}
    40  	tests := []struct {
    41  		name    string
    42  		args    args
    43  		want    int
    44  		wantErr bool
    45  	}{
    46  		{
    47  			name: "test two container",
    48  			args: args{
    49  				profilingManager: NewServiceProfilingManager(DummySPDFetcher{
    50  					SPD: &workloadapi.ServiceProfileDescriptor{
    51  						Status: workloadapi.ServiceProfileDescriptorStatus{
    52  							AggMetrics: []workloadapi.AggPodMetrics{
    53  								{
    54  									Aggregator: workloadapi.Avg,
    55  									Items: []v1beta1.PodMetrics{
    56  										{
    57  											Timestamp: metav1.NewTime(time.Date(1970, 0, 0, 0, 0, 0, 0, time.UTC)),
    58  											Window:    metav1.Duration{Duration: time.Hour},
    59  											Containers: []v1beta1.ContainerMetrics{
    60  												{
    61  													Name: "container-1",
    62  													Usage: map[v1.ResourceName]resource.Quantity{
    63  														consts.SPDAggMetricNameMemoryBandwidth: resource.MustParse("10"),
    64  													},
    65  												},
    66  												{
    67  													Name: "container-2",
    68  													Usage: map[v1.ResourceName]resource.Quantity{
    69  														consts.SPDAggMetricNameMemoryBandwidth: resource.MustParse("10"),
    70  													},
    71  												},
    72  											},
    73  										},
    74  										{
    75  											Timestamp: metav1.NewTime(time.Date(1970, 0, 0, 1, 0, 0, 0, time.UTC)),
    76  											Window:    metav1.Duration{Duration: time.Hour},
    77  											Containers: []v1beta1.ContainerMetrics{
    78  												{
    79  													Name: "container-1",
    80  													Usage: map[v1.ResourceName]resource.Quantity{
    81  														consts.SPDAggMetricNameMemoryBandwidth: resource.MustParse("20"),
    82  													},
    83  												},
    84  												{
    85  													Name: "container-2",
    86  													Usage: map[v1.ResourceName]resource.Quantity{
    87  														consts.SPDAggMetricNameMemoryBandwidth: resource.MustParse("20"),
    88  													},
    89  												},
    90  											},
    91  										},
    92  									},
    93  								},
    94  							},
    95  						},
    96  					},
    97  				}),
    98  				podMeta:    metav1.ObjectMeta{},
    99  				cpuRequest: 10,
   100  			},
   101  			want:    400,
   102  			wantErr: false,
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		tt := tt
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			t.Parallel()
   109  			got, err := GetContainerMemoryBandwidthRequest(tt.args.profilingManager, tt.args.podMeta, tt.args.cpuRequest)
   110  			if (err != nil) != tt.wantErr {
   111  				t.Errorf("GetContainerMemoryBandwidthRequest() error = %v, wantErr %v", err, tt.wantErr)
   112  				return
   113  			}
   114  			if got != tt.want {
   115  				t.Errorf("GetContainerMemoryBandwidthRequest() got = %v, want %v", got, tt.want)
   116  			}
   117  		})
   118  	}
   119  }