You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by st...@apache.org on 2021/03/02 10:29:14 UTC

[apisix-dashboard] branch master updated: feat: rewrite e2e test(upstream_chash_query_string_arg_xxx_test) with ginkgo (#1504)

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

starsz 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 a4c93e7  feat: rewrite e2e test(upstream_chash_query_string_arg_xxx_test) with ginkgo (#1504)
a4c93e7 is described below

commit a4c93e74391950c42c31d0851bae19a7034c50dc
Author: JinChen <36...@users.noreply.github.com>
AuthorDate: Tue Mar 2 18:29:04 2021 +0800

    feat: rewrite e2e test(upstream_chash_query_string_arg_xxx_test) with ginkgo (#1504)
---
 .../upstream_chash_query_string_arg_xxx_test.go    | 203 ------------------
 .../upstream_chash_query_string_arg_xxx_test.go    | 228 +++++++++++++++++++++
 2 files changed, 228 insertions(+), 203 deletions(-)

diff --git a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go
deleted file mode 100644
index b176692..0000000
--- a/api/test/e2e/upstream_chash_query_string_arg_xxx_test.go
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * 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"
-	"sort"
-	"strconv"
-	"testing"
-	"time"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestUpstream_chash_query_string(t *testing.T) {
-	tests := []HttpTestCase{
-		{
-			Desc:   "create chash upstream with key (query_string)",
-			Object: ManagerApiExpect(t),
-			Method: http.MethodPut,
-			Path:   "/apisix/admin/upstreams/1",
-			Body: `{
-					"nodes": [{
-						"host": "172.16.238.20",
-						"port": 1980,
-						"weight": 1
-					},
-					{
-						"host": "172.16.238.20",
-						"port": 1981,
-						"weight": 1
-					},
-					{
-						"host": "172.16.238.20",
-						"port": 1982,
-						"weight": 1
-					}],
-					"type": "chash",
-					"key": "query_string"
-				}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   []string{"\"id\":\"1\"", "\"key\":\"query_string\""},
-		},
-		{
-			Desc:   "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, t)
-	}
-
-	// hit routes
-	time.Sleep(time.Duration(500) * time.Millisecond)
-	basepath := "http://127.0.0.1:9080"
-	var url string
-	var respBody []byte
-	res := map[string]int{}
-	for i := 0; i < 180; i++ {
-		url = basepath + "/server_port?var=2&var2=" + strconv.Itoa(i)
-		req, err := http.NewRequest("GET", url, nil)
-		resp, err := http.DefaultClient.Do(req)
-		assert.Nil(t, err)
-		respBody, err = ioutil.ReadAll(resp.Body)
-		body := string(respBody)
-		if _, ok := res[body]; !ok {
-			res[body] = 1
-		} else {
-			res[body] += 1
-		}
-		resp.Body.Close()
-	}
-	var counts []int
-	for _, value := range res {
-		counts = append(counts, value)
-	}
-	sort.Ints(counts)
-	assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4) // todo: the result is unstable, fix it later
-}
-
-func TestUpstream_chash_arg_xxx(t *testing.T) {
-	tests := []HttpTestCase{
-		{
-			Desc:   "create chash upstream with key (arg_xxx)",
-			Object: ManagerApiExpect(t),
-			Method: http.MethodPut,
-			Path:   "/apisix/admin/upstreams/1",
-			Body: `{
-					"nodes": [{
-						"host": "172.16.238.20",
-						"port": 1980,
-						"weight": 1
-					},
-					{
-						"host": "172.16.238.20",
-						"port": 1981,
-						"weight": 1
-					},
-					{
-						"host": "172.16.238.20",
-						"port": 1982,
-						"weight": 1
-					}],
-					"type": "chash",
-					"key": "arg_device_id"
-				}`,
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-			ExpectBody:   []string{"\"id\":\"1\"", "\"key\":\"arg_device_id\""},
-		},
-	}
-
-	for _, tc := range tests {
-		testCaseCheck(tc, t)
-	}
-
-	// hit routes
-	time.Sleep(time.Duration(500) * time.Millisecond)
-	basepath := "http://127.0.0.1:9080"
-	var url string
-	var respBody []byte
-	res := map[string]int{}
-	for i := 0; i <= 17; i++ {
-		url = basepath + "/server_port?device_id=" + strconv.Itoa(i)
-		req, err := http.NewRequest("GET", url, nil)
-		resp, err := http.DefaultClient.Do(req)
-		assert.Nil(t, err)
-		respBody, err = ioutil.ReadAll(resp.Body)
-		body := string(respBody)
-		if _, ok := res[body]; !ok {
-			res[body] = 1
-		} else {
-			res[body] += 1
-		}
-		resp.Body.Close()
-	}
-	var counts []int
-	for _, value := range res {
-		counts = append(counts, value)
-	}
-	sort.Ints(counts)
-	assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4) // todo: the result is unstable, fix it later
-}
-
-func TestUpstream_Delete_chash(t *testing.T) {
-	tests := []HttpTestCase{
-		{
-			Desc:         "delete route",
-			Object:       ManagerApiExpect(t),
-			Method:       http.MethodDelete,
-			Path:         "/apisix/admin/routes/1",
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-		},
-		{
-			Desc:         "delete upstream",
-			Object:       ManagerApiExpect(t),
-			Method:       http.MethodDelete,
-			Path:         "/apisix/admin/upstreams/1",
-			Headers:      map[string]string{"Authorization": token},
-			ExpectStatus: http.StatusOK,
-		},
-		{
-			Desc:         "hit the route just deleted",
-			Object:       APISIXExpect(t),
-			Method:       http.MethodGet,
-			Path:         "/hello1",
-			ExpectStatus: http.StatusNotFound,
-			ExpectBody:   "{\"error_msg\":\"404 Route Not Found\"}\n",
-			Sleep:        sleepTime,
-		},
-	}
-
-	for _, tc := range tests {
-		testCaseCheck(tc, t)
-	}
-}
diff --git a/api/test/e2enew/upstream/upstream_chash_query_string_arg_xxx_test.go b/api/test/e2enew/upstream/upstream_chash_query_string_arg_xxx_test.go
new file mode 100644
index 0000000..8bd8a3d
--- /dev/null
+++ b/api/test/e2enew/upstream/upstream_chash_query_string_arg_xxx_test.go
@@ -0,0 +1,228 @@
+/*
+ * 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 upstream
+
+import (
+	"encoding/json"
+	"io/ioutil"
+	"net/http"
+	"sort"
+	"strconv"
+	"time"
+
+	"github.com/onsi/ginkgo"
+	"github.com/stretchr/testify/assert"
+
+	"e2enew/base"
+)
+
+var createUpstreamBody map[string]interface{} = map[string]interface{}{
+	"nodes": []map[string]interface{}{
+		{
+			"host":   base.UpstreamIp,
+			"port":   1980,
+			"weight": 1,
+		},
+		{
+			"host":   base.UpstreamIp,
+			"port":   1981,
+			"weight": 1,
+		},
+		{
+			"host":   base.UpstreamIp,
+			"port":   1982,
+			"weight": 1,
+		},
+	},
+	"type": "chash",
+}
+
+var _ = ginkgo.Describe("Upstream chash query string", func() {
+	ginkgo.It("create chash upstream with key (query_string)", func() {
+		t := ginkgo.GinkgoT()
+		createUpstreamBody["key"] = "query_string"
+		_createUpstreamBody, err := json.Marshal(createUpstreamBody)
+		assert.Nil(t, err)
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodPut,
+			Path:         "/apisix/admin/upstreams/1",
+			Body:         string(_createUpstreamBody),
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   []string{"\"id\":\"1\"", "\"key\":\"query_string\""},
+		})
+	})
+	ginkgo.It("create route using the upstream just created", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object: base.ManagerApiExpect(),
+			Method: http.MethodPut,
+			Path:   "/apisix/admin/routes/1",
+			Body: `{
+				"uri": "/server_port",
+				"upstream_id": "1"
+			}`,
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+			Sleep:        base.SleepTime,
+		})
+	})
+	ginkgo.It("hit routes(upstream query_string)", func() {
+		t := ginkgo.GinkgoT()
+		time.Sleep(time.Duration(500) * time.Millisecond)
+		basepath := base.APISIXHost
+		res := map[string]int{}
+		for i := 0; i < 180; i++ {
+			url := basepath + "/server_port?var=2&var2=" + strconv.Itoa(i)
+			req, err := http.NewRequest("GET", url, nil)
+			assert.Nil(t, err)
+			resp, err := http.DefaultClient.Do(req)
+			assert.Nil(t, err)
+			defer resp.Body.Close()
+			respBody, err := ioutil.ReadAll(resp.Body)
+			assert.Nil(t, err)
+			body := string(respBody)
+			if _, ok := res[body]; !ok {
+				res[body] = 1
+			} else {
+				res[body]++
+			}
+		}
+		var counts []int
+		for _, value := range res {
+			counts = append(counts, value)
+		}
+		sort.Ints(counts)
+		assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4)
+	})
+	ginkgo.It("delete route", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/1",
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+		})
+	})
+	ginkgo.It("delete upstream", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/upstreams/1",
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+		})
+	})
+	ginkgo.It("hit the route just deleted", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.APISIXExpect(),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   "{\"error_msg\":\"404 Route Not Found\"}\n",
+			Sleep:        base.SleepTime,
+		})
+	})
+})
+
+var _ = ginkgo.Describe("Upstream chash query string", func() {
+	ginkgo.It("create chash upstream with key (arg_xxx)", func() {
+		t := ginkgo.GinkgoT()
+		createUpstreamBody["key"] = "arg_device_id"
+		_createUpstreamBody, err := json.Marshal(createUpstreamBody)
+		assert.Nil(t, err)
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodPut,
+			Path:         "/apisix/admin/upstreams/1",
+			Body:         string(_createUpstreamBody),
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   []string{"\"id\":\"1\"", "\"key\":\"arg_device_id\""},
+		})
+	})
+	ginkgo.It("create route using the upstream just created", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object: base.ManagerApiExpect(),
+			Method: http.MethodPut,
+			Path:   "/apisix/admin/routes/1",
+			Body: `{
+				"uri": "/server_port",
+				"upstream_id": "1"
+			}`,
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+			Sleep:        base.SleepTime,
+		})
+	})
+	ginkgo.It("hit routes(upstream arg_device_id)", func() {
+		t := ginkgo.GinkgoT()
+		time.Sleep(time.Duration(500) * time.Millisecond)
+		basepath := base.APISIXHost
+		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)
+			assert.Nil(t, err)
+			resp, err := http.DefaultClient.Do(req)
+			assert.Nil(t, err)
+			defer resp.Body.Close()
+			respBody, err := ioutil.ReadAll(resp.Body)
+			assert.Nil(t, err)
+			body := string(respBody)
+			if _, ok := res[body]; !ok {
+				res[body] = 1
+			} else {
+				res[body]++
+			}
+		}
+		var counts []int
+		for _, value := range res {
+			counts = append(counts, value)
+		}
+		sort.Ints(counts)
+		assert.True(t, float64(counts[2]-counts[0])/float64(counts[1]) < 0.4)
+	})
+	ginkgo.It("delete route", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/1",
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+		})
+	})
+	ginkgo.It("delete upstream", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.ManagerApiExpect(),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/upstreams/1",
+			Headers:      map[string]string{"Authorization": base.GetToken()},
+			ExpectStatus: http.StatusOK,
+		})
+	})
+	ginkgo.It("hit the route just deleted", func() {
+		base.RunTestCase(base.HttpTestCase{
+			Object:       base.APISIXExpect(),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   "{\"error_msg\":\"404 Route Not Found\"}\n",
+			Sleep:        base.SleepTime,
+		})
+	})
+})