-
Notifications
You must be signed in to change notification settings - Fork 534
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 (query_string, arg_xxx) #932
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
42b6058
test: cHash upstream with key (query_string, arg_xxx)
idbeta 3c3a312
rename testcase file name
idbeta e4394f3
add sleep time
idbeta 6085f31
remove useless code
idbeta cd082c1
fix indentation and function name
idbeta f09efa1
chore: `cHash` -> `chash`
idbeta 5c3b6cf
Merge branch 'master' into e2e-test-upstream2
idbeta 575f6e3
test: synchronize the latest algorithm of apisix
idbeta dbb6afa
fix: remove useless step
idbeta 730d2e0
add todo keyword
idbeta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
api/test/e2e/upstream_chash_query_string_arg_xxx_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package e2e | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
"time" | ||
"sort" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpstream_chash_query_string(t *testing.T) { | ||
tests := []HttpTestCase{ | ||
{ | ||
caseDesc: "create chash upstream with key (query_string)", | ||
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", | ||
"key": "query_string" | ||
}`, | ||
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 | ||
time.Sleep(time.Duration(500) * time.Millisecond) | ||
basepath := "http://127.0.0.1:9080" | ||
var url string | ||
var respBody []byte | ||
res := map[string]int{} | ||
for i := 0; i < 180; i++ { | ||
url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) | ||
req, err := http.NewRequest("GET", url, nil) | ||
resp, err := http.DefaultClient.Do(req) | ||
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() | ||
} | ||
var counts []int | ||
for _, value := range res { | ||
counts = append(counts, value) | ||
} | ||
sort.Ints(counts) | ||
assert.True(t, float64(counts[2]-counts[0]) / float64(counts[1]) < 0.4) // the result is unstable, fix it later | ||
} | ||
|
||
func TestUpstream_chash_arg_xxx(t *testing.T) { | ||
tests := []HttpTestCase{ | ||
{ | ||
caseDesc: "create chash upstream with key (arg_xxx)", | ||
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", | ||
"key": "arg_device_id" | ||
}`, | ||
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, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} | ||
|
||
for _, tc := range tests { | ||
testCaseCheck(tc) | ||
} | ||
|
||
// hit routes | ||
time.Sleep(time.Duration(500) * time.Millisecond) | ||
basepath := "http://127.0.0.1:9080" | ||
var url string | ||
var respBody []byte | ||
res := map[string]int{} | ||
for i := 0; i <= 17; i++ { | ||
url = basepath + "/server_port?device_id=" + strconv.Itoa(i) | ||
req, err := http.NewRequest("GET", url, nil) | ||
resp, err := http.DefaultClient.Do(req) | ||
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() | ||
} | ||
var counts []int | ||
for _, value := range res { | ||
counts = append(counts, value) | ||
} | ||
sort.Ints(counts) | ||
assert.True(t, float64(counts[2]-counts[0]) / float64(counts[1]) < 0.4) // the result is unstable, fix it later | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
func TestUpstream_Delete_chash(t *testing.T) { | ||
tests := []HttpTestCase{ | ||
{ | ||
caseDesc: "delete route", | ||
Object: ManagerApiExpect(t), | ||
Method: http.MethodDelete, | ||
Path: "/apisix/admin/routes/1", | ||
Headers: map[string]string{"Authorization": token}, | ||
ExpectStatus: http.StatusOK, | ||
}, | ||
{ | ||
caseDesc: "delete upstream", | ||
Object: ManagerApiExpect(t), | ||
Method: http.MethodDelete, | ||
Path: "/apisix/admin/upstreams/1", | ||
Headers: map[string]string{"Authorization": token}, | ||
ExpectStatus: http.StatusOK, | ||
}, | ||
{ | ||
caseDesc: "hit the route just deleted", | ||
Object: APISIXExpect(t), | ||
Method: http.MethodGet, | ||
Path: "/hello1", | ||
ExpectStatus: http.StatusNotFound, | ||
ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n", | ||
Sleep: sleepTime, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
testCaseCheck(tc) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a
todo
keywordThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.