forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ephemeral.go
122 lines (105 loc) · 2.83 KB
/
ephemeral.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
// Start a redis server in memory-only mode on a random port.
import (
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
)
const (
localSrc = "./redis_src/"
executable = "redis-server"
)
type ephemeral exec.Cmd
// Redis starts a memory-only redis on a random port. Will panic if that
// doesn't work.
// Returns something which you'll have to Close(), and a string to give to Dial()
func Redis() (*ephemeral, string) {
return runRedis("")
}
// RedisAuth starts a memory-only redis on a random port. The redis has
// authentication enabled. See Redis()
func RedisAuth(passwd string) (*ephemeral, string) {
return runRedis(fmt.Sprintf("requirepass %s", passwd))
}
// RedisUserAuth starts a memory-only redis on a random port. The redis has
// ACL rules enabled. See Redis()
func RedisUserAuth(users map[string]string) (*ephemeral, string) {
acls := "user default on -@all +hello\n"
for user, pass := range users {
acls += fmt.Sprintf("user %s on +@all ~* >%s\n", user, pass)
}
return runRedis(acls)
}
// RedisCluster starts a memory-only redis on a random port. The redis has
// cluster mode enabled. See Redis()
func RedisCluster() (*ephemeral, string) {
return runRedis("cluster-enabled yes\ncluster-config-file nodes.conf")
}
func RedisTLS() (*ephemeral, string) {
port := arbitraryPort()
e, _ := runRedis(fmt.Sprintf(
`
tls-port %d
tls-cert-file ../testdata/server.crt
tls-key-file ../testdata/server.key
tls-ca-cert-file ../testdata/client.crt
`,
port))
addr := fmt.Sprintf("127.0.0.1:%d", port)
return e, addr
}
func runRedis(extraConfig string) (*ephemeral, string) {
port := arbitraryPort()
// we prefer the executable from ./redis_src, if any. See ./get_redis.sh
absSrc, err := filepath.Abs(localSrc)
if err != nil {
panic(err)
}
os.Setenv("PATH", fmt.Sprintf("%s:%s", absSrc, os.Getenv("PATH")))
c := exec.Command(executable, "-")
stdin, err := c.StdinPipe()
if err != nil {
panic(err)
}
fmt.Fprintf(stdin, "port %d\nbind 127.0.0.1\nappendonly no\n%s", port, extraConfig)
stdin.Close()
if err := c.Start(); err != nil {
panic(err)
}
addr := fmt.Sprintf("127.0.0.1:%d", port)
// Wait until the thing is ready
timeout := time.Now().Add(1 * time.Second)
for time.Now().Before(timeout) {
conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
e := ephemeral(*c)
return &e, addr
}
time.Sleep(3 * time.Millisecond)
}
panic(fmt.Sprintf("No connection on port %d", port))
}
func (e *ephemeral) Close() {
((*exec.Cmd)(e)).Process.Kill()
((*exec.Cmd)(e)).Wait()
}
// arbitraryPort returns a non-used port.
func arbitraryPort() int {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer l.Close()
addr := l.Addr().String()
_, port, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
p, _ := strconv.Atoi(port)
return p
}