Repository navigation
Expand file tree
/
Copy pathcontroller_test.go
More file actions
202 lines (166 loc) · 6.83 KB
/
Copy pathcontroller_test.go
File metadata and controls
202 lines (166 loc) · 6.83 KB
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
package controller
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/onestraw/golb/balancer"
"github.com/onestraw/golb/config"
"github.com/onestraw/golb/stats"
)
func mockBalancer(t *testing.T) *balancer.Balancer {
jsonBody := `{"virtual_server":[{"name":"web","address":"127.0.0.1:8082","server_name":"localhost","pool":[{"address":"127.0.0.1:10001","weight":1},{"address":"127.0.0.1:10002","weight":2}],"lb_method":"round-robin"}]}`
c, err := config.LoadFromString(jsonBody)
require.NoError(t, err)
b, err := balancer.New(c.VServers)
require.NoError(t, err)
return b
}
func testCtrlSuit(t *testing.T, h http.Handler, req *http.Request, expectCode int, expectBody string) {
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
resp := rr.Result()
assert.Equal(t, expectCode, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, expectBody, string(body))
}
func TestController(t *testing.T) {
c := New(&config.Controller{Address: "127.0.0.1:6587"})
b := mockBalancer(t)
c.Run(b)
}
func TestListAllVirtualServer(t *testing.T) {
b := mockBalancer(t)
h := listAllVirtualServer(b)
req := httptest.NewRequest("GET", "/vs", nil)
expect := "Name:web, Address:127.0.0.1:8082, Status:stopped, Pool:\n127.0.0.1:10001, 127.0.0.1:10002\n\n"
testCtrlSuit(t, h, req, 200, expect)
}
func TestListVirtualServer(t *testing.T) {
b := mockBalancer(t)
h := listVirtualServer(b)
req := httptest.NewRequest("GET", "/vs/web", nil)
req = mux.SetURLVars(req, map[string]string{"name": "web"})
expect := "127.0.0.1:10001, 127.0.0.1:10002"
testCtrlSuit(t, h, req, 200, expect)
req = mux.SetURLVars(req, map[string]string{"name": "not_exist"})
testCtrlSuit(t, h, req, 400, balancer.ErrVirtualServerNotFound.Error())
}
func TestStatsHandler(t *testing.T) {
b := mockBalancer(t)
h := statsHandler(b)
req := httptest.NewRequest("GET", "/stats", nil)
data := &stats.Data{
StatusCode: "200",
Method: "POST",
Path: "test/",
InBytes: 10,
OutBytes: 20,
}
b.VServers[0].ServerStats["127.0.0.1:10001"] = stats.New()
b.VServers[0].ServerStats["127.0.0.1:10002"] = stats.New()
b.VServers[0].ServerStats["127.0.0.1:10001"].Inc(data)
b.VServers[0].ServerStats["127.0.0.1:10001"].Inc(data)
b.VServers[0].ServerStats["127.0.0.1:10002"].Inc(data)
data.StatusCode = "500"
b.VServers[0].ServerStats["127.0.0.1:10001"].Inc(data)
expect := "Pool-web\n127.0.0.1:10001\nstatus_code: 200:2, 500:1\nmethod: POST:3\npath: test/:3\nrecv_bytes: 30\nsend_bytes: 60\n------\n127.0.0.1:10002\nstatus_code: 200:1\nmethod: POST:1\npath: test/:1\nrecv_bytes: 10\nsend_bytes: 20\n------"
testCtrlSuit(t, h, req, 200, expect)
}
func TestModifyVirtualServerStatus(t *testing.T) {
b := mockBalancer(t)
h := modifyVirtualServerStatus(b)
// enable
body, _ := json.Marshal(map[string]string{"action": "enable"})
req := httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
expect := "success"
t.Logf("Before enable: %s", b.VServers[0].Status())
testCtrlSuit(t, h, req, 200, expect)
time.Sleep(time.Second)
t.Logf("After enable: %s", b.VServers[0].Status())
// repeat enable
req = httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 200, "web is already enabled")
// disalbe
body, _ = json.Marshal(map[string]string{"action": "disable"})
req = httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 200, expect)
// virtual server not exist
body, _ = json.Marshal(map[string]string{"action": "enable"})
req = httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "not_exist"})
testCtrlSuit(t, h, req, 400, balancer.ErrVirtualServerNotFound.Error())
// unknown action
body, _ = json.Marshal(map[string]string{})
req = httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 400, "unknown action")
// bad request
req = httptest.NewRequest("POST", "/vs", strings.NewReader(""))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 400, "EOF")
}
func TestAddVirtualServer(t *testing.T) {
b := mockBalancer(t)
h := addVirtualServer(b)
body, _ := json.Marshal(map[string]string{"name": "redis", "address": "127.0.0.1:6379"})
req := httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
expect := "Add success"
testCtrlSuit(t, h, req, 200, expect)
assert.Equal(t, 2, len(b.VServers))
vs := b.VServers[1]
assert.Equal(t, "redis", vs.Name)
assert.Equal(t, "127.0.0.1:6379", vs.Address)
// test add fail
body, _ = json.Marshal(map[string]string{"address": "127.0.0.1:6379"})
req = httptest.NewRequest("POST", "/vs", bytes.NewReader(body))
testCtrlSuit(t, h, req, 400, balancer.ErrVirtualServerNameEmpty.Error())
// test bad request
req = httptest.NewRequest("POST", "/vs", strings.NewReader(""))
testCtrlSuit(t, h, req, 400, "EOF")
}
func TestAddPoolMember(t *testing.T) {
b := mockBalancer(t)
h := addPoolMember(b)
body, _ := json.Marshal(map[string]interface{}{"address": "127.0.0.1:10005"})
req := httptest.NewRequest("POST", "/vs/web/pool", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 200, "Add peer success")
assert.Equal(t, 3, b.VServers[0].Pool.Size())
// pool not exist
req = httptest.NewRequest("POST", "/vs/web/pool", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "db"})
testCtrlSuit(t, h, req, 400, balancer.ErrVirtualServerNotFound.Error())
// test bad request
req = httptest.NewRequest("POST", "/vs/web/pool", strings.NewReader(""))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 400, "EOF")
}
func TestDeletePoolMember(t *testing.T) {
b := mockBalancer(t)
h := deletePoolMember(b)
body, _ := json.Marshal(map[string]string{"address": "127.0.0.1:10001"})
req := httptest.NewRequest("DELETE", "/vs/web/pool", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 200, "Remove peer success")
assert.Equal(t, 1, b.VServers[0].Pool.Size())
req = httptest.NewRequest("DELETE", "/vs/web/pool", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"name": "db"})
testCtrlSuit(t, h, req, 400, balancer.ErrVirtualServerNotFound.Error())
// test bad request
req = httptest.NewRequest("DELETE", "/vs/web/pool", strings.NewReader(""))
req = mux.SetURLVars(req, map[string]string{"name": "web"})
testCtrlSuit(t, h, req, 400, "EOF")
}