Skip to content

Commit

Permalink
First commit. Add two new helpers and tests for one method
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 Nov 29, 2023
1 parent e8fcb84 commit 8bee3c3
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/scalers/azure_log_analytics_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -254,6 +255,69 @@ 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 "", nil
// Not sure what should we return in this case. Should we return empty string or defaultVal
} else if defaultVal != "" {
return defaultVal, nil
}
return "", fmt.Errorf("key not found. Either set the correct key, set isOptional to true or 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
154 changes: 154 additions & 0 deletions pkg/scalers/azure_log_analytics_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package scalers
import (
"context"
"net/http"
"reflect"
"testing"

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

const (
Expand Down Expand Up @@ -247,3 +249,155 @@ 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_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",
metadata: map[string]string{"key1": "value1"},
parameter: "key2",
useResolvedEnv: true,
useMetadata: true,
defaultVal: "default",
targetType: reflect.TypeOf(string("")),
expectedResult: "default", // Should return empty string
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, set isOptional to true or set defaultVal",
},
{
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.Contains(t, err.Error(), testData.errorMessage)
} else {
assert.Nil(t, err)
assert.Equalf(t, testData.expectedResult, val, "expected %s but got %s", testData.expectedResult, val)
}
}
}

0 comments on commit 8bee3c3

Please sign in to comment.