Skip to content

Commit

Permalink
Add unit tests for NATS JetStream scaler (#3521)
Browse files Browse the repository at this point in the history
Signed-off-by: Deepak <[email protected]>
  • Loading branch information
goku321 authored Aug 8, 2022
1 parent bbd4334 commit cdba613
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/scalers/nats_jetstream_scaler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package scalers

import (
"context"
"net/http"
"testing"
)

type parseNATSJetStreamMetadataTestData struct {
metadata map[string]string
authParams map[string]string
isError bool
}

type natsJetStreamMetricIdentifier struct {
metadataTestData *parseNATSJetStreamMetadataTestData
scalerIndex int
name string
}

var testNATSJetStreamMetadata = []parseNATSJetStreamMetadataTestData{
// nothing passed
{map[string]string{}, map[string]string{}, true},
// Missing account name, should fail
{map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222", "stream": "mystream", "consumer": "pull_consumer"}, map[string]string{}, true},
// Missing stream name, should fail
{map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222", "account": "$G", "consumer": "pull_consumer"}, map[string]string{}, true},
// Missing consumer name should fail
{map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222", "account": "$G", "stream": "mystream"}, map[string]string{}, true},
// Missing nats server monitoring endpoint, should fail
{map[string]string{"account": "$G", "stream": "mystream"}, map[string]string{}, true},
// All good.
{map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222", "account": "$G", "stream": "mystream", "consumer": "pull_consumer"}, map[string]string{}, false},
// All good + activationLagThreshold
{map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222", "account": "$G", "stream": "mystream", "consumer": "pull_consumer", "activationLagThreshold": "10"}, map[string]string{}, false},
// natsServerMonitoringEndpoint is defined in authParams
{map[string]string{"account": "$G", "stream": "mystream", "consumer": "pull_consumer"}, map[string]string{"natsServerMonitoringEndpoint": "nats.nats:8222"}, false},
// Missing nats server monitoring endpoint , should fail
{map[string]string{"account": "$G", "stream": "mystream", "consumer": "pull_consumer"}, map[string]string{"natsServerMonitoringEndpoint": ""}, true},
}

var natsJetStreamMetricIdentifiers = []natsJetStreamMetricIdentifier{
{&testNATSJetStreamMetadata[5], 0, "s0-nats-jetstream-mystream"},
{&testNATSJetStreamMetadata[5], 1, "s1-nats-jetstream-mystream"},
}

func TestNATSJetStreamParseMetadata(t *testing.T) {
for _, testData := range testNATSJetStreamMetadata {
_, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, AuthParams: testData.authParams})
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
} else if testData.isError && err == nil {
t.Error("Expected error but got success")
}
}
}

func TestNATSJetStreamGetMetricSpecForScaling(t *testing.T) {
for _, testData := range natsJetStreamMetricIdentifiers {
ctx := context.Background()
meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ScalerIndex: testData.scalerIndex})
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockStanScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
}

metricSpec := mockStanScaler.GetMetricSpecForScaling(ctx)
metricName := metricSpec[0].External.Metric.Name
if metricName != testData.name {
t.Error("Wrong External metric source name:", metricName)
}
}
}

0 comments on commit cdba613

Please sign in to comment.