Skip to content

Commit 7eb8236

Browse files
authored
Merge pull request #1300 from nguptaopensds/test_vg
Unit test cases for block volume group
2 parents 7d8aebb + 50c2624 commit 7eb8236

2 files changed

Lines changed: 174 additions & 3 deletions

File tree

pkg/api/controllers/volumeGroup_test.go

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@
1515
package controllers
1616

1717
import (
18+
"bytes"
19+
ctx "context"
1820
"encoding/json"
1921
"errors"
2022
"net/http"
2123
"net/http/httptest"
2224
"testing"
25+
"time"
26+
27+
"github.com/sodafoundation/api/pkg/utils/constants"
28+
29+
pb "github.com/sodafoundation/api/pkg/model/proto"
30+
ctrtest "github.com/sodafoundation/api/testutils/controller/testing"
2331

2432
"github.com/astaxie/beego"
33+
"github.com/astaxie/beego/context"
2534
c "github.com/sodafoundation/api/pkg/context"
2635
"github.com/sodafoundation/api/pkg/db"
2736
"github.com/sodafoundation/api/pkg/model"
@@ -30,8 +39,85 @@ import (
3039
)
3140

3241
func init() {
33-
beego.Router("/v1beta/block/volumeGroups", &VolumeGroupPortal{}, "post:CreateVolumeGroup;get:ListVolumeGroups")
34-
beego.Router("/v1beta/block/volumeGroups/:groupId", &VolumeGroupPortal{}, "put:UpdateVolumeGroup;get:GetVolumeGroup;delete:DeleteVolumeGroup")
42+
beego.Router("/v1beta/block/volumeGroups", NewFakeVolumeGroupPortal(), "post:CreateVolumeGroup;get:ListVolumeGroups")
43+
beego.Router("/v1beta/block/volumeGroups/:groupId", NewFakeVolumeGroupPortal(), "put:UpdateVolumeGroup;get:GetVolumeGroup;delete:DeleteVolumeGroup")
44+
}
45+
46+
func NewFakeVolumeGroupPortal() *VolumeGroupPortal {
47+
mockClient := new(ctrtest.Client)
48+
49+
mockClient.On("Connect", "localhost:50049").Return(nil)
50+
mockClient.On("Close").Return(nil)
51+
mockClient.On("CreateVolumeGroup", ctx.Background(), &pb.CreateVolumeGroupOpts{
52+
Context: c.NewAdminContext().ToJson(),
53+
}).Return(&pb.GenericResponse{}, nil)
54+
mockClient.On("DeleteVolumeGroup", ctx.Background(), &pb.DeleteVolumeGroupOpts{
55+
Context: c.NewAdminContext().ToJson(),
56+
}).Return(&pb.GenericResponse{}, nil)
57+
58+
return &VolumeGroupPortal{
59+
CtrClient: mockClient,
60+
}
61+
}
62+
63+
func TestCreateVolumeGroup(t *testing.T) {
64+
var jsonStr = []byte(`{
65+
"id": "3769855c-a102-11e7-b772-17b880d2f555",
66+
"name": "volumeGroup-demo",
67+
"description": "volume group test",
68+
"profiles": [
69+
"993c87dc-1928-498b-9767-9da8f901d6ce",
70+
"90d667f0-e9a9-427c-8a7f-cc714217c7bd"
71+
]
72+
}`)
73+
74+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
75+
mockClient := new(dbtest.Client)
76+
var volumeGroup = &model.VolumeGroupSpec{
77+
BaseModel: &model.BaseModel{
78+
Id: "3769855c-a102-11e7-b772-17b880d2f555",
79+
CreatedAt: time.Now().Format(constants.TimeFormat),
80+
},
81+
Status: "creating",
82+
AvailabilityZone: "default",
83+
}
84+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&volumeGroup)
85+
mockClient.On("CreateVolumeGroup", c.NewAdminContext(), volumeGroup).Return(&SampleVolumeGroups[0], nil)
86+
db.C = mockClient
87+
88+
r, _ := http.NewRequest("POST", "/v1beta/block/volumeGroups", bytes.NewBuffer(jsonStr))
89+
w := httptest.NewRecorder()
90+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
91+
httpCtx.Input.SetData("context", c.NewAdminContext())
92+
})
93+
beego.BeeApp.Handlers.ServeHTTP(w, r)
94+
var output model.VolumeGroupSpec
95+
json.Unmarshal(w.Body.Bytes(), &output)
96+
assertTestResult(t, w.Code, 202)
97+
assertTestResult(t, &output, &SampleVolumeGroups[0])
98+
})
99+
t.Run("Should return 400 if create volume group with bad request", func(t *testing.T) {
100+
vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{
101+
Id: "3769855c-a102-11e7-b772-17b880d2f555",
102+
CreatedAt: time.Now().Format(constants.TimeFormat),
103+
},
104+
Status: "creating",
105+
AvailabilityZone: "default",
106+
}
107+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg)
108+
mockClient := new(dbtest.Client)
109+
mockClient.On("CreateVolumeGroup", c.NewAdminContext(), &vg).Return(nil, errors.New("db error"))
110+
db.C = mockClient
111+
112+
r, _ := http.NewRequest("POST", "/v1beta/block/volumeGroups", bytes.NewBuffer(jsonStr))
113+
w := httptest.NewRecorder()
114+
r.Header.Set("Content-Type", "application/JSON")
115+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
116+
httpCtx.Input.SetData("context", c.NewAdminContext())
117+
})
118+
beego.BeeApp.Handlers.ServeHTTP(w, r)
119+
assertTestResult(t, w.Code, 400)
120+
})
35121
}
36122

