k8s.io/kubernetes@v1.29.3/pkg/controller/podautoscaler/metrics/utilization_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes 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 metrics 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 type resourceUtilizationRatioTestCase struct { 27 metrics PodMetricsInfo 28 requests map[string]int64 29 targetUtilization int32 30 31 expectedUtilizationRatio float64 32 expectedCurrentUtilization int32 33 expectedRawAverageValue int64 34 expectedErr error 35 } 36 37 func (tc *resourceUtilizationRatioTestCase) runTest(t *testing.T) { 38 actualUtilizationRatio, actualCurrentUtilization, actualRawAverageValue, actualErr := GetResourceUtilizationRatio(tc.metrics, tc.requests, tc.targetUtilization) 39 40 if tc.expectedErr != nil { 41 assert.Error(t, actualErr, "there should be an error getting the utilization ratio") 42 assert.Contains(t, fmt.Sprintf("%v", actualErr), fmt.Sprintf("%v", tc.expectedErr), "the error message should be as expected") 43 return 44 } 45 46 assert.NoError(t, actualErr, "there should be no error retrieving the utilization ratio") 47 assert.Equal(t, tc.expectedUtilizationRatio, actualUtilizationRatio, "the utilization ratios should be as expected") 48 assert.Equal(t, tc.expectedCurrentUtilization, actualCurrentUtilization, "the current utilization should be as expected") 49 assert.Equal(t, tc.expectedRawAverageValue, actualRawAverageValue, "the raw average value should be as expected") 50 } 51 52 type metricUsageRatioTestCase struct { 53 metrics PodMetricsInfo 54 targetUsage int64 55 56 expectedUsageRatio float64 57 expectedCurrentUsage int64 58 } 59 60 func (tc *metricUsageRatioTestCase) runTest(t *testing.T) { 61 actualUsageRatio, actualCurrentUsage := GetMetricUsageRatio(tc.metrics, tc.targetUsage) 62 63 assert.Equal(t, tc.expectedUsageRatio, actualUsageRatio, "the usage ratios should be as expected") 64 assert.Equal(t, tc.expectedCurrentUsage, actualCurrentUsage, "the current usage should be as expected") 65 } 66 67 func TestGetResourceUtilizationRatioBaseCase(t *testing.T) { 68 tc := resourceUtilizationRatioTestCase{ 69 metrics: PodMetricsInfo{ 70 "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76}, 71 }, 72 requests: map[string]int64{ 73 "test-pod-0": 100, "test-pod-1": 100, 74 }, 75 targetUtilization: 50, 76 expectedUtilizationRatio: 1.26, 77 expectedCurrentUtilization: 63, 78 expectedRawAverageValue: 63, 79 expectedErr: nil, 80 } 81 82 tc.runTest(t) 83 } 84 85 func TestGetResourceUtilizationRatioIgnorePodsWithNoRequest(t *testing.T) { 86 tc := resourceUtilizationRatioTestCase{ 87 metrics: PodMetricsInfo{ 88 "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76}, "test-pod-no-request": {Value: 100}, 89 }, 90 requests: map[string]int64{ 91 "test-pod-0": 100, "test-pod-1": 100, 92 }, 93 targetUtilization: 50, 94 expectedUtilizationRatio: 1.26, 95 expectedCurrentUtilization: 63, 96 expectedRawAverageValue: 63, 97 expectedErr: nil, 98 } 99 100 tc.runTest(t) 101 } 102 103 func TestGetResourceUtilizationRatioExtraRequest(t *testing.T) { 104 tc := resourceUtilizationRatioTestCase{ 105 metrics: PodMetricsInfo{ 106 "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76}, 107 }, 108 requests: map[string]int64{ 109 "test-pod-0": 100, "test-pod-1": 100, "test-pod-extra-request": 500, 110 }, 111 targetUtilization: 50, 112 expectedUtilizationRatio: 1.26, 113 expectedCurrentUtilization: 63, 114 expectedRawAverageValue: 63, 115 expectedErr: nil, 116 } 117 118 tc.runTest(t) 119 } 120 121 func TestGetResourceUtilizationRatioNoRequests(t *testing.T) { 122 tc := resourceUtilizationRatioTestCase{ 123 metrics: PodMetricsInfo{ 124 "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76}, 125 }, 126 requests: map[string]int64{}, 127 targetUtilization: 50, 128 129 expectedUtilizationRatio: 0, 130 expectedCurrentUtilization: 0, 131 expectedRawAverageValue: 0, 132 expectedErr: fmt.Errorf("no metrics returned matched known pods"), 133 } 134 135 tc.runTest(t) 136 } 137 138 func TestGetMetricUsageRatioBaseCase(t *testing.T) { 139 tc := metricUsageRatioTestCase{ 140 metrics: PodMetricsInfo{ 141 "test-pod-0": {Value: 5000}, "test-pod-1": {Value: 10000}, 142 }, 143 targetUsage: 10000, 144 expectedUsageRatio: .75, 145 expectedCurrentUsage: 7500, 146 } 147 148 tc.runTest(t) 149 }