forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy_test.go
316 lines (256 loc) · 7 KB
/
proxy_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package toxiproxy_test
import (
"bytes"
"encoding/hex"
"io"
"io/ioutil"
"net"
"testing"
"time"
"github.com/Shopify/toxiproxy"
"github.com/sirupsen/logrus"
tomb "gopkg.in/tomb.v1"
)
func init() {
logrus.SetLevel(logrus.FatalLevel)
}
func NewTestProxy(name, upstream string) *toxiproxy.Proxy {
proxy := toxiproxy.NewProxy()
proxy.Name = name
proxy.Listen = "localhost:0"
proxy.Upstream = upstream
return proxy
}
func WithTCPServer(t *testing.T, f func(string, chan []byte)) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal("Failed to create TCP server", err)
}
defer ln.Close()
response := make(chan []byte, 1)
tomb := tomb.Tomb{}
go func() {
defer tomb.Done()
src, err := ln.Accept()
if err != nil {
select {
case <-tomb.Dying():
default:
t.Fatal("Failed to accept client")
}
return
}
ln.Close()
val, err := ioutil.ReadAll(src)
if err != nil {
t.Fatal("Failed to read from client")
}
response <- val
}()
f(ln.Addr().String(), response)
tomb.Killf("Function body finished")
ln.Close()
tomb.Wait()
close(response)
}
func TestSimpleServer(t *testing.T) {
WithTCPServer(t, func(addr string, response chan []byte) {
conn, err := net.Dial("tcp", addr)
if err != nil {
t.Error("Unable to dial TCP server", err)
}
msg := []byte("hello world")
_, err = conn.Write(msg)
if err != nil {
t.Error("Failed writing to TCP server", err)
}
err = conn.Close()
if err != nil {
t.Error("Failed to close TCP connection", err)
}
resp := <-response
if !bytes.Equal(resp, msg) {
t.Error("Server didn't read bytes from client")
}
})
}
func WithTCPProxy(t *testing.T, f func(proxy net.Conn, response chan []byte, proxyServer *toxiproxy.Proxy)) {
WithTCPServer(t, func(upstream string, response chan []byte) {
proxy := NewTestProxy("test", upstream)
proxy.Start()
conn := AssertProxyUp(t, proxy.Listen, true)
f(conn, response, proxy)
proxy.Stop()
})
}
func TestProxySimpleMessage(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
msg := []byte("hello world")
_, err := conn.Write(msg)
if err != nil {
t.Error("Failed writing to TCP server", err)
}
err = conn.Close()
if err != nil {
t.Error("Failed to close TCP connection", err)
}
resp := <-response
if !bytes.Equal(resp, msg) {
t.Error("Server didn't read correct bytes from client", resp)
}
})
}
func TestProxyToDownUpstream(t *testing.T) {
proxy := NewTestProxy("test", "localhost:20009")
proxy.Start()
conn := AssertProxyUp(t, proxy.Listen, true)
// Check to make sure the connection is closed
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
_, err := conn.Read(make([]byte, 1))
if err != io.EOF {
t.Error("Proxy did not close connection when upstream down", err)
}
proxy.Stop()
}
func TestProxyBigMessage(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
buf := make([]byte, 32*1024)
msg := make([]byte, len(buf)*2)
hex.Encode(msg, buf)
_, err := conn.Write(msg)
if err != nil {
t.Error("Failed writing to TCP server", err)
}
err = conn.Close()
if err != nil {
t.Error("Failed to close TCP connection", err)
}
resp := <-response
if !bytes.Equal(resp, msg) {
t.Error("Server didn't read correct bytes from client", resp)
}
})
}
func TestProxyTwoPartMessage(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
msg1 := []byte("hello world")
msg2 := []byte("hello world")
_, err := conn.Write(msg1)
if err != nil {
t.Error("Failed writing to TCP server", err)
}
_, err = conn.Write(msg2)
if err != nil {
t.Error("Failed writing to TCP server", err)
}
err = conn.Close()
if err != nil {
t.Error("Failed to close TCP connection", err)
}
msg1 = append(msg1, msg2...)
resp := <-response
if !bytes.Equal(resp, msg1) {
t.Error("Server didn't read correct bytes from client", resp)
}
})
}
func TestClosingProxyMultipleTimes(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
proxy.Stop()
proxy.Stop()
proxy.Stop()
})
}
func TestStartTwoProxiesOnSameAddress(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
proxy2 := NewTestProxy("proxy_2", "localhost:3306")
proxy2.Listen = proxy.Listen
if err := proxy2.Start(); err == nil {
t.Fatal("Expected an err back from start")
}
})
}
func TestStopProxyBeforeStarting(t *testing.T) {
WithTCPServer(t, func(upstream string, response chan []byte) {
proxy := NewTestProxy("test", upstream)
AssertProxyUp(t, proxy.Listen, false)
proxy.Stop()
err := proxy.Start()
if err != nil {
t.Error("Proxy failed to start", err)
}
err = proxy.Start()
if err != toxiproxy.ErrProxyAlreadyStarted {
t.Error("Proxy did not fail to start when already started", err)
}
AssertProxyUp(t, proxy.Listen, true)
proxy.Stop()
AssertProxyUp(t, proxy.Listen, false)
})
}
func TestProxyUpdate(t *testing.T) {
WithTCPServer(t, func(upstream string, response chan []byte) {
proxy := NewTestProxy("test", upstream)
err := proxy.Start()
if err != nil {
t.Error("Proxy failed to start", err)
}
AssertProxyUp(t, proxy.Listen, true)
before := proxy.Listen
input := &toxiproxy.Proxy{Listen: "localhost:0", Upstream: proxy.Upstream, Enabled: true}
err = proxy.Update(input)
if err != nil {
t.Error("Failed to update proxy", err)
}
if proxy.Listen == before || proxy.Listen == input.Listen {
t.Errorf("Proxy update didn't change listen address: %s to %s", before, proxy.Listen)
}
AssertProxyUp(t, proxy.Listen, true)
input.Listen = proxy.Listen
err = proxy.Update(input)
if err != nil {
t.Error("Failed to update proxy", err)
}
AssertProxyUp(t, proxy.Listen, true)
input.Enabled = false
err = proxy.Update(input)
if err != nil {
t.Error("Failed to update proxy", err)
}
AssertProxyUp(t, proxy.Listen, false)
})
}
func TestRestartFailedToStartProxy(t *testing.T) {
WithTCPServer(t, func(upstream string, response chan []byte) {
proxy := NewTestProxy("test", upstream)
conflict := NewTestProxy("test2", upstream)
err := conflict.Start()
if err != nil {
t.Error("Proxy failed to start", err)
}
AssertProxyUp(t, conflict.Listen, true)
proxy.Listen = conflict.Listen
err = proxy.Start()
if err == nil || err == toxiproxy.ErrProxyAlreadyStarted {
t.Error("Proxy started when it should have conflicted")
}
conflict.Stop()
AssertProxyUp(t, conflict.Listen, false)
err = proxy.Start()
if err != nil {
t.Error("Proxy failed to start after conflict went away", err)
}
AssertProxyUp(t, proxy.Listen, true)
proxy.Stop()
AssertProxyUp(t, proxy.Listen, false)
})
}
func AssertProxyUp(t *testing.T, addr string, up bool) net.Conn {
conn, err := net.Dial("tcp", addr)
if err != nil && up {
t.Error("Expected proxy to be up:", err)
} else if err == nil && !up {
t.Error("Expected proxy to be down")
}
return conn
}