@@ -340,6 +340,72 @@ 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 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+ }
408+
343409
344410func TestListFileShareSnapshots (t * testing.T ) {
345411
0 commit comments