Skip to content

Commit

Permalink
add tests for expression and query get functions
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Pickerill <r.pickerill@gmail.com>
  • Loading branch information
robpickerill committed Apr 1, 2024
1 parent 6bce335 commit 618447d
Show file tree
Hide file tree
Showing 2 changed files with 478 additions and 259 deletions.
9 changes: 3 additions & 6 deletions pkg/scalers/aws_cloudwatch_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func NewAwsCloudwatchScaler(ctx context.Context, config *scalersconfig.ScalerCon

func createCloudwatchClient(ctx context.Context, metadata *awsCloudwatchMetadata) (*cloudwatch.Client, error) {
cfg, err := awsutils.GetAwsConfig(ctx, metadata.awsRegion, metadata.awsAuthorization)

if err != nil {
return nil, err
}
Expand All @@ -106,8 +105,8 @@ func getCloudWatchExpression(config *scalersconfig.ScalerConfig) (string, error)

// parseAwsCloudwatchMetadata parses the metric query parameters from the metadata, returning the namespace, metric name,
// metric unit, dimension names, dimension values, or error if the metadata was not parseable.
func parseCloudWatchMetricQuery(config *scalersconfig.ScalerConfig) (string, string, string, []string, []string, error) {
// namespace is a required parameter, that is a single string
func getCloudWatchMetricQuery(config *scalersconfig.ScalerConfig) (string, string, string, []string, []string, error) {
// namespace isa required parameter, that is a single string
namespaceUntyped, err := getParameterFromConfigV2(config, "namespace", reflect.TypeOf(""), IsOptional(false), UseMetadata(true))
if err != nil {
return "", "", "", nil, nil, fmt.Errorf("error parsing namespace: %w", err)
Expand Down Expand Up @@ -170,7 +169,7 @@ func parseAwsCloudwatchMetadata(config *scalersconfig.ScalerConfig) (*awsCloudwa
// if the expression is empty, try to get the metric query. The parameters in the query are now
// required, as the expression is not present.
if meta.expression == "" {
meta.namespace, meta.metricsName, meta.metricUnit, meta.dimensionName, meta.dimensionValue, err = parseCloudWatchMetricQuery(config)
meta.namespace, meta.metricsName, meta.metricUnit, meta.dimensionName, meta.dimensionValue, err = getCloudWatchMetricQuery(config)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -314,7 +313,6 @@ func computeQueryWindow(current time.Time, metricPeriodSec, metricEndTimeOffsetS

func (s *awsCloudwatchScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
metricValue, err := s.GetCloudwatchMetrics(ctx)

if err != nil {
s.logger.Error(err, "Error getting metric value")
return []external_metrics.ExternalMetricValue{}, false, err
Expand Down Expand Up @@ -397,7 +395,6 @@ func (s *awsCloudwatchScaler) GetCloudwatchMetrics(ctx context.Context) (float64
}

output, err := s.cwClient.GetMetricData(ctx, &input)

if err != nil {
s.logger.Error(err, "Failed to get output")
return -1, err
Expand Down
Loading

0 comments on commit 618447d

Please sign in to comment.