Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test cases for block volume and block volume snapshot #1299

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 209 additions & 5 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,68 @@ 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 400 if create volume with bad request", func(t *testing.T) {
volume := model.VolumeSpec{BaseModel: &model.BaseModel{
CreatedAt: time.Now().Format(constants.TimeFormat),
},
Status: "creating"}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volume)
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), volume.ProfileId).Return(&SampleProfiles[0], nil)
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, 400)
})
}

func TestListVolumes(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down Expand Up @@ -163,7 +227,7 @@ func TestGetVolume(t *testing.T) {
assertTestResult(t, &output, &SampleVolumes[0])
})

t.Run("Should return 404 if get volume replication with bad request", func(t *testing.T) {
t.Run("Should return 404 if resource does not exist", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, errors.New("db error"))
db.C = mockClient
Expand All @@ -184,6 +248,10 @@ func TestUpdateVolume(t *testing.T) {
"name":"fake Vol",
"description":"fake Vol"
}`)
var badjsonStr = []byte(`{
"name":"fake Vol",
"description":"fake Vol"
}`)
var expectedJson = []byte(`{
"id": "bd5b12a8-a101-11e7-941e-d77981b584d8",
"name": "fake Vol",
Expand Down Expand Up @@ -216,14 +284,14 @@ func TestUpdateVolume(t *testing.T) {
assertTestResult(t, &output, &expected)
})

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

r, _ := http.NewRequest("PUT", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d8", bytes.NewBuffer(jsonStr))
r, _ := http.NewRequest("PUT", "/v1beta/block/volumes/bd5b12a8-a101-11e7-941e-d77981b584d9", bytes.NewBuffer(jsonStr))
w := httptest.NewRecorder()
r.Header.Set("Content-Type", "application/JSON")
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
Expand Down Expand Up @@ -292,9 +360,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 Expand Up @@ -416,7 +579,7 @@ func TestUpdateVolumeSnapshot(t *testing.T) {
assertTestResult(t, &output, &expected)
})

t.Run("Should return 500 if update volume snapshot with bad request", func(t *testing.T) {
t.Run("Should return 400 if update 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)
Expand All @@ -434,3 +597,44 @@ func TestUpdateVolumeSnapshot(t *testing.T) {
assertTestResult(t, w.Code, 500)
})
}

func TestDeleteVolumeSnapshot(t *testing.T) {

t.Run("Should return 202 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), SampleSnapshots[0].ProfileId).Return(&SampleProfiles[0], nil)
mockClient.On("GetVolumeSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(&SampleSnapshots[0], nil)
mockClient.On("GetVolume", c.NewAdminContext(), SampleSnapshots[0].VolumeId).Return(&SampleVolumes[0], nil)
mockClient.On("UpdateVolumeSnapshot", c.NewAdminContext(), SampleSnapshots[0].Id, &SampleSnapshots[0]).Return(nil, nil)
mockClient.On("DeleteVolumeSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(&SampleSnapshots[0], nil)

db.C = mockClient

r, _ := http.NewRequest("DELETE", "/v1beta/block/snapshots/3769855c-a102-11e7-b772-17b880d2f537", 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 snapshot with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetVolume", c.NewAdminContext(), SampleSnapshots[0].VolumeId).Return(nil, nil)
mockClient.On("GetProfile", c.NewAdminContext(), SampleSnapshots[0].ProfileId).Return(&SampleProfiles[0], nil)
mockClient.On("GetVolumeSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil, nil)
mockClient.On("UpdateVolumeSnapshot", c.NewAdminContext(), SampleSnapshots[0].Id, &SampleSnapshots[0]).Return(nil, nil)
mockClient.On("DeleteVolumeSnapshot", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f537").Return(nil, errors.New("db error"))
db.C = mockClient
mockClient.On("Connect", "127.0.0.1")

r, _ := http.NewRequest("DELETE", "/v1beta/block/snapshots/3769855c-a102-11e7-b772-17b880d2f537", 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)
})
}
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",
},
{
nguptaopensds marked this conversation as resolved.
Show resolved Hide resolved
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