Skip to content
Open
Changes from 1 commit
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
117 changes: 117 additions & 0 deletions pkg/api/controllers/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -46,6 +47,84 @@ func init() {
// Tests for volume attachment //
////////////////////////////////////////////////////////////////////////////////

func TestCreateVolumeAttachment(t *testing.T) {
var jsonStr = []byte(`{
"id": "f2dda3d2-bf79-11e7-8665-f750b088f63e",
"name": "fake volume attachment",
"description": "fake volume attachment",
"hostId": "202964b5-8e73-46fd-b41b-a8e403f3c30b",
"volumeId": "bd5b12a8-a101-11e7-941e-d77981b584d8",
"attachMode": "ro"
}`)
var expectedJson = []byte(`{
Comment thread
nguptaopensds marked this conversation as resolved.
"id": "f2dda3d2-bf79-11e7-8665-f750b088f63e",
"name": "fake volume attachment",
"description": "fake volume attachment",
"status": "creating",
"volumeId": "bd5b12a8-a101-11e7-941e-d77981b584d8",
"hostId": "202964b5-8e73-46fd-b41b-a8e403f3c30b",
"connectionInfo": {
"driverVolumeType": "iscsi",
"connectionData": {
"targetDiscovered": true,
"targetIqn": "iqn.2017-10.io.opensds:volume:00000001",
"targetPortal": "127.0.0.0.1:3260",
"discard": false
}
}
}`)
var expected model.VolumeAttachmentSpec
json.Unmarshal(expectedJson, &expected)
fmt.Println(expected)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is for debugging, please remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

t.Run("Should return 202 if everything works well", func(t *testing.T) {
var attachment = model.VolumeAttachmentSpec{
BaseModel: &model.BaseModel{},
AccessProtocol: "rbd",
Status: "creating",
}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
mockClient := new(dbtest.Client)
mockClient.On("GetHost", c.NewAdminContext(), attachment.HostId).Return(&SampleHosts[0], nil)
mockClient.On("GetVolume", c.NewAdminContext(), attachment.VolumeId).Return(&SampleVolumes[0], nil)
mockClient.On("UpdateStatus", c.NewAdminContext(), &SampleVolumes[0], "attaching").Return(nil)
mockClient.On("GetPool", c.NewAdminContext(), SampleVolumes[0].PoolId).Return(&SamplePools[0], nil)
mockClient.On("CreateVolumeAttachment", c.NewAdminContext(), &attachment).
Return(&SampleAttachments[0], nil)
mockClient.On("Connect", "127.0.0.1").Return(nil)
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/block/attachments", bytes.NewReader(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.VolumeAttachmentSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 202)
assertTestResult(t, &output, &SampleAttachments[0])
})

t.Run("Should return 500 if create volume attachment with bad request", func(t *testing.T) {
attachment := model.VolumeAttachmentSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
mockClient := new(dbtest.Client)
mockClient.On("CreateVolumeAttachment", c.NewAdminContext(), &attachment).
Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/block/attachments", 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 TestListVolumeAttachments(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down Expand Up @@ -200,3 +279,41 @@ func TestUpdateVolumeAttachment(t *testing.T) {
assertTestResult(t, w.Code, 500)
})
}

Comment thread
nguptaopensds marked this conversation as resolved.
//func TestDeleteVolumeAttachment(t *testing.T) {
//
// t.Run("Should return 202 if everything works well", func(t *testing.T) {
// mockClient := new(dbtest.Client)
// mockClient.On("DeleteVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").
// Return(&SampleAttachments[1], nil)
//
// db.C = mockClient
// attachment := model.VolumeAttachmentSpec{
// BaseModel: &model.BaseModel{
// Id: "f2dda3d2-bf79-11e7-8665-f750b088f63e",
// },
// Status: "deleting",
// VolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
// HostId: "202964b5-8e73-46fd-b41b-a8e403f3c30b",
// ConnectionInfo: model.ConnectionInfo{
// DriverVolumeType: "iscsi",
// ConnectionData: map[string]interface{}{}},
// }
// mockClient.On("GetVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").Return(&SampleAttachments[1], nil)
// mockClient.On("GetVolume", c.NewAdminContext(), "bd5b12a8-a101-11e7-941e-d77981b584d8").Return(&SampleVolumes[0], nil)
// mockClient.On("GetHost", c.NewAdminContext(), "202964b5-8e73-46fd-b41b-a8e403f3c30b").Return(&SampleHosts[0], nil)
// mockClient.On("UpdateVolumeAttachment", c.NewAdminContext(), attachment.Id, &attachment).Return(&SampleAttachments[1], nil)
// mockClient.On("Connect", "127.0.0.1").Return(nil)
// r, _ := http.NewRequest("DELETE", "/v1beta/block/attachments/f2dda3d2-bf79-11e7-8665-f750b088f63e", nil)
// 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.VolumeAttachmentSpec
// json.Unmarshal(w.Body.Bytes(), &output)
// assertTestResult(t, w.Code, 202)
// assertTestResult(t, &output, &SampleAttachments[1])
// })
//}