Skip to content

Commit ebda4c1

Browse files
committed
Test cases for Create/Delete/Get Fileshare Snapshot/Acl
1 parent 8e7fab7 commit ebda4c1

File tree

2 files changed

+343
-7
lines changed

2 files changed

+343
-7
lines changed

pkg/api/controllers/fileshare_test.go

Lines changed: 291 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ import (
1919
ctx "context"
2020
"encoding/json"
2121
"errors"
22-
"github.com/sodafoundation/api/pkg/utils/constants"
2322
"net/http"
2423
"net/http/httptest"
2524
"testing"
2625
"time"
2726

27+
"github.com/sodafoundation/api/pkg/utils/constants"
28+
2829
"github.com/astaxie/beego"
2930
"github.com/astaxie/beego/context"
3031
c "github.com/sodafoundation/api/pkg/context"
@@ -50,6 +51,11 @@ func init() {
5051
beego.Router("/v1beta/file/snapshots/:snapshotId", &FileShareSnapshotPortal{},
5152
"get:GetFileShareSnapshot;put:UpdateFileShareSnapshot;delete:DeleteFileShareSnapshot")
5253

54+
beego.Router("/v1beta/file/acls", NewFakeFileSharePortal(),
55+
"post:CreateFileShareAcl;get:ListFileSharesAcl")
56+
beego.Router("/v1beta/file/acls/:aclId", NewFakeFileSharePortal(),
57+
"get:GetFileShareAcl;delete:DeleteFileShareAcl")
58+
5359
}
5460

5561
func NewFakeFileSharePortal() *FileSharePortal {
@@ -63,6 +69,12 @@ func NewFakeFileSharePortal() *FileSharePortal {
6369
mockClient.On("DeleteFileShare", ctx.Background(), &pb.DeleteFileShareOpts{
6470
Context: c.NewAdminContext().ToJson(),
6571
}).Return(&pb.GenericResponse{}, nil)
72+
mockClient.On("CreateFileShareAcl", ctx.Background(), &pb.CreateFileShareAclOpts{
73+
Context: c.NewAdminContext().ToJson(),
74+
}).Return(&pb.GenericResponse{}, nil)
75+
mockClient.On("DeleteFileShareAcl", ctx.Background(), &pb.DeleteFileShareAclOpts{
76+
Context: c.NewAdminContext().ToJson(),
77+
}).Return(&pb.GenericResponse{}, nil)
6678

6779
return &FileSharePortal{
6880
CtrClient: mockClient,
@@ -106,13 +118,13 @@ func TestCreateFileShare(t *testing.T) {
106118
CreatedAt: time.Now().Format(constants.TimeFormat),
107119
UpdatedAt: time.Now().Format(constants.TimeFormat),
108120
},
109-
Name: "File_share",
110-
Description: "fake File share",
111-
Status: "creating",
112-
Size: int64(1),
121+
Name: "File_share",
122+
Description: "fake File share",
123+
Status: "creating",
124+
Size: int64(1),
113125
AvailabilityZone: "default",
114-
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
115-
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
126+
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
127+
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
116128
}
117129
mockClient := new(dbtest.Client)
118130
mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleProfiles[0], nil)
@@ -340,6 +352,51 @@ func TestDeleteFileShare(t *testing.T) {
340352
////////////////////////////////////////////////////////////////////////////////
341353
// Tests for fileshare snapshot //
342354
////////////////////////////////////////////////////////////////////////////////
355+
func TestCreateFileShareSnapshot(t *testing.T) {
356+
var jsonStr = []byte(`{
357+
"id": "3769855c-a102-11e7-b772-17b880d2f537",
358+
"fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71ca",
359+
"name": "File_share_snapshot",
360+
"description": "fake File share snapshot",
361+
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c",
362+
"shareSize": 1,
363+
"snapshotSize": 1
364+
}`)
365+
366+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
367+
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{
368+
Id: "3769855c-a102-11e7-b772-17b880d2f537",
369+
CreatedAt: time.Now().Format(constants.TimeFormat),
370+
//UpdatedAt: time.Now().Format(constants.TimeFormat),
371+
},
372+
Name: "File_share_snapshot",
373+
Description: "fake File share snapshot",
374+
Status: "creating",
375+
FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca",
376+
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
377+
ShareSize: int64(1),
378+
SnapshotSize: int64(1),
379+
}
380+
mockClient := new(dbtest.Client)
381+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[2], nil)
382+
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
383+
mockClient.On("ListFileShareSnapshots", c.NewAdminContext()).Return(nil, nil)
384+
mockClient.On("CreateFileShareSnapshot", c.NewAdminContext(), &snapshot).Return(&SampleFileShareSnapshots[0], nil)
385+
db.C = mockClient
386+
387+
r, _ := http.NewRequest("POST", "/v1beta/file/snapshots", bytes.NewBuffer(jsonStr))
388+
w := httptest.NewRecorder()
389+
r.Header.Set("Content-Type", "application/JSON")
390+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
391+
httpCtx.Input.SetData("context", c.NewAdminContext())
392+
})
393+
beego.BeeApp.Handlers.ServeHTTP(w, r)
394+
var output model.FileShareSnapshotSpec
395+
json.Unmarshal(w.Body.Bytes(), &output)
396+
assertTestResult(t, w.Code, 202)
397+
assertTestResult(t, &output, &SampleFileShareSnapshots[0])
398+
})
399+
}
343400

