Skip to content

Commit

Permalink
move util function to scaler.go file
Browse files Browse the repository at this point in the history
Signed-off-by: dttung2905 <ttdao.2015@accountancy.smu.edu.sg>
  • Loading branch information
dttung2905 committed Dec 7, 2023
1 parent 5644dc6 commit 6e6f16c
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 306 deletions.
61 changes: 0 additions & 61 deletions pkg/scalers/azure_log_analytics_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"io"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -255,66 +254,6 @@ func getParameterFromConfig(config *ScalerConfig, parameter string, checkAuthPar
return "", fmt.Errorf("error parsing metadata. Details: %s was not found in metadata. Check your ScaledObject configuration", parameter)
}

func getParameterFromConfigV2(config *ScalerConfig, parameter string, useMetadata bool, useAuthentication bool, useResolvedEnv bool, isOptional bool, defaultVal string, targetType reflect.Type) (interface{}, error) {
if val, ok := config.AuthParams[parameter]; useAuthentication && ok && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
}
return returnedVal, nil
} else if val, ok := config.TriggerMetadata[parameter]; ok && useMetadata && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
}
return returnedVal, nil
} else if val, ok := config.TriggerMetadata[fmt.Sprintf("%sFromEnv", parameter)]; ok && useResolvedEnv && val != "" {
returnedVal, err := convertStringToType(val, targetType)
if err != nil {
return defaultVal, err
}
return returnedVal, nil
}

if isOptional {
return defaultVal, nil
}
return "", fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
}

func convertStringToType(input string, targetType reflect.Type) (interface{}, error) {
switch targetType.Kind() {
case reflect.String:
return input, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
result, err := strconv.ParseUint(input, 10, 64)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
case reflect.Float32, reflect.Float64:
result, err := strconv.ParseFloat(input, 64)
if err != nil {
return nil, err
}
return reflect.ValueOf(result).Convert(targetType).Interface(), nil
case reflect.Bool:
result, err := strconv.ParseBool(input)
if err != nil {
return nil, err
}
return result, nil
default:
return nil, fmt.Errorf("unsupported type: %v", targetType)
}
}

func (s *azureLogAnalyticsScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Expand Down
245 changes: 0 additions & 245 deletions pkg/scalers/azure_log_analytics_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ package scalers
import (
"context"
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/assert"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)

Expand Down Expand Up @@ -250,245 +247,3 @@ func TestLogAnalyticsParseMetadataUnsafeSsl(t *testing.T) {
}
}
}

type getParameterFromConfigTestData struct {
name string
authParams map[string]string
metadata map[string]string
parameter string
useAuthentication bool
useMetadata bool
useResolvedEnv bool
isOptional bool
defaultVal string
targetType reflect.Type
expectedResult interface{}
isError bool
errorMessage string
}

var getParameterFromConfigTestDataset = []getParameterFromConfigTestData{
{
name: "test_authParam_only",
authParams: map[string]string{"key1": "value1"},
parameter: "key1",
useAuthentication: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1",
isError: false,
},
{
name: "test_trigger_metadata_only",
metadata: map[string]string{"key1": "value1"},
parameter: "key1",
useMetadata: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1",
isError: false,
},
{
name: "test_resolved_env_only",
metadata: map[string]string{"key1FromEnv": "value1"},
parameter: "key1",
useResolvedEnv: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1",
isError: false,
},
{
name: "test_authParam_and_resolved_env_only",
authParams: map[string]string{"key1": "value1"},
metadata: map[string]string{"key1FromEnv": "value2"},
parameter: "key1",
useAuthentication: true,
useResolvedEnv: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1", // Should get from Auth
isError: false,
},
{
name: "test_authParam_and_trigger_metadata_only",
authParams: map[string]string{"key1": "value1"},
metadata: map[string]string{"key1": "value2"},
parameter: "key1",
useMetadata: true,
useAuthentication: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1", // Should get from auth
isError: false,
},
{
name: "test_trigger_metadata_and_resolved_env_only",
metadata: map[string]string{"key1": "value1", "key1FromEnv": "value2"},
parameter: "key1",
useResolvedEnv: true,
useMetadata: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "value1", // Should get from trigger metadata
isError: false,
},
{
name: "test_isOptional_key_not_found",
metadata: map[string]string{"key1": "value1"},
parameter: "key2",
useResolvedEnv: true,
useMetadata: true,
isOptional: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "", // Should return empty string
isError: false,
},
{
name: "test_default_value_key_not_found",
metadata: map[string]string{"key1": "value1"},
parameter: "key2",
useResolvedEnv: true,
useMetadata: true,
isOptional: true,
defaultVal: "default",
targetType: reflect.TypeOf(string("")),
expectedResult: "default",
isError: false,
},
{
name: "test_error",
metadata: map[string]string{"key1": "value1"},
parameter: "key2",
useResolvedEnv: true,
useMetadata: true,
targetType: reflect.TypeOf(string("")),
expectedResult: "default", // Should return empty string
isError: true,
errorMessage: "key not found. Either set the correct key or set isOptional to true and set defaultValx",
},
{
name: "test_authParam_bool",
authParams: map[string]string{"key1": "true"},
parameter: "key1",
useAuthentication: true,
targetType: reflect.TypeOf(true),
expectedResult: true,
},
}

