Skip to content

Commit

Permalink
Unit test cases for block volume and block volume snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
nguptaopensds committed Jun 17, 2020
1 parent fb04c0b commit 7f68c79
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
155 changes: 155 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,9 +352,104 @@ 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 //
////////////////////////////////////////////////////////////////////////////////
func TestCreateVolumeSnapshot(t *testing.T) {
var jsonStr = []byte(`{
"id": "3769855c-a102-11e7-b772-17b880d2f537",
"name":"fake snapshot",
"description":"fake snapshot",
"volumeId": "bd5b12a8-a101-11e7-941e-d77981b584d8",
"profileId": "1106b972-66ef-11e7-b172-db03f3689c9c"
}`)

t.Run("Should return 202 if everything works well", func(t *testing.T) {
snapshot := model.VolumeSnapshotSpec{BaseModel: &model.BaseModel{
Id: "3769855c-a102-11e7-b772-17b880d2f537",
CreatedAt: time.Now().Format(constants.TimeFormat),
},
VolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
Status: "creating",
Metadata: map[string]string{},
}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&snapshot)
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), snapshot.ProfileId).Return(&SampleProfiles[0], nil)
mockClient.On("GetVolume", c.NewAdminContext(), snapshot.VolumeId).Return(&SampleVolumes[2], nil)
mockClient.On("CreateVolumeSnapshot", c.NewAdminContext(), &snapshot).
Return(&SampleSnapshots[0], nil)
db.C = mockClient

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions testutils/collection/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ var (
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
SnapshotId: "3769855c-a102-11e7-b772-17b880d2f537",
},
{
BaseModel: &model.BaseModel{
Id: "bd5b12a8-a101-11e7-941e-d77981b584d8",
},
Name: "sample-volume",
Description: "This is a sample volume for testing",
AvailabilityZone: "default",
Size: int64(1),
Status: "available",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
}

SampleMultiVolumes = []model.VolumeSpec{
Expand Down

0 comments on commit 7f68c79

Please sign in to comment.