liuxiran commented on a change in pull request #1102: URL: https://github.com/apache/apisix-dashboard/pull/1102#discussion_r555511815
########## File path: api/test/e2e/import_test.go ########## @@ -0,0 +1,238 @@ +/* + * 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 ( + "github.com/stretchr/testify/assert" + "github.com/tidwall/gjson" + "io/ioutil" + "net/http" + "path/filepath" + "testing" +) + +func TestImport_default(t *testing.T) { + path, err := filepath.Abs("../testdata/import-test-default.yaml") + assert.Nil(t, err) + + headers := map[string]string{ + "Authorization": token, + } + files := []UploadFile{ + {Name: "file", Filepath: path}, + } + PostFile(ManagerAPIHost+"/apisix/admin/import", nil, files, headers) + + request, _ := http.NewRequest("GET", ManagerAPIHost+"/apisix/admin/routes", nil) + request.Header.Add("Authorization", token) + resp, err := http.DefaultClient.Do(request) + assert.Nil(t, err) + defer resp.Body.Close() + respBody, _ := ioutil.ReadAll(resp.Body) + list := gjson.Get(string(respBody), "data.rows").Value().([]interface{}) + + var tests []HttpTestCase + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "route patch for update status(online)", + Object: ManagerApiExpect(t), + Method: http.MethodPatch, + Path: "/apisix/admin/routes/" + route["id"].(string), + Body: `{"status":1}`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + Sleep: sleepTime, + } + tests = append(tests, tc) + } + + // verify route + tests = append(tests, HttpTestCase{ + Desc: "verify the route just imported", + Object: APISIXExpect(t), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: sleepTime, + }) + + // delete test data + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "delete route", + Object: ManagerApiExpect(t), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/" + route["id"].(string), + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + } + tests = append(tests, tc) + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} + +func TestImport_json(t *testing.T) { + path, err := filepath.Abs("../testdata/import-test.json") + assert.Nil(t, err) + + headers := map[string]string{ + "Authorization": token, + } + files := []UploadFile{ + {Name: "file", Filepath: path}, + } + PostFile(ManagerAPIHost+"/apisix/admin/import", nil, files, headers) + + request, _ := http.NewRequest("GET", ManagerAPIHost+"/apisix/admin/routes", nil) + request.Header.Add("Authorization", token) + resp, err := http.DefaultClient.Do(request) + assert.Nil(t, err) + defer resp.Body.Close() + respBody, _ := ioutil.ReadAll(resp.Body) + list := gjson.Get(string(respBody), "data.rows").Value().([]interface{}) + + var tests []HttpTestCase + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "route patch for update status(online)", + Object: ManagerApiExpect(t), + Method: http.MethodPatch, + Path: "/apisix/admin/routes/" + route["id"].(string), + Body: `{"status":1}`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + Sleep: sleepTime, + } + tests = append(tests, tc) + } + + // verify route + tests = append(tests, HttpTestCase{ + Desc: "verify the route just imported", + Object: APISIXExpect(t), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: sleepTime, + }) + + // delete test data + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "delete route", + Object: ManagerApiExpect(t), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/" + route["id"].(string), + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + } + tests = append(tests, tc) + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} + +func TestImport_with_plugins(t *testing.T) { + path, err := filepath.Abs("../testdata/import-test-plugins.yaml") + assert.Nil(t, err) + + headers := map[string]string{ + "Authorization": token, + } + files := []UploadFile{ + {Name: "file", Filepath: path}, + } + PostFile(ManagerAPIHost+"/apisix/admin/import", nil, files, headers) + + request, _ := http.NewRequest("GET", ManagerAPIHost+"/apisix/admin/routes", nil) + request.Header.Add("Authorization", token) + resp, err := http.DefaultClient.Do(request) + assert.Nil(t, err) + defer resp.Body.Close() + respBody, _ := ioutil.ReadAll(resp.Body) + list := gjson.Get(string(respBody), "data.rows").Value().([]interface{}) + + var tests []HttpTestCase + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "route patch for update status(online)", + Object: ManagerApiExpect(t), + Method: http.MethodPatch, + Path: "/apisix/admin/routes/" + route["id"].(string), + Body: `{"status":1}`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + Sleep: sleepTime, + } + tests = append(tests, tc) + } + + // verify route + verifyTests := []HttpTestCase{ + { + Desc: "verify the route just imported", + Object: APISIXExpect(t), + Method: http.MethodPost, + Path: "/hello", + Body: `{}`, + ExpectStatus: http.StatusBadRequest, + ExpectBody: `property "status" is required`, + Sleep: sleepTime, + }, + { + Desc: "verify the route just imported", + Object: APISIXExpect(t), + Method: http.MethodPost, + Path: "/hello", + Body: `{"status": "1"}`, + ExpectStatus: http.StatusUnauthorized, + ExpectBody: `{"message":"Missing authorization in request"}`, + Sleep: sleepTime, + }, + } + tests = append(tests, verifyTests...) + + // delete test data + for _, item := range list { + route := item.(map[string]interface{}) + tc := HttpTestCase{ + Desc: "delete route", + Object: ManagerApiExpect(t), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/" + route["id"].(string), + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + } + tests = append(tests, tc) + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} Review comment: while debugging Fe, I found the file below could also be imported, It would be better to add a test case to cover the wrong file. ```yaml # # 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. # conf: listen: host: 127.0.0.1 # `manager api` listening ip or host name port: 9000 # `manager api` listening port etcd: endpoints: # supports defining multiple etcd host addresses for an etcd cluster - 127.0.0.1:2379 # yamllint disable rule:comments-indentation # etcd basic auth info # username: "root" # ignore etcd username if not enable etcd auth # password: "123456" # ignore etcd password if not enable etcd auth log: error_log: level: warn # supports levels, lower to higher: debug, info, warn, error, panic, fatal file_path: logs/error.log # supports relative path, absolute path, standard output # such as: logs/error.log, /tmp/logs/error.log, /dev/stdout, /dev/stderr access_log: file_path: logs/access.log # supports relative path, absolute path, standard output # such as: logs/access.log, /tmp/logs/access.log, /dev/stdout, /dev/stderr # log example: 2020-12-09T16:38:09.039+0800 INFO filter/logging.go:46 /apisix/admin/routes/r1 {"status": 401, "host": "127.0.0.1:9000", "query": "asdfsafd=adf&a=a", "requestId": "3d50ecb8-758c-46d1-af5b-cd9d1c820156", "latency": 0, "remoteIP": "127.0.0.1", "method": "PUT", "errs": []} authentication: secret: secret # secret for jwt token generation. # NOTE: Highly recommended to modify this value to protect `manager api`. # if it's default value, when `manager api` start , it will generate a random string to replace it. expire_time: 3600 # jwt token expire time, in second users: # yamllint enable rule:comments-indentation - username: admin # username and password for login `manager api` password: admin - username: user password: user ``` ---------------------------------------------------------------- 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: [email protected]
