From a8d4c5bc8e015cc45fd0e7aaaf1a81fde18b6ef8 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 16:35:54 +0800 Subject: [PATCH 01/11] test: add upstream cHash hash_on consumer, header, cookie --- api/test/e2e/upstream_chash_hash_on_test.go | 373 ++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 api/test/e2e/upstream_chash_hash_on_test.go diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go new file mode 100644 index 0000000000..e57beba1d6 --- /dev/null +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -0,0 +1,373 @@ +/* + * 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" + "fmt" + + "github.com/stretchr/testify/assert" +) + +func TestUpstream_cHash_hash_on_custom_header(t *testing.T) { + tests := []HttpTestCase{ + { + caseDesc: "create cHash upstream with hash_on (custom_header)", + 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 + }], + "type": "chash", + "key": "custom_header", + "hash_on": "header" + }`, + 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(100) * 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 <= 3; i++ { + url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) + req, err = http.NewRequest("GET", url, nil) + req.Header.Add("custom_header", `custom-one`) + 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 + } + } + assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) + resp.Body.Close() +} + +func TestUpstream_cHash_hash_on_cookie(t *testing.T) { + tests := []HttpTestCase{ + { + caseDesc: "create cHash upstream with hash_on (cookie)", + 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 + }], + "type": "chash", + "key": "custom-cookie", + "hash_on": "cookie" + }`, + 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(100) * 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 <= 3; i++ { + url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) + req, err = http.NewRequest("GET", url, nil) + req.Header.Add("Cookie", `custom-cookie=cuscookie`) + 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 + } + } + assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) + resp.Body.Close() + + //hit routes with miss cookie + res = map[string]int{} + for i := 0; i <= 3; i++ { + url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) + req, err = http.NewRequest("GET", url, nil) + req.Header.Add("Cookie", `miss-custom-cookie=cuscookie`) + 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 + } + } + assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) + resp.Body.Close() +} + +func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { + tests := []HttpTestCase{ + { + caseDesc: "create cHash upstream with key contains uppercase letters and hyphen", + 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 + }], + "type": "chash", + "key": "X-Sessionid", + "hash_on": "header" + }`, + 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(100) * 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 <= 15; i++ { + url = basepath + "/server_port" + req, err = http.NewRequest("GET", url, nil) + req.Header.Add("X-Sessionid", `chash_val_`+strconv.Itoa(i)) + 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 + } + } + assert.Equal(t, true, res["1980"] == 8 && res["1981"] == 8) + resp.Body.Close() +} + +func TestUpstream_cHash_hash_on_consumer(t *testing.T) { + tests := []HttpTestCase{ + { + caseDesc: "create consumer with key-auth", + Object: ManagerApiExpect(t), + Method: http.MethodPut, + Path: "/apisix/admin/consumers", + Body: `{ + "username": "jack", + "plugins": { + "key-auth": { + "key": "auth-jack" + } + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + }, + { + caseDesc: "create route with key-auth", + Object: ManagerApiExpect(t), + Method: http.MethodPut, + Path: "/apisix/admin/routes/1", + Body: `{ + "uri": "/server_port", + "plugin": {}, + "upstream": { + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "hash_on": "consumer" + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + Sleep: sleepTime, + }, + } + + for _, tc := range tests { + testCaseCheck(tc) + } + + //hit routes + time.Sleep(time.Duration(100) * 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 <= 3; i++ { + url = basepath + "/server_port" + req, err = http.NewRequest("GET", url, nil) + req.Header.Add("apikey", `auth-jack`) + 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, true, res["1980"] == 4 || res["1981"] == 4) + 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 220a7bc25d9d27d2a647ec1b9a1d47daa5c77b98 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 16:44:08 +0800 Subject: [PATCH 02/11] fix: delete the consumer --- api/test/e2e/upstream_chash_hash_on_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index e57beba1d6..169c76b436 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -22,7 +22,6 @@ import ( "strconv" "testing" "time" - "fmt" "github.com/stretchr/testify/assert" ) @@ -333,13 +332,20 @@ func TestUpstream_cHash_hash_on_consumer(t *testing.T) { res[body] += 1 } } - fmt.Println(res) assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) resp.Body.Close() } func TestUpstream_Delete(t *testing.T) { tests := []HttpTestCase{ + { + caseDesc: "delete consumer", + Object: ManagerApiExpect(t), + Method: http.MethodDelete, + Path: "/apisix/admin/consumers/jack", + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + }, { caseDesc: "delete route", Object: ManagerApiExpect(t), From 279a993760a25d2af99405c2d8bdd7985a6ca1f1 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 17:06:47 +0800 Subject: [PATCH 03/11] fix indentation --- api/test/e2e/upstream_chash_hash_on_test.go | 102 ++++++++++---------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index 169c76b436..1c31fff8e8 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -34,20 +34,20 @@ func TestUpstream_cHash_hash_on_custom_header(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 - }], - "type": "chash", - "key": "custom_header", - "hash_on": "header" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "custom_header", + "hash_on": "header" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -57,9 +57,9 @@ func TestUpstream_cHash_hash_on_custom_header(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/routes/1", Body: `{ - "uri": "/server_port", - "upstream_id": "1" - }`, + "uri": "/server_port", + "upstream_id": "1" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, Sleep: sleepTime, @@ -105,20 +105,20 @@ func TestUpstream_cHash_hash_on_cookie(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 - }], - "type": "chash", - "key": "custom-cookie", - "hash_on": "cookie" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "custom-cookie", + "hash_on": "cookie" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -195,20 +195,20 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(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 - }], - "type": "chash", - "key": "X-Sessionid", - "hash_on": "header" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "X-Sessionid", + "hash_on": "header" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -266,13 +266,13 @@ func TestUpstream_cHash_hash_on_consumer(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/consumers", Body: `{ - "username": "jack", - "plugins": { - "key-auth": { - "key": "auth-jack" + "username": "jack", + "plugins": { + "key-auth": { + "key": "auth-jack" + } } - } - }`, + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, From 3863f659cf859178a6df35c19e6ad7d7f9d9d2ce Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 17:48:08 +0800 Subject: [PATCH 04/11] chore: modify function name --- api/test/e2e/upstream_chash_hash_on_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index 1c31fff8e8..fc102a2483 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -336,7 +336,7 @@ func TestUpstream_cHash_hash_on_consumer(t *testing.T) { resp.Body.Close() } -func TestUpstream_Delete(t *testing.T) { +func TestUpstream_Delete_hash_on(t *testing.T) { tests := []HttpTestCase{ { caseDesc: "delete consumer", From edd0dd50c1390ba0e0cbb4950480c1dc9843deb7 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 1 Dec 2020 17:57:57 +0800 Subject: [PATCH 05/11] remove useless code --- api/test/e2e/upstream_chash_hash_on_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index fc102a2483..8d7e726c71 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -151,7 +151,7 @@ func TestUpstream_cHash_hash_on_cookie(t *testing.T) { var respBody []byte res := map[string]int{} for i := 0; i <= 3; i++ { - url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) + url = basepath + "/server_port" req, err = http.NewRequest("GET", url, nil) req.Header.Add("Cookie", `custom-cookie=cuscookie`) resp, err = http.DefaultClient.Do(req) @@ -170,7 +170,7 @@ func TestUpstream_cHash_hash_on_cookie(t *testing.T) { //hit routes with miss cookie res = map[string]int{} for i := 0; i <= 3; i++ { - url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i) + url = basepath + "/server_port" req, err = http.NewRequest("GET", url, nil) req.Header.Add("Cookie", `miss-custom-cookie=cuscookie`) resp, err = http.DefaultClient.Do(req) From a99c0ac716112bf2e320200ad411b084b788341c Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 00:08:16 +0800 Subject: [PATCH 06/11] fix: 1. `cHash` -> `chash`. 2. add `key-auth` for the route. --- api/test/e2e/upstream_chash_hash_on_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index 8d7e726c71..62f494eb97 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -26,10 +26,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestUpstream_cHash_hash_on_custom_header(t *testing.T) { +func TestUpstream_chash_hash_on_custom_header(t *testing.T) { tests := []HttpTestCase{ { - caseDesc: "create cHash upstream with hash_on (custom_header)", + caseDesc: "create chash upstream with hash_on (custom_header)", Object: ManagerApiExpect(t), Method: http.MethodPut, Path: "/apisix/admin/upstreams/1", @@ -97,7 +97,7 @@ func TestUpstream_cHash_hash_on_custom_header(t *testing.T) { resp.Body.Close() } -func TestUpstream_cHash_hash_on_cookie(t *testing.T) { +func TestUpstream_chash_hash_on_cookie(t *testing.T) { tests := []HttpTestCase{ { caseDesc: "create cHash upstream with hash_on (cookie)", @@ -258,7 +258,7 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { resp.Body.Close() } -func TestUpstream_cHash_hash_on_consumer(t *testing.T) { +func TestUpstream_chash_hash_on_consumer(t *testing.T) { tests := []HttpTestCase{ { caseDesc: "create consumer with key-auth", @@ -283,7 +283,9 @@ func TestUpstream_cHash_hash_on_consumer(t *testing.T) { Path: "/apisix/admin/routes/1", Body: `{ "uri": "/server_port", - "plugin": {}, + "plugins": { + "key-auth": {} + }, "upstream": { "nodes": [{ "host": "172.16.238.20", From cba999d28827fc38aeed555e765ca4462e469be1 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 00:09:47 +0800 Subject: [PATCH 07/11] fix: `cHash` -> `chash` --- api/test/e2e/upstream_chash_hash_on_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index 62f494eb97..eb7451cc38 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -100,7 +100,7 @@ func TestUpstream_chash_hash_on_custom_header(t *testing.T) { func TestUpstream_chash_hash_on_cookie(t *testing.T) { tests := []HttpTestCase{ { - caseDesc: "create cHash upstream with hash_on (cookie)", + caseDesc: "create chash upstream with hash_on (cookie)", Object: ManagerApiExpect(t), Method: http.MethodPut, Path: "/apisix/admin/upstreams/1", @@ -190,7 +190,7 @@ func TestUpstream_chash_hash_on_cookie(t *testing.T) { func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { tests := []HttpTestCase{ { - caseDesc: "create cHash upstream with key contains uppercase letters and hyphen", + caseDesc: "create chash upstream with key contains uppercase letters and hyphen", Object: ManagerApiExpect(t), Method: http.MethodPut, Path: "/apisix/admin/upstreams/1", From 161759bc8473b1b889d6b01656ef4180b27670bd Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 15:31:04 +0800 Subject: [PATCH 08/11] update testcases --- api/test/e2e/upstream_chash_hash_on_test.go | 175 ++++++++++---------- 1 file changed, 83 insertions(+), 92 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index eb7451cc38..ff23ddb741 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -34,20 +34,20 @@ func TestUpstream_chash_hash_on_custom_header(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 - }], - "type": "chash", - "key": "custom_header", - "hash_on": "header" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "custom_header", + "hash_on": "header" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -57,9 +57,9 @@ func TestUpstream_chash_hash_on_custom_header(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/routes/1", Body: `{ - "uri": "/server_port", - "upstream_id": "1" - }`, + "uri": "/server_port", + "upstream_id": "1" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, Sleep: sleepTime, @@ -70,7 +70,7 @@ func TestUpstream_chash_hash_on_custom_header(t *testing.T) { testCaseCheck(tc) } - //hit routes + // hit routes time.Sleep(time.Duration(100) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request @@ -105,43 +105,30 @@ func TestUpstream_chash_hash_on_cookie(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 - }], - "type": "chash", - "key": "custom-cookie", - "hash_on": "cookie" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "custom-cookie", + "hash_on": "cookie" + }`, 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 + // hit routes time.Sleep(time.Duration(100) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request @@ -164,10 +151,11 @@ func TestUpstream_chash_hash_on_cookie(t *testing.T) { res[body] += 1 } } + // it is possible to hit any one of upstreams, and only one will be hit assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) resp.Body.Close() - //hit routes with miss cookie + // hit routes with miss cookie res = map[string]int{} for i := 0; i <= 3; i++ { url = basepath + "/server_port" @@ -183,6 +171,7 @@ func TestUpstream_chash_hash_on_cookie(t *testing.T) { res[body] += 1 } } + // it is possible to hit any one of upstreams, and only one will be hit assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) resp.Body.Close() } @@ -195,20 +184,20 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(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 - }], - "type": "chash", - "key": "X-Sessionid", - "hash_on": "header" - }`, + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "key": "X-Sessionid", + "hash_on": "header" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -218,9 +207,9 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/routes/1", Body: `{ - "uri": "/server_port", - "upstream_id": "1" - }`, + "uri": "/server_port", + "upstream_id": "1" + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, Sleep: sleepTime, @@ -231,7 +220,7 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { testCaseCheck(tc) } - //hit routes + // hit routes time.Sleep(time.Duration(100) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request @@ -254,6 +243,7 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(t *testing.T) { res[body] += 1 } } + // the X-Sessionid of each request is different, the weight of upstreams are the same, so these requests will be sent to each upstream equally assert.Equal(t, true, res["1980"] == 8 && res["1981"] == 8) resp.Body.Close() } @@ -266,13 +256,13 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/consumers", Body: `{ - "username": "jack", - "plugins": { - "key-auth": { - "key": "auth-jack" - } - } - }`, + "username": "jack", + "plugins": { + "key-auth": { + "key": "auth-jack" + } + } + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, }, @@ -282,25 +272,25 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) { Method: http.MethodPut, Path: "/apisix/admin/routes/1", Body: `{ - "uri": "/server_port", - "plugins": { - "key-auth": {} - }, - "upstream": { - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }, - { - "host": "172.16.238.20", - "port": 1981, - "weight": 1 - }], - "type": "chash", - "hash_on": "consumer" - } - }`, + "uri": "/server_port", + "plugins": { + "key-auth": {} + }, + "upstream": { + "nodes": [{ + "host": "172.16.238.20", + "port": 1980, + "weight": 1 + }, + { + "host": "172.16.238.20", + "port": 1981, + "weight": 1 + }], + "type": "chash", + "hash_on": "consumer" + } + }`, Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, Sleep: sleepTime, @@ -311,7 +301,7 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) { testCaseCheck(tc) } - //hit routes + // hit routes time.Sleep(time.Duration(100) * time.Millisecond) basepath := "http://127.0.0.1:9080" var req *http.Request @@ -334,6 +324,7 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) { res[body] += 1 } } + // it is possible to hit any one of upstreams, and only one will be hit assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) resp.Body.Close() } From 598f118e30e35952f6ce2bf6a5e84690e1c99632 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 15:37:07 +0800 Subject: [PATCH 09/11] remove useless steps --- api/test/e2e/upstream_chash_hash_on_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index ff23ddb741..a3713f598d 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -201,19 +201,6 @@ func TestUpstream_key_contains_uppercase_letters_and_hyphen(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 c8f87605c6eb449f22d486e8c6beaa51e5113b03 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 15:45:05 +0800 Subject: [PATCH 10/11] add todo --- api/test/e2e/upstream_chash_hash_on_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index a3713f598d..792e177acc 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/assert" ) +// todo: the code to access the route should be encapsulated as a function, like line 75-96, 134-154, 160-174, 212-233, 294-314 func TestUpstream_chash_hash_on_custom_header(t *testing.T) { tests := []HttpTestCase{ { From 8968f56d86ec37a6444204b498a6fc31fb023bdc Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 2 Dec 2020 16:33:14 +0800 Subject: [PATCH 11/11] add comment --- api/test/e2e/upstream_chash_hash_on_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/test/e2e/upstream_chash_hash_on_test.go b/api/test/e2e/upstream_chash_hash_on_test.go index 792e177acc..2619fefd1c 100644 --- a/api/test/e2e/upstream_chash_hash_on_test.go +++ b/api/test/e2e/upstream_chash_hash_on_test.go @@ -94,6 +94,7 @@ func TestUpstream_chash_hash_on_custom_header(t *testing.T) { res[body] += 1 } } + // it is possible to hit any one of upstreams, and only one will be hit assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4) resp.Body.Close() }