Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: e2e test chash upstream with key (remote_addr) #897

Merged
merged 11 commits into from
Dec 2, 2020
212 changes: 197 additions & 15 deletions api/test/e2e/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package e2e

import (
"io/ioutil"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUpstream_Create(t *testing.T) {
Expand Down Expand Up @@ -139,9 +142,9 @@ func TestRoute_Node_Host(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Body: `{
"uri": "/*",
"upstream_id": "1"
}`,
"uri": "/*",
"upstream_id": "1"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
Expand Down Expand Up @@ -188,10 +191,198 @@ func TestRoute_Node_Host(t *testing.T) {
}
}

//TODO cHash
//TODO websocket
func TestUpstream_chash_remote_addr(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "create chash upstream with key (remote_addr)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
},
{
"host": "172.16.238.20",
"port": 1981,
"weight": 1
},
{
"host": "172.16.238.20",
"port": 1982,
"weight": 1
}],
"type": "chash",
"hash_on":"header",
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
"key": "remote_addr"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the upstream just created",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Body: `{
"uri": "/server_port",
"upstream_id": "1"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}

//hit routes
basepath := "http://127.0.0.1:9080/"
request, err := http.NewRequest("GET", basepath+"/server_port", nil)
request.Header.Add("Authorization", token)
var resp *http.Response
var respBody []byte
var count int
res := map[string]int{}
for i := 0; i < 18; i++ {
resp, err = http.DefaultClient.Do(request)
assert.Nil(t, err)
respBody, err = ioutil.ReadAll(resp.Body)
body := string(respBody)
if _, ok := res[body]; !ok {
res[body] = 1
} else {
res[body] += 1
}
resp.Body.Close()
}
membphis marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, 18, res["1982"])

tests = []HttpTestCase{
{
caseDesc: "create chash upstream with key (remote_addr, weight equal 0 or 1)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [
{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
},
{
"host": "172.16.238.20",
"port": 1981,
"weight": 0
},
{
"host": "172.16.238.20",
"port": 1982,
"weight": 0
}],
"type": "chash",
"hash_on":"header",
"key": "remote_addr"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the upstream just created",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Body: `{
"uri": "/server_port",
"upstream_id": "1"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}

//hit routes
basepath = "http://127.0.0.1:9080/"
request, err = http.NewRequest("GET", basepath+"/server_port", nil)
request.Header.Add("Authorization", token)
count = 0
for i := 0; i <= 17; i++ {
resp, err = http.DefaultClient.Do(request)
assert.Nil(t, err)
respBody, err = ioutil.ReadAll(resp.Body)
if string(respBody) == "1980" {
count++
}
}
assert.Equal(t, 18, count)
defer resp.Body.Close()

tests = []HttpTestCase{
{
caseDesc: "create chash upstream with key (remote_addr, all weight equal 0)",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/upstreams/1",
Body: `{
"nodes": [
{
"host": "172.16.238.20",
"port": 1980,
"weight": 0
},
{
"host": "172.16.238.20",
"port": 1981,
"weight": 0
}],
"type": "chash",
"hash_on":"header",
"key": "remote_addr"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
caseDesc: "create route using the upstream just created",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/1",
Body: `{
"uri": "/server_port",
"upstream_id": "1"
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
caseDesc: "hit the route ",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/server_port",
ExpectStatus: http.StatusBadGateway,
ExpectBody: "<head><title>502 Bad Gateway</title></head>",
Sleep: sleepTime,
},
membphis marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tc := range tests {
testCaseCheck(tc)
}

}

func TestRoute_Delete(t *testing.T) {
func TestUpstream_Delete(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "delete not exist upstream",
Expand All @@ -201,15 +392,6 @@ func TestRoute_Delete(t *testing.T) {
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusNotFound,
},
// TODO it's a bug here, see: https://github.com/apache/apisix-dashboard/issues/728
//{
// caseDesc: "delete upstream - being used by route 1",
// Object: ManagerApiExpect(t),
// Method: http.MethodDelete,
// Path: "/apisix/admin/upstreams/1",
// Headers: map[string]string{"Authorization": token},
// ExpectStatus: http.StatusBadRequest,
//},
{
caseDesc: "delete route",
Object: ManagerApiExpect(t),
Expand Down