-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathredis_test.go
187 lines (159 loc) · 6.38 KB
/
redis_test.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"testing"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)
func TestGetRedisConfig(t *testing.T) {
// test for defaults
c, err := getRedisConfig("", "", "", "", "", "")
if err != nil {
assert.Fail(t, "configuration failed with:%v", err)
}
assert.Equal(t, false, c.usetls, "usetls expected to be false by default")
assert.Equal(t, true, c.tlsskipverify, "tlsskipverify expected to be true by default")
assert.Equal(t, 0, c.db, "db expected to be 0 by default")
assert.Equal(t, "", c.password, "password expected to be '' by default")
assert.Equal(t, "logstash", c.key, "key expected to be 'logstash' by default")
assert.Equal(t, 1, len(c.hosts), "it is expected to have one host by default")
assert.Equal(t, "127.0.0.1", c.hosts[0].hostname, "it is expected to have 127.0.0.1 as host by default")
assert.Equal(t, 6379, c.hosts[0].port, "it is expected to have 6379 as port by default")
assert.Equal(t, "hosts:[{127.0.0.1 6379}] db:0 usetls:false tlsskipverify:true key:logstash", c.String())
// valid configuration parameter passed
c, err = getRedisConfig("", "geheim", "1", "true", "false", "elastic")
if err != nil {
assert.Fail(t, "configuration failed with:%v", err)
}
assert.Equal(t, true, c.usetls, "usetls expected to be true")
assert.Equal(t, false, c.tlsskipverify, "tlsskipverify expected to be false")
assert.Equal(t, 1, c.db, "db expected to be 1")
assert.Equal(t, "geheim", c.password, "password expected to be 'geheim'")
assert.Equal(t, "elastic", c.key, "key expected to be 'elastic'")
// valid configuration for hosts without port
c, err = getRedisConfig("1.2.3.4", "", "", "", "", "")
if err != nil {
assert.Fail(t, "configuration failed with:%v", err)
}
assert.Equal(t, 1, len(c.hosts), "it is expected to have one host")
assert.Equal(t, "1.2.3.4", c.hosts[0].hostname, "it is expected to have 1.2.3.4")
assert.Equal(t, 6379, c.hosts[0].port, "it is expected to have 6379")
// valid configuration for hosts with port
c, err = getRedisConfig("1.2.3.4:42", "", "", "", "", "")
if err != nil {
assert.Fail(t, "configuration failed with:%v", err)
}
assert.Equal(t, 1, len(c.hosts), "it is expected to have one host")
assert.Equal(t, "1.2.3.4", c.hosts[0].hostname, "it is expected to have 1.2.3.4")
assert.Equal(t, 42, c.hosts[0].port, "it is expected to have 6379")
// valid configuration for hosts with port
c, err = getRedisConfig("1.2.3.4:42 1.2.3.5", "", "", "", "", "")
if err != nil {
assert.Fail(t, "configuration failed with:%v", err)
}
assert.Equal(t, 2, len(c.hosts), "it is expected to have two host")
assert.Equal(t, "1.2.3.4", c.hosts[0].hostname, "it is expected to have 1.2.3.4")
assert.Equal(t, 42, c.hosts[0].port, "it is expected to have 42")
assert.Equal(t, "1.2.3.5", c.hosts[1].hostname, "it is expected to have 1.2.3.5")
assert.Equal(t, 6379, c.hosts[1].port, "it is expected to have 6379")
assert.Equal(t, "hosts:[{1.2.3.4 42} {1.2.3.5 6379}] db:0 usetls:false tlsskipverify:true key:logstash", c.String())
// invalid configurations
_, err = getRedisConfig("", "", "A", "", "", "")
if err != nil {
assert.Equal(t, "db must be a integer: strconv.Atoi: parsing \"A\": invalid syntax", err.Error())
}
_, err = getRedisConfig("", "", "", "xxx", "", "")
if err != nil {
assert.Equal(t, "usetls must be a bool: strconv.ParseBool: parsing \"xxx\": invalid syntax", err.Error())
}
_, err = getRedisConfig("", "", "", "", "xxx", "")
if err != nil {
assert.Equal(t, "tlsskipverify must be a bool: strconv.ParseBool: parsing \"xxx\": invalid syntax", err.Error())
}
_, err = getRedisConfig("ahost:aport", "", "", "", "", "")
if err != nil {
assert.Equal(t, "port must be numeric:strconv.Atoi: parsing \"aport\": invalid syntax", err.Error())
}
_, err = getRedisConfig("ahost:42:43", "", "", "", "", "")
if err != nil {
assert.Equal(t, "hosts must be in the form host:port but is:ahost:42:43", err.Error())
}
_, err = getRedisConfig("ahost:-1", "", "", "", "", "")
if err != nil {
assert.Equal(t, "port must between 0-65535 not:-1", err.Error())
}
_, err = getRedisConfig("ahost:65536", "", "", "", "", "")
if err != nil {
assert.Equal(t, "port must between 0-65535 not:65536", err.Error())
}
}
func TestGetRedisConnectionFromPools(t *testing.T) {
pools := []*redis.Pool{}
rp := &redisPools{
pools: pools,
}
_, err := rp.getRedisPoolFromPools()
if err != nil {
assert.Equal(t, "pool is empty", err.Error())
}
pool := newPool("1.2.3.5", 6379, 0, "", false, false)
rp.pools = append(rp.pools, pool)
p, err := rp.getRedisPoolFromPools()
if err != nil {
assert.Fail(t, err.Error())
}
assert.NotNil(t, p, "pool is not to be expected nil")
pool = newPool("1.2.3.4", 42, 0, "", false, false)
rp.pools = append(rp.pools, pool)
p1, _ := rp.getRedisPoolFromPools()
p2, _ := rp.getRedisPoolFromPools()
assert.NotNil(t, p1, "pool is not to be expected nil")
assert.NotNil(t, p2, "pool is not to be expected nil")
}
func TestPoolsFromConfiguration(t *testing.T) {
cfg, err := getRedisConfig("ahost:23456 bhost:12345 chost:45678", "", "", "", "", "")
assert.NoError(t, err, "configuration should be parseable")
pools := newPoolsFromConfig(cfg)
assert.Len(t, pools.pools, 3, "there should be three pools")
}
type testConnection struct {
invokes [][]byte
flushed bool
fail string
}
func (r *testConnection) Send(cmd string, args ...interface{}) error {
data := args[1].([]byte)
if string(data) == r.fail {
return fmt.Errorf("expected fail %q is encountered", r.fail)
}
r.invokes = append(r.invokes, data)
return nil
}
func (r *testConnection) Flush() error {
r.flushed = true
return nil
}
func TestRedisSendMessage(t *testing.T) {
rc := &redisClient{}
values := []*logmessage{
&logmessage{data: []byte("test1")},
&logmessage{data: []byte("test2")},
}
conn := &testConnection{}
err := rc.sendImpl(conn, values)
assert.NoError(t, err, "send should be ok")
assert.Equal(t, len(values), len(conn.invokes), "every messages should be sended")
assert.True(t, conn.flushed, "data should be flushed")
}
func TestRedisSendFailureMessage(t *testing.T) {
rc := &redisClient{}
values := []*logmessage{
&logmessage{data: []byte("test1")},
&logmessage{data: []byte("failure")},
&logmessage{data: []byte("test2")},
}
conn := &testConnection{fail: "failure"}
err := rc.sendImpl(conn, values)
assert.Error(t, err, "send should not be ok")
assert.False(t, conn.flushed, "data should not be flushed")
}