Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cluster refresh failed when using pasword file with isCluster mode #836

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions contrib/docker-compose-for-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "" {
Expand All @@ -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},
Expand Down
45 changes: 45 additions & 0 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package exporter

import (
"bytes"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

func TestHostVariations(t *testing.T) {
Expand Down Expand Up @@ -111,3 +113,46 @@ 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
refreshError bool
}{
{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) {
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 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")
}

})
}

}
Loading