Skip to content

Commit 26e40ca

Browse files
authored
test: e2e test balancer (roundrobin upstream) (#972)
close #988
1 parent 0383021 commit 26e40ca

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed

api/test/e2e/balancer_test.go

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package e2e
18+
19+
import (
20+
"io/ioutil"
21+
"net/http"
22+
"testing"
23+
"time"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestBalancer_roundrobin_with_weight(t *testing.T) {
29+
tests := []HttpTestCase{
30+
{
31+
caseDesc: "create upstream (roundrobin with same weight)",
32+
Object: ManagerApiExpect(t),
33+
Method: http.MethodPut,
34+
Path: "/apisix/admin/upstreams/1",
35+
Body: `{
36+
"nodes": [{
37+
"host": "172.16.238.20",
38+
"port": 1980,
39+
"weight": 1
40+
},
41+
{
42+
"host": "172.16.238.20",
43+
"port": 1981,
44+
"weight": 1
45+
},
46+
{
47+
"host": "172.16.238.20",
48+
"port": 1982,
49+
"weight": 1
50+
}],
51+
"type": "roundrobin"
52+
}`,
53+
Headers: map[string]string{"Authorization": token},
54+
ExpectStatus: http.StatusOK,
55+
},
56+
{
57+
caseDesc: "create route using the upstream just created",
58+
Object: ManagerApiExpect(t),
59+
Method: http.MethodPut,
60+
Path: "/apisix/admin/routes/1",
61+
Body: `{
62+
"uri": "/server_port",
63+
"upstream_id": "1"
64+
}`,
65+
Headers: map[string]string{"Authorization": token},
66+
ExpectStatus: http.StatusOK,
67+
Sleep: sleepTime,
68+
},
69+
}
70+
71+
for _, tc := range tests {
72+
testCaseCheck(tc)
73+
}
74+
75+
// hit routes
76+
time.Sleep(200 * time.Millisecond)
77+
basepath := "http://127.0.0.1:9080/"
78+
request, err := http.NewRequest("GET", basepath+"/server_port", nil)
79+
request.Header.Add("Authorization", token)
80+
var resp *http.Response
81+
var respBody []byte
82+
res := map[string]int{}
83+
for i := 0; i < 18; i++ {
84+
resp, err = http.DefaultClient.Do(request)
85+
assert.Nil(t, err)
86+
respBody, err = ioutil.ReadAll(resp.Body)
87+
body := string(respBody)
88+
if _, ok := res[body]; !ok {
89+
res[body] = 1
90+
} else {
91+
res[body] += 1
92+
}
93+
resp.Body.Close()
94+
}
95+
assert.True(t, res["1982"] == 6)
96+
assert.True(t, res["1981"] == 6)
97+
assert.True(t, res["1980"] == 6)
98+
99+
tests = []HttpTestCase{
100+
{
101+
caseDesc: "create upstream (roundrobin with different weight)",
102+
Object: ManagerApiExpect(t),
103+
Method: http.MethodPut,
104+
Path: "/apisix/admin/upstreams/1",
105+
Body: `{
106+
"nodes": [{
107+
"host": "172.16.238.20",
108+
"port": 1980,
109+
"weight": 1
110+
},
111+
{
112+
"host": "172.16.238.20",
113+
"port": 1981,
114+
"weight": 2
115+
},
116+
{
117+
"host": "172.16.238.20",
118+
"port": 1982,
119+
"weight": 3
120+
}],
121+
"type": "roundrobin"
122+
}`,
123+
Headers: map[string]string{"Authorization": token},
124+
ExpectStatus: http.StatusOK,
125+
},
126+
}
127+
for _, tc := range tests {
128+
testCaseCheck(tc)
129+
}
130+
131+
// hit routes
132+
time.Sleep(200 * time.Millisecond)
133+
res = map[string]int{}
134+
for i := 0; i < 18; i++ {
135+
resp, err = http.DefaultClient.Do(request)
136+
assert.Nil(t, err)
137+
respBody, err = ioutil.ReadAll(resp.Body)
138+
body := string(respBody)
139+
if _, ok := res[body]; !ok {
140+
res[body] = 1
141+
} else {
142+
res[body] += 1
143+
}
144+
resp.Body.Close()
145+
}
146+
assert.True(t, res["1980"] == 3)
147+
assert.True(t, res["1981"] == 6)
148+
assert.True(t, res["1982"] == 9)
149+
150+
tests = []HttpTestCase{
151+
{
152+
caseDesc: "create upstream (roundrobin with weight 1 and 0) ",
153+
Object: ManagerApiExpect(t),
154+
Method: http.MethodPut,
155+
Path: "/apisix/admin/upstreams/1",
156+
Body: `{
157+
"nodes": [{
158+
"host": "172.16.238.20",
159+
"port": 1980,
160+
"weight": 1
161+
},
162+
{
163+
"host": "172.16.238.20",
164+
"port": 1981,
165+
"weight": 0
166+
}],
167+
"type": "roundrobin"
168+
}`,
169+
Headers: map[string]string{"Authorization": token},
170+
ExpectStatus: http.StatusOK,
171+
},
172+
}
173+
for _, tc := range tests {
174+
testCaseCheck(tc)
175+
}
176+
177+
// hit routes
178+
time.Sleep(200 * time.Millisecond)
179+
res = map[string]int{}
180+
for i := 0; i < 18; i++ {
181+
resp, err = http.DefaultClient.Do(request)
182+
assert.Nil(t, err)
183+
respBody, err = ioutil.ReadAll(resp.Body)
184+
body := string(respBody)
185+
if _, ok := res[body]; !ok {
186+
res[body] = 1
187+
} else {
188+
res[body] += 1
189+
}
190+
resp.Body.Close()
191+
}
192+
assert.True(t, res["1980"] == 18)
193+
194+
tests = []HttpTestCase{
195+
{
196+
caseDesc: "create upstream (roundrobin with weight only 1 ) ",
197+
Object: ManagerApiExpect(t),
198+
Method: http.MethodPut,
199+
Path: "/apisix/admin/upstreams/1",
200+
Body: `{
201+
"nodes": [{
202+
"host": "172.16.238.20",
203+
"port": 1980,
204+
"weight": 1
205+
}],
206+
"type": "roundrobin"
207+
}`,
208+
Headers: map[string]string{"Authorization": token},
209+
ExpectStatus: http.StatusOK,
210+
},
211+
}
212+
for _, tc := range tests {
213+
testCaseCheck(tc)
214+
}
215+
216+
// hit routes
217+
time.Sleep(200 * time.Millisecond)
218+
res = map[string]int{}
219+
for i := 0; i < 18; i++ {
220+
resp, err = http.DefaultClient.Do(request)
221+
assert.Nil(t, err)
222+
respBody, err = ioutil.ReadAll(resp.Body)
223+
body := string(respBody)
224+
if _, ok := res[body]; !ok {
225+
res[body] = 1
226+
} else {
227+
res[body] += 1
228+
}
229+
resp.Body.Close()
230+
}
231+
assert.True(t, res["1980"] == 18)
232+
}
233+
234+
func TestBalancer_Delete(t *testing.T) {
235+
tests := []HttpTestCase{
236+
{
237+
caseDesc: "delete route",
238+
Object: ManagerApiExpect(t),
239+
Method: http.MethodDelete,
240+
Path: "/apisix/admin/routes/1",
241+
Headers: map[string]string{"Authorization": token},
242+
ExpectStatus: http.StatusOK,
243+
},
244+
{
245+
caseDesc: "delete upstream",
246+
Object: ManagerApiExpect(t),
247+
Method: http.MethodDelete,
248+
Path: "/apisix/admin/upstreams/1",
249+
Headers: map[string]string{"Authorization": token},
250+
ExpectStatus: http.StatusOK,
251+
},
252+
{
253+
caseDesc: "hit the route just deleted",
254+
Object: APISIXExpect(t),
255+
Method: http.MethodGet,
256+
Path: "/server_port",
257+
ExpectStatus: http.StatusNotFound,
258+
ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
259+
Sleep: sleepTime,
260+
},
261+
}
262+
263+
for _, tc := range tests {
264+
testCaseCheck(tc)
265+
}
266+
}

0 commit comments

Comments
 (0)