Skip to content

Commit 7195ca7

Browse files
committed
Attachment unit test cases
1 parent d81676b commit 7195ca7

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

pkg/api/controllers/attachment_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"encoding/json"
2020
"errors"
21+
"fmt"
2122
"net/http"
2223
"net/http/httptest"
2324
"testing"
@@ -46,6 +47,84 @@ func init() {
4647
// Tests for volume attachment //
4748
////////////////////////////////////////////////////////////////////////////////
4849

50+
func TestCreateVolumeAttachment(t *testing.T) {
51+
var jsonStr = []byte(`{
52+
"id": "f2dda3d2-bf79-11e7-8665-f750b088f63e",
53+
"name": "fake volume attachment",
54+
"description": "fake volume attachment",
55+
"hostId": "202964b5-8e73-46fd-b41b-a8e403f3c30b",
56+
"volumeId": "bd5b12a8-a101-11e7-941e-d77981b584d8",
57+
"attachMode": "ro"
58+
}`)
59+
var expectedJson = []byte(`{
60+
"id": "f2dda3d2-bf79-11e7-8665-f750b088f63e",
61+
"name": "fake volume attachment",
62+
"description": "fake volume attachment",
63+
"status": "creating",
64+
"volumeId": "bd5b12a8-a101-11e7-941e-d77981b584d8",
65+
"hostId": "202964b5-8e73-46fd-b41b-a8e403f3c30b",
66+
"connectionInfo": {
67+
"driverVolumeType": "iscsi",
68+
"connectionData": {
69+
"targetDiscovered": true,
70+
"targetIqn": "iqn.2017-10.io.opensds:volume:00000001",
71+
"targetPortal": "127.0.0.0.1:3260",
72+
"discard": false
73+
}
74+
}
75+
}`)
76+
var expected model.VolumeAttachmentSpec
77+
json.Unmarshal(expectedJson, &expected)
78+
fmt.Println(expected)
79+
t.Run("Should return 202 if everything works well", func(t *testing.T) {
80+
var attachment = model.VolumeAttachmentSpec{
81+
BaseModel: &model.BaseModel{},
82+
AccessProtocol: "rbd",
83+
Status: "creating",
84+
}
85+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
86+
mockClient := new(dbtest.Client)
87+
mockClient.On("GetHost", c.NewAdminContext(), attachment.HostId).Return(&SampleHosts[0], nil)
88+
mockClient.On("GetVolume", c.NewAdminContext(), attachment.VolumeId).Return(&SampleVolumes[0], nil)
89+
mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumes[0], "attaching").Return(nil)
90+
mockClient.On("GetPool", c.NewAdminContext(), SampleVolumes[0].PoolId).Return(&SamplePools[0], nil)
91+
mockClient.On("CreateVolumeAttachment", c.NewAdminContext(), &attachment).
92+
Return(&SampleAttachments[0], nil)
93+
mockClient.On("Connect", "127.0.0.1").Return(nil)
94+
db.C = mockClient
95+
96+
r, _ := http.NewRequest("POST", "/v1beta/block/attachments", bytes.NewReader(jsonStr))
97+
w := httptest.NewRecorder()
98+
r.Header.Set("Content-Type", "application/JSON")
99+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
100+
httpCtx.Input.SetData("context", c.NewAdminContext())
101+
})
102+
beego.BeeApp.Handlers.ServeHTTP(w, r)
103+
var output model.VolumeAttachmentSpec
104+
json.Unmarshal(w.Body.Bytes(), &output)
105+
assertTestResult(t, w.Code, 202)
106+
assertTestResult(t, &output, &SampleAttachments[0])
107+
})
108+
109+
t.Run("Should return 500 if create volume attachment with bad request", func(t *testing.T) {
110+
attachment := model.VolumeAttachmentSpec{BaseModel: &model.BaseModel{}}
111+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
112+
mockClient := new(dbtest.Client)
113+
mockClient.On("CreateVolumeAttachment", c.NewAdminContext(), &attachment).
114+
Return(nil, errors.New("db error"))
115+
db.C = mockClient
116+
117+
r, _ := http.NewRequest("POST", "/v1beta/block/attachments", bytes.NewBuffer(jsonStr))
118+
w := httptest.NewRecorder()
119+
r.Header.Set("Content-Type", "application/JSON")
120+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
121+
httpCtx.Input.SetData("context", c.NewAdminContext())
122+
})
123+
beego.BeeApp.Handlers.ServeHTTP(w, r)
124+
assertTestResult(t, w.Code, 500)
125+
})
126+
}
127+
49128
func TestListVolumeAttachments(t *testing.T) {
50129

51130
t.Run("Should return 200 if everything works well", func(t *testing.T) {
@@ -200,3 +279,41 @@ func TestUpdateVolumeAttachment(t *testing.T) {
200279
assertTestResult(t, w.Code, 500)
201280
})
202281
}
282+
283+
//func TestDeleteVolumeAttachment(t *testing.T) {
284+
//
285+
// t.Run("Should return 202 if everything works well", func(t *testing.T) {
286+
// mockClient := new(dbtest.Client)
287+
// mockClient.On("DeleteVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").
288+
// Return(&SampleAttachments[1], nil)
289+
//
290+
// db.C = mockClient
291+
// attachment := model.VolumeAttachmentSpec{
292+
// BaseModel: &model.BaseModel{
293+
// Id: "f2dda3d2-bf79-11e7-8665-f750b088f63e",
294+
// },
295+
// Status: "deleting",
296+
// VolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
297+
// HostId: "202964b5-8e73-46fd-b41b-a8e403f3c30b",
298+
// ConnectionInfo: model.ConnectionInfo{
299+
// DriverVolumeType: "iscsi",
300+
// ConnectionData: map[string]interface{}{}},
301+
// }
302+
// mockClient.On("GetVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").Return(&SampleAttachments[1], nil)
303+
// mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[0], nil)
304+
// mockClient.On("GetHost", c.NewAdminContext(), "202964b5-8e73-46fd-b41b-a8e403f3c30b").Return(&SampleHosts[0], nil)
305+
// mockClient.On("UpdateVolumeAttachment", c.NewAdminContext(), attachment.Id, &attachment).Return(&SampleAttachments[1], nil)
306+
// mockClient.On("Connect", "127.0.0.1").Return(nil)
307+
// r, _ := http.NewRequest("DELETE", "/v1beta/block/attachments/f2dda3d2-bf79-11e7-8665-f750b088f63e", nil)
308+
// w := httptest.NewRecorder()
309+
// beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
310+
// httpCtx.Input.SetData("context", c.NewAdminContext())
311+
// })
312+
// beego.BeeApp.Handlers.ServeHTTP(w, r)
313+
//
314+
// var output model.VolumeAttachmentSpec
315+
// json.Unmarshal(w.Body.Bytes(), &output)
316+
// assertTestResult(t, w.Code, 202)
317+
// assertTestResult(t, &output, &SampleAttachments[1])
318+
// })
319+
//}

0 commit comments

Comments
 (0)