Skip to content

Commit

Permalink
test: add test cases for import the file exported.
Browse files Browse the repository at this point in the history
  • Loading branch information
johzchen committed Jan 27, 2021
1 parent 5f8e31a commit a99c92b
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 11 deletions.
5 changes: 4 additions & 1 deletion api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ func init() {
Token = token
}

func httpGet(url string) ([]byte, int, error) {
func httpGet(url string, headers map[string]string) ([]byte, int, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, 0, err
}
for key, val := range headers {
req.Header.Add(key, val)
}

client := &http.Client{}
resp, err := client.Do(req)
Expand Down
11 changes: 7 additions & 4 deletions api/test/e2e/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type UploadFile struct {

var httpClient = &http.Client{}

func PostFile(reqUrl string, reqParams map[string]string, files []UploadFile, headers map[string]string) string {
func PostFile(reqUrl string, reqParams map[string]string, files []UploadFile, headers map[string]string) ([]byte, int, error) {
return post(reqUrl, reqParams, "multipart/form-data", files, headers)
}

func post(reqUrl string, reqParams map[string]string, contentType string, files []UploadFile, headers map[string]string) string {
func post(reqUrl string, reqParams map[string]string, contentType string, files []UploadFile, headers map[string]string) ([]byte, int, error) {
requestBody, realContentType := getReader(reqParams, contentType, files)
httpRequest, _ := http.NewRequest("POST", reqUrl, requestBody)
httpRequest.Header.Add("Content-Type", realContentType)
Expand All @@ -56,9 +56,12 @@ func post(reqUrl string, reqParams map[string]string, contentType string, files

defer resp.Body.Close()

response, _ := ioutil.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}

return string(response)
return body, resp.StatusCode, nil
}

func getReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string) {
Expand Down
5 changes: 2 additions & 3 deletions api/test/e2e/route_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1463,13 +1463,13 @@ func TestExportRoute_With_Jwt_Plugin(t *testing.T) {
time.Sleep(sleepTime)

// sign jwt token
body, status, err := httpGet("http://127.0.0.10:9080/apisix/plugin/jwt/sign?key=user-key")
body, status, err := httpGet("http://127.0.0.10:9080/apisix/plugin/jwt/sign?key=user-key", nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, status)
jwtToken := string(body)

// sign jwt token with not exists key
body, status, err = httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=not-exist-key")
body, status, err = httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=not-exist-key", nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, status)

Expand Down Expand Up @@ -2485,4 +2485,3 @@ func replaceStr(str string) string {
str = strings.Replace(str, " ", "", -1)
return str
}

194 changes: 194 additions & 0 deletions api/test/e2e/import_test.go → api/test/e2e/route_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -377,3 +378,196 @@ func TestImport_with_multi_routes(t *testing.T) {
testCaseCheck(tc, t)
}
}

func TestRoute_export_import(t *testing.T) {
// create routes
tests := []HttpTestCase{
{
Desc: "Create a route",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"uris": ["/test-test1"],
"name": "route_all",
"desc": "所有",
"methods": ["GET"],
"hosts": ["test.com"],
"status": 1,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
Desc: "Create a route2",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r2",
Body: `{
"uris": ["/test-test2"],
"name": "route_all",
"desc": "所有1",
"methods": ["GET"],
"hosts": ["test.com"],
"status": 1,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
{
Desc: "Create a route3",
Object: ManagerApiExpect(t),
Method: http.MethodPut,
Path: "/apisix/admin/routes/r3",
Body: `{
"uris": ["/test-test3"],
"name": "route_all",
"desc": "所有2",
"methods": ["GET"],
"hosts": ["test.com"],
"status": 1,
"upstream": {
"nodes": {
"172.16.238.20:1980": 1
},
"type": "roundrobin"
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
},
}
for _, tc := range tests {
testCaseCheck(tc, t)
}

// export routes
time.Sleep(sleepTime)
tmpPath := "/tmp/export.json"
headers := map[string]string{
"Authorization": token,
}
body, status, err := httpGet(ManagerAPIHost+"/apisix/admin/export/routes", headers)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, status)

content := gjson.Get(string(body), "data")
err = ioutil.WriteFile(tmpPath, []byte(content.Raw), 0644)
assert.Nil(t, err)

// import routes (should failed -- duplicate)
files := []UploadFile{
{Name: "file", Filepath: tmpPath},
}
respBody, status, err := PostFile(ManagerAPIHost+"/apisix/admin/import/routes", nil, files, headers)
assert.Nil(t, err)
assert.Equal(t, 400, status)
assert.True(t, strings.Contains(string(respBody), "duplicate"))
time.Sleep(sleepTime)

// delete routes
tests = []HttpTestCase{
{
Desc: "delete the route1 just created",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r1",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
Desc: "delete the route2 just created",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r2",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
{
Desc: "delete the route3 just created",
Object: ManagerApiExpect(t),
Method: http.MethodDelete,
Path: "/apisix/admin/routes/r3",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}
for _, tc := range tests {
testCaseCheck(tc, t)
}

// import again
time.Sleep(sleepTime)
respBody, status, err = PostFile(ManagerAPIHost+"/apisix/admin/import/routes", nil, files, headers)
assert.Nil(t, err)
assert.Equal(t, 200, status)
assert.True(t, strings.Contains(string(respBody), `"data":{"paths":3,"routes":3}`))
time.Sleep(sleepTime)

// sleep for data sync
time.Sleep(sleepTime)

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{})

assert.Equal(t, 3, len(list))

// verify route data
tests = []HttpTestCase{}
for _, item := range list {
route := item.(map[string]interface{})
tcDataVerify := HttpTestCase{
Desc: "verify data of route2",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/apisix/admin/routes/" + route["id"].(string),
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: []string{`"methods":["GET"]`,
`"desc":"所有`,
`"hosts":["test.com"]`,
`"upstream":{"nodes":[{"host":"172.16.238.20","port":1980,"weight":1}],"type":"roundrobin"}`,
},
Sleep: sleepTime,
}
tests = append(tests, tcDataVerify)
}

// 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)
}
}
2 changes: 1 addition & 1 deletion api/test/e2e/route_online_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func TestRoute_Online_Debug_Route_With_Jwt_Auth(t *testing.T) {
time.Sleep(sleepTime)

// sign jwt token
body, status, err := httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key")
body, status, err := httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key", nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, status)
jwtToken := string(body)
Expand Down
4 changes: 2 additions & 2 deletions api/test/e2e/route_with_plugin_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) {
time.Sleep(sleepTime)

// sign jwt token
body, status, err := httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key")
body, status, err := httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key", nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, status)
jwtToken := string(body)

// sign jwt token with not exists key
body, status, err = httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=not-exist-key")
body, status, err = httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=not-exist-key", nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, status)

Expand Down

0 comments on commit a99c92b

Please sign in to comment.