Skip to content

Commit

Permalink
Adding create fileshare unit test case, coverage 17.7% to 25.1%
Browse files Browse the repository at this point in the history
  • Loading branch information
nguptaopensds committed Jun 15, 2020
1 parent fb04c0b commit f8487a3
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/api/controllers/fileshare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
ctx "context"
"encoding/json"
"errors"
"github.com/sodafoundation/api/pkg/utils/constants"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
Expand Down Expand Up @@ -88,6 +90,72 @@ var (
fakeFileShares = []*model.FileShareSpec{fakeFileShare}
)

func TestCreateFileShare(t *testing.T) {
var jsonStr = []byte(`{
"id": "d2975ebe-d82c-430f-b28e-f373746a71ca",
"name": "File_share",
"description": "fake File share",
"size": 1,
"profileId": "b3585ebe-c42c-120g-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{
Id: "d2975ebe-d82c-430f-b28e-f373746a71ca",
CreatedAt: time.Now().Format(constants.TimeFormat),
UpdatedAt: time.Now().Format(constants.TimeFormat),
},
Name: "File_share",
Description: "fake File share",
Status: "creating",
Size: int64(1),
AvailabilityZone: "default",
ProfileId: "b3585ebe-c42c-120g-b28e-f373746a71ca",
SnapshotId: "b7602e18-771e-11e7-8f38-dbd6d291f4eg",
}
mockClient := new(dbtest.Client)
mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&SampleProfiles[0], nil)
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("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 500 if create file share with bad request", func(t *testing.T) {
fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
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("CreateFileShare", c.NewAdminContext(), &fileshare).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 TestListFileShares(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down

0 comments on commit f8487a3

Please sign in to comment.