Skip to content

Commit

Permalink
gemini: invalid strategy unmarshaling fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Johansson committed Feb 12, 2020
1 parent cf3e043 commit 2ff1a4c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/gemini/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func getReplicationStrategy(rs string, fallback *replication.Replication, logger
return replication.NewSimpleStrategy()
default:
replicationStrategy := &replication.Replication{}
if err := json.Unmarshal([]byte(strings.ReplaceAll(rs, "'", "\"")), rs); err != nil {
if err := json.Unmarshal([]byte(strings.ReplaceAll(rs, "'", "\"")), replicationStrategy); err != nil {
logger.Error("unable to parse replication strategy", zap.String("strategy", rs), zap.Error(err))
return fallback
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/gemini/strategies_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"testing"

"github.com/scylladb/gemini/replication"

"github.com/google/go-cmp/cmp"

"go.uber.org/zap"
)

func TestGetReplicationStrategy(t *testing.T) {
var tests = map[string]struct {
strategy string
expected string
}{
"simple strategy": {
strategy: "{\"class\": \"SimpleStrategy\", \"replication_factor\": \"1\"}",
expected: "{'class':'SimpleStrategy','replication_factor':'1'}",
},
"simple strategy single quotes": {
strategy: "{'class': 'SimpleStrategy', 'replication_factor': '1'}",
expected: "{'class':'SimpleStrategy','replication_factor':'1'}",
},
"network topology strategy": {
strategy: "{\"class\": \"NetworkTopologyStrategy\", \"dc1\": 3, \"dc2\": 3}",
expected: "{'class':'NetworkTopologyStrategy','dc1':3,'dc2':3}",
},
"network topology strategy single quotes": {
strategy: "{'class': 'NetworkTopologyStrategy', 'dc1': 3, 'dc2': 3}",
expected: "{'class':'NetworkTopologyStrategy','dc1':3,'dc2':3}",
},
}
logger := zap.NewNop()
fallback := replication.NewSimpleStrategy()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := getReplicationStrategy(tc.strategy, fallback, logger)
if diff := cmp.Diff(got.ToCQL(), tc.expected); diff != "" {
t.Errorf("expected=%s, got=%s,diff=%s", tc.strategy, got.ToCQL(), diff)
}
})
}
}

0 comments on commit 2ff1a4c

Please sign in to comment.