Skip to content

Commit

Permalink
fix: route test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ezioruan committed Mar 9, 2021
1 parent 7e40ea1 commit fa29e19
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ api/dag-to-lua/
# frontend e2e test output
web/.nyc_output
web/coverage
web/cypress/downloads/

# vim
*.swp

6 changes: 6 additions & 0 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) {
input.Route.ID = input.ID
}

// check duplicate name
if err := h.checkDuplicateName(c, input.Name, input.ID); err != nil {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
consts.InvalidParam(err.Error())
}

//check depend
if input.ServiceID != nil {
serviceID := utils.InterfaceToString(input.ServiceID)
Expand Down
115 changes: 107 additions & 8 deletions api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,49 @@ func TestRoute_Create(t *testing.T) {

luaCode, err := generateLuaCode(scriptMap)
assert.Nil(t, err)
mockData := []*entity.Route{
{
BaseInfo: entity.BaseInfo{CreateTime: 1609742634},
Name: "r1",
URI: "/test_r1",
Labels: map[string]string{
"version": "v1",
"build": "16",
},
Upstream: &entity.UpstreamDef{
Nodes: []interface{}{
map[string]interface{}{
"host": "39.97.63.215",
"port": float64(80),
"weight": float64(1),
},
},
},
},
}

tests := []testCase{
{
caseDesc: "create route failed, duplicate name",
giveInput: &entity.Route{
BaseInfo: entity.BaseInfo{
ID: "s1",
CreateTime: 1609746531,
},
Name: "r1",
Desc: "test_route",
UpstreamID: "u1",
ServiceID: "s1",
Script: "",
Labels: map[string]string{
"version": "v1",
},
},
wantRet: &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
wantErr: consts.InvalidParam(fmt.Sprintf("Route name %s is reduplicate", "r1")),
serviceRet: nil,
serviceErr: data.ErrNotFound,
},
{
caseDesc: "create route success",
giveInput: &entity.Route{
Expand Down Expand Up @@ -785,7 +826,7 @@ func TestRoute_Create(t *testing.T) {
ID: "s2",
CreateTime: 1609746531,
},
Name: "s1",
Name: "s2",
Desc: "test_route",
UpstreamID: "u1",
ServiceID: "not_found",
Expand All @@ -806,7 +847,7 @@ func TestRoute_Create(t *testing.T) {
ID: "r1",
CreateTime: 1609746531,
},
Name: "r1",
Name: "s2",
Desc: "test route",
UpstreamID: "r1",
// mock store will return err if service is s3
Expand All @@ -829,7 +870,7 @@ func TestRoute_Create(t *testing.T) {
ID: "s2",
CreateTime: 1609746531,
},
Name: "s1",
Name: "s2",
Desc: "test_route",
UpstreamID: "not_found",
ServiceID: "s2",
Expand All @@ -850,7 +891,7 @@ func TestRoute_Create(t *testing.T) {
ID: "s2",
CreateTime: 1609746531,
},
Name: "s1",
Name: "s2",
Desc: "test_route",
UpstreamID: "error",
ServiceID: "s2",
Expand All @@ -871,7 +912,7 @@ func TestRoute_Create(t *testing.T) {
ID: "s2",
CreateTime: 1609746531,
},
Name: "s1",
Name: "s2",
Desc: "test_route",
UpstreamID: "u1",
ServiceID: "s2",
Expand Down Expand Up @@ -962,6 +1003,25 @@ func TestRoute_Create(t *testing.T) {
getCalled := false

mStore := &store.MockInterface{}
mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
}).Return(func(input store.ListInput) *store.ListOutput {
var returnData []interface{}
for _, c := range mockData {
if input.Predicate(c) {
if input.Format == nil {
returnData = append(returnData, c)
continue
}

returnData = append(returnData, input.Format(c))
}
}

return &store.ListOutput{
Rows: returnData,
TotalSize: len(returnData),
}
}, tc.mockErr)
mStore.On("Create", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
getCalled = true
route := args.Get(1).(*entity.Route)
Expand Down Expand Up @@ -994,6 +1054,26 @@ func TestRoute_Update(t *testing.T) {
scriptMap := make(map[string]interface{})
err := json.Unmarshal([]byte(DagScript), &scriptMap)
assert.Nil(t, err)
mockData := []*entity.Route{
{
BaseInfo: entity.BaseInfo{CreateTime: 1609742634},
Name: "r0",
URI: "/test_r1",
Labels: map[string]string{
"version": "v1",
"build": "16",
},
Upstream: &entity.UpstreamDef{
Nodes: []interface{}{
map[string]interface{}{
"host": "39.97.63.215",
"port": float64(80),
"weight": float64(1),
},
},
},
},
}

luaCode, err := generateLuaCode(scriptMap)
assert.Nil(t, err)
Expand Down Expand Up @@ -1190,18 +1270,37 @@ func TestRoute_Update(t *testing.T) {
},
mockErr: fmt.Errorf("route update error"),
wantErr: fmt.Errorf("route update error"),
wantRet: &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError},
wantRet: &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
serviceRet: "service",
upstreamRet: "upstream",
serviceInput: "s1",
called: true,
called: false,
},
}

for _, tc := range tests {
t.Run(tc.caseDesc, func(t *testing.T) {
getCalled := false
routeStore := &store.MockInterface{}
routeStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
}).Return(func(input store.ListInput) *store.ListOutput {
var returnData []interface{}
for _, c := range mockData {
if input.Predicate(c) {
if input.Format == nil {
returnData = append(returnData, c)
continue
}

returnData = append(returnData, input.Format(c))
}
}

return &store.ListOutput{
Rows: returnData,
TotalSize: len(returnData),
}
}, tc.mockErr)

routeStore.On("Update", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
getCalled = true
Expand Down Expand Up @@ -1597,7 +1696,7 @@ func TestRoute_Exist(t *testing.T) {
Exclude: "002",
},
wantRet: &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
wantErr: consts.InvalidParam("Route name is reduplicate"),
wantErr: consts.InvalidParam("Route name r1 is reduplicate"),
called: true,
},
{
Expand Down
3 changes: 2 additions & 1 deletion api/test/e2e/route_with_management_fileds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestRoute_with_name_desc(t *testing.T) {
Query: "name=jack",
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
ExpectBody: "Route name is reduplicate",
ExpectBody: fmt.Sprintf("Route name %s is reduplicate", "jack"),
Sleep: sleepTime,
},
{
Expand Down
2 changes: 2 additions & 0 deletions api/test/e2e/route_with_plugin_http_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestRoute_With_Log_Plugin(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"name": "r1",
"uri": "/hello_",
"plugins": {
"http-logger": {
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestRoute_With_Log_Plugin(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r2",
Body: `{
"name": "r2",
"uri": "/hello",
"plugins": {
"http-logger": {
Expand Down
2 changes: 2 additions & 0 deletions api/test/e2e/route_with_priority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestRoute_with_priority(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r1",
Body: `{
"name": "r1",
"uri": "/server_port",
"methods": ["GET"],
"upstream": {
Expand Down Expand Up @@ -58,6 +59,7 @@ func TestRoute_with_priority(t *testing.T) {
Method: http.MethodPut,
Path: "/apisix/admin/routes/r2",
Body: `{
"name": "r2",
"uri": "/server_port",
"methods": ["GET"],
"priority": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,37 @@ context('Create and Delete Route', () => {
});
});

it('should duplicate the route', function () {
cy.visit('/');
cy.contains('Route').click();

cy.get(this.domSelector.nameSelector).type(newName);
cy.contains('Search').click();
cy.contains(newName).siblings().contains('Duplicate').click();

const duplicateNewName = `duplicateName${new Date().valueOf()}`;

cy.get(this.domSelector.name).clear().type(duplicateNewName);
cy.get(this.domSelector.description).clear().type(this.data.description2);
cy.contains('Next').click();
cy.contains('Next').click();
cy.contains('Next').click();
cy.contains('Submit').click();
cy.contains(this.data.submitSuccess);
cy.contains('Goto List').click();
cy.url().should('contains', 'routes/list');
cy.contains(duplicateNewName).siblings().should('contain', this.data.description2);

// test view
cy.contains(duplicateNewName).siblings().contains('View').click();
cy.get(this.domSelector.drawer).should('be.visible');

cy.get(this.domSelector.codemirrorScroll).within(() => {
cy.contains('upstream').should("exist");
cy.contains(duplicateNewName).should('exist');
});
});

it('should delete the route', function () {
cy.visit('/routes/list');
cy.get(this.domSelector.nameSelector).type(newName);
Expand Down

0 comments on commit fa29e19

Please sign in to comment.