Skip to content

Commit

Permalink
Attachment unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nguptaopensds committed Jun 8, 2020
1 parent d81676b commit 1574990
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 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,93 @@ 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(`{
"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)
t.Run("Should return 200 if everything works well", func(t *testing.T) {
attachment := model.VolumeAttachmentSpec{
BaseModel: &model.BaseModel{},
Status: "creating",
VolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
HostId: "202964b5-8e73-46fd-b41b-a8e403f3c30b",
AccessProtocol: "rbd",
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,
}},
}
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(&expected, 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, 200)
assertTestResult(t, &output, &expected)
})

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 +288,61 @@ func TestUpdateVolumeAttachment(t *testing.T) {
assertTestResult(t, w.Code, 500)
})
}

func TestDeleteVolumeAttachment(t *testing.T) {

t.Run("Should return 200 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",
AccessProtocol: "rbd",
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(), "f2dda3d2-bf79-11e7-8665-f750b088f63e", &attachment).Return(&SampleAttachments[0], 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, 200)
assertTestResult(t, &output, &SampleAttachments[0])
})

//t.Run("Should return 404 if delete volume attachment with bad request", func(t *testing.T) {
// mockClient := new(dbtest.Client)
// mockClient.On("DeleteVolumeAttachment", c.NewAdminContext(), "f2dda3d2-bf79-11e7-8665-f750b088f63e").
// Return(nil, errors.New("db error"))
// db.C = mockClient
//
// 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, 404)
//})
}

0 comments on commit 1574990

Please sign in to comment.