Skip to content

Commit 8e7fab7

Browse files
committed
Adding create fileshare unit test case and delete file share, coverage 17.7% to 28.7%
1 parent fb04c0b commit 8e7fab7

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

pkg/api/controllers/fileshare_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ import (
1919
ctx "context"
2020
"encoding/json"
2121
"errors"
22+
"github.com/sodafoundation/api/pkg/utils/constants"
2223
"net/http"
2324
"net/http/httptest"
2425
"testing"
26+
"time"
2527

2628
"github.com/astaxie/beego"
2729
"github.com/astaxie/beego/context"
@@ -88,6 +90,72 @@ var (
8890
fakeFileShares = []*model.FileShareSpec{fakeFileShare}
8991
)
9092

93+
func TestCreateFileShare(t *testing.T) {
94+
var jsonStr = []byte(`{
95+
"id": "d2975ebe-d82c-430f-b28e-f373746a71ca",
96+
"name": "File_share",
97+
"description": "fake File share",
98+
"size": 1,
99+
"profileId": "b3585ebe-c42c-120g-b28e-f373746a71ca",
100+
"snapshotId": "b7602e18-771e-11e7-8f38-dbd6d291f4eg"
101+
}`)
102+
103+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
104+
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{
105+
Id: "d2975ebe-d82c-430f-b28e-f373746a71ca",
106+
CreatedAt: time.Now().Format(constants.TimeFormat),
107+
UpdatedAt: time.Now().Format(constants.TimeFormat),
108+
},
109+
Name: "File_share",
110+
Description: "fake File share",
111+
Status: "creating",
112+
Size: int64(1),
113+
AvailabilityZone: "default",
114+
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
115+
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
116+
}
117+
mockClient := new(dbtest.Client)
118+
mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleProfiles[0], nil)
119+
mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil)
120+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
121+
mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil)
122+
mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&SampleFileShares[0], nil)
123+
db.C = mockClient
124+
125+
r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr))
126+
w := httptest.NewRecorder()
127+
r.Header.Set("Content-Type", "application/JSON")
128+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
129+
httpCtx.Input.SetData("context", c.NewAdminContext())
130+
})
131+
beego.BeeApp.Handlers.ServeHTTP(w, r)
132+
var output model.FileShareSpec
133+
json.Unmarshal(w.Body.Bytes(), &output)
134+
assertTestResult(t, w.Code, 202)
135+
assertTestResult(t, &output, &SampleFileShares[0])
136+
})
137+
138+
t.Run("Should return 500 if create file share with bad request", func(t *testing.T) {
139+
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
140+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
141+
mockClient := new(dbtest.Client)
142+
mockClient.On("GetFileShareSnapshot", c.NewAdminContext(), fileshare.SnapshotId).Return(&SampleFileShareSnapshots[0], nil)
143+
mockClient.On("GetFileShare", c.NewAdminContext(), SampleFileShareSnapshots[0].FileShareId).Return(&SampleFileShares[0], nil)
144+
mockClient.On("GetProfile", c.NewAdminContext(), fileshare.ProfileId).Return(&SampleFileShareProfiles[0], nil)
145+
mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(nil, errors.New("db error"))
146+
db.C = mockClient
147+
148+
r, _ := http.NewRequest("POST", "/v1beta/file/shares", bytes.NewBuffer(jsonStr))
149+
w := httptest.NewRecorder()
150+
r.Header.Set("Content-Type", "application/JSON")
151+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
152+
httpCtx.Input.SetData("context", c.NewAdminContext())
153+
})
154+
beego.BeeApp.Handlers.ServeHTTP(w, r)
155+
assertTestResult(t, w.Code, 500)
156+
})
157+
}
158+
91159
func TestListFileShares(t *testing.T) {
92160

93161
t.Run("Should return 200 if everything works well", func(t *testing.T) {
@@ -226,6 +294,49 @@ func TestUpdateFileShare(t *testing.T) {
226294
})
227295
}
228296

297+
func TestDeleteFileShare(t *testing.T) {
298+
299+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
300+
//var snapshot []*model.FileShareSnapshotSpec
301+
mockClient := new(dbtest.Client)
302+
mockClient.On("GetFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(&SampleFileShares[0], nil)
303+
mockClient.On("GetProfile", c.NewAdminContext(), SampleFileShares[0].ProfileId).Return(&SampleFileShareProfiles[0], nil)
304+
mockClient.On("ListSnapshotsByShareId", c.NewAdminContext(), SampleFileShares[0].Id).Return(nil, nil)
305+
mockClient.On("ListFileShareAclsByShareId", c.NewAdminContext(), SampleFileShares[0].Id).Return(nil, nil)
306+
mockClient.On("UpdateFileShare", c.NewAdminContext(), &SampleFileShares[0]).Return(nil, nil)
307+
mockClient.On("DeleteFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(nil)
308+
db.C = mockClient
309+
310+
r, _ := http.NewRequest("DELETE",
311+
"/v1beta/file/shares/d2975ebe-d82c-430f-b28e-f373746a71ca", nil)
312+
w := httptest.NewRecorder()
313+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
314+
httpCtx.Input.SetData("context", c.NewAdminContext())
315+
})
316+
beego.BeeApp.Handlers.ServeHTTP(w, r)
317+
assertTestResult(t, w.Code, 202)
318+
})
319+
320+
t.Run("Should return 500 if delete file share with bad request", func(t *testing.T) {
321+
mockClient := new(dbtest.Client)
322+
mockClient.On("GetFileShare", c.NewAdminContext(), "d2975ebe-d82c-430f-b28e-f373746a71ca").Return(&SampleFileShares[1], nil)
323+
mockClient.On("GetProfile", c.NewAdminContext(), SampleFileShares[0].ProfileId).Return(&SampleFileShareProfiles[0], nil)
324+
mockClient.On("ListSnapshotsByShareId", c.NewAdminContext(), SampleFileShares[1].Id).Return(nil, nil)
325+
mockClient.On("ListFileShareAclsByShareId", c.NewAdminContext(), SampleFileShares[1].Id).Return(nil, nil)
326+
mockClient.On("UpdateFileShare", c.NewAdminContext(), &SampleFileShares[1]).Return(nil, nil)
327+
db.C = mockClient
328+
329+
r, _ := http.NewRequest("DELETE",
330+
"/v1beta/file/shares/d2975ebe-d82c-430f-b28e-f373746a71ca", nil)
331+
w := httptest.NewRecorder()
332+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
333+
httpCtx.Input.SetData("context", c.NewAdminContext())
334+
})
335+
beego.BeeApp.Handlers.ServeHTTP(w, r)
336+
assertTestResult(t, w.Code, 500)
337+
})
338+
}
339+
229340
////////////////////////////////////////////////////////////////////////////////
230341
// Tests for fileshare snapshot //
231342
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)