From dd805c728f7b903156ab35e23d6965358d9c28d4 Mon Sep 17 00:00:00 2001 From: nguptaopensds Date: Wed, 17 Jun 2020 20:34:04 +0530 Subject: [PATCH] Unit test cases for block volume group and update PR with address comments --- pkg/api/controllers/volumeGroup_test.go | 167 +++++++++++++++++++++++- testutils/collection/data.go | 10 ++ 2 files changed, 174 insertions(+), 3 deletions(-) diff --git a/pkg/api/controllers/volumeGroup_test.go b/pkg/api/controllers/volumeGroup_test.go index 76dc2b0ea..7d0ea4ec4 100644 --- a/pkg/api/controllers/volumeGroup_test.go +++ b/pkg/api/controllers/volumeGroup_test.go @@ -15,13 +15,22 @@ package controllers import ( + "bytes" + ctx "context" "encoding/json" "errors" "net/http" "net/http/httptest" "testing" + "time" + + "github.com/sodafoundation/api/pkg/utils/constants" + + pb "github.com/sodafoundation/api/pkg/model/proto" + ctrtest "github.com/sodafoundation/api/testutils/controller/testing" "github.com/astaxie/beego" + "github.com/astaxie/beego/context" c "github.com/sodafoundation/api/pkg/context" "github.com/sodafoundation/api/pkg/db" "github.com/sodafoundation/api/pkg/model" @@ -30,8 +39,85 @@ import ( ) func init() { - beego.Router("/v1beta/block/volumeGroups", &VolumeGroupPortal{}, "post:CreateVolumeGroup;get:ListVolumeGroups") - beego.Router("/v1beta/block/volumeGroups/:groupId", &VolumeGroupPortal{}, "put:UpdateVolumeGroup;get:GetVolumeGroup;delete:DeleteVolumeGroup") + beego.Router("/v1beta/block/volumeGroups", NewFakeVolumeGroupPortal(), "post:CreateVolumeGroup;get:ListVolumeGroups") + beego.Router("/v1beta/block/volumeGroups/:groupId", NewFakeVolumeGroupPortal(), "put:UpdateVolumeGroup;get:GetVolumeGroup;delete:DeleteVolumeGroup") +} + +func NewFakeVolumeGroupPortal() *VolumeGroupPortal { + mockClient := new(ctrtest.Client) + + mockClient.On("Connect", "localhost:50049").Return(nil) + mockClient.On("Close").Return(nil) + mockClient.On("CreateVolumeGroup", ctx.Background(), &pb.CreateVolumeGroupOpts{ + Context: c.NewAdminContext().ToJson(), + }).Return(&pb.GenericResponse{}, nil) + mockClient.On("DeleteVolumeGroup", ctx.Background(), &pb.DeleteVolumeGroupOpts{ + Context: c.NewAdminContext().ToJson(), + }).Return(&pb.GenericResponse{}, nil) + + return &VolumeGroupPortal{ + CtrClient: mockClient, + } +} + +func TestCreateVolumeGroup(t *testing.T) { + var jsonStr = []byte(`{ + "id": "3769855c-a102-11e7-b772-17b880d2f555", + "name": "volumeGroup-demo", + "description": "volume group test", + "profiles": [ + "993c87dc-1928-498b-9767-9da8f901d6ce", + "90d667f0-e9a9-427c-8a7f-cc714217c7bd" + ] + }`) + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + mockClient := new(dbtest.Client) + var volumeGroup = &model.VolumeGroupSpec{ + BaseModel: &model.BaseModel{ + Id: "3769855c-a102-11e7-b772-17b880d2f555", + CreatedAt: time.Now().Format(constants.TimeFormat), + }, + Status: "creating", + AvailabilityZone: "default", + } + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volumeGroup) + mockClient.On("CreateVolumeGroup", c.NewAdminContext(), volumeGroup).Return(&SampleVolumeGroups[0], nil) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumeGroups", bytes.NewBuffer(jsonStr)) + w := httptest.NewRecorder() + beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) { + httpCtx.Input.SetData("context", c.NewAdminContext()) + }) + beego.BeeApp.Handlers.ServeHTTP(w, r) + var output model.VolumeGroupSpec + json.Unmarshal(w.Body.Bytes(), &output) + assertTestResult(t, w.Code, 202) + assertTestResult(t, &output, &SampleVolumeGroups[0]) + }) + t.Run("Should return 400 if create volume group with bad request", func(t *testing.T) { + vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{ + Id: "3769855c-a102-11e7-b772-17b880d2f555", + CreatedAt: time.Now().Format(constants.TimeFormat), + }, + Status: "creating", + AvailabilityZone: "default", + } + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg) + mockClient := new(dbtest.Client) + mockClient.On("CreateVolumeGroup", c.NewAdminContext(), &vg).Return(nil, errors.New("db error")) + db.C = mockClient + + r, _ := http.NewRequest("POST", "/v1beta/block/volumeGroups", 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 TestListVolumeGroups(t *testing.T) { @@ -91,7 +177,7 @@ func TestGetVolumeGroup(t *testing.T) { assertTestResult(t, &output, &SampleVolumeGroups[0]) }) - t.Run("Should return 404 if get volume group with bad request", func(t *testing.T) { + t.Run("Should return 404 if get volume group resource does not exist", func(t *testing.T) { mockClient := new(dbtest.Client) mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, errors.New("db error")) db.C = mockClient @@ -102,3 +188,78 @@ func TestGetVolumeGroup(t *testing.T) { assertTestResult(t, w.Code, 404) }) } + +func TestUpdateVolumeGroup(t *testing.T) { + var jsonStr = []byte(`{ + "id": "3769855c-a102-11e7-b772-17b880d2f555", + "name": "volumeGroup-demo", + "description": "volumeGroup test" + }`) + + t.Run("Should return 202 if everything works well", func(t *testing.T) { + vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{ + Id: "3769855c-a102-11e7-b772-17b880d2f555", + UpdatedAt: time.Now().Format(constants.TimeFormat), + }, + Status: "available", + } + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg) + mockClient := new(dbtest.Client) + mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(&SampleVolumeGroups[1], nil) + mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), SampleVolumeGroups[1].Id).Return(nil, nil) + mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[0], nil) + mockClient.On("UpdateVolumeGroup", c.NewAdminContext(), &vg).Return(&SampleVolumeGroups[1], nil) + db.C = mockClient + + r, _ := http.NewRequest("PUT", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", 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.VolumeGroupSpec + json.Unmarshal(w.Body.Bytes(), &output) + assertTestResult(t, w.Code, 202) + assertTestResult(t, &output, &SampleVolumeGroups[1]) + }) + + t.Run("Should return 400 if update volume fails with bad request", func(t *testing.T) { + vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{}} + json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg) + mockClient := new(dbtest.Client) + mockClient.On("GetVolumeGroup", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&vg, nil) + mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil) + mockClient.On("UpdateVolumeGroup", c.NewAdminContext(), &vg).Return(nil, errors.New("db error")) + db.C = mockClient + + r, _ := http.NewRequest("PUT", "/v1beta/block/volumeGroups/bd5b12a8-a101-11e7-941e-d77981b584d8", 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 TestDeleteVolumeGroup(t *testing.T) { + t.Run("Should return 202 if everything works well", func(t *testing.T) { + mockClient := new(dbtest.Client) + var volumesUpdate []*model.VolumeSpec + mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(&SampleVolumeGroups[0], nil) + mockClient.On("GetDockByPoolId", c.NewAdminContext(), SampleVolumeGroups[0].PoolId).Return(&SampleDocks[0], nil) + mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, nil) + mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return( nil, nil) + mockClient.On("UpdateStatus", c.NewAdminContext(), volumesUpdate, "").Return( nil) + mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumeGroups[0], "deleting").Return( nil) + mockClient.On("DeleteVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil) + db.C = mockClient + + r, _ := http.NewRequest("DELETE", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", nil) + w := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(w, r) + assertTestResult(t, w.Code, 202) + }) +} diff --git a/testutils/collection/data.go b/testutils/collection/data.go index 4b1d7e3de..222bca0d5 100644 --- a/testutils/collection/data.go +++ b/testutils/collection/data.go @@ -603,6 +603,16 @@ var ( Status: "available", PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248", }, + { + BaseModel: &model.BaseModel{ + Id: "3769855c-a102-11e7-b772-17b880d2f555", + }, + Name: "sample-group-01", + Description: "This is the first sample group for testing", + Status: "available", + PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248", + Profiles: []string{"1106b972-66ef-11e7-b172-db03f3689c9c"}, + }, } SampleHosts = []model.HostSpec{