func TestGetParameterFromConfigV2(t *testing.T) {
for _, testData := range getParameterFromConfigTestDataset {
val, err := getParameterFromConfigV2(
&ScalerConfig{TriggerMetadata: testData.metadata, AuthParams: testData.authParams},
testData.parameter,
testData.useMetadata,
testData.useAuthentication,
testData.useResolvedEnv,
testData.isOptional,
testData.defaultVal,
testData.targetType,
)
if testData.isError {
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
assert.Containsf(t, err.Error(), testData.errorMessage, "test %s: %v", testData.name, err.Error())
} else {
assert.Nilf(t, err, "test %s:%v", testData.name, err)
assert.Equalf(t, testData.expectedResult, val, "test %s: expected %s but got %s", testData.name, testData.expectedResult, val)
}
}
}

type convertStringToTypeTestData struct {
name string
input string
targetType reflect.Type
expectedOutput interface{}
isError bool
errorMessage string
}

var convertStringToTypeDataset = []convertStringToTypeTestData{
{
name: "test string",
input: "test",
targetType: reflect.TypeOf(string("")),
expectedOutput: "test",
},
{
name: "test int",
input: "6",
targetType: reflect.TypeOf(int(6)),
expectedOutput: 6,
},
{
name: "test int64 max",
input: "9223372036854775807", // int64 max
targetType: reflect.TypeOf(int64(6)),
expectedOutput: int64(9223372036854775807),
},
{
name: "test int64 min",
input: "-9223372036854775808", // int64 min
targetType: reflect.TypeOf(int64(6)),
expectedOutput: int64(-9223372036854775808),
},
{
name: "test uint64 max",
input: "18446744073709551615", // uint64 max
targetType: reflect.TypeOf(uint64(6)),
expectedOutput: uint64(18446744073709551615),
},
{
name: "test float32",
input: "3.14",
targetType: reflect.TypeOf(float32(3.14)),
expectedOutput: float32(3.14),
},
{
name: "test float64",
input: "0.123456789121212121212",
targetType: reflect.TypeOf(float64(0.123456789121212121212)),
expectedOutput: float64(0.123456789121212121212),
},
{
name: "test bool",
input: "true",
targetType: reflect.TypeOf(true),
expectedOutput: true,
},
{
name: "test bool 2",
input: "True",
targetType: reflect.TypeOf(true),
expectedOutput: true,
},
{
name: "test bool 3",
input: "false",
targetType: reflect.TypeOf(false),
expectedOutput: false,
},
{
name: "test bool 4",
input: "False",
targetType: reflect.TypeOf(false),
expectedOutput: false,
},
{
name: "unsupported type",
input: "Unsupported Type",
targetType: reflect.TypeOf([]int{}),
expectedOutput: "error",
isError: true,
errorMessage: "unsupported type: []int",
},
}

func TestConvertStringToType(t *testing.T) {
for _, testData := range convertStringToTypeDataset {
val, err := convertStringToType(testData.input, testData.targetType)

if testData.isError {
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
assert.Contains(t, err.Error(), testData.errorMessage)
} else {
assert.Nil(t, err)
assert.Equalf(t, testData.expectedOutput, val, "test %s: expected %s but got %s", testData.name, testData.expectedOutput, val)
}
}
}
Loading

0 comments on commit 6e6f16c

Please sign in to comment.