Skip to content

Commit

Permalink
fix(config): redis sentinel (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored Sep 20, 2024
1 parent 75239e8 commit 9e90688
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
26 changes: 14 additions & 12 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,20 @@ type RedisConfig struct {
// GetSettings returns redis config parsed from moira config files.
func (config *RedisConfig) GetSettings() redis.DatabaseConfig {
return redis.DatabaseConfig{
MasterName: config.MasterName,
Addrs: strings.Split(config.Addrs, ","),
Username: config.Username,
Password: config.Password,
MaxRetries: config.MaxRetries,
MetricsTTL: to.Duration(config.MetricsTTL),
DialTimeout: to.Duration(config.DialTimeout),
ReadTimeout: to.Duration(config.ReadTimeout),
WriteTimeout: to.Duration(config.WriteTimeout),
ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,
MasterName: config.MasterName,
Addrs: strings.Split(config.Addrs, ","),
Username: config.Username,
Password: config.Password,
SentinelUsername: config.SentinelUsername,
SentinelPassword: config.SentinelPassword,
MaxRetries: config.MaxRetries,
MetricsTTL: to.Duration(config.MetricsTTL),
DialTimeout: to.Duration(config.DialTimeout),
ReadTimeout: to.Duration(config.ReadTimeout),
WriteTimeout: to.Duration(config.WriteTimeout),
ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,
}
}

Expand Down
61 changes: 61 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"testing"
"time"

"github.com/moira-alert/moira/database/redis"
. "github.com/smartystreets/goconvey/convey"
)

func TestRedisConfig(t *testing.T) {
Convey("Test RedisConfig.GetSettings", t, func() {
Convey("With empty config", func() {
redisCfg := RedisConfig{}

expected := redis.DatabaseConfig{
Addrs: []string{""},
}
databaseCfg := redisCfg.GetSettings()
So(databaseCfg, ShouldResemble, expected)
})

Convey("With filled config", func() {
redisCfg := RedisConfig{
MasterName: "test-master",
Addrs: "redis1:6379",
SentinelUsername: "sentinel-user",
SentinelPassword: "sentinel-pass",
Username: "user",
Password: "pass",
MetricsTTL: "1m",
DialTimeout: "1m",
ReadTimeout: "1m",
WriteTimeout: "1m",
MaxRetries: 3,
ReadOnly: true,
RouteByLatency: true,
RouteRandomly: true,
}

expected := redis.DatabaseConfig{
MasterName: "test-master",
Addrs: []string{"redis1:6379"},
SentinelUsername: "sentinel-user",
SentinelPassword: "sentinel-pass",
Username: "user",
Password: "pass",
MetricsTTL: time.Minute,
DialTimeout: time.Minute,
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
MaxRetries: 3,
ReadOnly: true,
RouteByLatency: true,
RouteRandomly: true,
}
databaseCfg := redisCfg.GetSettings()
So(databaseCfg, ShouldResemble, expected)
})
})
}

0 comments on commit 9e90688

Please sign in to comment.