diff --git a/pkg/api/controllers/fileshare_test.go b/pkg/api/controllers/fileshare_test.go index c47510120..9761e899c 100644 --- a/pkg/api/controllers/fileshare_test.go +++ b/pkg/api/controllers/fileshare_test.go @@ -340,6 +340,71 @@ 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[0], 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 500 if create file share snapshot with bad request", func(t *testing.T) { + snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot) + mockClient := new(dbtest.Client) + //mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil) + //mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil) + //mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil) + mockClient.On("CreateFileShareSnapshot", c.NewAdminContext(), &snapshot).Return(nil, errors.New("db error")) + 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, 500) + }) +} func TestListFileShareSnapshots(t *testing.T) { @@ -479,3 +544,40 @@ 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) + }) +}