Merge pull request #5705 from steven-zou/fix_redis_parsing_issue

Fix the issue of parsing redis connection address in chart API
This commit is contained in:
Steven Zou 2018-08-23 15:06:08 +08:00 committed by GitHub
commit d895ebef13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 13 deletions

View File

@ -68,22 +68,32 @@ func parseRedisConfig(redisConfigV string) (string, error) {
redisConfig := make(map[string]string) redisConfig := make(map[string]string)
redisConfig["key"] = cacheCollectionName redisConfig["key"] = cacheCollectionName
//The full pattern //Try best to parse the configuration segments.
if strings.Index(redisConfigV, ",") != -1 { //If the related parts are missing, assign default value.
//Read only the previous 4 segments //The default database index for UI process is 0.
configSegments := strings.SplitN(redisConfigV, ",", 4) configSegments := strings.Split(redisConfigV, ",")
if len(configSegments) != 4 { for i, segment := range configSegments {
return "", errors.New("invalid redis config, it should be address:port[,weight,password,db_index]") if i > 3 {
//ignore useless segments
break
} }
redisConfig["conn"] = configSegments[0] switch i {
redisConfig["password"] = configSegments[2] //address:port
redisConfig["dbNum"] = configSegments[3] case 0:
} else { redisConfig["conn"] = segment
//The short pattern //password, may not exist
redisConfig["conn"] = redisConfigV case 2:
redisConfig["password"] = segment
//database index, may not exist
case 3:
redisConfig["dbNum"] = segment
}
}
//Assign default value
if len(redisConfig["dbNum"]) == 0 {
redisConfig["dbNum"] = "0" redisConfig["dbNum"] = "0"
redisConfig["password"] = ""
} }
//Try to validate the connection address //Try to validate the connection address

View File

@ -0,0 +1,42 @@
package chartserver
import (
"strings"
"testing"
)
//Test the utility function parseRedisConfig
func TestParseRedisConfig(t *testing.T) {
//Case 1: empty addr
redisAddr := ""
if _, err := parseRedisConfig(redisAddr); err == nil {
t.Fatal("expect non nil error but got nil one if addr is empty")
}
//Case 2: short pattern, addr:port
redisAddr = "redis:6379"
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
t.Fatalf("expect nil error but got non nil one if addr is short pattern: %s\n", parsedConnStr)
}
//Case 3: long pattern but miss some parts
redisAddr = "redis:6379,100"
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
t.Fatalf("expect nil error but got non nil one if addr is long pattern with some parts missing: %s\n", parsedConnStr)
} else {
if strings.Index(parsedConnStr, `"dbNum":"0"`) == -1 {
t.Fatalf("expect 'dbNum:0' in the parsed conn str but got nothing: %s\n", parsedConnStr)
}
}
//Case 4: long pattern
redisAddr = "redis:6379,100,Passw0rd,1"
if parsedConnStr, err := parseRedisConfig(redisAddr); err != nil {
t.Fatal("expect nil error but got non nil one if addr is long pattern")
} else {
if strings.Index(parsedConnStr, `"dbNum":"1"`) == -1 ||
strings.Index(parsedConnStr, `"password":"Passw0rd"`) == -1 {
t.Fatalf("expect 'dbNum:0' and 'password:Passw0rd' in the parsed conn str but got nothing: %s", parsedConnStr)
}
}
}