Skip to content

Commit

Permalink
Move createMetricSpec inside test code
Browse files Browse the repository at this point in the history
  • Loading branch information
yoongon committed Jul 12, 2023
1 parent 508064d commit 7720f5d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
26 changes: 20 additions & 6 deletions pkg/scaling/scale_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/kedacore/keda/v2/pkg/scalers"
"github.com/kedacore/keda/v2/pkg/scaling/cache"
"github.com/kedacore/keda/v2/pkg/scaling/cache/metricscache"
"github.com/kedacore/keda/v2/pkg/scaling/scaledjob"
)

func TestGetScaledObjectMetrics_DirectCall(t *testing.T) {
Expand All @@ -56,7 +55,7 @@ func TestGetScaledObjectMetrics_DirectCall(t *testing.T) {
mockExecutor := mock_executor.NewMockScaleExecutor(ctrl)
mockStatusWriter := mock_client.NewMockStatusWriter(ctrl)

metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(10, metricName)}
metricsSpecs := []v2.MetricSpec{createMetricSpec(10, metricName)}
metricValue := scalers.GenerateMetricInMili(metricName, float64(10))

metricsRecord := map[string]metricscache.MetricsRecord{}
Expand Down Expand Up @@ -147,7 +146,7 @@ func TestGetScaledObjectMetrics_FromCache(t *testing.T) {
mockExecutor := mock_executor.NewMockScaleExecutor(ctrl)
mockStatusWriter := mock_client.NewMockStatusWriter(ctrl)

metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(10, metricName)}
metricsSpecs := []v2.MetricSpec{createMetricSpec(10, metricName)}
metricValue := scalers.GenerateMetricInMili(metricName, float64(10))

metricsRecord := map[string]metricscache.MetricsRecord{}
Expand Down Expand Up @@ -231,7 +230,7 @@ func TestCheckScaledObjectScalersWithError(t *testing.T) {
mockExecutor := mock_executor.NewMockScaleExecutor(ctrl)
recorder := record.NewFakeRecorder(1)

metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(1, "metric-name")}
metricsSpecs := []v2.MetricSpec{createMetricSpec(1, "metric-name")}

scaler := mock_scalers.NewMockScaler(ctrl)
scaler.EXPECT().GetMetricSpecForScaling(gomock.Any()).Return(metricsSpecs)
Expand Down Expand Up @@ -292,7 +291,7 @@ func TestCheckScaledObjectFindFirstActiveNotIgnoreOthers(t *testing.T) {
mockExecutor := mock_executor.NewMockScaleExecutor(ctrl)
recorder := record.NewFakeRecorder(1)

metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(1, "metric-name")}
metricsSpecs := []v2.MetricSpec{createMetricSpec(1, "metric-name")}

activeFactory := func() (scalers.Scaler, *scalers.ScalerConfig, error) {
scaler := mock_scalers.NewMockScaler(ctrl)
Expand Down Expand Up @@ -603,7 +602,7 @@ func createScaledJob(minReplicaCount int32, maxReplicaCount int32, multipleScale

func createScaler(ctrl *gomock.Controller, queueLength int64, averageValue int64, isActive bool, metricName string) *mock_scalers.MockScaler {
scaler := mock_scalers.NewMockScaler(ctrl)
metricsSpecs := []v2.MetricSpec{scaledjob.CreateMetricSpec(averageValue, metricName)}
metricsSpecs := []v2.MetricSpec{createMetricSpec(averageValue, metricName)}

metrics := []external_metrics.ExternalMetricValue{
{
Expand All @@ -616,3 +615,18 @@ func createScaler(ctrl *gomock.Controller, queueLength int64, averageValue int64
scaler.EXPECT().Close(gomock.Any())
return scaler
}

// createMetricSpec creates MetricSpec for given metric name and target value.
func createMetricSpec(averageValue int64, metricName string) v2.MetricSpec {
qty := resource.NewQuantity(averageValue, resource.DecimalSI)
return v2.MetricSpec{
External: &v2.ExternalMetricSource{
Target: v2.MetricTarget{
AverageValue: qty,
},
Metric: v2.MetricIdentifier{
Name: metricName,
},
},
}
}
16 changes: 0 additions & 16 deletions pkg/scaling/scaledjob/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math"

v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/metrics/pkg/apis/external_metrics"
)

Expand Down Expand Up @@ -45,21 +44,6 @@ func CalculateQueueLengthAndMaxValue(metrics []external_metrics.ExternalMetricVa
return queueLength, maxValue, targetAverageValue
}

// CreateMetricSpec creates MetricSpec for given metric name and target value.
func CreateMetricSpec(averageValue int64, metricName string) v2.MetricSpec {
qty := resource.NewQuantity(averageValue, resource.DecimalSI)
return v2.MetricSpec{
External: &v2.ExternalMetricSource{
Target: v2.MetricTarget{
AverageValue: qty,
},
Metric: v2.MetricIdentifier{
Name: metricName,
},
},
}
}

type ScalerMetrics struct {
QueueLength float64
MaxValue float64
Expand Down
32 changes: 24 additions & 8 deletions pkg/scaling/scaledjob/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestTargetAverageValue(t *testing.T) {
Expand All @@ -15,26 +16,41 @@ func TestTargetAverageValue(t *testing.T) {
assert.Equal(t, float64(0), targetAverageValue)
// 1 1
specs = []v2.MetricSpec{
CreateMetricSpec(1, metricName),
CreateMetricSpec(1, metricName),
createMetricSpec(1, metricName),
createMetricSpec(1, metricName),
}
targetAverageValue = getTargetAverageValue(specs)
assert.Equal(t, float64(1), targetAverageValue)
// 5 5 3 -> 4.333333333333333
specs = []v2.MetricSpec{
CreateMetricSpec(5, metricName),
CreateMetricSpec(5, metricName),
CreateMetricSpec(3, metricName),
createMetricSpec(5, metricName),
createMetricSpec(5, metricName),
createMetricSpec(3, metricName),
}
targetAverageValue = getTargetAverageValue(specs)
assert.Equal(t, 4.333333333333333, targetAverageValue)

// 5 5 4 -> 4.666666666666667
specs = []v2.MetricSpec{
CreateMetricSpec(5, metricName),
CreateMetricSpec(5, metricName),
CreateMetricSpec(4, metricName),
createMetricSpec(5, metricName),
createMetricSpec(5, metricName),
createMetricSpec(4, metricName),
}
targetAverageValue = getTargetAverageValue(specs)
assert.Equal(t, 4.666666666666667, targetAverageValue)
}

// createMetricSpec creates MetricSpec for given metric name and target value.
func createMetricSpec(averageValue int64, metricName string) v2.MetricSpec {
qty := resource.NewQuantity(averageValue, resource.DecimalSI)
return v2.MetricSpec{
External: &v2.ExternalMetricSource{
Target: v2.MetricTarget{
AverageValue: qty,
},
Metric: v2.MetricIdentifier{
Name: metricName,
},
},
}
}

0 comments on commit 7720f5d

Please sign in to comment.