Skip to content

Commit 52b2d2d

Browse files
committed
Attachment unit test cases
1 parent d81676b commit 52b2d2d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

pkg/api/controllers/attachment_test.go

Lines changed: 88 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,93 @@ 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 200 if everything works well", func(t *testing.T) {
80+
attachment := model.VolumeAttachmentSpec{
81+
BaseModel: &model.BaseModel{},
82+
Status: "creating",
83+
VolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
84+
HostId: "202964b5-8e73-46fd-b41b-a8e403f3c30b",
85+
AccessProtocol: "rbd",
86+
ConnectionInfo: model.ConnectionInfo{
87+
DriverVolumeType: "iscsi",
88+
ConnectionData: map[string]interface{}{
89+
"targetDiscovered": true,
90+
"targetIqn": "iqn.2017-10.io.opensds:volume:00000001",
91+
"targetPortal": "127.0.0.0.1:3260",
92+
"discard": false,
93+
}},
94+
}
95+
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
96+
mockClient := new(dbtest.Client)
97+
mockClient.On("GetHost", c.NewAdminContext(), attachment.HostId).Return(&SampleHosts[0], nil)
98+
mockClient.On("GetVolume", c.NewAdminContext(), attachment.VolumeId).Return(&SampleVolumes[0], nil)
99+
mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumes[0], "attaching").Return(nil)
100+
mockClient.On("GetPool", c.NewAdminContext(), SampleVolumes[0].PoolId).Return(&SamplePools[0], nil)
101+
mockClient.On("CreateVolumeAttachment", c.NewAdminContext(), &attachment).
102+
Return(&expected, nil)
103+
db.C = mockClient
104+
105+
r, _ := http.NewRequest("POST", "/v1beta/block/attachments", bytes.NewReader(jsonStr))
106+
w := httptest.NewRecorder()
107+
r.Header.Set("Content-Type", "application/JSON")
108+
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
109+
httpCtx.Input.SetData("context", c.NewAdminContext())
110+
})
111+
beego.BeeApp.Handlers.ServeHTTP(w, r)
112+
var output model.VolumeAttachmentSpec
113+
json.Unmarshal(w.Body.Bytes(), &output)
114+
assertTestResult(t, w.Code, 200)
115+
assertTestResult(t, &output, &expected)
116+
})
117+
118+
//t.Run("Should return 500 if update volume attachment with bad request", func(t *testing.T) {
119+
// attachment := model.VolumeAttachmentSpec{BaseModel: &model.BaseModel{}}
120+
// json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
121+
// mockClient := new(dbtest.Client)
122+
// mockClient.On("UpdateVolumeAttachment", c.NewAdminContext(), attachment.Id, &attachment).
123+
// Return(nil, errors.New("db error"))
124+
// db.C = mockClient
125+
//
126+
// r, _ := http.NewRequest("PUT", "/v1beta/block/attachments/f2dda3d2-bf79-11e7-8665-f750b088f63e", bytes.NewBuffer(jsonStr))
127+
// w := httptest.NewRecorder()
128+
// r.Header.Set("Content-Type", "application/JSON")
129+
// beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
130+
// httpCtx.Input.SetData("context", c.NewAdminContext())
131+
// })
132+
// beego.BeeApp.Handlers.ServeHTTP(w, r)
133+
// assertTestResult(t, w.Code, 500)
134+
//})
135+
}
136+
49137
func TestListVolumeAttachments(t *testing.T) {
50138

51139
t.Run("Should return 200 if everything works well", func(t *testing.T) {

0 commit comments

Comments
 (0)