membphis commented on a change in pull request #1128:
URL: https://github.com/apache/apisix-dashboard/pull/1128#discussion_r554795005



##########
File path: api/test/e2e/service_test.go
##########
@@ -0,0 +1,259 @@
+/*
+ * 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 (
+       "fmt"
+       "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,
+               },
+               {
+                       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)
+       }
+
+       // hit routes

Review comment:
       bad comment

##########
File path: api/test/e2e/service_test.go
##########
@@ -0,0 +1,259 @@
+/*
+ * 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 (
+       "fmt"
+       "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,
+               },
+               {
+                       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)
+       }
+
+       // hit routes
+       time.Sleep(sleepTime)
+       // batch test /server_port api
+       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)
+       if err != nil {
+               fmt.Printf("server not responding %s", err.Error())

Review comment:
       I think we should throw an error here




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to