Skip to content

Commit

Permalink
Redis Lists - Connection Parameters (#962)
Browse files Browse the repository at this point in the history
Co-authored-by: Travis Bickford <tbickford@shutterstock.com>
  • Loading branch information
tbickford and sstk-tbickford authored Aug 1, 2020
1 parent 41978fc commit 08c3600
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 77 deletions.
47 changes: 24 additions & 23 deletions pkg/scalers/redis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,39 @@ func parseRedisMetadata(metadata, resolvedEnv, authParams map[string]string) (*r
return nil, fmt.Errorf("no list name given")
}

address := defaultRedisAddress
host := defaultHost
port := defaultPort
if val, ok := metadata["address"]; ok && val != "" {
address = val
} else {
if val, ok := metadata["host"]; ok && val != "" {
host = val
} else {
return nil, fmt.Errorf("no address or host given. address should be in the format of host:port or you should set the host/port values")
if val, ok := authParams["address"]; ok {
meta.address = val
} else if addressEnvName, ok := metadata["address"]; ok && addressEnvName != "" {
if val, ok := resolvedEnv[addressEnvName]; ok {
meta.address = val
}
if val, ok := metadata["port"]; ok && val != "" {
port = val
} else {
return nil, fmt.Errorf("no address or port given. address should be in the format of host:port or you should set the host/port values")
if meta.address == "" {
return nil, fmt.Errorf("no address given")
}
}

if val, ok := resolvedEnv[address]; ok {
meta.address = val
} else {
if val, ok := resolvedEnv[host]; ok {
if val, ok := authParams["host"]; ok {
meta.host = val
} else if hostEnvName, ok := metadata["host"]; ok && hostEnvName != "" {
if val, ok := resolvedEnv[hostEnvName]; ok {
meta.host = val
} else {
return nil, fmt.Errorf("no host given. Address should be in the format of host:port or you should provide both host and port")
}
} else {
return nil, fmt.Errorf("no address given or host given. Address should be in the format of host:port or you should provide both host and port")
return nil, fmt.Errorf("no host given. address should be in the format of host:port or you should set the host/port values")
}

if val, ok := resolvedEnv[port]; ok {
if val, ok := authParams["port"]; ok {
meta.port = val
} else if portEnvName, ok := metadata["port"]; ok && portEnvName != "" {
if val, ok := resolvedEnv[portEnvName]; ok {
meta.port = val
} else {
return nil, fmt.Errorf("no port given. Address should be in the format of host:port or you should provide both host and port")
}
} else {
return nil, fmt.Errorf("no address or port given. Address should be in the format of host:port or you should provide both host and port")
return nil, fmt.Errorf("no port given. address should be in the format of host:port or you should set the host/port values")
}

meta.address = fmt.Sprintf("%s:%s", meta.host, meta.port)
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/scalers/redis_scaler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scalers

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -35,16 +36,23 @@ var testRedisMetadata = []parseRedisMetadataTestData{
{map[string]string{"listName": "mylist", "listLength": "0", "address": "REDIS_WRONG", "password": ""}, true, map[string]string{}},
// password is defined in the authParams
{map[string]string{"listName": "mylist", "listLength": "0", "address": "REDIS_WRONG"}, true, map[string]string{"password": ""}},
}
// address is defined in the authParams
{map[string]string{"listName": "mylist", "listLength": "0"}, false, map[string]string{"address": "localhost:6379"}},
// host and port is defined in the authParams
{map[string]string{"listName": "mylist", "listLength": "0"}, false, map[string]string{"host": "localhost", "port": "6379"}},
// host only is defined in the authParams
{map[string]string{"listName": "mylist", "listLength": "0"}, true, map[string]string{"host": "localhost"}}}

func TestRedisParseMetadata(t *testing.T) {
testCaseNum := 1
for _, testData := range testRedisMetadata {
_, err := parseRedisMetadata(testData.metadata, testRedisResolvedEnv, testData.authParams)
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
t.Error(fmt.Sprintf("Expected success but got error for unit test # %v", testCaseNum), err)
}
if testData.isError && err == nil {
t.Error("Expected error but got success")
t.Error(fmt.Sprintf("Expected error but got success for unit test #%v", testCaseNum))
}
testCaseNum++
}
}
Loading

0 comments on commit 08c3600

Please sign in to comment.