Skip to content

Commit

Permalink
test TLS-enabled Redis/Valkey instance
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed Oct 14, 2024
1 parent 68bd63f commit 3e3be31
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ jobs:
make test
- name: Run tests - valkey 7
- name: Run tests - valkey 8
env:
LOG_LEVEL: "info"
TEST_REDIS_URI: "redis://localhost:16384"
TEST_REDIS_URI: "redis://localhost:16382"
TEST_VALKEY8_TLS_URI: "valkeys://localhost:16386"
TEST_PWD_REDIS_URI: "redis://:redis-password@localhost:16380"
run: |
go test -v -race -p 1 ./...
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ test:

TEST_VALKEY7_URI="valkey://localhost:16384" \
TEST_VALKEY8_URI="valkey://localhost:16382" \

TEST_VALKEY8_TLS_URI="valkeys://localhost:16386" \
TEST_REDIS7_TLS_URI="rediss://localhost:16386" \
TEST_REDIS_URI="redis://localhost:16385" \
TEST_REDIS7_URI="redis://localhost:16385" \
TEST_REDIS5_URI="redis://localhost:16383" \
Expand Down
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,38 @@ services:
- "16385:6379"
- "6379:6379"

redis74-tls:
image: redis:7.4
volumes:
- ./contrib/tls:/tls
command: |
valkey-server --enable-debug-command yes --protected-mode no
--tls-port 6379 --port 0
--tls-cert-file /tls/redis.crt
--tls-key-file /tls/redis.key
--tls-ca-cert-file /tls/ca.crt
ports:
- "16387:6379"

valkey8:
image: valkey/valkey:8
command: "valkey-server --enable-debug-command yes --protected-mode no"
ports:
- "16382:6379"

valkey8-tls:
image: valkey/valkey:8
volumes:
- ./contrib/tls:/tls
command: |
valkey-server --enable-debug-command yes --protected-mode no
--tls-port 6379 --port 0
--tls-cert-file /tls/redis.crt
--tls-key-file /tls/redis.key
--tls-ca-cert-file /tls/ca.crt
ports:
- "16386:6379"

valkey7:
image: valkey/valkey:7.2
command: "valkey-server --enable-debug-command yes --protected-mode no"
Expand Down
13 changes: 11 additions & 2 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,20 @@ type Options struct {
}

// NewRedisExporter returns a new exporter of Redis metrics.
func NewRedisExporter(redisURI string, opts Options) (*Exporter, error) {
func NewRedisExporter(uri string, opts Options) (*Exporter, error) {
log.Debugf("NewRedisExporter options: %#v", opts)

switch {
case strings.HasPrefix(uri, "valkey://"):
uri = strings.Replace(uri, "valkey://", "redis://", 1)
case strings.HasPrefix(uri, "valkeys://"):
uri = strings.Replace(uri, "valkeys://", "rediss://", 1)
}

log.Debugf("NewRedisExporter = using redis uri: %s", uri)

e := &Exporter{
redisAddr: redisURI,
redisAddr: uri,
options: opts,
namespace: opts.Namespace,

Expand Down
4 changes: 2 additions & 2 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func setupDBKeys(t *testing.T, uri string) error {
}

func setupDBKeysCluster(t *testing.T, uri string) error {
e := Exporter{redisAddr: uri}
e, _ := NewRedisExporter(uri, Options{})
c, err := e.connectToRedisCluster()
if err != nil {
return err
Expand Down Expand Up @@ -193,7 +193,7 @@ func deleteKeysFromDB(t *testing.T, addr string) error {
}

func deleteKeysFromDBCluster(addr string) error {
e := Exporter{redisAddr: addr}
e, _ := NewRedisExporter(addr, Options{})
c, err := e.connectToRedisCluster()
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions exporter/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ func TestSimultaneousMetricsHttpRequests(t *testing.T) {
os.Getenv("TEST_REDIS_URI"),
os.Getenv("TEST_REDIS_2_8_URI"),

os.Getenv("TEST_REDIS7_URI"),
os.Getenv("TEST_REDIS7_TLS_URI"),

os.Getenv("TEST_VALKEY8_URI"),

os.Getenv("TEST_KEYDB01_URI"),
os.Getenv("TEST_KEYDB02_URI"),

Expand Down
7 changes: 0 additions & 7 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) {
uri = "redis://" + uri
}

switch {
case strings.HasPrefix(uri, "valkey://"):
uri = strings.Replace(uri, "valkey://", "redis://", 1)
case strings.HasPrefix(uri, "valkeys://"):
uri = strings.Replace(uri, "valkeys://", "rediss://", 1)
}

options, err := e.configureOptions(uri)
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestHostVariations(t *testing.T) {
}
}

// todo: also test valkeys://...
func TestValkeyScheme(t *testing.T) {
host := os.Getenv("TEST_VALKEY8_URI")

Expand Down
55 changes: 55 additions & 0 deletions exporter/tls_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package exporter

import (
"os"
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
)

func TestCreateClientTLSConfig(t *testing.T) {
Expand Down Expand Up @@ -40,6 +44,57 @@ func TestCreateClientTLSConfig(t *testing.T) {
}
}

func TestValkeyTLSScheme(t *testing.T) {

for _, host := range []string{
os.Getenv("TEST_REDIS7_TLS_URI"),
os.Getenv("TEST_VALKEY8_TLS_URI"),
} {

e, _ := NewRedisExporter(host,
Options{
SkipTLSVerification: true,
ClientCertFile: "../contrib/tls/redis.crt",
ClientKeyFile: "../contrib/tls/redis.key",
},
)
c, err := e.connectToRedis()
if err != nil {
t.Fatalf("connectToRedis() err: %s", err)
}

if _, err := c.Do("PING", ""); err != nil {
t.Errorf("PING err: %s", err)
}

c.Close()

chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()

tsts := []struct {
in string
found bool
}{
{in: "db_keys"},
{in: "commands_total"},
{in: "total_connections_received"},
{in: "used_memory"},
}
for m := range chM {
desc := m.Desc().String()
for i := range tsts {
if strings.Contains(desc, tsts[i].in) {
tsts[i].found = true
}
}
}
}
}

func TestCreateServerTLSConfig(t *testing.T) {
e := getTestExporter()

Expand Down

0 comments on commit 3e3be31

Please sign in to comment.