2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-05 17:10:35 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
// Package testredis is package for starting a redis test server
|
|
|
|
package testredis
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-10-29 11:58:36 +00:00
|
|
|
"context"
|
2018-09-05 17:10:35 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-10-29 11:58:36 +00:00
|
|
|
"runtime/pprof"
|
2018-09-05 17:10:35 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-02-06 12:56:23 +00:00
|
|
|
"github.com/alicebob/miniredis/v2"
|
2018-09-05 17:10:35 +01:00
|
|
|
"github.com/go-redis/redis"
|
2018-10-16 12:43:44 +01:00
|
|
|
|
2020-03-03 09:31:51 +00:00
|
|
|
"storj.io/common/processgroup"
|
2018-09-05 17:10:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fallbackAddr = "localhost:6379"
|
|
|
|
fallbackPort = 6379
|
|
|
|
)
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
// Server represents a redis server.
|
|
|
|
type Server interface {
|
|
|
|
Addr() string
|
|
|
|
Close() error
|
2021-03-09 17:42:10 +00:00
|
|
|
// FastForward is a function for enforce the TTL of keys in
|
2020-12-21 17:27:12 +00:00
|
|
|
// implementations what they have not exercise the expiration by themselves
|
|
|
|
// (e.g. Minitredis). This method is a no-op in implementations which support
|
|
|
|
// the expiration as usual.
|
|
|
|
//
|
|
|
|
// All the keys whose TTL minus d become <= 0 will be removed.
|
2021-03-09 17:42:10 +00:00
|
|
|
FastForward(d time.Duration)
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
func freeport() (addr string, port int) {
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return fallbackAddr, fallbackPort
|
|
|
|
}
|
|
|
|
|
|
|
|
netaddr := listener.Addr().(*net.TCPAddr)
|
|
|
|
addr = netaddr.String()
|
|
|
|
port = netaddr.Port
|
|
|
|
_ = listener.Close()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
return addr, port
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Start starts a redis-server when available, otherwise falls back to miniredis.
|
2020-10-29 11:58:36 +00:00
|
|
|
func Start(ctx context.Context) (Server, error) {
|
|
|
|
server, err := Process(ctx)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println("failed to start redis-server: ", err)
|
2020-10-29 11:58:36 +00:00
|
|
|
return Mini(ctx)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
2020-01-31 18:28:42 +00:00
|
|
|
return server, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Process starts a redis-server test process.
|
2020-10-29 11:58:36 +00:00
|
|
|
func Process(ctx context.Context) (Server, error) {
|
2018-09-05 17:10:35 +01:00
|
|
|
tmpdir, err := ioutil.TempDir("", "storj-redis")
|
|
|
|
if err != nil {
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// find a suitable port for listening
|
2020-01-31 18:28:42 +00:00
|
|
|
addr, port := freeport()
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
// write a configuration file, because redis doesn't support flags
|
|
|
|
confpath := filepath.Join(tmpdir, "test.conf")
|
|
|
|
arguments := []string{
|
|
|
|
"daemonize no",
|
|
|
|
"bind 127.0.0.1",
|
|
|
|
"port " + strconv.Itoa(port),
|
|
|
|
"timeout 0",
|
|
|
|
"databases 2",
|
|
|
|
"dbfilename dump.rdb",
|
|
|
|
"dir " + tmpdir,
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := strings.Join(arguments, "\n") + "\n"
|
|
|
|
err = ioutil.WriteFile(confpath, []byte(conf), 0755)
|
|
|
|
if err != nil {
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// start the process
|
|
|
|
cmd := exec.Command("redis-server", confpath)
|
2018-09-11 14:57:12 +01:00
|
|
|
processgroup.Setup(cmd)
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
read, write, err := os.Pipe()
|
|
|
|
if err != nil {
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Stdout = write
|
|
|
|
if err := cmd.Start(); err != nil {
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
cleanup := func() {
|
2018-09-11 14:57:12 +01:00
|
|
|
processgroup.Kill(cmd)
|
2018-09-05 17:10:35 +01:00
|
|
|
_ = os.RemoveAll(tmpdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for redis to become ready
|
|
|
|
waitForReady := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
// wait for the message that looks like
|
|
|
|
// v3 "The server is now ready to accept connections on port 6379"
|
|
|
|
// v4 "Ready to accept connections"
|
|
|
|
scanner := bufio.NewScanner(read)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Contains(line, "to accept") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waitForReady <- scanner.Err()
|
|
|
|
_, _ = io.Copy(ioutil.Discard, read)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-waitForReady:
|
|
|
|
if err != nil {
|
|
|
|
cleanup()
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, err
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
cleanup()
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, errors.New("redis timeout")
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// test whether we can actually connect
|
|
|
|
if err := pingServer(addr); err != nil {
|
|
|
|
cleanup()
|
2020-01-31 18:28:42 +00:00
|
|
|
return nil, fmt.Errorf("unable to ping: %v", err)
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
return &process{addr, cleanup}, nil
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
type process struct {
|
|
|
|
addr string
|
|
|
|
close func()
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
func (process *process) Addr() string {
|
|
|
|
return process.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (process *process) Close() error {
|
|
|
|
process.close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
func (process *process) FastForward(_ time.Duration) {}
|
2020-01-31 18:28:42 +00:00
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
func pingServer(addr string) error {
|
|
|
|
client := redis.NewClient(&redis.Options{Addr: addr, DB: 1})
|
|
|
|
defer func() { _ = client.Close() }()
|
|
|
|
return client.Ping().Err()
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Mini starts miniredis server.
|
2020-10-29 11:58:36 +00:00
|
|
|
func Mini(ctx context.Context) (Server, error) {
|
|
|
|
var server *miniredis.Miniredis
|
|
|
|
var err error
|
|
|
|
|
|
|
|
pprof.Do(ctx, pprof.Labels("db", "miniredis"), func(ctx context.Context) {
|
|
|
|
server, err = miniredis.Run()
|
|
|
|
})
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|
2020-01-31 18:28:42 +00:00
|
|
|
return &miniserver{server}, nil
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:28:42 +00:00
|
|
|
type miniserver struct {
|
|
|
|
*miniredis.Miniredis
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
2019-10-31 17:27:38 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes the underlying miniredis server.
|
2020-01-31 18:28:42 +00:00
|
|
|
func (s *miniserver) Close() error {
|
|
|
|
s.Miniredis.Close()
|
2019-10-31 17:27:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
|
2021-03-09 17:42:10 +00:00
|
|
|
func (s *miniserver) FastForward(d time.Duration) {
|
|
|
|
s.Miniredis.FastForward(d)
|
2020-12-21 17:27:12 +00:00
|
|
|
}
|