From 13decb07cb7c8fdfc721e49a21ce3196775b8759 Mon Sep 17 00:00:00 2001 From: "alexey.makhonin" Date: Tue, 13 Jun 2023 13:19:30 +0300 Subject: [PATCH 1/2] Refactor proxy, add new retry flag, add tests Signed-off-by: alexey.makhonin --- Dockerfile | 18 ++- Makefile | 12 ++ README.md | 8 +- go.mod | 5 + go.sum | 2 + main.go | 134 +++++------------ pkg/master_resolver/master_resolver.go | 136 ++++++++++++++++++ pkg/master_resolver/master_resolver_test.go | 92 ++++++++++++ pkg/proxy/proxy.go | 91 ++++++++++++ pkg/utils/utils.go | 14 ++ test/docker-compose.yml | 63 ++++++++ test/redis_sentinel/Dockerfile | 5 + test/redis_sentinel/sentinel_entrypoint.sh | 15 ++ test/redis_sentinel_proxy_test/Dockerfile | 9 ++ test/redis_sentinel_proxy_test/go.mod | 22 +++ test/redis_sentinel_proxy_test/go.sum | 101 +++++++++++++ .../redis_sentinel_proxy_test.go | 126 ++++++++++++++++ test/test.sh | 10 ++ 18 files changed, 757 insertions(+), 106 deletions(-) create mode 100644 Makefile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/master_resolver/master_resolver.go create mode 100644 pkg/master_resolver/master_resolver_test.go create mode 100644 pkg/proxy/proxy.go create mode 100644 pkg/utils/utils.go create mode 100644 test/docker-compose.yml create mode 100644 test/redis_sentinel/Dockerfile create mode 100755 test/redis_sentinel/sentinel_entrypoint.sh create mode 100644 test/redis_sentinel_proxy_test/Dockerfile create mode 100644 test/redis_sentinel_proxy_test/go.mod create mode 100644 test/redis_sentinel_proxy_test/go.sum create mode 100644 test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go create mode 100755 test/test.sh diff --git a/Dockerfile b/Dockerfile index 0ec2c16..5aae26b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,18 @@ -FROM golang:1.17 AS builder +FROM golang:1.20 AS builder LABEL Andrey Kolashtov -ADD . /redis-sentinel-proxy/ -WORKDIR /redis-sentinel-proxy -RUN go mod init redis-sentinel-proxy && \ - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o redis-sentinel-proxy . +WORKDIR /src +COPY go.mod go.sum /src/ +RUN go mod download -FROM alpine:3.14 +COPY main.go Makefile /src/ +COPY pkg /src/pkg -COPY --from=builder /redis-sentinel-proxy/redis-sentinel-proxy /usr/local/bin/redis-sentinel-proxy +RUN make build CGO_ENABLED=0 GOOS=linux GOARCH=amd64 + +FROM alpine:3.17 + +COPY --from=builder /src/bin/redis-sentinel-proxy /usr/local/bin/redis-sentinel-proxy RUN apk --update --no-cache add redis ENTRYPOINT ["/usr/local/bin/redis-sentinel-proxy"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6022ae3 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) +CGO_ENABLED ?= 0 + +.PHONY: test +test: + go test -v ./... + cd test && ./test.sh + +.PHONY: build +build: + CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags '-s -w -extldflags "-static"' -o bin/redis-sentinel-proxy . \ No newline at end of file diff --git a/README.md b/README.md index 842619a..2255bff 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,11 @@ Small command utility that: * Proxies all tcp requests that it receives on `PORT` to that master - Usage: -`./redis-sentinel-proxy -listen IP:PORT -sentinel :SENTINEL_PORT -master NAME` +`./redis-sentinel-proxy -listen IP:PORT -sentinel :SENTINEL_PORT -master NAME --resolve-retries 10` + +testing +============ +- install `docker` and `docker-compose`. +- run `make test` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b9c1959 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/flant/redis-sentinel-proxy + +go 1.20 + +require golang.org/x/sync v0.2.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4051f4f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/main.go b/main.go index 5e4e13c..680758e 100644 --- a/main.go +++ b/main.go @@ -1,120 +1,60 @@ package main import ( - "errors" + "context" "flag" - "fmt" - "io" "log" "net" - "strings" - "time" -) - -var ( - masterAddr *net.TCPAddr - raddr *net.TCPAddr - saddr *net.TCPAddr + "os" + "os/signal" + "syscall" - localAddr = flag.String("listen", ":9999", "local address") - sentinelAddr = flag.String("sentinel", ":26379", "remote address") - masterName = flag.String("master", "", "name of the master redis node") + masterresolver "github.com/flant/redis-sentinel-proxy/pkg/master_resolver" + "github.com/flant/redis-sentinel-proxy/pkg/proxy" + "golang.org/x/sync/errgroup" ) func main() { + var ( + localAddr = ":9999" + sentinelAddr = ":26379" + masterName = "mymaster" + masterResolveRetries = 3 + ) + + flag.StringVar(&localAddr, "listen", localAddr, "local address") + flag.StringVar(&sentinelAddr, "sentinel", sentinelAddr, "remote address") + flag.StringVar(&masterName, "master", masterName, "name of the master redis node") + flag.IntVar(&masterResolveRetries, "resolve-retries", masterResolveRetries, "number of consecutive retries of the redis master node resolve") flag.Parse() - laddr, err := net.ResolveTCPAddr("tcp", *localAddr) - if err != nil { - log.Fatal("Failed to resolve local address: %s", err) - } - saddr, err = net.ResolveTCPAddr("tcp", *sentinelAddr) - if err != nil { - log.Fatal("Failed to resolve sentinel address: %s", err) - } - - go master() - - listener, err := net.ListenTCP("tcp", laddr) - if err != nil { - log.Fatal(err) - } - - for { - conn, err := listener.AcceptTCP() - if err != nil { - log.Println(err) - continue - } - - go proxy(conn, masterAddr) - } -} - -func master() { - var err error - for { - masterAddr, err = getMasterAddr(saddr, *masterName) - if err != nil { - log.Println(err) - } - time.Sleep(1 * time.Second) - } -} - -func pipe(r io.Reader, w io.WriteCloser) { - io.Copy(w, r) - w.Close() -} - -func proxy(local io.ReadWriteCloser, remoteAddr *net.TCPAddr) { - d := net.Dialer{Timeout: 1 * time.Second} - remote, err := d.Dial("tcp", remoteAddr.String()) - if err != nil { - log.Println(err) - local.Close() - return + if err := runProxying(localAddr, sentinelAddr, masterName, masterResolveRetries); err != nil { + log.Fatalln(err) } - go pipe(local, remote) - go pipe(remote, local) + log.Println("Exiting...") } -func getMasterAddr(sentinelAddress *net.TCPAddr, masterName string) (*net.TCPAddr, error) { - conn, err := net.DialTCP("tcp", nil, sentinelAddress) - if err != nil { - return nil, err - } - - defer conn.Close() +func runProxying(localAddr, sentinelAddr, masterName string, masterResolveRetries int) error { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer cancel() - conn.Write([]byte(fmt.Sprintf("sentinel get-master-addr-by-name %s\n", masterName))) + laddr := resolveTCPAddr(localAddr) + saddr := resolveTCPAddr(sentinelAddr) - b := make([]byte, 256) - _, err = conn.Read(b) - if err != nil { - log.Fatal(err) - } + masterAddrResolver := masterresolver.NewRedisMasterResolver(masterName, saddr, masterResolveRetries) + rsp := proxy.NewRedisSentinelProxy(laddr, masterAddrResolver) - parts := strings.Split(string(b), "\r\n") - - if len(parts) < 5 { - err = errors.New("Couldn't get master address from sentinel") - return nil, err - } + eg, ctx := errgroup.WithContext(ctx) + eg.Go(func() error { return masterAddrResolver.UpdateMasterAddressLoop(ctx) }) + eg.Go(func() error { return rsp.Run(ctx) }) - //getting the string address for the master node - stringaddr := fmt.Sprintf("%s:%s", parts[2], parts[4]) - addr, err := net.ResolveTCPAddr("tcp", stringaddr) + return eg.Wait() +} +func resolveTCPAddr(addr string) *net.TCPAddr { + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { - return nil, err - } - - //check that there's actually someone listening on that address - conn2, err := net.DialTCP("tcp", nil, addr) - if err == nil { - defer conn2.Close() + log.Fatalf("Failed resolving tcp address: %s", err) } - - return addr, err + return tcpAddr } diff --git a/pkg/master_resolver/master_resolver.go b/pkg/master_resolver/master_resolver.go new file mode 100644 index 0000000..fd6c5ea --- /dev/null +++ b/pkg/master_resolver/master_resolver.go @@ -0,0 +1,136 @@ +package masterresolver + +import ( + "context" + "errors" + "fmt" + "log" + "net" + "strings" + "sync" + "time" + + "github.com/flant/redis-sentinel-proxy/pkg/utils" +) + +type RedisMasterResolver struct { + masterName string + sentinelAddr *net.TCPAddr + retryOnMasterResolveFail int + + masterAddrLock *sync.RWMutex + initialMasterResolveLock chan struct{} + + masterAddr string +} + +func NewRedisMasterResolver(masterName string, sentinelAddr *net.TCPAddr, retryOnMasterResolveFail int) *RedisMasterResolver { + return &RedisMasterResolver{ + masterName: masterName, + sentinelAddr: sentinelAddr, + retryOnMasterResolveFail: retryOnMasterResolveFail, + masterAddrLock: &sync.RWMutex{}, + initialMasterResolveLock: make(chan struct{}), + } +} + +func (r *RedisMasterResolver) MasterAddress() string { + <-r.initialMasterResolveLock + + r.masterAddrLock.RLock() + defer r.masterAddrLock.RUnlock() + return r.masterAddr +} + +func (r *RedisMasterResolver) setMasterAddress(masterAddr *net.TCPAddr) { + r.masterAddrLock.Lock() + defer r.masterAddrLock.Unlock() + r.masterAddr = masterAddr.String() +} + +func (r *RedisMasterResolver) updateMasterAddress() error { + masterAddr, err := redisMasterFromSentinelAddr(r.sentinelAddr, r.masterName) + if err != nil { + log.Println(err) + return err + } + r.setMasterAddress(masterAddr) + return nil +} + +func (r *RedisMasterResolver) UpdateMasterAddressLoop(ctx context.Context) error { + if err := r.initialMasterAdressResolve(); err != nil { + return err + } + + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + + var err error + for errCount := 0; errCount <= r.retryOnMasterResolveFail; { + select { + case <-ctx.Done(): + return nil + case <-ticker.C: + } + + err = r.updateMasterAddress() + if err != nil { + errCount++ + } else { + errCount = 0 + } + } + return err +} + +func (r *RedisMasterResolver) initialMasterAdressResolve() error { + defer close(r.initialMasterResolveLock) + return r.updateMasterAddress() +} + +func redisMasterFromSentinelAddr(sentinelAddress *net.TCPAddr, masterName string) (*net.TCPAddr, error) { + conn, err := utils.TCPConnectWithTimeout(sentinelAddress.String()) + if err != nil { + return nil, fmt.Errorf("error connecting to sentinel: %w", err) + } + defer conn.Close() + + getMasterCommand := fmt.Sprintf("sentinel get-master-addr-by-name %s\n", masterName) + if _, err := conn.Write([]byte(getMasterCommand)); err != nil { + return nil, fmt.Errorf("error writing to sentinel: %w", err) + } + + b := make([]byte, 256) + if _, err := conn.Read(b); err != nil { + return nil, fmt.Errorf("error getting info from sentinel: %w", err) + } + + parts := strings.Split(string(b), "\r\n") + + if len(parts) < 5 { + return nil, errors.New("couldn't get master address from sentinel") + } + + // getting the string address for the master node + stringaddr := fmt.Sprintf("%s:%s", parts[2], parts[4]) + addr, err := net.ResolveTCPAddr("tcp", stringaddr) + if err != nil { + return nil, fmt.Errorf("error resolving redis master: %w", err) + } + + // check that there's actually someone listening on that address + if err := checkTCPConnect(addr); err != nil { + return nil, fmt.Errorf("error checking redis master: %w", err) + } + return addr, nil +} + +func checkTCPConnect(addr *net.TCPAddr) error { + conn, err := utils.TCPConnectWithTimeout(addr.String()) + if err != nil { + return err + } + defer conn.Close() + return nil +} diff --git a/pkg/master_resolver/master_resolver_test.go b/pkg/master_resolver/master_resolver_test.go new file mode 100644 index 0000000..8ac4a57 --- /dev/null +++ b/pkg/master_resolver/master_resolver_test.go @@ -0,0 +1,92 @@ +package masterresolver + +import ( + "bytes" + "log" + "net" + "reflect" + "strconv" + "strings" + "testing" +) + +func Test_redisMasterFromSentinelAddr(t *testing.T) { + type args struct { + sentinelAddress *net.TCPAddr + masterName string + } + + mockServerAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12700} + tests := []struct { + name string + args args + want *net.TCPAddr + wantErr bool + }{ + { + name: "all is ok", + want: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12700}, + args: args{ + sentinelAddress: mockServerAddr, + masterName: "test-master", + }, + }, + { + name: "fail with error", + wantErr: true, + args: args{ + sentinelAddress: mockServerAddr, + masterName: "bad-master", + }, + }, + } + + go mockSentinelServer(mockServerAddr) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := redisMasterFromSentinelAddr(tt.args.sentinelAddress, tt.args.masterName) + if (err != nil) != tt.wantErr { + t.Errorf("redisMasterFromSentinelAddr() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("redisMasterFromSentinelAddr() = %v, want %v", got, tt.want) + } + }) + } +} + +func mockSentinelServer(addr *net.TCPAddr) { + listener, err := net.ListenTCP("tcp", addr) + if err != nil { + return + } + for { + conn, err := listener.AcceptTCP() + if err != nil || conn == nil { + log.Println("could not accept connection") + } + log.Println("accepted test connection") + testAccept(conn, addr) + } +} + +func testAccept(conn net.Conn, addr *net.TCPAddr) { + defer conn.Close() + out := make([]byte, 256) + if _, err := conn.Read(out); err != nil { + return + } + + var masterAddr string + if bytes.HasPrefix(out, []byte("sentinel get-master-addr-by-name test-master")) { + masterAddr = strings.Join([]string{"tralala", "tralala", addr.IP.String(), "tralala", strconv.Itoa(addr.Port)}, "\r\n") + } else { + masterAddr = strings.Join([]string{"tralala", "tralala", addr.IP.String(), "tralala", "40"}, "\r\n") + } + + if _, err := conn.Write([]byte(masterAddr)); err != nil { + log.Println("could not write payload to TCP server:", err) + } +} diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go new file mode 100644 index 0000000..791d828 --- /dev/null +++ b/pkg/proxy/proxy.go @@ -0,0 +1,91 @@ +package proxy + +import ( + "context" + "io" + "log" + "net" + + "github.com/flant/redis-sentinel-proxy/pkg/utils" + "golang.org/x/sync/errgroup" +) + +type masterResolver interface { + MasterAddress() string +} + +type RedisSentinelProxy struct { + localAddr *net.TCPAddr + masterResolver masterResolver +} + +func NewRedisSentinelProxy(localAddr *net.TCPAddr, mResolver masterResolver) *RedisSentinelProxy { + return &RedisSentinelProxy{ + localAddr: localAddr, + masterResolver: mResolver, + } +} + +func (r *RedisSentinelProxy) Run(bigCtx context.Context) error { + listener, err := net.ListenTCP("tcp", r.localAddr) + if err != nil { + return err + } + + errGr, ctx := errgroup.WithContext(bigCtx) + errGr.Go(func() error { return r.runListenLoop(ctx, listener) }) + errGr.Go(func() error { return closeListenerByContext(ctx, listener) }) + + return errGr.Wait() +} + +func (r *RedisSentinelProxy) runListenLoop(ctx context.Context, listener *net.TCPListener) error { + log.Println("Waiting for connections...") + for { + if err := ctx.Err(); err != nil { + return nil + } + + conn, err := listener.AcceptTCP() + if err != nil { + log.Println(err) + continue + } + + go r.proxy(conn) + } +} + +func (r *RedisSentinelProxy) proxy(incoming io.ReadWriteCloser) { + defer incoming.Close() + + remote, err := utils.TCPConnectWithTimeout(r.masterResolver.MasterAddress()) + if err != nil { + log.Printf("Error connecting to master: %s", err) + return + } + defer remote.Close() + + sigChan := make(chan struct{}) + defer close(sigChan) + + go pipe(remote, incoming, sigChan) + go pipe(incoming, remote, sigChan) + + <-sigChan + <-sigChan +} + +func closeListenerByContext(ctx context.Context, listener *net.TCPListener) error { + defer listener.Close() + <-ctx.Done() + return nil +} + +func pipe(w io.WriteCloser, r io.Reader, sigChan chan<- struct{}) { + defer func() { sigChan <- struct{}{} }() + + if _, err := io.Copy(w, r); err != nil { + log.Printf("Error writing content: %s", err) + } +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go new file mode 100644 index 0000000..5afb8c3 --- /dev/null +++ b/pkg/utils/utils.go @@ -0,0 +1,14 @@ +package utils + +import ( + "net" + "time" +) + +func TCPConnectWithTimeout(addr string) (net.Conn, error) { + remote, err := net.DialTimeout("tcp", addr, time.Second) + if err != nil { + return nil, err + } + return remote, nil +} diff --git a/test/docker-compose.yml b/test/docker-compose.yml new file mode 100644 index 0000000..0f0d00e --- /dev/null +++ b/test/docker-compose.yml @@ -0,0 +1,63 @@ +version: '3.9' + +services: + redis-sentinel-proxy-tests: + build: + context: ./redis_sentinel_proxy_test + command: ["go test ./..."] + depends_on: + - redis-sentinel-proxy + networks: + - redis-test + + redis-sentinel-proxy: + build: + context: ../ + command: + - --listen=:9999 + - --sentinel=redis-sentinel:26379 + - --master=redismaster + - --resolve-retries=10 + ports: + - 9999:9999 + depends_on: + - redis-master + - redis-sentinel + networks: + - redis-test + + redis-master: + image: redis:7.0.5 + networks: + - redis-test + ports: + - 6379:6379 + + redis-slave: + image: redis:7.0.5 + command: + - redis-server + - --slaveof + - redis-master + - "6379" + networks: + - redis-test + depends_on: + - redis-master + deploy: + replicas: 2 + + redis-sentinel: + build: + context: ./redis_sentinel + networks: + - redis-test + depends_on: + - redis-master + deploy: + replicas: 1 + + +networks: + redis-test: + name: redis-test diff --git a/test/redis_sentinel/Dockerfile b/test/redis_sentinel/Dockerfile new file mode 100644 index 0000000..64998c9 --- /dev/null +++ b/test/redis_sentinel/Dockerfile @@ -0,0 +1,5 @@ +FROM redis:7.0.5 +WORKDIR /redis +COPY sentinel_entrypoint.sh /usr/local/bin/ +EXPOSE 26379 +ENTRYPOINT ["sentinel_entrypoint.sh"] diff --git a/test/redis_sentinel/sentinel_entrypoint.sh b/test/redis_sentinel/sentinel_entrypoint.sh new file mode 100755 index 0000000..e5065c1 --- /dev/null +++ b/test/redis_sentinel/sentinel_entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +cat < /dev/null +port 26379 + +dir /tmp + +sentinel resolve-hostnames yes +sentinel monitor redismaster redis-master 6379 2 +sentinel down-after-milliseconds redismaster 1000 +sentinel parallel-syncs redismaster 1 +sentinel failover-timeout redismaster 1000 +EOF + +redis-server /redis/sentinel.conf --sentinel \ No newline at end of file diff --git a/test/redis_sentinel_proxy_test/Dockerfile b/test/redis_sentinel_proxy_test/Dockerfile new file mode 100644 index 0000000..763abc8 --- /dev/null +++ b/test/redis_sentinel_proxy_test/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:1.20 + +WORKDIR /src +COPY go.mod go.sum /src/ +RUN go mod download + +COPY redis_sentinel_proxy_test.go /src/ + +ENTRYPOINT ["/bin/bash", "-c"] diff --git a/test/redis_sentinel_proxy_test/go.mod b/test/redis_sentinel_proxy_test/go.mod new file mode 100644 index 0000000..38fcc9c --- /dev/null +++ b/test/redis_sentinel_proxy_test/go.mod @@ -0,0 +1,22 @@ +module redis_sentinel_proxy_tests + +go 1.20 + +require ( + github.com/onsi/ginkgo v1.16.5 + github.com/onsi/gomega v1.27.7 + github.com/redis/go-redis/v9 v9.0.5 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fsnotify/fsnotify v1.4.9 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/nxadm/tail v1.4.8 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/test/redis_sentinel_proxy_test/go.sum b/test/redis_sentinel_proxy_test/go.sum new file mode 100644 index 0000000..bb32066 --- /dev/null +++ b/test/redis_sentinel_proxy_test/go.sum @@ -0,0 +1,101 @@ +github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= +github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU= +github.com/onsi/gomega v1.27.7/go.mod h1:1p8OOlwo2iUUDsHnOrjE5UKYJ+e3W8eQ3qSlRahPmr4= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= +github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go b/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go new file mode 100644 index 0000000..77d2bf2 --- /dev/null +++ b/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go @@ -0,0 +1,126 @@ +package redis_sentinel_proxy_test + +import ( + "context" + "testing" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/redis/go-redis/v9" +) + +func Test(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "") +} + +var _ = Describe("redis-sentinel-proxy :: tests", func() { + var rspClient, rmClient *redis.Client + BeforeEach(func() { + rspClient = connect("redis-sentinel-proxy:9999") + rmClient = connect("redis-master:6379") + }) + + AfterEach(func() { + defer rmClient.Close() + defer rmClient.Close() + }) + + Context("ping-pong RSP", func() { + It("returns PONG", func() { + ping(rspClient) + }) + }) + + Context("ping-pong master", func() { + It("returns PONG", func() { + ping(rmClient) + }) + }) + + Context("set from RSP", func() { + BeforeEach(func() { + setKey(rmClient, "test-key-rsp", "test-value-1") + }) + + AfterEach(func() { + delKey(rmClient, "test-key-rsp") + }) + + It("returns key from RSP", func() { + getKey(rspClient, "test-key-rsp", "test-value-1") + }) + + It("returns key from master", func() { + getKey(rmClient, "test-key-rsp", "test-value-1") + }) + }) + + Context("set from master", func() { + BeforeEach(func() { + setKey(rmClient, "test-key-rm", "test-value-2") + }) + + AfterEach(func() { + delKey(rmClient, "test-key-rm") + }) + + It("returns key from RSP", func() { + getKey(rspClient, "test-key-rm", "test-value-2") + }) + + It("returns key from master", func() { + getKey(rmClient, "test-key-rm", "test-value-2") + }) + }) + +}) + +func connect(addr string) *redis.Client { + return redis.NewClient(&redis.Options{ + Addr: addr, + Password: "", + DB: 0, + }) +} + +func ping(client *redis.Client) { + ctx, cancel := ctxTimeout() + defer cancel() + ans, err := client.Ping(ctx).Result() + errAnswer(ans, "PONG", err) +} + +func setKey(client *redis.Client, key, value string) { + ctx, cancel := ctxTimeout() + defer cancel() + + ans, err := client.Set(ctx, key, value, time.Hour).Result() + errAnswer(ans, "OK", err) +} + +func getKey(client *redis.Client, key, value string) { + ctx, cancel := ctxTimeout() + defer cancel() + + ans, err := client.Get(ctx, key).Result() + errAnswer(ans, value, err) +} + +func delKey(client *redis.Client, key string) { + ctx, cancel := ctxTimeout() + defer cancel() + + ans, err := client.Del(ctx, key).Result() + errAnswer(ans, int64(1), err) +} + +func ctxTimeout() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.Background(), time.Second*3) +} + +func errAnswer(ans, expected interface{}, err error) { + Expect(err).NotTo(HaveOccurred()) + Expect(ans).To(Equal(expected)) +} diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..797c42e --- /dev/null +++ b/test/test.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail + +function stop(){ + docker-compose down > dev/null 2>&1 +} +trap stop ERR SIGINT SIGTERM SIGHUP SIGQUIT + +{ docker-compose up --build --exit-code-from redis-sentinel-proxy-tests && echo "tests successful"; } || echo "tests failed" From 05db9e41e358287e8dcf6d5a86dd4d0fbca5503d Mon Sep 17 00:00:00 2001 From: "alexey.makhonin" Date: Wed, 21 Jun 2023 07:47:00 +0300 Subject: [PATCH 2/2] Fix tests defer Signed-off-by: alexey.makhonin --- test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go b/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go index 77d2bf2..0b44008 100644 --- a/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go +++ b/test/redis_sentinel_proxy_test/redis_sentinel_proxy_test.go @@ -24,7 +24,7 @@ var _ = Describe("redis-sentinel-proxy :: tests", func() { AfterEach(func() { defer rmClient.Close() - defer rmClient.Close() + defer rspClient.Close() }) Context("ping-pong RSP", func() {