From 1af8facff1a8292d5f7109ef5d8e7835687c7c58 Mon Sep 17 00:00:00 2001 From: Chetra Tep Date: Thu, 21 Sep 2023 14:05:54 +0900 Subject: [PATCH 1/5] Fix cluster refresh failed when using pasword file with isCluster mode --- exporter/redis.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/exporter/redis.go b/exporter/redis.go index 55c2b7ee..3d9be768 100644 --- a/exporter/redis.go +++ b/exporter/redis.go @@ -67,6 +67,16 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) { func (e *Exporter) connectToRedisCluster() (redis.Conn, error) { uri := e.redisAddr + if !strings.Contains(uri, "://") { + uri = "redis://" + uri + } + + options, err := e.configureOptions(uri) + if err != nil { + return nil, err + } + + // remove url scheme for redis.Cluster.StartupNodes if strings.Contains(uri, "://") { u, _ := url.Parse(uri) if u.Port() == "" { @@ -80,11 +90,6 @@ func (e *Exporter) connectToRedisCluster() (redis.Conn, error) { } } - options, err := e.configureOptions(uri) - if err != nil { - return nil, err - } - log.Debugf("Creating cluster object") cluster := redisc.Cluster{ StartupNodes: []string{uri}, From 318b6d97576c2afdb07c2e436517a78d2eae868b Mon Sep 17 00:00:00 2001 From: chetratep Date: Wed, 11 Oct 2023 09:50:04 +0900 Subject: [PATCH 2/5] unittest --- Makefile | 3 +- contrib/docker-compose-for-tests.yml | 10 ++++++ exporter/redis_test.go | 46 ++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2c5f3e56..0be3764f 100644 --- a/Makefile +++ b/Makefile @@ -40,9 +40,10 @@ test: TEST_USER_PWD_REDIS_URI="redis://exporter:exporter-password@pwd-redis6:6390" \ TEST_REDIS_CLUSTER_MASTER_URI="redis://redis-cluster:7000" \ TEST_REDIS_CLUSTER_SLAVE_URI="redis://redis-cluster:7005" \ + TEST_REDIS_CLUSTER_PASSWORD_URI="redis://redis-cluster-password:7006" \ TEST_TILE38_URI="redis://tile38:9851" \ TEST_REDIS_SENTINEL_URI="redis://redis-sentinel:26379" \ - go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt -p 1 ./... + go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt -p 1 ./... .PHONY: lint lint: diff --git a/contrib/docker-compose-for-tests.yml b/contrib/docker-compose-for-tests.yml index ebc2d4fd..9d99fcc6 100644 --- a/contrib/docker-compose-for-tests.yml +++ b/contrib/docker-compose-for-tests.yml @@ -53,6 +53,16 @@ services: image: grokzen/redis-cluster ports: [ "7000", "7001", "7002", "7003", "7004", "7005" ] + redis-cluster-password: + image: bitnami/redis-cluster + environment: + - REDIS_PORT_NUMBER=7006 + - REDIS_PASSWORD=redis-password + - REDIS_CLUSTER_CREATOR=yes + - REDIS_NODES=redis-cluster-password:7006 + ports: + - "7006" + redis-sentinel: image: docker.io/bitnami/redis-sentinel:6.2-debian-10 environment: diff --git a/exporter/redis_test.go b/exporter/redis_test.go index 5fd1ca14..6471dc1b 100644 --- a/exporter/redis_test.go +++ b/exporter/redis_test.go @@ -1,6 +1,7 @@ package exporter import ( + "bytes" "net/http/httptest" "net/url" "os" @@ -8,6 +9,7 @@ import ( "testing" "github.com/prometheus/client_golang/prometheus" + log "github.com/sirupsen/logrus" ) func TestHostVariations(t *testing.T) { @@ -111,3 +113,47 @@ func TestPasswordInvalid(t *testing.T) { t.Errorf(`error, expected string "%s" in body, got body: \n\n%s`, want, body) } } + +func TestConnectToClusterUingPasswordFile(t *testing.T) { + cluster_host := os.Getenv("TEST_REDIS_CLUSTER_PASSWORD_URI") + if cluster_host == "" { + t.Skipf("TEST_REDIS_CLUSTER_PASSWORD_URI is not set") + } + passMap := map[string]string{"redis://redis-cluster-password:7006": "redis-password"} + wrongPassMap := map[string]string{"redis://redis-cluster-password-wrong:7006": "redis-password"} + + tsts := []struct { + name string + isCluster bool + passMap map[string]string + expectEr bool + }{ + {name: "ConnectToCluster using passord file witch cluster mode", isCluster: true, passMap: passMap, expectEr: false}, + {name: "ConnectToCluster using password file without cluster mode", isCluster: false, passMap: passMap, expectEr: false}, + {name: "ConnectToCluster using password file witch cluster mode failed", isCluster: false, passMap: wrongPassMap, expectEr: true}, + } + for _, tst := range tsts { + t.Run(tst.name, func(t *testing.T) { + e, _ := NewRedisExporter(cluster_host, Options{ + SkipTLSVerification: true, + PasswordMap: tst.passMap, + IsCluster: tst.isCluster, + }) + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + _, err := e.connectToRedisCluster() + if !tst.expectEr && err != nil { + t.Errorf("Test Cluster connection Failed-connection error") + } else if tst.expectEr { + if !strings.Contains(buf.String(), "Cluster refresh failed:") { + t.Errorf("Test Cluster connection Failed error") + } + } + + }) + } + +} From 30cf05498839a3c05abd33fe0522bf46ca111924 Mon Sep 17 00:00:00 2001 From: chetratep Date: Wed, 11 Oct 2023 11:03:56 +0900 Subject: [PATCH 3/5] unittest --- .drone.yml | 10 ++++++++++ exporter/redis_test.go | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/.drone.yml b/.drone.yml index 1e49510a..f4156bcb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -74,6 +74,16 @@ services: pull: if-not-exists ports: [ 7000, 7001, 7002, 7003, 7004, 7005 ] + - name: redis-cluster-password: + image: bitnami/redis-cluster + environment: + - REDIS_PORT_NUMBER=7006 + - REDIS_PASSWORD=redis-password + - REDIS_CLUSTER_CREATOR=yes + - REDIS_NODES=redis-cluster-password:7006 + ports: + - 7006 + - name: tile38 image: tile38/tile38:latest pull: if-not-exists diff --git a/exporter/redis_test.go b/exporter/redis_test.go index 6471dc1b..44b8ebd7 100644 --- a/exporter/redis_test.go +++ b/exporter/redis_test.go @@ -123,14 +123,14 @@ func TestConnectToClusterUingPasswordFile(t *testing.T) { wrongPassMap := map[string]string{"redis://redis-cluster-password-wrong:7006": "redis-password"} tsts := []struct { - name string - isCluster bool - passMap map[string]string - expectEr bool + name string + isCluster bool + passMap map[string]string + refreshError bool }{ - {name: "ConnectToCluster using passord file witch cluster mode", isCluster: true, passMap: passMap, expectEr: false}, - {name: "ConnectToCluster using password file without cluster mode", isCluster: false, passMap: passMap, expectEr: false}, - {name: "ConnectToCluster using password file witch cluster mode failed", isCluster: false, passMap: wrongPassMap, expectEr: true}, + {name: "ConnectToCluster using passord file witch cluster mode", isCluster: true, passMap: passMap, refreshError: false}, + {name: "ConnectToCluster using password file without cluster mode", isCluster: false, passMap: passMap, refreshError: false}, + {name: "ConnectToCluster using password file witch cluster mode failed", isCluster: false, passMap: wrongPassMap, refreshError: true}, } for _, tst := range tsts { t.Run(tst.name, func(t *testing.T) { @@ -145,12 +145,11 @@ func TestConnectToClusterUingPasswordFile(t *testing.T) { log.SetOutput(os.Stderr) }() _, err := e.connectToRedisCluster() - if !tst.expectEr && err != nil { + if strings.Contains(buf.String(), "Cluster refresh failed:") && !tst.refreshError { + t.Errorf("Test Cluster connection Failed error") + } + if err != nil { t.Errorf("Test Cluster connection Failed-connection error") - } else if tst.expectEr { - if !strings.Contains(buf.String(), "Cluster refresh failed:") { - t.Errorf("Test Cluster connection Failed error") - } } }) From c703c08357e3c3094ba47804f2f761a140c64abd Mon Sep 17 00:00:00 2001 From: chetratep Date: Wed, 11 Oct 2023 11:44:31 +0900 Subject: [PATCH 4/5] fix --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index f4156bcb..05506cc5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -74,7 +74,7 @@ services: pull: if-not-exists ports: [ 7000, 7001, 7002, 7003, 7004, 7005 ] - - name: redis-cluster-password: + - name: redis-cluster-password image: bitnami/redis-cluster environment: - REDIS_PORT_NUMBER=7006 From 881ba7379dfe9713eddd64920b4efdfcee70dcf7 Mon Sep 17 00:00:00 2001 From: chetratep Date: Wed, 11 Oct 2023 12:07:54 +0900 Subject: [PATCH 5/5] .drong env --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index 05506cc5..9a8b2f84 100644 --- a/.drone.yml +++ b/.drone.yml @@ -77,10 +77,10 @@ services: - name: redis-cluster-password image: bitnami/redis-cluster environment: - - REDIS_PORT_NUMBER=7006 - - REDIS_PASSWORD=redis-password - - REDIS_CLUSTER_CREATOR=yes - - REDIS_NODES=redis-cluster-password:7006 + REDIS_PORT_NUMBER: 7006 + REDIS_PASSWORD: redis-password + REDIS_CLUSTER_CREATOR: yes + REDIS_NODES: redis-cluster-password:7006 ports: - 7006