Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment unit test cases in SODA API pkg/controller #1287

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
132 changes: 125 additions & 7 deletions pkg/api/controllers/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,89 @@ 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(`{
nguptaopensds marked this conversation as resolved.
Show resolved Hide 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)
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 400 if create volume attachment with bad request", func(t *testing.T) {
attachment := model.VolumeAttachmentSpec{BaseModel: &model.BaseModel{}}
volume := model.VolumeSpec{}
host := model.HostSpec{}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&attachment)
mockClient := new(dbtest.Client)
mockClient.On("GetHost", c.NewAdminContext(), attachment.HostId).Return(&host, nil)
mockClient.On("GetVolume", c.NewAdminContext(), attachment.VolumeId).Return(&volume, nil)
mockClient.On("UpdateStatus", c.NewAdminContext(), attachment, "").Return(nil)
mockClient.On("GetPool", c.NewAdminContext(), volume.PoolId).Return(nil, nil)
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, 400)
})
}

func TestListVolumeAttachments(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
Expand Down Expand Up @@ -75,20 +158,16 @@ func TestListVolumeAttachments(t *testing.T) {
assertTestResult(t, output, sampleAttachments)
})

t.Run("Should return 500 if list volume attachments with bad request", func(t *testing.T) {
t.Run("Should return 500 if list volume attachments internal server error", func(t *testing.T) {
mockClient := new(dbtest.Client)
m := map[string][]string{
"volumeId": {"bd5b12a8-a101-11e7-941e-d77981b584d8"},
"offset": {"0"},
"limit": {"1"},
"sortDir": {"asc"},
"sortKey": {"name"},
}
mockClient.On("ListVolumeAttachmentsWithFilter", c.NewAdminContext(), m).Return(nil, errors.New("db error"))
mockClient.On("ListVolumeAttachmentsWithFilter", c.NewAdminContext(), m).Return(nil, errors.New("internal server error"))
db.C = mockClient

r, _ := http.NewRequest("GET",
"/v1beta/block/attachments?volumeId=bd5b12a8-a101-11e7-941e-d77981b584d8&offset=0&limit=1&sortDir=asc&sortKey=name", nil)
"/v1beta/block/attachments?volumeId=bd5b12a8-a101-11e7-941e-d77981b584d8", nil)
w := httptest.NewRecorder()
beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
httpCtx.Input.SetData("context", c.NewAdminContext())
Expand Down Expand Up @@ -200,3 +279,42 @@ func TestUpdateVolumeAttachment(t *testing.T) {
assertTestResult(t, w.Code, 500)
})
}

nguptaopensds marked this conversation as resolved.
Show resolved Hide 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[0], 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{}{
"targetDiscovered": true,
"targetIqn": "iqn.2017-10.io.opensds:volume:00000001",
"targetPortal": "127.0.0.0.1:3260",
"discard": false,
},
}}
mockClient.On("GetVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").Return(&SampleAttachments[0], 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[0], 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)
assertTestResult(t, w.Code, 202)
})
}