Skip to content

Commit

Permalink
Test cases for Create/Delete/Get Fileshare Snapshot/Acl
Browse files Browse the repository at this point in the history
  • Loading branch information
nguptaopensds committed Jun 16, 2020
1 parent 8e7fab7 commit ebda4c1
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 7 deletions.
298 changes: 291 additions & 7 deletions pkg/api/controllers/fileshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
ctx "context"
"encoding/json"
"errors"
"github.com/sodafoundation/api/pkg/utils/constants"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/sodafoundation/api/pkg/utils/constants"

"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
c "github.com/sodafoundation/api/pkg/context"
Expand All @@ -50,6 +51,11 @@ func init() {
beego.Router("/v1beta/file/snapshots/:snapshotId", &FileShareSnapshotPortal{},
"get:GetFileShareSnapshot;put:UpdateFileShareSnapshot;delete:DeleteFileShareSnapshot")

beego.Router("/v1beta/file/acls", NewFakeFileSharePortal(),
"post:CreateFileShareAcl;get:ListFileSharesAcl")
beego.Router("/v1beta/file/acls/:aclId", NewFakeFileSharePortal(),
"get:GetFileShareAcl;delete:DeleteFileShareAcl")

}

func NewFakeFileSharePortal() *FileSharePortal {
Expand All @@ -63,6 +69,12 @@ func NewFakeFileSharePortal() *FileSharePortal {
mockClient.On("DeleteFileShare", ctx.Background(), &pb.DeleteFileShareOpts{
Context: c.NewAdminContext().ToJson(),
}).Return(&pb.GenericResponse{}, nil)
mockClient.On("CreateFileShareAcl", ctx.Background(), &pb.CreateFileShareAclOpts{
Context: c.NewAdminContext().ToJson(),
}).Return(&pb.GenericResponse{}, nil)
mockClient.On("DeleteFileShareAcl", ctx.Background(), &pb.DeleteFileShareAclOpts{
Context: c.NewAdminContext().ToJson(),
}).Return(&pb.GenericResponse{}, nil)

return &FileSharePortal{
CtrClient: mockClient,
Expand Down Expand Up @@ -106,13 +118,13 @@ func TestCreateFileShare(t *testing.T) {
CreatedAt: time.Now().Format(constants.TimeFormat),
UpdatedAt: time.Now().Format(constants.TimeFormat),
},
Name: "File_share",
Description: "fake File share",
Status: "creating",
Size: int64(1),
Name: "File_share",
Description: "fake File share",
Status: "creating",
Size: int64(1),
AvailabilityZone: "default",
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
}
mockClient := new(dbtest.Client)
mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleProfiles[0], nil)
Expand Down Expand Up @@ -340,6 +352,51 @@ func TestDeleteFileShare(t *testing.T) {
////////////////////////////////////////////////////////////////////////////////
// Tests for fileshare snapshot //
////////////////////////////////////////////////////////////////////////////////
func TestCreateFileShareSnapshot(t *testing.T) {
var jsonStr = []byte(`{
"id": "3769855c-a102-11e7-b772-17b880d2f537",
"fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71ca",
"name": "File_share_snapshot",
"description": "fake File share snapshot",
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c",
"shareSize": 1,
"snapshotSize": 1
}`)

t.Run("Should return 202 if everything works well", func(t *testing.T) {
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{
Id: "3769855c-a102-11e7-b772-17b880d2f537",
CreatedAt: time.Now().Format(constants.TimeFormat),
//UpdatedAt: time.Now().Format(constants.TimeFormat),
},
Name: "File_share_snapshot",
Description: "fake File share snapshot",
Status: "creating",
FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
ShareSize: int64(1),
SnapshotSize: int64(1),
}
mockClient := new(dbtest.Client)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[2], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
mockClient.On("ListFileShareSnapshots", c.NewAdminContext()).Return(nil, nil)
mockClient.On("CreateFileShareSnapshot", c.NewAdminContext(), &snapshot).Return(&SampleFileShareSnapshots[0], nil)
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/file/snapshots", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
var output model.FileShareSnapshotSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 202)
assertTestResult(t, &output, &SampleFileShareSnapshots[0])
})
}

func TestListFileShareSnapshots(t *testing.T) {

Expand Down Expand Up @@ -479,3 +536,230 @@ func TestUpdateFileShareSnapshot(t *testing.T) {
assertTestResult(t, w.Code, 500)
})
}

