From 8e4c26f4f31aaef4d8ad24fedd09fb36dc2a2397 Mon Sep 17 00:00:00 2001 From: Danil Tarasov <87192879+almostinf@users.noreply.github.com> Date: Fri, 27 Sep 2024 12:34:57 +0300 Subject: [PATCH] fix(config): redis sentinel in v2.12 (#1105) --- cmd/config.go | 26 +++++++++++--------- cmd/config_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 cmd/config_test.go diff --git a/cmd/config.go b/cmd/config.go index 7a5ce2321..dfc78687a 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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, } } diff --git a/cmd/config_test.go b/cmd/config_test.go new file mode 100644 index 000000000..f41583d00 --- /dev/null +++ b/cmd/config_test.go @@ -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) + }) + }) +}