forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_collection_test.go
120 lines (95 loc) · 2.86 KB
/
proxy_collection_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
package toxiproxy_test
import (
"bytes"
"net"
"testing"
"github.com/Shopify/toxiproxy/v2"
)
func TestAddProxyToCollection(t *testing.T) {
collection := toxiproxy.NewProxyCollection()
proxy := NewTestProxy("test", "localhost:20000")
if _, err := collection.Get(proxy.Name); err == nil {
t.Error("Expected proxies to be empty")
}
err := collection.Add(proxy, false)
if err != nil {
t.Error("Expected to be able to add first proxy to collection")
}
if _, err := collection.Get(proxy.Name); err != nil {
t.Error("Expected proxy to be added to map")
}
}
func TestAddTwoProxiesToCollection(t *testing.T) {
collection := toxiproxy.NewProxyCollection()
proxy := NewTestProxy("test", "localhost:20000")
err := collection.Add(proxy, false)
if err != nil {
t.Error("Expected to be able to add first proxy to collection")
}
err = collection.Add(proxy, false)
if err == nil {
t.Error("Expected to not be able to add proxy with same name")
}
}
func TestListProxies(t *testing.T) {
collection := toxiproxy.NewProxyCollection()
proxy := NewTestProxy("test", "localhost:20000")
err := collection.Add(proxy, false)
if err != nil {
t.Error("Expected to be able to add first proxy to collection")
}
proxies := collection.Proxies()
proxy, ok := proxies[proxy.Name]
if !ok {
t.Error("Expected to be able to see existing proxy")
} else if proxy.Enabled {
t.Error("Expected proxy not to be running")
}
}
func TestAddProxyAndStart(t *testing.T) {
collection := toxiproxy.NewProxyCollection()
proxy := NewTestProxy("test", "localhost:20000")
err := collection.Add(proxy, true)
if err != nil {
t.Error("Expected to be able to add proxy to collection:", err)
}
proxies := collection.Proxies()
proxy, ok := proxies[proxy.Name]
if !ok {
t.Error("Expected to be able to see existing proxy")
} else if !proxy.Enabled {
t.Error("Expected proxy to be running")
}
}
func TestAddAndRemoveProxyFromCollection(t *testing.T) {
WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {
collection := toxiproxy.NewProxyCollection()
if _, err := collection.Get(proxy.Name); err == nil {
t.Error("Expected proxies to be empty")
}
err := collection.Add(proxy, false)
if err != nil {
t.Error("Expected to be able to add first proxy to collection")
}
if _, err := collection.Get(proxy.Name); err != nil {
t.Error("Expected proxy to be added to map")
}
msg := []byte("go away")
_, err = conn.Write(msg)
if err != nil {
t.Error("Failed writing to socket to shut down server")
}
conn.Close()
resp := <-response
if !bytes.Equal(resp, msg) {
t.Error("Server didn't read bytes from client")
}
err = collection.Remove(proxy.Name)
if err != nil {
t.Error("Expected to remove proxy from collection")
}
if _, err := collection.Get(proxy.Name); err == nil {
t.Error("Expected proxies to be empty")
}
})
}