This is an automated email from the ASF dual-hosted git repository.
chenjunxu 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 845cf99 feat: rewrite e2e test(service_test) with ginkgo (#1545)
845cf99 is described below
commit 845cf9915e1538f356f35f338a5d577747ba14a0
Author: JinChen <[email protected]>
AuthorDate: Wed Mar 3 15:26:54 2021 +0800
feat: rewrite e2e test(service_test) with ginkgo (#1545)
---
api/test/e2e/service_test.go | 357 --------------------
api/test/e2enew/service/service_suite_test.go | 36 ++
api/test/e2enew/service/service_test.go | 464 ++++++++++++++++++++++++++
3 files changed, 500 insertions(+), 357 deletions(-)
diff --git a/api/test/e2e/service_test.go b/api/test/e2e/service_test.go
deleted file mode 100644
index 9e03fca..0000000
--- a/api/test/e2e/service_test.go
+++ /dev/null
@@ -1,357 +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 (
- "net/http"
- "testing"
- "time"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestService(t *testing.T) {
- tests := []HttpTestCase{
- {
- Desc: "create service without plugin",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": token},
- Body: `{
- "name": "testservice",
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "172.16.238.20",
- "port": 1980,
- "weight": 1
- },
- {
- "host": "172.16.238.20",
- "port": 1981,
- "weight": 2
- },
- {
- "host": "172.16.238.20",
- "port": 1982,
- "weight": 3
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- ExpectBody: []string{"\"id\":\"s1\"",
"\"name\":\"testservice\""},
- },
- {
- Desc: "get the service s1",
- Object: ManagerApiExpect(t),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": token},
- ExpectCode: http.StatusOK,
- ExpectBody:
"\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1},{\"host\":\"172.16.238.20\",\"port\":1981,\"weight\":2},{\"host\":\"172.16.238.20\",\"port\":1982,\"weight\":3}],\"type\":\"roundrobin\"}",
- },
- {
- Desc: "create route using the service just created",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "uri": "/server_port",
- "service_id": "s1"
- }`,
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- Sleep: sleepTime,
- },
- }
- for _, tc := range tests {
- testCaseCheck(tc, t)
- }
-
- // batch test /server_port api
- time.Sleep(sleepTime)
- res := BatchTestServerPort(t, 18)
- assert.True(t, res["1980"] == 3)
- assert.True(t, res["1981"] == 6)
- assert.True(t, res["1982"] == 9)
-
- tests = []HttpTestCase{
- {
- Desc: "create service with plugin",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": token},
- Body: `{
- "name": "testservice",
- "plugins": {
- "limit-count": {
- "count": 100,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr"
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "172.16.238.20",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- },
- {
- Desc: "get the service s1",
- Object: ManagerApiExpect(t),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": token},
- ExpectCode: http.StatusOK,
- ExpectBody:
"\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}}",
- },
- {
- Desc: "create route using the service just created",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "uri": "/server_port",
- "service_id": "s1"
- }`,
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- Sleep: sleepTime,
- },
- }
- for _, tc := range tests {
- testCaseCheck(tc, t)
- }
-
- // hit routes and check the response header
- time.Sleep(sleepTime)
- basepath := "http://127.0.0.1:9080"
- request, _ := http.NewRequest("GET", basepath+"/server_port", nil)
- request.Header.Add("Authorization", token)
- resp, err := http.DefaultClient.Do(request)
- assert.Nil(t, err)
- defer resp.Body.Close()
- assert.Equal(t, 200, resp.StatusCode)
- assert.Equal(t, "100", resp.Header["X-Ratelimit-Limit"][0])
- assert.Equal(t, "99", resp.Header["X-Ratelimit-Remaining"][0])
-
- // Create another service(id=s2) via HTTP POST to check the
- // returned value, still attached to route(id=r1)
- tests = []HttpTestCase{
- {
- Desc: "create service with all options via POST
method",
- Object: ManagerApiExpect(t),
- Method: http.MethodPost,
- Path: "/apisix/admin/services",
- Headers: map[string]string{"Authorization": token},
- Body: `{
- "id": "s2",
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "enable_websocket":true,
- "plugins": {
- "limit-count": {
- "count": 100,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr"
- }
- },
- "upstream": {
- "type": "roundrobin",
- "create_time":1602883670,
- "update_time":1602893670,
- "nodes": [{
- "host": "172.16.238.20",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- ExpectBody: "\"id\":\"s2\"",
- },
- {
- Desc: "get the service s2",
- Object: ManagerApiExpect(t),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": token},
- ExpectCode: http.StatusOK,
- ExpectBody:
"\"name\":\"testservice\",\"desc\":\"testservice_desc\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"enable_websocket\":true}",
- },
- {
- Desc: "create route using the service just created",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "uri": "/hello",
- "service_id": "s2"
- }`,
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- Sleep: sleepTime,
- },
- {
- Desc: "verify route",
- Object: APISIXExpect(t),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusOK,
- ExpectBody: "hello world",
- },
- }
- for _, tc := range tests {
- testCaseCheck(tc, t)
- }
-}
-
-func TestService_Teardown(t *testing.T) {
- tests := []HttpTestCase{
- {
- // Delete the service created via HTTP PUT
- Desc: "delete the service(id=s1)",
- Object: ManagerApiExpect(t),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- Sleep: sleepTime,
- },
- {
- // Delete the service created via HTTP POST
- Desc: "delete the service(id=s2)",
- Object: ManagerApiExpect(t),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- Sleep: sleepTime,
- },
- {
- Desc: "delete route",
- Object: ManagerApiExpect(t),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- },
- }
- for _, tc := range tests {
- testCaseCheck(tc, t)
- }
-}
-
-func TestService_Update_Use_Patch_Method(t *testing.T) {
- tests := []HttpTestCase{
- {
- Desc: "create service without plugin",
- Object: ManagerApiExpect(t),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": token},
- Body: `{
- "name": "testservice",
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "172.16.238.20",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- ExpectBody:
"\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
- },
- {
- Desc: "update service use patch method",
- Object: ManagerApiExpect(t),
- Method: http.MethodPatch,
- Path: "/apisix/admin/services/s5",
- Body: `{
- "name": "testpatch",
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "172.16.238.20",
- "port": 1981,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- },
- {
- Desc: "get the service s5",
- Object: ManagerApiExpect(t),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": token},
- ExpectCode: http.StatusOK,
- ExpectBody:
"\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1981,\"weight\":1}],\"type\":\"roundrobin\"}}",
- },
- {
- Desc: "Update service using path parameter patch
method",
- Object: ManagerApiExpect(t),
- Method: http.MethodPatch,
- Path: "/apisix/admin/services/s5/upstream",
- Body:
`{"type":"roundrobin","nodes":[{"host":"172.16.238.20","port":1980,"weight":1}]}`,
- Headers: map[string]string{
- "Authorization": token,
- "Content-Type": "text/plain",
- },
- ExpectStatus: http.StatusOK,
- },
- {
- Desc: "get service data",
- Object: ManagerApiExpect(t),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- ExpectBody:
"\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"172.16.238.20\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
- },
- {
- Desc: "delete service",
- Object: ManagerApiExpect(t),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": token},
- ExpectStatus: http.StatusOK,
- },
- }
-
- for _, tc := range tests {
- testCaseCheck(tc, t)
- }
-}
diff --git a/api/test/e2enew/service/service_suite_test.go
b/api/test/e2enew/service/service_suite_test.go
new file mode 100644
index 0000000..0e75694
--- /dev/null
+++ b/api/test/e2enew/service/service_suite_test.go
@@ -0,0 +1,36 @@
+/*
+ * 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 service
+
+import (
+ "testing"
+ "time"
+
+ "github.com/onsi/ginkgo"
+
+ "e2enew/base"
+)
+
+func TestRoute(t *testing.T) {
+ ginkgo.RunSpecs(t, "service suite")
+}
+
+var _ = ginkgo.AfterSuite(func() {
+ base.CleanResource("services")
+ base.CleanResource("routes")
+ time.Sleep(base.SleepTime)
+})
diff --git a/api/test/e2enew/service/service_test.go
b/api/test/e2enew/service/service_test.go
new file mode 100644
index 0000000..ac29374
--- /dev/null
+++ b/api/test/e2enew/service/service_test.go
@@ -0,0 +1,464 @@
+/*
+ * 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 service
+
+import (
+ "encoding/json"
+ "net/http"
+ "time"
+
+ "github.com/onsi/ginkgo"
+ "github.com/stretchr/testify/assert"
+
+ "e2enew/base"
+)
+
+var _ = ginkgo.Describe("create service without plugin", func() {
+ ginkgo.It("create service without plugin", func() {
+ t := ginkgo.GinkgoT()
+ var createServiceBody map[string]interface{} =
map[string]interface{}{
+ "name": "testservice",
+ "upstream": map[string]interface{}{
+ "type": "roundrobin",
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1980,
+ "weight": 1,
+ },
+ {
+ "host": base.UpstreamIp,
+ "port": 1981,
+ "weight": 2,
+ },
+ {
+ "host": base.UpstreamIp,
+ "port": 1982,
+ "weight": 3,
+ },
+ },
+ },
+ }
+ _createServiceBody, err := json.Marshal(createServiceBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ Body: string(_createServiceBody),
+ ExpectStatus: http.StatusOK,
+ ExpectBody: []string{"\"id\":\"s1\"",
"\"name\":\"testservice\""},
+ })
+ })
+ ginkgo.It("get the service s1", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectCode: http.StatusOK,
+ ExpectBody:
"\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1980,\"weight\":1},{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1981,\"weight\":2},{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1982,\"weight\":3}],\"type\":\"roundrobin\"}",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("create route using the service just created", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "uri": "/server_port",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("batch test /server_port api", func() {
+ t := ginkgo.GinkgoT()
+ time.Sleep(time.Duration(500) * time.Millisecond)
+ res := base.BatchTestServerPort(18)
+ assert.True(t, res["1980"] == 3)
+ assert.True(t, res["1981"] == 6)
+ assert.True(t, res["1982"] == 9)
+ })
+ ginkgo.It("delete route", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ })
+ })
+ ginkgo.It("delete service", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("hit the route just deleted", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.APISIXExpect(),
+ Method: http.MethodGet,
+ Path: "/server_port",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not
Found\"}\n",
+ Sleep: base.SleepTime,
+ })
+ })
+})
+
+var _ = ginkgo.Describe("create service with plugin", func() {
+ ginkgo.It("create service without plugin", func() {
+ t := ginkgo.GinkgoT()
+ var createServiceBody map[string]interface{} =
map[string]interface{}{
+ "name": "testservice",
+ "plugins": map[string]interface{}{
+ "limit-count": map[string]interface{}{
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr",
+ },
+ },
+ "upstream": map[string]interface{}{
+ "type": "roundrobin",
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1980,
+ "weight": 1,
+ },
+ },
+ },
+ }
+ _createServiceBody, err := json.Marshal(createServiceBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ Body: string(_createServiceBody),
+ ExpectStatus: http.StatusOK,
+ ExpectBody: []string{"\"id\":\"s1\"",
"\"name\":\"testservice\""},
+ })
+ })
+ ginkgo.It("get the service s1", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectCode: http.StatusOK,
+ ExpectBody: "\"upstream\":{\"nodes\":[{\"host\":\"" +
base.UpstreamIp +
"\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}}",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("create route using the service just created", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "uri": "/server_port",
+ "service_id": "s1"
+ }`,
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It(" hit routes and check the response header", func() {
+ t := ginkgo.GinkgoT()
+ time.Sleep(time.Duration(500) * time.Millisecond)
+ basepath := base.APISIXHost
+ request, err := http.NewRequest("GET", basepath+"/server_port",
nil)
+ assert.Nil(t, err)
+ request.Header.Add("Authorization", base.GetToken())
+ resp, err := http.DefaultClient.Do(request)
+ assert.Nil(t, err)
+ defer resp.Body.Close()
+ assert.Equal(t, 200, resp.StatusCode)
+ assert.Equal(t, "100", resp.Header["X-Ratelimit-Limit"][0])
+ assert.Equal(t, "99", resp.Header["X-Ratelimit-Remaining"][0])
+ })
+ ginkgo.It("delete route", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ })
+ })
+ ginkgo.It("delete service", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("hit the route just deleted", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.APISIXExpect(),
+ Method: http.MethodGet,
+ Path: "/server_port",
+ ExpectStatus: http.StatusNotFound,
+ ExpectBody: "{\"error_msg\":\"404 Route Not
Found\"}\n",
+ Sleep: base.SleepTime,
+ })
+ })
+})
+
+var _ = ginkgo.Describe("create service with all options via POST method",
func() {
+ ginkgo.It("create service with all options via POST method", func() {
+ t := ginkgo.GinkgoT()
+ var createServiceBody map[string]interface{} =
map[string]interface{}{
+ "id": "s2",
+ "name": "testservice",
+ "desc": "testservice_desc",
+ "labels": map[string]interface{}{
+ "build": "16",
+ "env": "production",
+ "version": "v2",
+ },
+ "enable_websocket": true,
+ "plugins": map[string]interface{}{
+ "limit-count": map[string]interface{}{
+ "count": 100,
+ "time_window": 60,
+ "rejected_code": 503,
+ "key": "remote_addr",
+ },
+ },
+ "upstream": map[string]interface{}{
+ "type": "roundrobin",
+ "create_time": 1602883670,
+ "update_time": 1602893670,
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1980,
+ "weight": 1,
+ },
+ },
+ },
+ }
+ _createServiceBody, err := json.Marshal(createServiceBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Desc: "create service with all options via POST
method",
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPost,
+ Path: "/apisix/admin/services",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ Body: string(_createServiceBody),
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "\"id\":\"s2\"",
+ })
+ })
+ ginkgo.It("get the service s2", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectCode: http.StatusOK,
+ ExpectBody:
"\"name\":\"testservice\",\"desc\":\"testservice_desc\",\"upstream\":{\"nodes\":[{\"host\":\""
+ base.UpstreamIp +
"\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"},\"plugins\":{\"limit-count\":{\"count\":100,\"key\":\"remote_addr\",\"rejected_code\":503,\"time_window\":60}},\"labels\":{\"build\":\"16\",\"env\":\"production\",\"version\":\"v2\"},\"enable_websocket\":true}",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("create route using the service just created", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/routes/r1",
+ Body: `{
+ "uri": "/hello",
+ "service_id": "s2"
+ }`,
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("verify route", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.APISIXExpect(),
+ Method: http.MethodGet,
+ Path: "/hello",
+ ExpectStatus: http.StatusOK,
+ ExpectBody: "hello world",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("delete route", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/routes/r1",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ })
+ })
+ ginkgo.It("delete service", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s2",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ 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("service update use patch method", func() {
+ ginkgo.It("create service without plugin", func() {
+ t := ginkgo.GinkgoT()
+ var createServiceBody map[string]interface{} =
map[string]interface{}{
+ "name": "testservice",
+ "upstream": map[string]interface{}{
+ "type": "roundrobin",
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1980,
+ "weight": 1,
+ },
+ },
+ },
+ }
+ _createServiceBody, err := json.Marshal(createServiceBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Desc: "create service without plugin",
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPut,
+ Path: "/apisix/admin/services/s5",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ Body: string(_createServiceBody),
+ ExpectStatus: http.StatusOK,
+ ExpectBody:
"\"name\":\"testservice\",\"upstream\":{\"nodes\":[{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
+ })
+ })
+ ginkgo.It("update service use patch method", func() {
+ t := ginkgo.GinkgoT()
+ var createServiceBody map[string]interface{} =
map[string]interface{}{
+ "name": "testpatch",
+ "upstream": map[string]interface{}{
+ "type": "roundrobin",
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1981,
+ "weight": 1,
+ },
+ },
+ },
+ }
+ _createServiceBody, err := json.Marshal(createServiceBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPatch,
+ Path: "/apisix/admin/services/s5",
+ Body: string(_createServiceBody),
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("get the service s5", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s5",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectCode: http.StatusOK,
+ ExpectBody:
"\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1981,\"weight\":1}],\"type\":\"roundrobin\"}}",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("Update service using path parameter patch method", func() {
+ t := ginkgo.GinkgoT()
+ var createUpstreamBody map[string]interface{} =
map[string]interface{}{
+ "type": "roundrobin",
+ "nodes": []map[string]interface{}{
+ {
+ "host": base.UpstreamIp,
+ "port": 1980,
+ "weight": 1,
+ },
+ },
+ }
+ _createUpstreamBody, err := json.Marshal(createUpstreamBody)
+ assert.Nil(t, err)
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodPatch,
+ Path: "/apisix/admin/services/s5/upstream",
+ Body: string(_createUpstreamBody),
+ Headers: map[string]string{
+ "Authorization": base.GetToken(),
+ "Content-Type": "text/plain",
+ },
+ ExpectStatus: http.StatusOK,
+ })
+ })
+ ginkgo.It("get service data", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodGet,
+ Path: "/apisix/admin/services/s5",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ ExpectBody:
"\"name\":\"testpatch\",\"upstream\":{\"nodes\":[{\"host\":\"" +
base.UpstreamIp + "\",\"port\":1980,\"weight\":1}],\"type\":\"roundrobin\"}}",
+ Sleep: base.SleepTime,
+ })
+ })
+ ginkgo.It("delete service", func() {
+ base.RunTestCase(base.HttpTestCase{
+ Object: base.ManagerApiExpect(),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/services/s5",
+ Headers: map[string]string{"Authorization":
base.GetToken()},
+ ExpectStatus: http.StatusOK,
+ })
+ })
+})