This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new ffcecfa  test: e2e test chash upstream with key (wrong key, vars) 
(#971)
ffcecfa is described below

commit ffcecfa7909b6a8b6a869fdd63b0e0542aa74bb5
Author: idbeta <[email protected]>
AuthorDate: Wed Dec 9 15:06:55 2020 +0800

    test: e2e test chash upstream with key (wrong key, vars) (#971)
    
    close #993
---
 api/test/e2e/upstream_chash_hash_on_test.go | 131 +++++++++++++++++++++++++++-
 1 file changed, 130 insertions(+), 1 deletion(-)

diff --git a/api/test/e2e/upstream_chash_hash_on_test.go 
b/api/test/e2e/upstream_chash_hash_on_test.go
index 2619fef..c0c927c 100644
--- a/api/test/e2e/upstream_chash_hash_on_test.go
+++ b/api/test/e2e/upstream_chash_hash_on_test.go
@@ -318,6 +318,135 @@ func TestUpstream_chash_hash_on_consumer(t *testing.T) {
        resp.Body.Close()
 }
 
+func TestUpstream_chash_hash_on_wrong_key(t *testing.T) {
+       tests := []HttpTestCase{
+               {
+                       caseDesc: "create chash upstream with wrong key",
+                       Object:   ManagerApiExpect(t),
+                       Method:   http.MethodPut,
+                       Path:     "/apisix/admin/upstreams/2",
+                       Body: `{
+                                       "nodes": [{
+                                               "host": "172.16.238.20",
+                                               "port": 1980,
+                                               "weight": 1
+                                       },
+                                       {
+                                               "host": "172.16.238.20",
+                                               "port": 1981,
+                                               "weight": 1
+                                       }],
+                                       "type": "chash",
+                                       "key": "not_support"
+                               }`,
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusBadRequest,
+                       ExpectBody:   "schema validate failed: (root): Does not 
match pattern 
'^((uri|server_name|server_addr|request_uri|remote_port|remote_addr|query_string|host|hostname)|arg_[0-9a-zA-z_-]+)",
+               },
+               {
+                       caseDesc:     "verify upstream with wrong key",
+                       Object:       ManagerApiExpect(t),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/routes/2",
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusNotFound,
+                       Sleep:        sleepTime,
+               },
+       }
+
+       for _, tc := range tests {
+               testCaseCheck(tc)
+       }
+
+}
+
+func TestUpstream_chash_hash_on_vars(t *testing.T) {
+       tests := []HttpTestCase{
+               {
+                       caseDesc: "create chash upstream hash_on (vars)",
+                       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",
+                                       "hash_on": "vars",
+                                       "key": "arg_device_id"
+                               }`,
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusOK,
+               },
+               {
+                       caseDesc:     "verify upstream",
+                       Object:       ManagerApiExpect(t),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/upstreams/1",
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   
"\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1},{\"host\":\"172.16.238.20\",\"port\":1981,\"weight\":1}],\"type\":\"chash\",\"hash_on\":\"vars\",\"key\":\"arg_device_id\"",
+                       Sleep:        sleepTime,
+               },
+               {
+                       caseDesc: "create route using the upstream just 
created",
+                       Object:   ManagerApiExpect(t),
+                       Method:   http.MethodPut,
+                       Path:     "/apisix/admin/routes/1",
+                       Body: `{
+                               "uri": "/server_port",
+                               "upstream_id": "1"
+                       }`,
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusOK,
+                       Sleep:        sleepTime,
+               },
+               {
+                       caseDesc:     "verify route",
+                       Object:       ManagerApiExpect(t),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/routes/1",
+                       Headers:      map[string]string{"Authorization": token},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   
"\"uri\":\"/server_port\",\"upstream_id\":\"1\"",
+                       Sleep:        sleepTime,
+               },
+       }
+
+       for _, tc := range tests {
+               testCaseCheck(tc)
+       }
+
+       // hit routes
+       time.Sleep(time.Duration(500) * time.Millisecond)
+       basepath := "http://127.0.0.1:9080";
+       var url string
+       var respBody []byte
+       res := map[string]int{}
+       for i := 0; i <= 17; i++ {
+               url = basepath + "/server_port?device_id=" + strconv.Itoa(i)
+               req, err := http.NewRequest("GET", url, nil)
+               resp, err := http.DefaultClient.Do(req)
+               assert.Nil(t, err)
+               respBody, err = ioutil.ReadAll(resp.Body)
+               body := string(respBody)
+               if _, ok := res[body]; !ok {
+                       res[body] = 1
+               } else {
+                       res[body] += 1
+               }
+               resp.Body.Close()
+       }
+       assert.True(t, res["1980"] == 9 && res["1981"] == 9)
+}
+
 func TestUpstream_Delete_hash_on(t *testing.T) {
        tests := []HttpTestCase{
                {
@@ -348,7 +477,7 @@ func TestUpstream_Delete_hash_on(t *testing.T) {
                        caseDesc:     "hit the route just deleted",
                        Object:       APISIXExpect(t),
                        Method:       http.MethodGet,
-                       Path:         "/hello1",
+                       Path:         "/server_port",
                        ExpectStatus: http.StatusNotFound,
                        ExpectBody:   "{\"error_msg\":\"404 Route Not 
Found\"}\n",
                        Sleep:        sleepTime,

Reply via email to