func TestDeleteFileShareSnapshot(t *testing.T) {

t.Run("Should return 202 if everything works well", func(t *testing.T) {
//var snapshot []*model.FileShareSnapshotSpec
mockClient := new(dbtest.Client)
mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(&SampleFileShareSnapshots[0], nil)
mockClient.On("GetProfile", c.NewAdminContext(), SampleFileShareSnapshots[0].ProfileId).Return(&SampleFileShareProfiles[0], nil)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
mockClient.On("UpdateFileShareSnapshot", c.NewAdminContext(), SampleFileShareSnapshots[0].Id, &SampleFileShareSnapshots[0]).Return(nil, nil)
mockClient.On("DeleteFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil)
db.C = mockClient

r, _ := http.NewRequest("DELETE",
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 202)
})

t.Run("Should return 500 if delete file share snapshot with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
db.C = mockClient

r, _ := http.NewRequest("DELETE",
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 500)
})
}

////////////////////////////////////////////////////////////////////////////////
// Tests for fileshare ACL //
////////////////////////////////////////////////////////////////////////////////
func TestCreateFileShareAcl(t *testing.T) {
var jsonStr = []byte(`{
"id": "6ad25d59-a160-45b2-8920-211be282e2df",
"fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71cb",
"type": "ip",
"accessCapability": [
"Read", "Write"
],
"accessTo": "10.32.109.15",
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c",
"description": "This is a sample Acl for testing"
}`)

t.Run("Should return 202 if everything works well", func(t *testing.T) {
acl := model.FileShareAclSpec{BaseModel: &model.BaseModel{
Id: "6ad25d59-a160-45b2-8920-211be282e2df",
CreatedAt: time.Now().Format(constants.TimeFormat),
UpdatedAt: time.Now().Format(constants.TimeFormat),
},
Description: "This is a sample Acl for testing",
Status: "available",
Type: "ip",
FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71cb",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
AccessCapability: []string{"Read", "Write"},
AccessTo: "10.32.109.15",
}
mockClient := new(dbtest.Client)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[4].FileShareId).Return(&SampleFileShares[3], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
//mockClient.On("ListFileShareSnapshots", c.NewAdminContext()).Return(nil, nil)
mockClient.On("CreateFileShareAcl", c.NewAdminContext(), &acl).Return(&SampleFileSharesAcl[4], nil)
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/file/acls", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
var output model.FileShareAclSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 202)
assertTestResult(t, &output, &SampleFileSharesAcl[4])
})

t.Run("Should return 500 if create file share acl with bad request", func(t *testing.T) {
acl := model.FileShareAclSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&acl)
mockClient := new(dbtest.Client)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[4].FileShareId).Return(&SampleFileShares[3], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
mockClient.On("CreateFileShareAcl", c.NewAdminContext(), &acl).Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/file/acls", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 500)
})
}

func TestListFileSharesAcl(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
var sampleAcls = []*model.FileShareAclSpec{&SampleFileSharesAcl[0], &SampleFileSharesAcl[1], &SampleFileSharesAcl[2]}
mockClient := new(dbtest.Client)
m := map[string][]string{
"offset": {"0"},
"limit": {"1"},
"sortDir": {"asc"},
"sortKey": {"name"},
}
mockClient.On("ListFileSharesAclWithFilter", c.NewAdminContext(), m).Return(sampleAcls, nil)
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/file/acls?offset=0&limit=1&sortDir=asc&sortKey=name", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)

var output []*model.FileShareAclSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, output, sampleAcls)
})

t.Run("Should return 500 if list fileshare acl with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
m := map[string][]string{
"offset": {"0"},
"limit": {"1"},
"sortDir": {"asc"},
"sortKey": {"name"},
}
mockClient.On("ListFileSharesAclWithFilter", c.NewAdminContext(), m).Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/file/acls?offset=0&limit=1&sortDir=asc&sortKey=name", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 500)
})
}

func TestGetFileShareAcl(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[2], nil)
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
var output model.FileShareAclSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, &output, &SampleFileSharesAcl[2])
})

t.Run("Should return 404 if get fileshare acl with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 404)
})
}

func TestDeleteFileShareAcl(t *testing.T) {

t.Run("Should return 202 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[2], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "b3585ebe-c42c-120g-b28e-f373746a71ca").Return(&SampleFileShareProfiles[0], nil)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[2].FileShareId).Return(&SampleFileShares[0], nil)
mockClient.On("UpdateFileShareAcl", c.NewAdminContext(), &SampleFileSharesAcl[2]).Return(nil, nil)
mockClient.On("DeleteFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil)
db.C = mockClient

r, _ := http.NewRequest("DELETE",
"/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 202)
})

t.Run("Should return 500 if delete file share snapshot with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
db.C = mockClient
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[3], nil)
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[3].FileShareId).Return(&SampleFileShares[0], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "b3585ebe-c42c-120g-b28e-f373746a71ca").Return(&SampleFileShareProfiles[0], nil)
r, _ := http.NewRequest("DELETE",
"/v1beta/file/acls/ad25d59-a160-45b2-8920-211be282e2dfh", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
})
beego.BeeApp.Handlers.ServeHTTP(w, r)
assertTestResult(t, w.Code, 500)
})
}

0 comments on commit ebda4c1

Please sign in to comment.