Skip to content

Commit

Permalink
Unit test cases for block volume
Browse files Browse the repository at this point in the history
  • Loading branch information
nguptaopensds committed Jun 17, 2020
1 parent fb04c0b commit 800a12b
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/api/controllers/volume_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 @@ -96,6 +98,64 @@ var (
fakeVolumes = []*model.VolumeSpec{fakeVolume}
)

func TestCreateVolume(t *testing.T) {
var jsonStr = []byte(`{
"id": "bd5b12a8-a101-11e7-941e-d77981b584d8",
"name":"fake Vol",
"description":"fake Vol",
"size": 1,
"availabilityZone": "default",
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c"
}`)

t.Run("Should return 202 if everything works well", func(t *testing.T) {
volume := model.VolumeSpec{BaseModel: &model.BaseModel{
Id: "bd5b12a8-a101-11e7-941e-d77981b584d8",
CreatedAt: time.Now().Format(constants.TimeFormat),
},
Size: int64(1),
AvailabilityZone: "default",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
Status: "creating",
}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume)
mockClient := new(dbtest.Client)
mockClient.On("GetDefaultProfile", c.NewAdminContext()).Return(&SampleProfiles[0], nil)
mockClient.On("GetProfile", c.NewAdminContext(), SampleVolumes[0].ProfileId).Return(&SampleProfiles[0], nil)
mockClient.On("CreateVolume", c.NewAdminContext(), &volume).Return(&SampleVolumes[0], nil)
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/block/volumes", 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.VolumeSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 202)
assertTestResult(t, &output, &SampleVolumes[0])
})

t.Run("Should return 500 if create volume with bad request", func(t *testing.T) {
volume := model.VolumeSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume)
mockClient := new(dbtest.Client)
mockClient.On("CreateVolume", c.NewAdminContext(), &volume).Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/block/volumes", 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 TestListVolumes(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down Expand Up @@ -292,6 +352,43 @@ func TestExtendVolume(t *testing.T) {
})
}

func TestDeleteVolume(t *testing.T) {

t.Run("Should return 202 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[1], nil)
mockClient.On("GetProfile", c.NewAdminContext(), "1106b972-66ef-11e7-b172-db03f3689c9c").Return(&SampleProfiles[0], nil)
mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil)
mockClient.On("ListAttachmentsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil)
mockClient.On("UpdateVolume", c.NewAdminContext(), &SampleVolumes[1]).Return(nil, nil)
mockClient.On("DeleteVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[1], nil)
db.C = mockClient

r, _ := http.NewRequest("DELETE", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d8", 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 volume with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("DeleteVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("DELETE", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d8", 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)
})
}

////////////////////////////////////////////////////////////////////////////////
// Tests for volume snapshot //
////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 800a12b

Please sign in to comment.