zaunist commented on a change in pull request #2200: URL: https://github.com/apache/apisix-dashboard/pull/2200#discussion_r762761431
########## File path: api/test/e2enew/route/route_export_test.go ########## @@ -0,0 +1,2490 @@ +/* + * 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 route + +import ( + "net/http" + "strings" + "time" + + "github.com/onsi/ginkgo" + "github.com/stretchr/testify/assert" + + "github.com/apisix/manager-api/test/e2enew/base" +) + +var _ = ginkgo.Describe("Route", func() { + ginkgo.Context("test route export data empty", func() { + ginkgo.It("Export route when data is empty", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/apisix/admin/export/routes", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + ExpectBody: "{\"code\":10000,\"message\":\"Route data is empty, cannot be exported\",\"data\":null", + }) + }) + }) + + ginkgo.Context("test route export", func() { + // 1.Export data as the route of URIs Hosts + exportStrR1 := ` + "/hello_": { + "get": { + "operationId": "aaaaGET", + "requestBody": {}, + "responses": { + "default": { + "description": "" + } + }, + "security": [], + "x-apisix-enable_websocket": false, + "x-apisix-hosts": ["foo.com", "*.bar.com"], + "x-apisix-labels": { + "build": "16", + "env": "production", + "version": "v2" + }, + "x-apisix-plugins": { + "limit-count": { + "count": 2, + "key": "remote_addr", + "rejected_code": 503, + "time_window": 60 + } + }, + "x-apisix-priority": 0, + "x-apisix-status": 1, + "x-apisix-upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + }, + "post": { + "operationId": "aaaaPOST", + "requestBody": {}, + "responses": { + "default": { + "description": "" + } + }, + "security": [], + "x-apisix-enable_websocket": false, + "x-apisix-hosts": ["foo.com", "*.bar.com"], + "x-apisix-labels": { + "build": "16", + "env": "production", + "version": "v2" + }, + "x-apisix-plugins": { + "limit-count": { + "count": 2, + "key": "remote_addr", + "rejected_code": 503, + "time_window": 60 + } + }, + "x-apisix-priority": 0, + "x-apisix-status": 1, + "x-apisix-upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + } + }` + exportStrR1 = replaceStr(exportStrR1) + ginkgo.It("hit route that not exist", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello_", + Headers: map[string]string{"Host": "foo.com"}, + ExpectStatus: http.StatusNotFound, + ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n", + }) + }) + ginkgo.It("create route with uris and hosts to test whether the uris parsing is correct", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: `{ + "name": "aaaa", + "labels": { + "build":"16", + "env":"production", + "version":"v2" + }, + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + } + }, + "status": 1, + "uris": ["/hello_"], + "hosts": ["foo.com", "*.bar.com"], + "methods": ["GET", "POST"], + "upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + + ginkgo.It("export route", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/apisix/admin/export/routes/r1", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR1 + "}}", + }) + }) + // 2.Export data as the route of URI host + exportStrR2 := ` + "/hello2": { + "get": { + "operationId": "aaaa2GET", + "requestBody": {}, + "responses": { + "default": { + "description": "" + } + }, + "security": [], + "x-apisix-enable_websocket": false, + "x-apisix-host": "*.bar.com", + "x-apisix-labels": { + "build": "16", + "env": "production", + "version": "v2" + }, + "x-apisix-plugins": { + "limit-count": { + "count": 2, + "key": "remote_addr", + "rejected_code": 503, + "time_window": 60 + } + }, + "x-apisix-priority": 0, + "x-apisix-status": 1, + "x-apisix-upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + }, + "post": { + "operationId": "aaaa2POST", + "requestBody": {}, + "responses": { + "default": { + "description": "" + } + }, + "security": [], + "x-apisix-enable_websocket": false, + "x-apisix-host": "*.bar.com", + "x-apisix-labels": { + "build": "16", + "env": "production", + "version": "v2" + }, + "x-apisix-plugins": { + "limit-count": { + "count": 2, + "key": "remote_addr", + "rejected_code": 503, + "time_window": 60 + } + }, + "x-apisix-priority": 0, + "x-apisix-status": 1, + "x-apisix-upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + } + }` + exportStrR2 = replaceStr(exportStrR2) + + ginkgo.It("hit route2 that not exist", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello2", + Headers: map[string]string{"Host": "bar.com"}, + ExpectStatus: http.StatusNotFound, + ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n", + }) + }) + ginkgo.It("create route2 with uri and host to test whether the uri parsing is correct", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r2", + Body: `{ + "name": "aaaa2", + "labels": { + "build":"16", + "env":"production", + "version":"v2" + }, + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + } + }, + "status": 1, + "uri": "/hello2", + "host": "*.bar.com", + "methods": ["GET", "POST"], + "upstream": { + "nodes": { + "` + base.UpstreamIp + `:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("export route2", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/apisix/admin/export/routes/r2", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "}}", + }) + }) + ginkgo.It("export route and route2", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/apisix/admin/export/routes/r1,r2", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}", + }) + }) + ginkgo.It("use the exportall interface to export all routes", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodGet, + Path: "/apisix/admin/export/routes", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + ExpectBody: "{\"components\":{},\"info\":{\"title\":\"RoutesExport\",\"version\":\"3.0.0\"},\"openapi\":\"3.0.0\",\"paths\":{" + exportStrR2 + "," + exportStrR1 + "}}", + }) + }) + ginkgo.It("delete the route just created", 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("hit the route just deleted", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello_", + Headers: map[string]string{"Host": "bar.com"}, + ExpectStatus: http.StatusNotFound, + ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("delete the route2 just created", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r2", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("hit the route2 just deleted", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: "/hello2", + Headers: map[string]string{"Host": "bar.com"}, + ExpectStatus: http.StatusNotFound, + ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n", + Sleep: base.SleepTime, + }) + }) + // 4.Create a service that contains complete data and use the service_ id create route + serviceStrS1 := ` + "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 + ` + serviceStrS1 = replaceStr(serviceStrS1) + + exportStrR3 := `{ + "components": {}, Review comment: OK -- 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. To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org