From d29f12e272c4451424efb92b2110cc5f82e8acc3 Mon Sep 17 00:00:00 2001 From: Shruthi-1MN Date: Wed, 23 Sep 2020 23:15:54 +0530 Subject: [PATCH] apiserver ut's --- pkg/api/controllers/fileshare_test.go | 438 +++++++++++++++++++++++- pkg/api/controllers/host_test.go | 108 ++++++ pkg/api/controllers/pool_test.go | 19 + pkg/api/controllers/volumeGroup_test.go | 56 ++- pkg/api/controllers/volume_test.go | 163 ++++++++- 5 files changed, 769 insertions(+), 15 deletions(-) diff --git a/pkg/api/controllers/fileshare_test.go b/pkg/api/controllers/fileshare_test.go index d40fd6b07..048a03bea 100644 --- a/pkg/api/controllers/fileshare_test.go +++ b/pkg/api/controllers/fileshare_test.go @@ -22,6 +22,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/astaxie/beego" "github.com/astaxie/beego/context" @@ -29,6 +30,7 @@ import ( "github.com/sodafoundation/api/pkg/db" "github.com/sodafoundation/api/pkg/model" pb "github.com/sodafoundation/api/pkg/model/proto" + "github.com/sodafoundation/api/pkg/utils/constants" . "github.com/sodafoundation/api/testutils/collection" ctrtest "github.com/sodafoundation/api/testutils/controller/testing" dbtest "github.com/sodafoundation/api/testutils/db/testing" @@ -43,11 +45,15 @@ func init() { beego.Router("/v1beta/file/shares/:fileshareId", NewFakeFileSharePortal(), "get:GetFileShare;put:UpdateFileShare;delete:DeleteFileShare") - beego.Router("/v1beta/file/snapshots", &FileShareSnapshotPortal{}, + beego.Router("/v1beta/file/snapshots", NewFakeFileShareSnapshotPortal(), "post:CreateFileShareSnapshot;get:ListFileShareSnapshots") - beego.Router("/v1beta/file/snapshots/:snapshotId", &FileShareSnapshotPortal{}, + beego.Router("/v1beta/file/snapshots/:snapshotId", NewFakeFileShareSnapshotPortal(), "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 { @@ -56,10 +62,45 @@ func NewFakeFileSharePortal() *FileSharePortal { mockClient.On("Connect", "localhost:50049").Return(nil) mockClient.On("Close").Return(nil) mockClient.On("CreateFileShare", ctx.Background(), &pb.CreateFileShareOpts{ - Context: c.NewAdminContext().ToJson(), + Id: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Name: "sample-fileshare-01", + Size: int64(1), + Description: "This is first sample fileshare for testing", + AvailabilityZone: "default", + PoolId: "a5965ebe-dg2c-434t-b28e-f373746a71ca", + Context: c.NewAdminContext().ToJson(), + Profile: SampleFileShareProfiles[0].ToJson(), + ExportLocations: []string{"192.168.100.100"}, + SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg", + SnapshotName: "sample-snapshot-01", }).Return(&pb.GenericResponse{}, nil) mockClient.On("DeleteFileShare", ctx.Background(), &pb.DeleteFileShareOpts{ - Context: c.NewAdminContext().ToJson(), + Id: "d2975ebe-d82c-430f-b28e-f373746a71ca", + PoolId: "a5965ebe-dg2c-434t-b28e-f373746a71ca", + Context: c.NewAdminContext().ToJson(), + Profile: SampleFileShareProfiles[0].ToJson(), + ExportLocations: []string{"192.168.100.100"}, + }).Return(&pb.GenericResponse{}, nil) + mockClient.On("CreateFileShareAcl", ctx.Background(), &pb.CreateFileShareAclOpts{ + Id: "6ad25d59-a160-45b2-8920-211be282e2df", + FileshareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Description: "This is a sample Acl for testing", + Type: "ip", + AccessCapability: []string{"Read", "Write"}, + AccessTo: "10.32.109.15", + Profile: SampleFileShareProfiles[0].ToJson(), + Context: c.NewAdminContext().ToJson(), + }).Return(&pb.GenericResponse{}, nil) + mockClient.On("DeleteFileShareAcl", ctx.Background(), &pb.DeleteFileShareAclOpts{ + Id: "6ad25d59-a160-45b2-8920-211be282e2df", + FileshareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Description: "This is a sample Acl for testing", + Type: "ip", + AccessCapability: []string{"Read", "Write"}, + AccessTo: "10.32.109.15", + Profile: SampleFileShareProfiles[0].ToJson(), + Context: c.NewAdminContext().ToJson(), + Metadata: map[string]string{}, }).Return(&pb.GenericResponse{}, nil) return &FileSharePortal{ @@ -67,6 +108,29 @@ func NewFakeFileSharePortal() *FileSharePortal { } } +func NewFakeFileShareSnapshotPortal() *FileShareSnapshotPortal { + mockClient := new(ctrtest.Client) + mockClient.On("Connect", "localhost:50049").Return(nil) + mockClient.On("Close").Return(nil) + mockClient.On("CreateFileShareSnapshot", ctx.Background(), &pb.CreateFileShareSnapshotOpts{ + Id: "3769855c-a102-11e7-b772-17b880d2f537", + Name: "sample-snapshot-01", + Description: "This is the first sample snapshot for testing", + FileshareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Context: c.NewAdminContext().ToJson(), + Profile: SampleFileShareProfiles[0].ToJson(), + }).Return(&pb.GenericResponse{}, nil) + mockClient.On("DeleteFileShareSnapshot", ctx.Background(), &pb.DeleteFileShareSnapshotOpts{ + Id: "3769855c-a102-11e7-b772-17b880d2f537", + FileshareId: "d2975ebe-d82c-430f-b28e-f373746a71ca", + Context: c.NewAdminContext().ToJson(), + Profile: SampleFileShareProfiles[0].ToJson(), + }).Return(&pb.GenericResponse{}, nil) + return &FileShareSnapshotPortal{ + CtrClient: mockClient, + } +} + //////////////////////////////////////////////////////////////////////////////// // Tests for FileShare // //////////////////////////////////////////////////////////////////////////////// @@ -226,6 +290,100 @@ func TestUpdateFileShare(t *testing.T) { }) } +func TestCreateFileShare(t *testing.T) { + var jsonStr = []byte(`{ + "id": "d2975ebe-d82c-430f-b28e-f373746a71ca", + "name":"sample-fileshare-01", + "description":"This is first sample fileshare for testing", + "size":1, + "poolId": "a5965ebe-dg2c-434t-b28e-f373746a71ca", + "snapshotId": "b7602e18-771e-11e7-8f38-dbd6d291f4eg" + }`) + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare) + fileshare.CreatedAt = time.Now().Format(constants.TimeFormat) + fileshare.UpdatedAt = time.Now().Format(constants.TimeFormat) + fileshare.AvailabilityZone = "default" + fileshare.Status = "creating" + fileshare.ProfileId = "1106b972-66ef-11e7-b172-db03f3689c9c" + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleFileShareProfiles[0], nil) + mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "b7602e18-771e-11e7-8f38-dbd6d291f4eg").Return(&SampleFileShareSnapshots[0], nil) + mockClient.On("GetFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(&SampleFileShares[0], nil) + mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&SampleFileShares[0], nil) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/file/shares", 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.FileShareSpec + json.Unmarshal(w.Body.Bytes(), &output) + assertTestResult(t, w.Code, 202) + assertTestResult(t, &output, &SampleFileShares[0]) + }) + + t.Run("Should return 404 if create file share with bad request - Provided snapshot id not found in db", func(t *testing.T) { + fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare) + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleFileShareProfiles[0], nil) + mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "b7602e18-771e-11e7-8f38-dbd6d291f4eg").Return(nil, errors.New("specified fileshare snapshot(b7602e18-771e-11e7-8f38-dbd6d291f4eg) can't find")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/file/shares", 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, 404) + }) +} + +func TestDeleteFileShare(t *testing.T) { + + t.Run("Should return 202 if delete file share is ok", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(&SampleFileShares[0], nil) + mockClient.On("GetProfile", c.NewAdminContext(), "b3585ebe-c42c-120g-b28e-f373746a71ca").Return(&SampleFileShareProfiles[0], nil) + mockClient.On("ListSnapshotsByShareId", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(nil, nil) + mockClient.On("ListFileShareAclsByShareId", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(nil, nil) + mockClient.On("UpdateFileShare", c.NewAdminContext(), &SampleFileShares[0]).Return(nil, nil) + mockClient.On("DeleteFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(nil) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", "/v1beta/file/shares/d2975ebe-d82c-430f-b28e-f373746a71ca", 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 404 if delete file share with bad request - file share id not found", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(nil, errors.New("specified fileshare(d2975ebe-d82c-430f-b28e-f373746a71ca) can't find")) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", + "/v1beta/file/shares/d2975ebe-d82c-430f-b28e-f373746a71ca", 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) + }) +} + //////////////////////////////////////////////////////////////////////////////// // Tests for fileshare snapshot // //////////////////////////////////////////////////////////////////////////////// @@ -368,3 +526,275 @@ func TestUpdateFileShareSnapshot(t *testing.T) { assertTestResult(t, w.Code, 500) }) } + +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{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot) + snapshot.Status = "creating" + snapshot.CreatedAt = time.Now().Format(constants.TimeFormat) + + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[1], 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]) + }) + + t.Run("Should return 404 if create file share snapshot with bad request - fileshare id which doesn't exists in db", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(nil, errors.New("specified fileshare (b7602e18-771e-11e7-8f38-dbd6d291f4eg) can't find")) + 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) + assertTestResult(t, w.Code, 404) + }) +} + +func TestDeleteFileShareSnapshot(t *testing.T) { + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + 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 404 if delete file share snapshot with bad request - snapshot id doesn't exist in db ", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil, errors.New("specified fileshare snapshot(b7602e18-771e-11e7-8f38-dbd6d291f4eg) can't find")) + 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, 404) + }) +} + +//////////////////////////////////////////////////////////////////////////////// +// Tests for fileshare ACL // +//////////////////////////////////////////////////////////////////////////////// + +func TestCreateFileShareAcl(t *testing.T) { + var jsonStr = []byte(`{ + "id": "6ad25d59-a160-45b2-8920-211be282e2df", + "fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71ca", + "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{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&acl) + acl.CreatedAt = time.Now().Format(constants.TimeFormat) + acl.UpdatedAt = time.Now().Format(constants.TimeFormat) + acl.Status = "available" + SampleFileShares[0].Status = "available" + acl.Metadata = map[string]string(nil) + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[2].FileShareId).Return(&SampleFileShares[0], nil) + mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil) + mockClient.On("CreateFileShareAcl", c.NewAdminContext(), &acl).Return(&SampleFileSharesAcl[2], 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[2]) + }) + + t.Run("Should return 400 if create file share acl with bad request - fileshare acl id which doesn't exists in db", func(t *testing.T) { + acl := model.FileShareAclSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&acl) + acl.CreatedAt = time.Now().Format(constants.TimeFormat) + acl.UpdatedAt = time.Now().Format(constants.TimeFormat) + acl.Status = "available" + mockClient := new(dbtest.Client) + mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileSharesAcl[2].FileShareId).Return(nil, errors.New("specified fileshare (b7602e18-771e-11e7-8f38-dbd6d291f4eg) can't find")) + 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, 400) + }) +} + +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]} + 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 - db error to list acls", 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 - acl id not found in db", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil, errors.New("specified fileshare snapshot(6ad25d59-a160-45b2-8920-211be282e2df) can't find")) + 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) { + SampleFileSharesAcl[2].Status = "available" + 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 404 if delete file share acl with bad request - invalid file share acl id", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetFileShareAcl", c.NewAdminContext(), "6ad25d59-a160-45b2-8920-211be282e2df").Return(nil, errors.New("specified fileshare acl(6ad25d59-a160-45b2-8920-211be282e2df) can't find")) + 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, 404) + }) +} diff --git a/pkg/api/controllers/host_test.go b/pkg/api/controllers/host_test.go index b4654e2d3..0c1978c8b 100644 --- a/pkg/api/controllers/host_test.go +++ b/pkg/api/controllers/host_test.go @@ -17,6 +17,7 @@ package controllers import ( "bytes" "encoding/json" + "errors" "net/http" "net/http/httptest" "testing" @@ -102,6 +103,56 @@ func TestCreateHost(t *testing.T) { assertTestResult(t, &output, fakeHost) }) + t.Run("Should return 400 for invalid request - lists not found db error", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("ListHostsByName", c.NewAdminContext(), hostReq.HostName).Return(nil, errors.New("List hosts failed: ")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/host/hosts", bytes.NewBuffer(ByteHostReq)) + 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, 400) + + }) + t.Run("Should return 400 - for some db error while creating host", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("ListHostsByName", c.NewAdminContext(), hostReq.HostName).Return(nil, nil) + mockClient.On("CreateHost", c.NewAdminContext(), &hostReq).Return(nil, errors.New("db error")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/host/hosts", bytes.NewBuffer(ByteHostReq)) + 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, 400) + }) + t.Run("Should return 400 - the host with same name already exists in the system", func(t *testing.T) { + fakeHost := []*model.HostSpec{&SampleHosts[0], &SampleHosts[1]} + + mockClient := new(dbtest.Client) + mockClient.On("ListHostsByName", c.NewAdminContext(), hostReq.HostName).Return(fakeHost, nil) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/host/hosts", bytes.NewBuffer(ByteHostReq)) + 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, 400) + + }) } func TestListHosts(t *testing.T) { @@ -114,12 +165,29 @@ func TestListHosts(t *testing.T) { r, _ := http.NewRequest("GET", "/v1beta/host/hosts", 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.HostSpec json.Unmarshal(w.Body.Bytes(), &output) assertTestResult(t, w.Code, 200) assertTestResult(t, output, fakeHosts) }) + + t.Run("Should return 400 - when lists fails with db error", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("ListHosts", c.NewAdminContext(), map[string][]string{}).Return(nil, errors.New("When list hosts in db:")) + db.C = mockClient + + r, _ := http.NewRequest("GET", "/v1beta/host/hosts", 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, 400) + }) } func TestGetHost(t *testing.T) { @@ -132,12 +200,30 @@ func TestGetHost(t *testing.T) { r, _ := http.NewRequest("GET", "/v1beta/host/hosts/"+SampleHosts[0].Id, 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.HostSpec json.Unmarshal(w.Body.Bytes(), &output) assertTestResult(t, w.Code, 200) assertTestResult(t, &output, fakeHost) }) + + t.Run("Should return 404 - specified host id doesn't exists in db", func(t *testing.T) { + fakeHost := &SampleHosts[0] + mockClient := new(dbtest.Client) + mockClient.On("GetHost", c.NewAdminContext(), fakeHost.Id).Return(nil, errors.New("specified host(202964b5-8e73-46fd-b41b-a8e403f3c30b) can't find")) + db.C = mockClient + + r, _ := http.NewRequest("GET", "/v1beta/host/hosts/"+SampleHosts[0].Id, 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 TestUpdateHost(t *testing.T) { @@ -168,6 +254,28 @@ func TestUpdateHost(t *testing.T) { assertTestResult(t, &output, fakeHost) }) + + t.Run("Should return 400 - update host failed with db error", func(t *testing.T) { + fakeHost := &SampleHosts[0] + var fakeHostUpdateReq model.HostSpec + tmp, _ := json.Marshal(&hostReq) + json.Unmarshal(tmp, &fakeHostUpdateReq) + fakeHostUpdateReq.Id = fakeHost.Id + + mockClient := new(dbtest.Client) + mockClient.On("UpdateHost", c.NewAdminContext(), &fakeHostUpdateReq).Return(nil, errors.New("update host failed:")) + db.C = mockClient + + r, _ := http.NewRequest("PUT", "/v1beta/host/hosts/"+fakeHost.Id, bytes.NewBuffer(ByteHostReq)) + 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, 400) + }) } func TestDeleteHost(t *testing.T) { diff --git a/pkg/api/controllers/pool_test.go b/pkg/api/controllers/pool_test.go index 3100a411d..a8fcbb91d 100755 --- a/pkg/api/controllers/pool_test.go +++ b/pkg/api/controllers/pool_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/astaxie/beego" + "github.com/astaxie/beego/context" c "github.com/sodafoundation/api/pkg/context" "github.com/sodafoundation/api/pkg/db" "github.com/sodafoundation/api/pkg/model" @@ -45,12 +46,30 @@ func TestListAvailabilityZones(t *testing.T) { r, _ := http.NewRequest("GET", "/v1beta/availabilityZones", 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 []string json.Unmarshal(w.Body.Bytes(), &output) assertTestResult(t, w.Code, 200) assertTestResult(t, output, SampleAvailabilityZones) }) + + t.Run("Should return 500 - get AvailabilityZones for pools failed as db error", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("ListAvailabilityZones", c.NewAdminContext()).Return(nil, errors.New("get AvailabilityZones for pools failed:")) + db.C = mockClient + + r, _ := http.NewRequest("GET", "/v1beta/availabilityZones", 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 TestListPools(t *testing.T) { diff --git a/pkg/api/controllers/volumeGroup_test.go b/pkg/api/controllers/volumeGroup_test.go index 7d0ea4ec4..0b457ffd4 100644 --- a/pkg/api/controllers/volumeGroup_test.go +++ b/pkg/api/controllers/volumeGroup_test.go @@ -49,9 +49,14 @@ func NewFakeVolumeGroupPortal() *VolumeGroupPortal { mockClient.On("Connect", "localhost:50049").Return(nil) mockClient.On("Close").Return(nil) mockClient.On("CreateVolumeGroup", ctx.Background(), &pb.CreateVolumeGroupOpts{ - Context: c.NewAdminContext().ToJson(), + Id: "3769855c-a102-11e7-b772-17b880d2f555", + Name: "sample-group-01", + Description: "This is the first sample group for testing", + Context: c.NewAdminContext().ToJson(), }).Return(&pb.GenericResponse{}, nil) mockClient.On("DeleteVolumeGroup", ctx.Background(), &pb.DeleteVolumeGroupOpts{ + Id: "3769855c-a102-11e7-b772-17b880d2f555", + PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248", Context: c.NewAdminContext().ToJson(), }).Return(&pb.GenericResponse{}, nil) @@ -63,8 +68,9 @@ func NewFakeVolumeGroupPortal() *VolumeGroupPortal { func TestCreateVolumeGroup(t *testing.T) { var jsonStr = []byte(`{ "id": "3769855c-a102-11e7-b772-17b880d2f555", - "name": "volumeGroup-demo", - "description": "volume group test", + "name": "sample-group-01", + "description": "This is the first sample group for testing", + "poolId": "084bf71e-a102-11e7-88a8-e31fe6d52248", "profiles": [ "993c87dc-1928-498b-9767-9da8f901d6ce", "90d667f0-e9a9-427c-8a7f-cc714217c7bd" @@ -98,11 +104,11 @@ func TestCreateVolumeGroup(t *testing.T) { }) t.Run("Should return 400 if create volume group with bad request", func(t *testing.T) { vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{ - Id: "3769855c-a102-11e7-b772-17b880d2f555", + Id: "3769855c-a102-11e7-b772-17b880d2f555", CreatedAt: time.Now().Format(constants.TimeFormat), }, - Status: "creating", - AvailabilityZone: "default", + Status: "creating", + AvailabilityZone: "default", } json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg) mockClient := new(dbtest.Client) @@ -242,6 +248,21 @@ func TestUpdateVolumeGroup(t *testing.T) { beego.BeeApp.Handlers.ServeHTTP(w, r) assertTestResult(t, w.Code, 400) }) + + t.Run("Should return 400 if update volume fails with bad request - volume group not found", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetVolumeGroup", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, errors.New("volume group bd5b12a8-a101-11e7-941e-d77981b584d8 not found:")) + db.C = mockClient + + r, _ := http.NewRequest("PUT", "/v1beta/block/volumeGroups/bd5b12a8-a101-11e7-941e-d77981b584d8", 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, 400) + }) } func TestDeleteVolumeGroup(t *testing.T) { @@ -251,15 +272,32 @@ func TestDeleteVolumeGroup(t *testing.T) { mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(&SampleVolumeGroups[0], nil) mockClient.On("GetDockByPoolId", c.NewAdminContext(), SampleVolumeGroups[0].PoolId).Return(&SampleDocks[0], nil) mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, nil) - mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return( nil, nil) - mockClient.On("UpdateStatus", c.NewAdminContext(), volumesUpdate, "").Return( nil) - mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumeGroups[0], "deleting").Return( nil) + mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil) + mockClient.On("UpdateStatus", c.NewAdminContext(), volumesUpdate, "").Return(nil) + mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumeGroups[0], "deleting").Return(nil) mockClient.On("DeleteVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil) db.C = mockClient r, _ := http.NewRequest("DELETE", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", 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 404 - specified volume group id can't find in db", func(t *testing.T) { + mockClient := new(dbtest.Client) + mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, errors.New("volume group 3769855c-a102-11e7-b772-17b880d2f555 not found: When get volume group in db")) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", 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) + }) } diff --git a/pkg/api/controllers/volume_test.go b/pkg/api/controllers/volume_test.go index 5d1e434f2..7a963507d 100755 --- a/pkg/api/controllers/volume_test.go +++ b/pkg/api/controllers/volume_test.go @@ -22,6 +22,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/astaxie/beego" "github.com/astaxie/beego/context" @@ -29,6 +30,7 @@ import ( "github.com/sodafoundation/api/pkg/db" "github.com/sodafoundation/api/pkg/model" pb "github.com/sodafoundation/api/pkg/model/proto" + "github.com/sodafoundation/api/pkg/utils/constants" . "github.com/sodafoundation/api/testutils/collection" ctrtest "github.com/sodafoundation/api/testutils/controller/testing" dbtest "github.com/sodafoundation/api/testutils/db/testing" @@ -58,7 +60,16 @@ func NewFakeVolumePortal() *VolumePortal { mockClient.On("Connect", "localhost:50049").Return(nil) mockClient.On("Close").Return(nil) mockClient.On("CreateVolume", ctx.Background(), &pb.CreateVolumeOpts{ - Context: c.NewAdminContext().ToJson(), + Id: "bd5b12a8-a101-11e7-941e-d77981b584d8", + Name: "sample-volume", + Size: 1, + Description: "This is a sample volume for testing", + //SnapshotId: "", + AvailabilityZone: "default", + ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c", + PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248", + Context: c.NewAdminContext().ToJson(), + Profile: SampleProfiles[0].ToJson(), }).Return(&pb.GenericResponse{}, nil) mockClient.On("ExtendVolume", ctx.Background(), &pb.ExtendVolumeOpts{ Id: "bd5b12a8-a101-11e7-941e-d77981b584d8", @@ -67,7 +78,11 @@ func NewFakeVolumePortal() *VolumePortal { Profile: SampleProfiles[0].ToJson(), }).Return(&pb.GenericResponse{}, nil) mockClient.On("DeleteVolume", ctx.Background(), &pb.DeleteVolumeOpts{ - Context: c.NewAdminContext().ToJson(), + Id: "bd5b12a8-a101-11e7-941e-d77981b584d8", + ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c", + PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248", + Context: c.NewAdminContext().ToJson(), + Profile: SampleProfiles[0].ToJson(), }).Return(&pb.GenericResponse{}, nil) return &VolumePortal{ @@ -292,6 +307,150 @@ func TestExtendVolume(t *testing.T) { }) } +func TestCreateVolume(t *testing.T) { + var jsonStr = []byte(`{ + "id": "bd5b12a8-a101-11e7-941e-d77981b584d8", + "name":"fake Vol", + "size": 1, + "description":"fake Vol" + }`) + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + volume := model.VolumeSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume) + volume.CreatedAt = time.Now().Format(constants.TimeFormat) + volume.AvailabilityZone = "default" + volume.Status = "creating" + volume.ProfileId = "1106b972-66ef-11e7-b172-db03f3689c9c" + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfile", c.NewAdminContext()).Return(&SampleProfiles[0], nil) + mockClient.On("CreateVolume", c.NewAdminContext(), &volume).Return(&SampleVolumes[0], nil) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumes", 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.VolumeSpec + json.Unmarshal(w.Body.Bytes(), &output) + assertTestResult(t, w.Code, 202) + assertTestResult(t, &output, &SampleVolumes[0]) + }) + + t.Run("Should return 400 - get default profile failed: db error", func(t *testing.T) { + volume := model.VolumeSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume) + volume.CreatedAt = time.Now().Format(constants.TimeFormat) + volume.AvailabilityZone = "default" + volume.Status = "creating" + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfile", c.NewAdminContext()).Return(nil, errors.New("get default profile failed:")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumes", 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, 400) + }) + + t.Run("Should return 400 - get profile failed: db error", func(t *testing.T) { + var testjsonStr = []byte(`{ + "id": "bd5b12a8-a101-11e7-941e-d77981b584d8", + "name":"fake Vol", + "size": 1, + "profileId": "1106b972-66ef-11e7-b172-db03f3689c9c", + "description":"fake Vol" + }`) + volume := model.VolumeSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume) + volume.CreatedAt = time.Now().Format(constants.TimeFormat) + volume.AvailabilityZone = "default" + volume.Status = "creating" + volume.ProfileId = "1106b972-66ef-11e7-b172-db03f3689c9c" + mockClient := new(dbtest.Client) + mockClient.On("GetProfile", c.NewAdminContext(), volume.ProfileId).Return(nil, errors.New("get profile failed:")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumes", bytes.NewBuffer(testjsonStr)) + 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, 400) + }) + + t.Run("Should return 400 - get default profile failed: db error", func(t *testing.T) { + volume := model.VolumeSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume) + volume.CreatedAt = time.Now().Format(constants.TimeFormat) + volume.AvailabilityZone = "default" + volume.Status = "creating" + volume.ProfileId = "1106b972-66ef-11e7-b172-db03f3689c9c" + mockClient := new(dbtest.Client) + mockClient.On("GetDefaultProfile", c.NewAdminContext()).Return(&SampleProfiles[0], nil) + mockClient.On("CreateVolume", c.NewAdminContext(), &volume).Return(nil, errors.New("create volume failed:")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumes", 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, 400) + }) +} + +func TestDeleteVolume(t *testing.T) { + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + SampleVolumes[0].Status = "available" + mockClient := new(dbtest.Client) + mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[0], nil) + mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleProfiles[0], nil) + mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil) + mockClient.On("ListAttachmentsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil) + mockClient.On("UpdateVolume", c.NewAdminContext(), &SampleVolumes[0]).Return(nil, nil) + mockClient.On("DeleteVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d8", 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 404 - Get volume id failed: db error ", func(t *testing.T) { + SampleVolumes[0].Status = "available" + mockClient := new(dbtest.Client) + mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, errors.New("specified volume(bd5b12a8-a101-11e7-941e-d77981b584d8) can't find")) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d8", 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) + }) +} + //////////////////////////////////////////////////////////////////////////////// // Tests for volume snapshot // ////////////////////////////////////////////////////////////////////////////////