344401
func TestListFileShareSnapshots(t *testing.T) {
345402

@@ -479,3 +536,230 @@ func TestUpdateFileShareSnapshot(t *testing.T) {
479536
assertTestResult(t, w.Code, 500)
480537
})
481538
}
539+
540+
func TestDeleteFileShareSnapshot(t *testing.T) {
541+
542+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
543+
//var snapshot []*model.FileShareSnapshotSpec
544+
mockClient := new(dbtest.Client)
545+
mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(&SampleFileShareSnapshots[0], nil)
546+
mockClient.On("GetProfile", c.NewAdminContext(), SampleFileShareSnapshots[0].ProfileId).Return(&SampleFileShareProfiles[0], nil)
547+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
548+
mockClient.On("UpdateFileShareSnapshot", c.NewAdminContext(), SampleFileShareSnapshots[0].Id, &SampleFileShareSnapshots[0]).Return(nil, nil)
549+
mockClient.On("DeleteFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil)
550+
db.C = mockClient
551+
552+
r, _ := http.NewRequest("DELETE",
553+
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
554+
w := httptest.NewRecorder()
555+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
556+
httpCtx.Input.SetData("context", c.NewAdminContext())
557+
})
558+
beego.BeeApp.Handlers.ServeHTTP(w, r)
559+
assertTestResult(t, w.Code, 202)
560+
})
561+
562+
t.Run("Should return 500 if delete file share snapshot with bad request", func(t *testing.T) {
563+
mockClient := new(dbtest.Client)
564+
db.C = mockClient
565+
566+
r, _ := http.NewRequest("DELETE",
567+
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
568+
w := httptest.NewRecorder()
569+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
570+
httpCtx.Input.SetData("context", c.NewAdminContext())
571+
})
572+
beego.BeeApp.Handlers.ServeHTTP(w, r)
573+
assertTestResult(t, w.Code, 500)
574+
})
575+
}
576+
577+
////////////////////////////////////////////////////////////////////////////////
578+
// Tests for fileshare ACL //
579+
////////////////////////////////////////////////////////////////////////////////
580+
func TestCreateFileShareAcl(t *testing.T) {
581+
var jsonStr = []byte(`{
582+
"id": "6ad25d59-a160-45b2-8920-211be282e2df",
583+
"fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71cb",
584+
"type": "ip",
585+
"accessCapability": [
586+
"Read", "Write"
587+
],
588+
"accessTo": "10.32.109.15",
589+
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c",
590+
"description": "This is a sample Acl for testing"
591+
}`)
592+
593+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
594+
acl := model.FileShareAclSpec{BaseModel: &model.BaseModel{
595+
Id: "6ad25d59-a160-45b2-8920-211be282e2df",
596+
CreatedAt: time.Now().Format(constants.TimeFormat),
597+
UpdatedAt: time.Now().Format(constants.TimeFormat),
598+
},
599+
Description: "This is a sample Acl for testing",
600+
Status: "available",
601+
Type: "ip",
602+
FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71cb",
603+
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
604+
AccessCapability: []string{"Read", "Write"},
605+
AccessTo: "10.32.109.15",
606+
}
607+
mockClient := new(dbtest.Client)
608+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[4].FileShareId).Return(&SampleFileShares[3], nil)
609+
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
610+
//mockClient.On("ListFileShareSnapshots", c.NewAdminContext()).Return(nil, nil)
611+
mockClient.On("CreateFileShareAcl", c.NewAdminContext(), &acl).Return(&SampleFileSharesAcl[4], nil)
612+
db.C = mockClient
613+
614+
r, _ := http.NewRequest("POST", "/v1beta/file/acls", bytes.NewBuffer(jsonStr))
615+
w := httptest.NewRecorder()
616+
r.Header.Set("Content-Type", "application/JSON")
617+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
618+
httpCtx.Input.SetData("context", c.NewAdminContext())
619+
})
620+
beego.BeeApp.Handlers.ServeHTTP(w, r)
621+
var output model.FileShareAclSpec
622+
json.Unmarshal(w.Body.Bytes(), &output)
623+
assertTestResult(t, w.Code, 202)
624+
assertTestResult(t, &output, &SampleFileSharesAcl[4])
625+
})
626+
627+
t.Run("Should return 500 if create file share acl with bad request", func(t *testing.T) {
628+
acl := model.FileShareAclSpec{BaseModel: &model.BaseModel{}}
629+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&acl)
630+
mockClient := new(dbtest.Client)
631+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[4].FileShareId).Return(&SampleFileShares[3], nil)
632+
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
633+
mockClient.On("CreateFileShareAcl", c.NewAdminContext(), &acl).Return(nil, errors.New("db error"))
634+
db.C = mockClient
635+
636+
r, _ := http.NewRequest("POST", "/v1beta/file/acls", bytes.NewBuffer(jsonStr))
637+
w := httptest.NewRecorder()
638+
r.Header.Set("Content-Type", "application/JSON")
639+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
640+
httpCtx.Input.SetData("context", c.NewAdminContext())
641+
})
642+
beego.BeeApp.Handlers.ServeHTTP(w, r)
643+
assertTestResult(t, w.Code, 500)
644+
})
645+
}
646+
647+
func TestListFileSharesAcl(t *testing.T) {
648+
649+
t.Run("Should return 200 if everything works well", func(t *testing.T) {
650+
var sampleAcls = []*model.FileShareAclSpec{&SampleFileSharesAcl[0], &SampleFileSharesAcl[1], &SampleFileSharesAcl[2]}
651+
mockClient := new(dbtest.Client)
652+
m := map[string][]string{
653+
"offset": {"0"},
654+
"limit": {"1"},
655+
"sortDir": {"asc"},
656+
"sortKey": {"name"},
657+
}
658+
mockClient.On("ListFileSharesAclWithFilter", c.NewAdminContext(), m).Return(sampleAcls, nil)
659+
db.C = mockClient
660+
661+
r, _ := http.NewRequest("GET", "/v1beta/file/acls?offset=0&limit=1&sortDir=asc&sortKey=name", nil)
662+
w := httptest.NewRecorder()
663+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
664+
httpCtx.Input.SetData("context", c.NewAdminContext())
665+
})
666+
beego.BeeApp.Handlers.ServeHTTP(w, r)
667+
668+
var output []*model.FileShareAclSpec
669+
json.Unmarshal(w.Body.Bytes(), &output)
670+
assertTestResult(t, w.Code, 200)
671+
assertTestResult(t, output, sampleAcls)
672+
})
673+
674+
t.Run("Should return 500 if list fileshare acl with bad request", func(t *testing.T) {
675+
mockClient := new(dbtest.Client)
676+
m := map[string][]string{
677+
"offset": {"0"},
678+
"limit": {"1"},
679+
"sortDir": {"asc"},
680+
"sortKey": {"name"},
681+
}
682+
mockClient.On("ListFileSharesAclWithFilter", c.NewAdminContext(), m).Return(nil, errors.New("db error"))
683+
db.C = mockClient
684+
685+
r, _ := http.NewRequest("GET", "/v1beta/file/acls?offset=0&limit=1&sortDir=asc&sortKey=name", nil)
686+
w := httptest.NewRecorder()
687+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
688+
httpCtx.Input.SetData("context", c.NewAdminContext())
689+
})
690+
beego.BeeApp.Handlers.ServeHTTP(w, r)
691+
assertTestResult(t, w.Code, 500)
692+
})
693+
}
694+
695+
func TestGetFileShareAcl(t *testing.T) {
696+
697+
t.Run("Should return 200 if everything works well", func(t *testing.T) {
698+
mockClient := new(dbtest.Client)
699+
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[2], nil)
700+
db.C = mockClient
701+
702+
r, _ := http.NewRequest("GET", "/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
703+
w := httptest.NewRecorder()
704+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
705+
httpCtx.Input.SetData("context", c.NewAdminContext())
706+
})
707+
beego.BeeApp.Handlers.ServeHTTP(w, r)
708+
var output model.FileShareAclSpec
709+
json.Unmarshal(w.Body.Bytes(), &output)
710+
assertTestResult(t, w.Code, 200)
711+
assertTestResult(t, &output, &SampleFileSharesAcl[2])
712+
})
713+
714+
t.Run("Should return 404 if get fileshare acl with bad request", func(t *testing.T) {
715+
mockClient := new(dbtest.Client)
716+
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil, errors.New("db error"))
717+
db.C = mockClient
718+
719+
r, _ := http.NewRequest("GET", "/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
720+
w := httptest.NewRecorder()
721+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
722+
httpCtx.Input.SetData("context", c.NewAdminContext())
723+
})
724+
beego.BeeApp.Handlers.ServeHTTP(w, r)
725+
assertTestResult(t, w.Code, 404)
726+
})
727+
}
728+
729+
func TestDeleteFileShareAcl(t *testing.T) {
730+
731+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
732+
mockClient := new(dbtest.Client)
733+
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[2], nil)
734+
mockClient.On("GetProfile", c.NewAdminContext(), "b3585ebe-c42c-120g-b28e-f373746a71ca").Return(&SampleFileShareProfiles[0], nil)
735+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[2].FileShareId).Return(&SampleFileShares[0], nil)
736+
mockClient.On("UpdateFileShareAcl", c.NewAdminContext(), &SampleFileSharesAcl[2]).Return(nil, nil)
737+
mockClient.On("DeleteFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil)
738+
db.C = mockClient
739+
740+
r, _ := http.NewRequest("DELETE",
741+
"/v1beta/file/acls/6ad25d59-a160-45b2-8920-211be282e2df", nil)
742+
w := httptest.NewRecorder()
743+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
744+
httpCtx.Input.SetData("context", c.NewAdminContext())
745+
})
746+
beego.BeeApp.Handlers.ServeHTTP(w, r)
747+
assertTestResult(t, w.Code, 202)
748+
})
749+
750+
t.Run("Should return 500 if delete file share snapshot with bad request", func(t *testing.T) {
751+
mockClient := new(dbtest.Client)
752+
db.C = mockClient
753+
mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(&SampleFileSharesAcl[3], nil)
754+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[3].FileShareId).Return(&SampleFileShares[0], nil)
755+
mockClient.On("GetProfile", c.NewAdminContext(), "b3585ebe-c42c-120g-b28e-f373746a71ca").Return(&SampleFileShareProfiles[0], nil)
756+
r, _ := http.NewRequest("DELETE",
757+
"/v1beta/file/acls/ad25d59-a160-45b2-8920-211be282e2dfh", nil)
758+
w := httptest.NewRecorder()
759+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
760+
httpCtx.Input.SetData("context", c.NewAdminContext())
761+
})
762+
beego.BeeApp.Handlers.ServeHTTP(w, r)
763+
assertTestResult(t, w.Code, 500)
764+
})
765+
}

0 commit comments

Comments
 (0)