37123
func TestListVolumeGroups(t *testing.T) {
@@ -91,7 +177,7 @@ func TestGetVolumeGroup(t *testing.T) {
91177
assertTestResult(t, &output, &SampleVolumeGroups[0])
92178
})
93179

94-
t.Run("Should return 404 if get volume group with bad request", func(t *testing.T) {
180+
t.Run("Should return 404 if get volume group resource does not exist", func(t *testing.T) {
95181
mockClient := new(dbtest.Client)
96182
mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, errors.New("db error"))
97183
db.C = mockClient
@@ -102,3 +188,78 @@ func TestGetVolumeGroup(t *testing.T) {
102188
assertTestResult(t, w.Code, 404)
103189
})
104190
}
191+
192+
func TestUpdateVolumeGroup(t *testing.T) {
193+
var jsonStr = []byte(`{
194+
"id": "3769855c-a102-11e7-b772-17b880d2f555",
195+
"name": "volumeGroup-demo",
196+
"description": "volumeGroup test"
197+
}`)
198+
199+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
200+
vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{
201+
Id: "3769855c-a102-11e7-b772-17b880d2f555",
202+
UpdatedAt: time.Now().Format(constants.TimeFormat),
203+
},
204+
Status: "available",
205+
}
206+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg)
207+
mockClient := new(dbtest.Client)
208+
mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(&SampleVolumeGroups[1], nil)
209+
mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), SampleVolumeGroups[1].Id).Return(nil, nil)
210+
mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[0], nil)
211+
mockClient.On("UpdateVolumeGroup", c.NewAdminContext(), &vg).Return(&SampleVolumeGroups[1], nil)
212+
db.C = mockClient
213+
214+
r, _ := http.NewRequest("PUT", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", bytes.NewBuffer(jsonStr))
215+
w := httptest.NewRecorder()
216+
r.Header.Set("Content-Type", "application/JSON")
217+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
218+
httpCtx.Input.SetData("context", c.NewAdminContext())
219+
})
220+
beego.BeeApp.Handlers.ServeHTTP(w, r)
221+
var output model.VolumeGroupSpec
222+
json.Unmarshal(w.Body.Bytes(), &output)
223+
assertTestResult(t, w.Code, 202)
224+
assertTestResult(t, &output, &SampleVolumeGroups[1])
225+
})
226+
227+
t.Run("Should return 400 if update volume fails with bad request", func(t *testing.T) {
228+
vg := model.VolumeGroupSpec{BaseModel: &model.BaseModel{}}
229+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&vg)
230+
mockClient := new(dbtest.Client)
231+
mockClient.On("GetVolumeGroup", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&vg, nil)
232+
mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(nil, nil)
233+
mockClient.On("UpdateVolumeGroup", c.NewAdminContext(), &vg).Return(nil, errors.New("db error"))
234+
db.C = mockClient
235+
236+
r, _ := http.NewRequest("PUT", "/v1beta/block/volumeGroups/bd5b12a8-a101-11e7-941e-d77981b584d8", bytes.NewBuffer(jsonStr))
237+
w := httptest.NewRecorder()
238+
r.Header.Set("Content-Type", "application/JSON")
239+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
240+
httpCtx.Input.SetData("context", c.NewAdminContext())
241+
})
242+
beego.BeeApp.Handlers.ServeHTTP(w, r)
243+
assertTestResult(t, w.Code, 400)
244+
})
245+
}
246+
247+
func TestDeleteVolumeGroup(t *testing.T) {
248+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
249+
mockClient := new(dbtest.Client)
250+
var volumesUpdate []*model.VolumeSpec
251+
mockClient.On("GetVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(&SampleVolumeGroups[0], nil)
252+
mockClient.On("GetDockByPoolId", c.NewAdminContext(), SampleVolumeGroups[0].PoolId).Return(&SampleDocks[0], nil)
253+
mockClient.On("ListVolumesByGroupId", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil, nil)
254+
mockClient.On("ListSnapshotsByVolumeId", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return( nil, nil)
255+
mockClient.On("UpdateStatus", c.NewAdminContext(), volumesUpdate, "").Return( nil)
256+
mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumeGroups[0], "deleting").Return( nil)
257+
mockClient.On("DeleteVolumeGroup", c.NewAdminContext(), "3769855c-a102-11e7-b772-17b880d2f555").Return(nil)
258+
db.C = mockClient
259+
260+
r, _ := http.NewRequest("DELETE", "/v1beta/block/volumeGroups/3769855c-a102-11e7-b772-17b880d2f555", nil)
261+
w := httptest.NewRecorder()
262+
beego.BeeApp.Handlers.ServeHTTP(w, r)
263+
assertTestResult(t, w.Code, 202)
264+
})
265+
}

testutils/collection/data.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,16 @@ var (
603603
Status: "available",
604604
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
605605
},
606+
{
607+
BaseModel: &model.BaseModel{
608+
Id: "3769855c-a102-11e7-b772-17b880d2f555",
609+
},
610+
Name: "sample-group-01",
611+
Description: "This is the first sample group for testing",
612+
Status: "available",
613+
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
614+
Profiles: []string{"1106b972-66ef-11e7-b172-db03f3689c9c"},
615+
},
606616
}
607617

608618
SampleHosts = []model.HostSpec{

0 commit comments

Comments
 (0)