requeted changes: fix variables in test cases

This commit is contained in:
Kushagra 2022-09-22 09:17:15 +00:00
parent 01b553145c
commit dc321b4d2a

View File

@ -78,9 +78,9 @@ type metricInfo struct {
selector *metav1.LabelSelector selector *metav1.LabelSelector
metricType metricType metricType metricType
targetUtilization int64 targetUsage int64
perPodTargetUtilization int64 perPodTargetUsage int64
expectedUtilization int64 expectedUsage int64
} }
type replicaCalcTestCase struct { type replicaCalcTestCase struct {
@ -376,38 +376,38 @@ func (tc *replicaCalcTestCase) runTest(t *testing.T) {
} }
var outReplicas int32 var outReplicas int32
var outUtilization int64 var outUsage int64
var outTimestamp time.Time var outTimestamp time.Time
switch tc.metric.metricType { switch tc.metric.metricType {
case objectMetric: case objectMetric:
if tc.metric.singleObject == nil { if tc.metric.singleObject == nil {
t.Fatal("Metric specified as objectMetric but metric.singleObject is nil.") t.Fatal("Metric specified as objectMetric but metric.singleObject is nil.")
} }
outReplicas, outUtilization, outTimestamp, err = replicaCalc.GetObjectMetricReplicas(tc.currentReplicas, tc.metric.targetUtilization, tc.metric.name, testNamespace, tc.metric.singleObject, selector, nil) outReplicas, outUsage, outTimestamp, err = replicaCalc.GetObjectMetricReplicas(tc.currentReplicas, tc.metric.targetUsage, tc.metric.name, testNamespace, tc.metric.singleObject, selector, nil)
case objectPerPodMetric: case objectPerPodMetric:
if tc.metric.singleObject == nil { if tc.metric.singleObject == nil {
t.Fatal("Metric specified as objectMetric but metric.singleObject is nil.") t.Fatal("Metric specified as objectMetric but metric.singleObject is nil.")
} }
outReplicas, outUtilization, outTimestamp, err = replicaCalc.GetObjectPerPodMetricReplicas(tc.currentReplicas, tc.metric.perPodTargetUtilization, tc.metric.name, testNamespace, tc.metric.singleObject, nil) outReplicas, outUsage, outTimestamp, err = replicaCalc.GetObjectPerPodMetricReplicas(tc.currentReplicas, tc.metric.perPodTargetUsage, tc.metric.name, testNamespace, tc.metric.singleObject, nil)
case externalMetric: case externalMetric:
if tc.metric.selector == nil { if tc.metric.selector == nil {
t.Fatal("Metric specified as externalMetric but metric.selector is nil.") t.Fatal("Metric specified as externalMetric but metric.selector is nil.")
} }
if tc.metric.targetUtilization <= 0 { if tc.metric.targetUsage <= 0 {
t.Fatalf("Metric specified as externalMetric but metric.targetUtilization is %d which is <=0.", tc.metric.targetUtilization) t.Fatalf("Metric specified as externalMetric but metric.targetUsage is %d which is <=0.", tc.metric.targetUsage)
} }
outReplicas, outUtilization, outTimestamp, err = replicaCalc.GetExternalMetricReplicas(tc.currentReplicas, tc.metric.targetUtilization, tc.metric.name, testNamespace, tc.metric.selector, selector) outReplicas, outUsage, outTimestamp, err = replicaCalc.GetExternalMetricReplicas(tc.currentReplicas, tc.metric.targetUsage, tc.metric.name, testNamespace, tc.metric.selector, selector)
case externalPerPodMetric: case externalPerPodMetric:
if tc.metric.selector == nil { if tc.metric.selector == nil {
t.Fatal("Metric specified as externalPerPodMetric but metric.selector is nil.") t.Fatal("Metric specified as externalPerPodMetric but metric.selector is nil.")
} }
if tc.metric.perPodTargetUtilization <= 0 { if tc.metric.perPodTargetUsage <= 0 {
t.Fatalf("Metric specified as externalPerPodMetric but metric.perPodTargetUtilization is %d which is <=0.", tc.metric.perPodTargetUtilization) t.Fatalf("Metric specified as externalPerPodMetric but metric.perPodTargetUsage is %d which is <=0.", tc.metric.perPodTargetUsage)
} }
outReplicas, outUtilization, outTimestamp, err = replicaCalc.GetExternalPerPodMetricReplicas(tc.currentReplicas, tc.metric.perPodTargetUtilization, tc.metric.name, testNamespace, tc.metric.selector) outReplicas, outUsage, outTimestamp, err = replicaCalc.GetExternalPerPodMetricReplicas(tc.currentReplicas, tc.metric.perPodTargetUsage, tc.metric.name, testNamespace, tc.metric.selector)
case podMetric: case podMetric:
outReplicas, outUtilization, outTimestamp, err = replicaCalc.GetMetricReplicas(tc.currentReplicas, tc.metric.targetUtilization, tc.metric.name, testNamespace, selector, nil) outReplicas, outUsage, outTimestamp, err = replicaCalc.GetMetricReplicas(tc.currentReplicas, tc.metric.targetUsage, tc.metric.name, testNamespace, selector, nil)
default: default:
t.Fatalf("Unknown metric type: %d", tc.metric.metricType) t.Fatalf("Unknown metric type: %d", tc.metric.metricType)
} }
@ -419,7 +419,7 @@ func (tc *replicaCalcTestCase) runTest(t *testing.T) {
} }
require.NoError(t, err, "there should not have been an error calculating the replica count") require.NoError(t, err, "there should not have been an error calculating the replica count")
assert.Equal(t, tc.expectedReplicas, outReplicas, "replicas should be as expected") assert.Equal(t, tc.expectedReplicas, outReplicas, "replicas should be as expected")
assert.Equal(t, tc.metric.expectedUtilization, outUtilization, "utilization should be as expected") assert.Equal(t, tc.metric.expectedUsage, outUsage, "usage should be as expected")
assert.True(t, tc.timestamp.Equal(outTimestamp), "timestamp should be as expected") assert.True(t, tc.timestamp.Equal(outTimestamp), "timestamp should be as expected")
} }
func makePodMetricLevels(containerMetric ...int64) [][]int64 { func makePodMetricLevels(containerMetric ...int64) [][]int64 {
@ -676,8 +676,8 @@ func TestReplicaCalcScaleUpCM(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{20000, 10000, 30000}, levels: []int64{20000, 10000, 30000},
targetUtilization: 15000, targetUsage: 15000,
expectedUtilization: 20000, expectedUsage: 20000,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -693,8 +693,8 @@ func TestReplicaCalcScaleUpCMUnreadyHotCpuNoLessScale(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{50000, 10000, 30000}, levels: []int64{50000, 10000, 30000},
targetUtilization: 15000, targetUsage: 15000,
expectedUtilization: 30000, expectedUsage: 30000,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -710,8 +710,8 @@ func TestReplicaCalcScaleUpCMUnreadyHotCpuScaleWouldScaleDown(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{50000, 15000, 30000}, levels: []int64{50000, 15000, 30000},
targetUtilization: 15000, targetUsage: 15000,
expectedUtilization: 31666, expectedUsage: 31666,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -725,8 +725,8 @@ func TestReplicaCalcScaleUpCMObject(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{20000}, levels: []int64{20000},
targetUtilization: 15000, targetUsage: 15000,
expectedUtilization: 20000, expectedUsage: 20000,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -745,8 +745,8 @@ func TestReplicaCalcScaleUpCMPerPodObject(t *testing.T) {
metricType: objectPerPodMetric, metricType: objectPerPodMetric,
name: "qps", name: "qps",
levels: []int64{20000}, levels: []int64{20000},
perPodTargetUtilization: 5000, perPodTargetUsage: 5000,
expectedUtilization: 6667, expectedUsage: 6667,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -765,8 +765,8 @@ func TestReplicaCalcScaleUpCMObjectIgnoresUnreadyPods(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{50000}, levels: []int64{50000},
targetUtilization: 10000, targetUsage: 10000,
expectedUtilization: 50000, expectedUsage: 50000,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -784,8 +784,8 @@ func TestReplicaCalcScaleUpCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
targetUtilization: 4400, targetUsage: 4400,
expectedUtilization: 8600, expectedUsage: 8600,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: podMetric, metricType: podMetric,
}, },
@ -801,8 +801,8 @@ func TestReplicaCalcScaleUpCMExternalIgnoresUnreadyPods(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
targetUtilization: 4400, targetUsage: 4400,
expectedUtilization: 8600, expectedUsage: 8600,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalMetric, metricType: externalMetric,
}, },
@ -817,8 +817,8 @@ func TestReplicaCalcScaleUpCMExternalNoLabels(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
targetUtilization: 4400, targetUsage: 4400,
expectedUtilization: 8600, expectedUsage: 8600,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -832,8 +832,8 @@ func TestReplicaCalcScaleUpPerPodCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
perPodTargetUtilization: 2150, perPodTargetUsage: 2150,
expectedUtilization: 2867, expectedUsage: 2867,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalPerPodMetric, metricType: externalPerPodMetric,
}, },
@ -883,8 +883,8 @@ func TestReplicaCalcScaleDownCM(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{12000, 12000, 12000, 12000, 12000}, levels: []int64{12000, 12000, 12000, 12000, 12000},
targetUtilization: 20000, targetUsage: 20000,
expectedUtilization: 12000, expectedUsage: 12000,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -898,8 +898,8 @@ func TestReplicaCalcScaleDownPerPodCMObject(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{6000}, levels: []int64{6000},
perPodTargetUtilization: 2000, perPodTargetUsage: 2000,
expectedUtilization: 1200, expectedUsage: 1200,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -918,8 +918,8 @@ func TestReplicaCalcScaleDownCMObject(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{12000}, levels: []int64{12000},
targetUtilization: 20000, targetUsage: 20000,
expectedUtilization: 12000, expectedUsage: 12000,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -937,8 +937,8 @@ func TestReplicaCalcScaleDownCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
targetUtilization: 14334, targetUsage: 14334,
expectedUtilization: 8600, expectedUsage: 8600,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalMetric, metricType: externalMetric,
}, },
@ -953,8 +953,8 @@ func TestReplicaCalcScaleDownPerPodCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
perPodTargetUtilization: 2867, perPodTargetUsage: 2867,
expectedUtilization: 1720, expectedUsage: 1720,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalPerPodMetric, metricType: externalPerPodMetric,
}, },
@ -1180,8 +1180,8 @@ func TestReplicaCalcToleranceCM(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{20000, 21000, 21000}, levels: []int64{20000, 21000, 21000},
targetUtilization: 20000, targetUsage: 20000,
expectedUtilization: 20666, expectedUsage: 20666,
metricType: podMetric, metricType: podMetric,
}, },
} }
@ -1195,8 +1195,8 @@ func TestReplicaCalcToleranceCMObject(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{20666}, levels: []int64{20666},
targetUtilization: 20000, targetUsage: 20000,
expectedUtilization: 20666, expectedUsage: 20666,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -1215,8 +1215,8 @@ func TestReplicaCalcTolerancePerPodCMObject(t *testing.T) {
metricType: objectPerPodMetric, metricType: objectPerPodMetric,
name: "qps", name: "qps",
levels: []int64{20166}, levels: []int64{20166},
perPodTargetUtilization: 5000, perPodTargetUsage: 5000,
expectedUtilization: 5042, expectedUsage: 5042,
singleObject: &autoscalingv2.CrossVersionObjectReference{ singleObject: &autoscalingv2.CrossVersionObjectReference{
Kind: "Deployment", Kind: "Deployment",
APIVersion: "apps/v1", APIVersion: "apps/v1",
@ -1234,8 +1234,8 @@ func TestReplicaCalcToleranceCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
targetUtilization: 8888, targetUsage: 8888,
expectedUtilization: 8600, expectedUsage: 8600,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalMetric, metricType: externalMetric,
}, },
@ -1250,8 +1250,8 @@ func TestReplicaCalcTolerancePerPodCMExternal(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{8600}, levels: []int64{8600},
perPodTargetUtilization: 2900, perPodTargetUsage: 2900,
expectedUtilization: 2867, expectedUsage: 2867,
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}}, selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
metricType: externalPerPodMetric, metricType: externalPerPodMetric,
}, },
@ -1508,8 +1508,8 @@ func TestReplicaCalcDuringRollingUpdateWithMaxSurgeCM(t *testing.T) {
metric: &metricInfo{ metric: &metricInfo{
name: "qps", name: "qps",
levels: []int64{10000, 10000}, levels: []int64{10000, 10000},
targetUtilization: 17000, targetUsage: 17000,
expectedUtilization: 10000, expectedUsage: 10000,
metricType: podMetric, metricType: podMetric,
}, },
} }