From 8b1c13e0376bc43a2443a0b312567718b4afbcd9 Mon Sep 17 00:00:00 2001 From: Arnaud Rebillout Date: Sat, 3 Feb 2024 22:50:47 +0700 Subject: [PATCH] chore: run test server with enable-debug-command (#654) Run the Redis test server with --enable-debug-command local on Redis >= 7.0 which disables MODULE and DEBUG commands by default for better security. Without this, some unit tests fail on later Redis versions. --- redis/test_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/redis/test_test.go b/redis/test_test.go index fcbb7bd1..11c34734 100644 --- a/redis/test_test.go +++ b/redis/test_test.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "os" "os/exec" + "regexp" "strconv" "strings" "sync" @@ -54,7 +55,51 @@ type Server struct { done chan struct{} } +type version struct { + major int + minor int + patch int +} + +func redisServerVersion() (*version, error) { + out, err := exec.Command("redis-server", "--version").Output() + if err != nil { + return nil, fmt.Errorf("server version: %w", err) + } + + ver := string(out) + re := regexp.MustCompile(`v=(\d+)\.(\d+)\.(\d+)`) + match := re.FindStringSubmatch(ver) + if len(match) != 4 { + return nil, fmt.Errorf("no server version found in %q", ver) + } + + var v version + if v.major, err = strconv.Atoi(match[1]); err != nil { + return nil, fmt.Errorf("invalid major version %q", match[1]) + } + + if v.minor, err = strconv.Atoi(match[2]); err != nil { + return nil, fmt.Errorf("invalid minor version %q", match[2]) + } + + if v.patch, err = strconv.Atoi(match[3]); err != nil { + return nil, fmt.Errorf("invalid patch version %q", match[3]) + } + + return &v, nil +} + func NewServer(name string, args ...string) (*Server, error) { + version, err := redisServerVersion() + if err != nil { + return nil, err + } + + if version.major >= 7 { + args = append(args, "--enable-debug-command", "local") + } + s := &Server{ name: name, cmd: exec.Command(*serverPath, args...),