From ebda4c1aa4d53cf768ccdd3bd54a08d8f80160eb Mon Sep 17 00:00:00 2001 From: nguptaopensds Date: Tue, 16 Jun 2020 14:15:42 +0530 Subject: [PATCH] Test cases for Create/Delete/Get Fileshare Snapshot/Acl --- pkg/api/controllers/fileshare_test.go | 298 +++++++++++++++++++++++++- testutils/collection/data.go | 52 +++++ 2 files changed, 343 insertions(+), 7 deletions(-) diff --git a/pkg/api/controllers/fileshare_test.go b/pkg/api/controllers/fileshare_test.go index c47510120..063a4a892 100644 --- a/pkg/api/controllers/fileshare_test.go +++ b/pkg/api/controllers/fileshare_test.go @@ -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" @@ -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 { @@ -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, @@ -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) @@ -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) { @@ -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) + }) +} diff --git a/testutils/collection/data.go b/testutils/collection/data.go index 4b1d7e3de..52d13af62 100644 --- a/testutils/collection/data.go +++ b/testutils/collection/data.go @@ -285,6 +285,34 @@ var ( AvailabilityZone: "default", ExportLocations: []string{"192.168.100.101"}, }, + { + BaseModel: &model.BaseModel{ + Id: "d2975ebe-d82c-430f-b28e-f373746a71ca", + }, + Name: "sample-fileshare-01", + Description: "This is first sample fileshare for testing", + Size: int64(1), + Status: "available", + PoolId: "a5965ebe-dg2c-434t-b28e-f373746a71ca", + ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca", + SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg", + AvailabilityZone: "default", + ExportLocations: []string{"192.168.100.100"}, + }, + { + BaseModel: &model.BaseModel{ + Id: "d2975ebe-d82c-430f-b28e-f373746a71cb", + }, + Name: "sample-fileshare-01", + Description: "This is first sample fileshare for testing", + Size: int64(1), + Status: "available", + PoolId: "a5965ebe-dg2c-434t-b28e-f373746a71ca", + ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca", + SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg", + AvailabilityZone: "default", + ExportLocations: []string{"192.168.100.100"}, + }, } SampleFileSharesAcl = []model.FileShareAclSpec{ @@ -310,6 +338,7 @@ var ( AccessCapability: []string{"Read", "Write"}, AccessTo: "10.32.109.15", FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Status: "available", }, { BaseModel: &model.BaseModel{ @@ -321,6 +350,19 @@ var ( AccessCapability: []string{"Read", "Write"}, AccessTo: "10.32.109.151", FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Status: "available", + }, + { + BaseModel: &model.BaseModel{ + Id: "6ad25d59-a160-45b2-8920-211be282e2df", + }, + Description: "This is a sample Acl for testing", + ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c", + Type: "ip", + AccessCapability: []string{"Read", "Write"}, + AccessTo: "10.32.109.15", + FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71cb", + Status: "available", }, } @@ -344,6 +386,16 @@ var ( SnapshotSize: int64(1), Status: "available", }, + { + BaseModel: &model.BaseModel{ + Id: "3769855c-a102-11e7-b772-17b880d2f538", + }, + Name: "sample-snapshot-03", + Description: "This is the first sample snapshot for testing", + SnapshotSize: int64(1), + FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Status: "available", + }, } SampleVolumeNames = []string{}