You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/01 08:37:32 UTC
[GitHub] [apisix-dashboard] idbeta opened a new pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
idbeta opened a new pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936
Please answer these questions before submitting a pull request
- Why submit this pull request?
- [x] E2E test
- [ ] New feature provided
- [ ] Improve performance
- Related issues
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] idbeta commented on a change in pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
idbeta commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533929008
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
I will improve this in the future.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (edd0dd5) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/18744a5082c4286a62503f35f2e1c60656262cc3?el=desc) (18744a5) will **increase** coverage by `0.27%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
==========================================
+ Coverage 42.82% 43.10% +0.27%
==========================================
Files 18 18
Lines 1289 1290 +1
==========================================
+ Hits 552 556 +4
+ Misses 645 642 -3
Partials 92 92
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/format.go](https://codecov.io/gh/apache/apisix-dashboard/pull/936/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2Zvcm1hdC5nbw==) | `60.46% <0.00%> (+8.08%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [18744a5...edd0dd5](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] idbeta commented on a change in pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
idbeta commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533980093
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
I add it to the todo list.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (e5db44e) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/95f26122fee30d2fcbd18d98433b106c09e9bdca?el=desc) (95f2612) will **decrease** coverage by `0.07%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
==========================================
- Coverage 43.10% 43.02% -0.08%
==========================================
Files 18 18
Lines 1290 1290
==========================================
- Hits 556 555 -1
- Misses 642 643 +1
Partials 92 92
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/936/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `78.57% <0.00%> (-0.65%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [95f2612...e5db44e](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] idbeta commented on a change in pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
idbeta commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533256004
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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)
Review comment:
fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis commented on a change in pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533838388
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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
Review comment:
style
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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,
+ },
+ {
Review comment:
I think we can delete this step, same as: https://github.com/apache/apisix-dashboard/pull/936/files#diff-d07aed971e7a3caa6a1ae0527badd308d4be4053dafaa873aec158761b6c426cR55
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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"
+ 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"
+ 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",
+ "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,
+ },
+ }
+
+ 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
+ }
+ }
+ assert.Equal(t, true, res["1980"] == 4 || res["1981"] == 4)
Review comment:
ditto
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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"
+ 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"
+ 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",
Review comment:
ditto
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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"
+ 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"
+ 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)
Review comment:
need some comment, it is not easy to understand
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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"
+ 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"
+ 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)
Review comment:
ditto
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (a99c0ac) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/18744a5082c4286a62503f35f2e1c60656262cc3?el=desc) (18744a5) will **increase** coverage by `0.27%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
==========================================
+ Coverage 42.82% 43.10% +0.27%
==========================================
Files 18 18
Lines 1289 1290 +1
==========================================
+ Hits 552 556 +4
+ Misses 645 642 -3
Partials 92 92
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/format.go](https://codecov.io/gh/apache/apisix-dashboard/pull/936/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2Zvcm1hdC5nbw==) | `60.46% <0.00%> (+8.08%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [18744a5...cba999d](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis merged pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
membphis merged pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] idbeta commented on a change in pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
idbeta commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533952356
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,381 @@
+/*
+ * 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"
+
+ "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,
+ },
+ {
Review comment:
fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (279a993) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/18744a5082c4286a62503f35f2e1c60656262cc3?el=desc) (18744a5) will **decrease** coverage by `0.07%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
==========================================
- Coverage 42.82% 42.74% -0.08%
==========================================
Files 18 18
Lines 1289 1289
==========================================
- Hits 552 551 -1
- Misses 645 646 +1
Partials 92 92
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/store/store.go](https://codecov.io/gh/apache/apisix-dashboard/pull/936/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvc3RvcmUvc3RvcmUuZ28=) | `78.57% <0.00%> (-0.65%)` | :arrow_down: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [18744a5...279a993](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] membphis commented on a change in pull request #936: test: e2e test chash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533963206
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,360 @@
+/*
+ * 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"
+
+ "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{
+ {
+ 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)
Review comment:
need a comment too
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io edited a comment on pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (3863f65) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/18744a5082c4286a62503f35f2e1c60656262cc3?el=desc) (18744a5) will **increase** coverage by `0.27%`.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
==========================================
+ Coverage 42.82% 43.10% +0.27%
==========================================
Files 18 18
Lines 1289 1290 +1
==========================================
+ Hits 552 556 +4
+ Misses 645 642 -3
Partials 92 92
```
| [Impacted Files](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/internal/core/entity/format.go](https://codecov.io/gh/apache/apisix-dashboard/pull/936/diff?src=pr&el=tree#diff-YXBpL2ludGVybmFsL2NvcmUvZW50aXR5L2Zvcm1hdC5nbw==) | `60.46% <0.00%> (+8.08%)` | :arrow_up: |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [18744a5...edd0dd5](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] codecov-io commented on pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#issuecomment-736325325
# [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=h1) Report
> Merging [#936](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=desc) (220a7bc) into [master](https://codecov.io/gh/apache/apisix-dashboard/commit/18744a5082c4286a62503f35f2e1c60656262cc3?el=desc) (18744a5) will **not change** coverage.
> The diff coverage is `n/a`.
[](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #936 +/- ##
=======================================
Coverage 42.82% 42.82%
=======================================
Files 18 18
Lines 1289 1289
=======================================
Hits 552 552
Misses 645 645
Partials 92 92
```
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=footer). Last update [18744a5...220a7bc](https://codecov.io/gh/apache/apisix-dashboard/pull/936?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #936: test: e2e test cHash upstream hash on (consumer, header, cookie)
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #936:
URL: https://github.com/apache/apisix-dashboard/pull/936#discussion_r533198351
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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)
Review comment:
here is testing cookie ? why has args ?
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
we could put them into a function and reuse it
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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)
Review comment:
ditto
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
reuse.
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
ditto
##########
File path: api/test/e2e/upstream_chash_hash_on_test.go
##########
@@ -0,0 +1,379 @@
+/*
+ * 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"
+
+ "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
+ }
+ }
Review comment:
could use a same function with above cases.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org