Skip to content

Commit

Permalink
gcp_pubsub: fix scaled object default value (kedacore#5093)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Gustavsson <jim.a.gustavsson@gmail.com>
  • Loading branch information
jimgus committed Oct 19, 2023
1 parent 308cec4 commit 6b24d86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/scalers/gcp_pubsub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewPubSubScaler(config *ScalerConfig) (Scaler, error) {

func parsePubSubMetadata(config *ScalerConfig, logger logr.Logger) (*pubsubMetadata, error) {
// set subscription size to the default mode
meta := pubsubMetadata{mode: pubSubModeSubscriptionSize}
meta := pubsubMetadata{mode: pubSubModeSubscriptionSize, value: pubSubDefaultValue}

mode, modePresent := config.TriggerMetadata["mode"]
value, valuePresent := config.TriggerMetadata["value"]
Expand All @@ -83,7 +83,6 @@ func parsePubSubMetadata(config *ScalerConfig, logger logr.Logger) (*pubsubMetad
} else {
if modePresent {
meta.mode = mode
meta.value = pubSubDefaultValue
}

if valuePresent {
Expand Down
24 changes: 24 additions & 0 deletions pkg/scalers/gcp_pubsub_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ var testPubSubMetadata = []parsePubSubMetadataTestData{
{nil, map[string]string{"subscriptionName": "projects/myproject/mysubscription", "subscriptionSize": "7", "credentialsFromEnv": "SAMPLE_CREDS"}, false},
// properly formed float value and activationTargetValue
{nil, map[string]string{"subscriptionName": "mysubscription", "value": "7.1", "credentialsFromEnv": "SAMPLE_CREDS", "activationValue": "2.1"}, false},
// All optional omitted
{nil, map[string]string{"subscriptionName": "mysubscription", "credentialsFromEnv": "SAMPLE_CREDS"}, false},
// value omittted when mode present
{nil, map[string]string{"subscriptionName": "mysubscription", "mode": "SubscriptionSize", "credentialsFromEnv": "SAMPLE_CREDS"}, false},
}

var gcpPubSubMetricIdentifiers = []gcpPubSubMetricIdentifier{
Expand All @@ -70,6 +74,11 @@ var gcpSubscriptionNameTests = []gcpPubSubSubscription{
{&testPubSubMetadata[12], 1, "projects/myproject/mysubscription", ""},
}

var gcpSubscriptionDefaults = []gcpPubSubSubscription{
{&testPubSubMetadata[14], 0, "", ""},
{&testPubSubMetadata[15], 0, "", ""},
}

func TestPubSubParseMetadata(t *testing.T) {
for _, testData := range testPubSubMetadata {
_, err := parsePubSubMetadata(&ScalerConfig{AuthParams: testData.authParams, TriggerMetadata: testData.metadata, ResolvedEnv: testPubSubResolvedEnv}, logr.Discard())
Expand All @@ -82,6 +91,21 @@ func TestPubSubParseMetadata(t *testing.T) {
}
}

func TestPubSubMetadataDefaultValues(t *testing.T) {
for _, testData := range gcpSubscriptionDefaults {
metaData, err := parsePubSubMetadata(&ScalerConfig{AuthParams: testData.metadataTestData.authParams, TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testPubSubResolvedEnv}, logr.Discard())
if err != nil {
t.Error("Expected success but got error", err)
}
if pubSubModeSubscriptionSize != metaData.mode {
t.Errorf(`Expected mode "%s" but got "%s"`, pubSubModeSubscriptionSize, metaData.mode)
}
if pubSubDefaultValue != metaData.value {
t.Errorf(`Expected value "%d" but got "%f"`, pubSubDefaultValue, metaData.value)
}
}
}

func TestGcpPubSubGetMetricSpecForScaling(t *testing.T) {
for _, testData := range gcpPubSubMetricIdentifiers {
meta, err := parsePubSubMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ResolvedEnv: testPubSubResolvedEnv, ScalerIndex: testData.scalerIndex}, logr.Discard())
Expand Down

0 comments on commit 6b24d86

Please sign in to comment.