Skip to content

Commit f0d23c4

Browse files
committed
Test cases for Create and DeleteFileShareSnapshot
1 parent 8e7fab7 commit f0d23c4

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

pkg/api/controllers/fileshare_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,71 @@ func TestDeleteFileShare(t *testing.T) {
340340
////////////////////////////////////////////////////////////////////////////////
341341
// Tests for fileshare snapshot //
342342
////////////////////////////////////////////////////////////////////////////////
343+
func TestCreateFileShareSnapshot(t *testing.T) {
344+
var jsonStr = []byte(`{
345+
"id": "3769855c-a102-11e7-b772-17b880d2f537",
346+
"fileshareId": "d2975ebe-d82c-430f-b28e-f373746a71ca",
347+
"name": "File_share_snapshot",
348+
"description": "fake File share snapshot",
349+
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c",
350+
"shareSize": 1,
351+
"snapshotSize": 1
352+
}`)
353+
354+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
355+
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{
356+
Id: "3769855c-a102-11e7-b772-17b880d2f537",
357+
CreatedAt: time.Now().Format(constants.TimeFormat),
358+
//UpdatedAt: time.Now().Format(constants.TimeFormat),
359+
},
360+
Name: "File_share_snapshot",
361+
Description: "fake File share snapshot",
362+
Status: "creating",
363+
FileShareId: "d2975ebe-d82c-430f-b28e-f373746a71ca",
364+
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
365+
ShareSize: int64(1),
366+
SnapshotSize: int64(1),
367+
}
368+
mockClient := new(dbtest.Client)
369+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
370+
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleFileShareProfiles[0], nil)
371+
mockClient.On("ListFileShareSnapshots", c.NewAdminContext()).Return(nil, nil)
372+
mockClient.On("CreateFileShareSnapshot", c.NewAdminContext(), &snapshot).Return(&SampleFileShareSnapshots[0], nil)
373+
db.C = mockClient
374+
375+
r, _ := http.NewRequest("POST", "/v1beta/file/snapshots", bytes.NewBuffer(jsonStr))
376+
w := httptest.NewRecorder()
377+
r.Header.Set("Content-Type", "application/JSON")
378+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
379+
httpCtx.Input.SetData("context", c.NewAdminContext())
380+
})
381+
beego.BeeApp.Handlers.ServeHTTP(w, r)
382+
var output model.FileShareSnapshotSpec
383+
json.Unmarshal(w.Body.Bytes(), &output)
384+
assertTestResult(t, w.Code, 202)
385+
assertTestResult(t, &output, &SampleFileShareSnapshots[0])
386+
})
387+
388+
t.Run("Should return 500 if create file share snapshot with bad request", func(t *testing.T) {
389+
snapshot := model.FileShareSnapshotSpec{BaseModel: &model.BaseModel{}}
390+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot)
391+
mockClient := new(dbtest.Client)
392+
//mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil)
393+
//mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
394+
//mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil)
395+
mockClient.On("CreateFileShareSnapshot", c.NewAdminContext(), &snapshot).Return(nil, errors.New("db error"))
396+
db.C = mockClient
397+
398+
r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr))
399+
w := httptest.NewRecorder()
400+
r.Header.Set("Content-Type", "application/JSON")
401+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
402+
httpCtx.Input.SetData("context", c.NewAdminContext())
403+
})
404+
beego.BeeApp.Handlers.ServeHTTP(w, r)
405+
assertTestResult(t, w.Code, 500)
406+
})
407+
}
343408

344409
func TestListFileShareSnapshots(t *testing.T) {
345410

@@ -479,3 +544,40 @@ func TestUpdateFileShareSnapshot(t *testing.T) {
479544
assertTestResult(t, w.Code, 500)
480545
})
481546
}
547+
548+
func TestDeleteFileShareSnapshot(t *testing.T) {
549+
550+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
551+
//var snapshot []*model.FileShareSnapshotSpec
552+
mockClient := new(dbtest.Client)
553+
mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(&SampleFileShareSnapshots[0], nil)
554+
mockClient.On("GetProfile", c.NewAdminContext(), SampleFileShareSnapshots[0].ProfileId).Return(&SampleFileShareProfiles[0], nil)
555+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
556+
mockClient.On("UpdateFileShareSnapshot", c.NewAdminContext(), SampleFileShareSnapshots[0].Id, &SampleFileShareSnapshots[0]).Return(nil, nil)
557+
mockClient.On("DeleteFileShareSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil)
558+
db.C = mockClient
559+
560+
r, _ := http.NewRequest("DELETE",
561+
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
562+
w := httptest.NewRecorder()
563+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
564+
httpCtx.Input.SetData("context", c.NewAdminContext())
565+
})
566+
beego.BeeApp.Handlers.ServeHTTP(w, r)
567+
assertTestResult(t, w.Code, 202)
568+
})
569+
570+
t.Run("Should return 500 if delete file share snapshot with bad request", func(t *testing.T) {
571+
mockClient := new(dbtest.Client)
572+
db.C = mockClient
573+
574+
r, _ := http.NewRequest("DELETE",
575+
"/v1beta/file/snapshots/3769855c-a102-11e7-b772-17b880d2f537", nil)
576+
w := httptest.NewRecorder()
577+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
578+
httpCtx.Input.SetData("context", c.NewAdminContext())
579+
})
580+
beego.BeeApp.Handlers.ServeHTTP(w, r)
581+
assertTestResult(t, w.Code, 500)
582+
})
583+
}

0 commit comments

Comments
 (0)