From 42b6058398dc5875f5a27e62b304aa9a005cd238 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 13:55:04 +0800 Subject: [PATCH 1/9] test: cHash upstream with key (query_string, arg_xxx) --- ...stream_chash_ query_string_arg_xxx_test.go | 213 ++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go diff --git a/api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go new file mode 100644 index 0000000000..5332ea683d --- /dev/null +++ b/api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go @@ -0,0 +1,213 @@ +/* + * 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 ( + "fmt" + "io/ioutil" + "net/http" + "strconv" + "testing" + + "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 + basepath := "http://127.0.0.1:9080" + var req *http.Request + var err error + var url string + var resp *http.Response + var respBody []byte + res := map[string]int{} + for i := 0; i <= 17; 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 + } + } + fmt.Println(res) + assert.Equal(t, 4, res["1980"]) + assert.Equal(t, 9, res["1981"]) + assert.Equal(t, 5, res["1982"]) + defer resp.Body.Close() +} + +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, + }, + } + + for _, tc := range tests { + testCaseCheck(tc) + } + + //hit routes + basepath := "http://127.0.0.1:9080" + var req *http.Request + var err error + var url string + var resp *http.Response + 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 + } + } + fmt.Println(res) + assert.Equal(t, 7, res["1980"]) + assert.Equal(t, 6, res["1981"]) + assert.Equal(t, 5, res["1982"]) + defer resp.Body.Close() +} + +func TestUpstream_Delete(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) + } +} From 3c3a312d0d634b80b8c89f0db0e2c41e193a8666 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 14:24:40 +0800 Subject: [PATCH 2/9] rename testcase file name --- ...rg_xxx_test.go => upstream_chash_query_string_arg_xxx_test.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename api/test/e2e/{upstream_chash_ query_string_arg_xxx_test.go => upstream_chash_query_string_arg_xxx_test.go} (100%) diff --git a/api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go similarity index 100% rename from api/test/e2e/upstream_chash_ query_string_arg_xxx_test.go rename to api/test/e2e/upstream_chash_query_string_arg_xxx_test.go From e4394f312280df9d3662e5b09a57459c10a5c0ac Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 14:37:33 +0800 Subject: [PATCH 3/9] add sleep time --- api/test/e2e/upstream_chash_query_string_arg_xxx_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 5332ea683d..6f6fea3109 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -22,6 +22,7 @@ import ( "net/http" "strconv" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -75,6 +76,7 @@ func TestUpstream_cHash_query_string(t *testing.T) { } //hit routes + time.Sleep(time.Duration(500) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request var err error @@ -151,6 +153,7 @@ func TestUpstream_cHash_arg_xxx(t *testing.T) { } //hit routes + time.Sleep(time.Duration(500) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request var err error From 6085f3173f52065f441ca381fb04673748131fcc Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 15:10:06 +0800 Subject: [PATCH 4/9] remove useless code --- ...pstream_chash_query_string_arg_xxx_test.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 6f6fea3109..58132979fb 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -78,16 +78,13 @@ func TestUpstream_cHash_query_string(t *testing.T) { //hit routes time.Sleep(time.Duration(500) * time.Millisecond) basepath := "http://127.0.0.1:9080" - var req *http.Request - var err error var url string - var resp *http.Response var respBody []byte res := map[string]int{} for i := 0; i <= 17; i++ { url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) - req, err = http.NewRequest("GET", url, nil) - resp, err = http.DefaultClient.Do(req) + 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) @@ -96,12 +93,12 @@ func TestUpstream_cHash_query_string(t *testing.T) { } else { res[body] += 1 } + resp.Body.Close() } fmt.Println(res) assert.Equal(t, 4, res["1980"]) assert.Equal(t, 9, res["1981"]) assert.Equal(t, 5, res["1982"]) - defer resp.Body.Close() } func TestUpstream_cHash_arg_xxx(t *testing.T) { @@ -155,16 +152,13 @@ func TestUpstream_cHash_arg_xxx(t *testing.T) { //hit routes time.Sleep(time.Duration(500) * time.Millisecond) basepath := "http://127.0.0.1:9080" - var req *http.Request - var err error var url string - var resp *http.Response 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) + 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) @@ -173,12 +167,13 @@ func TestUpstream_cHash_arg_xxx(t *testing.T) { } else { res[body] += 1 } + resp.Body.Close() } fmt.Println(res) assert.Equal(t, 7, res["1980"]) assert.Equal(t, 6, res["1981"]) assert.Equal(t, 5, res["1982"]) - defer resp.Body.Close() + } func TestUpstream_Delete(t *testing.T) { From cd082c17e05b8121b080402f05d87564475bdc41 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 17:44:41 +0800 Subject: [PATCH 5/9] fix indentation and function name --- ...pstream_chash_query_string_arg_xxx_test.go | 81 +++++++++---------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 58132979fb..9904433b1c 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -17,7 +17,6 @@ package e2e import ( - "fmt" "io/ioutil" "net/http" "strconv" @@ -35,24 +34,24 @@ func TestUpstream_cHash_query_string(t *testing.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" - }`, + "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, }, @@ -95,7 +94,7 @@ func TestUpstream_cHash_query_string(t *testing.T) { } resp.Body.Close() } - fmt.Println(res) + //the results will be distributed among the 3 upstream assert.Equal(t, 4, res["1980"]) assert.Equal(t, 9, res["1981"]) assert.Equal(t, 5, res["1982"]) @@ -109,24 +108,24 @@ func TestUpstream_cHash_arg_xxx(t *testing.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" - }`, + "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, }, @@ -169,14 +168,14 @@ func TestUpstream_cHash_arg_xxx(t *testing.T) { } resp.Body.Close() } - fmt.Println(res) + //the results will be distributed among the 3 upstream assert.Equal(t, 7, res["1980"]) assert.Equal(t, 6, res["1981"]) assert.Equal(t, 5, res["1982"]) - + } -func TestUpstream_Delete(t *testing.T) { +func TestUpstream_Delete_chash(t *testing.T) { tests := []HttpTestCase{ { caseDesc: "delete route", From f09efa11cbc80d9c3462ceee654a22c5d2453d68 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 00:15:54 +0800 Subject: [PATCH 6/9] chore: `cHash` -> `chash` --- api/test/e2e/upstream_chash_query_string_arg_xxx_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 9904433b1c..fc5410625a 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -26,10 +26,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestUpstream_cHash_query_string(t *testing.T) { +func TestUpstream_chash_query_string(t *testing.T) { tests := []HttpTestCase{ { - caseDesc: "create cHash upstream with key (query_string)", + caseDesc: "create chash upstream with key (query_string)", Object: ManagerApiExpect(t), Method: http.MethodPut, Path: "/apisix/admin/upstreams/1", @@ -100,10 +100,10 @@ func TestUpstream_cHash_query_string(t *testing.T) { assert.Equal(t, 5, res["1982"]) } -func TestUpstream_cHash_arg_xxx(t *testing.T) { +func TestUpstream_chash_arg_xxx(t *testing.T) { tests := []HttpTestCase{ { - caseDesc: "create cHash upstream with key (arg_xxx)", + caseDesc: "create chash upstream with key (arg_xxx)", Object: ManagerApiExpect(t), Method: http.MethodPut, Path: "/apisix/admin/upstreams/1", From 575f6e30942984571ed6892da666ea1f70a57bc8 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 13:38:54 +0800 Subject: [PATCH 7/9] test: synchronize the latest algorithm of apisix --- ...pstream_chash_query_string_arg_xxx_test.go | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index fc5410625a..5df6d5f778 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -22,6 +22,7 @@ import ( "strconv" "testing" "time" + "sort" "github.com/stretchr/testify/assert" ) @@ -74,13 +75,13 @@ func TestUpstream_chash_query_string(t *testing.T) { testCaseCheck(tc) } - //hit routes + // 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++ { + 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) @@ -94,10 +95,12 @@ func TestUpstream_chash_query_string(t *testing.T) { } resp.Body.Close() } - //the results will be distributed among the 3 upstream - assert.Equal(t, 4, res["1980"]) - assert.Equal(t, 9, res["1981"]) - assert.Equal(t, 5, res["1982"]) + 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) { @@ -148,7 +151,7 @@ func TestUpstream_chash_arg_xxx(t *testing.T) { testCaseCheck(tc) } - //hit routes + // hit routes time.Sleep(time.Duration(500) * time.Millisecond) basepath := "http://127.0.0.1:9080" var url string @@ -168,11 +171,12 @@ func TestUpstream_chash_arg_xxx(t *testing.T) { } resp.Body.Close() } - //the results will be distributed among the 3 upstream - assert.Equal(t, 7, res["1980"]) - assert.Equal(t, 6, res["1981"]) - assert.Equal(t, 5, res["1982"]) - + 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_Delete_chash(t *testing.T) { From dbb6afa230a2248868285016a22a81ec1e12b402 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 13:40:38 +0800 Subject: [PATCH 8/9] fix: remove useless step --- .../e2e/upstream_chash_query_string_arg_xxx_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 5df6d5f778..3e326e37bc 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -132,19 +132,6 @@ func TestUpstream_chash_arg_xxx(t *testing.T) { 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 { From 730d2e05b65af0cca5ddcf36d7bfe5aa80018e2b Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 15:34:15 +0800 Subject: [PATCH 9/9] add todo keyword --- api/test/e2e/upstream_chash_query_string_arg_xxx_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go index 3e326e37bc..08a60350f9 100644 --- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go +++ b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go @@ -19,10 +19,10 @@ package e2e import ( "io/ioutil" "net/http" + "sort" "strconv" "testing" "time" - "sort" "github.com/stretchr/testify/assert" ) @@ -100,7 +100,7 @@ func TestUpstream_chash_query_string(t *testing.T) { 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 + assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4) // todo: the result is unstable, fix it later } func TestUpstream_chash_arg_xxx(t *testing.T) { @@ -163,7 +163,7 @@ func TestUpstream_chash_arg_xxx(t *testing.T) { 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 + assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4) // todo: the result is unstable, fix it later } func TestUpstream_Delete_chash(t *testing.T) {