Skip to content

Commit

Permalink
Merge pull request #795 from Shruthi-1MN/profile-fileshare-ut
Browse files Browse the repository at this point in the history
File share profile test cases
  • Loading branch information
leonwanghui committed May 29, 2019
2 parents 90b958f + 280658a commit 038d649
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 0 deletions.
242 changes: 242 additions & 0 deletions pkg/api/controllers/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,248 @@ func TestDeleteProfile(t *testing.T) {
})
}

////////////////////////////////////////////////////////////////////////////////
// Tests for file share profile //
////////////////////////////////////////////////////////////////////////////////

func TestFileShareCreateProfile(t *testing.T) {
var fakeBody = `{
"name": "silver",
"description": "silver policy",
"storageType": "file",
"provisioningProperties":{
"dataStorage":{
"storageAccessCapability": ["Read","Write","Execute"],
"provisioningPolicy": "Thin",
"isSpaceEfficient": true
},
"ioConnectivity": {
"accessProtocol": "NFS",
"maxIOPS": 5000000,
"maxBWS": 500
}
}
}`

t.Run("Should return 200 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("CreateProfile", c.NewAdminContext(), &model.ProfileSpec{
BaseModel: &model.BaseModel{},
Name: "silver",
Description: "silver policy",
StorageType: "file",
ProvisioningProperties: model.ProvisioningPropertiesSpec{
DataStorage: model.DataStorageLoS{
StorageAccessCapability: []string{"Read", "Write", "Execute"},
ProvisioningPolicy: "Thin",
IsSpaceEfficient: true,
},
IOConnectivity: model.IOConnectivityLoS{
AccessProtocol: "NFS",
MaxIOPS: 5000000,
MaxBWS: 500,
},
}}).Return(&SampleFileShareProfiles[1], nil)
db.C = mockClient

r, _ := http.NewRequest("POST", "/v1beta/profiles", strings.NewReader(fakeBody))
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.ProfileSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, &output, &SampleFileShareProfiles[1])
})
}
func TestFileShareUpdateProfile(t *testing.T) {
var jsonStr = []byte(`{
"id": "2f9c0a04-66ef-11e7-ade2-43158893e017",
"name": "silver",
"description": "silver policy"
}`)
var expectedJson = []byte(`{
"id": "2f9c0a04-66ef-11e7-ade2-43158893e017",
"name": "silver",
"description": "silver policy",
"storageType": "file",
"provisioningProperties":{
"dataStorage":{
"storageAccessCapability": ["Read","Write","Execute"],
"provisioningPolicy": "Thin",
"isSpaceEfficient": true
},
"ioConnectivity": {
"accessProtocol": "NFS",
"maxIOPS": 5000000,
"maxBWS": 500
}
}
}`)
var expected model.ProfileSpec
json.Unmarshal(expectedJson, &expected)

t.Run("Should return 200 if everything works well", func(t *testing.T) {
profile := model.ProfileSpec{BaseModel: &model.BaseModel{}}
json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&profile)
mockClient := new(dbtest.Client)
mockClient.On("UpdateProfile", c.NewAdminContext(), profile.Id, &profile).
Return(&expected, nil)
db.C = mockClient

r, _ := http.NewRequest("PUT", "/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", bytes.NewBuffer(jsonStr))
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.ProfileSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, &output, &expected)
})

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

r, _ := http.NewRequest("PUT", "/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", bytes.NewBuffer(jsonStr))
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, 500)
})
}

func TestListFileShareProfiles(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
var sampleProfiles = []*model.ProfileSpec{&SampleFileShareProfiles[1]}
mockClient := new(dbtest.Client)
m := map[string][]string{
"offset": {"0"},
"limit": {"1"},
"sortDir": {"asc"},
"sortKey": {"name"},
}
mockClient.On("ListProfilesWithFilter", c.NewAdminContext(), m).Return(
sampleProfiles, nil)
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/profiles?offset=0&limit=1&sortDir=asc&sortKey=name", 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.ProfileSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, output, sampleProfiles)
})

t.Run("Should return 500 if list profiles with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
m := map[string][]string{
"offset": {"0"},
"limit": {"1"},
"sortDir": {"asc"},
"sortKey": {"name"},
}
mockClient.On("ListProfilesWithFilter", c.NewAdminContext(), m).Return(nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/profiles?offset=0&limit=1&sortDir=asc&sortKey=name", 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, 500)
})
}

func TestGetFileShareProfile(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), "2f9c0a04-66ef-11e7-ade2-43158893e017").
Return(&SampleFileShareProfiles[1], nil)
db.C = mockClient

r, _ := http.NewRequest("GET", "/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", 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.ProfileSpec
json.Unmarshal(w.Body.Bytes(), &output)
assertTestResult(t, w.Code, 200)
assertTestResult(t, &output, &SampleFileShareProfiles[1])
})

t.Run("Should return 404 if get profile with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), "2f9c0a04-66ef-11e7-ade2-43158893e017").Return(
nil, errors.New("db error"))
db.C = mockClient

r, _ := http.NewRequest("GET",
"/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", 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)
})
}

func TestDeleteFileShareProfile(t *testing.T) {

t.Run("Should return 200 if everything works well", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), "2f9c0a04-66ef-11e7-ade2-43158893e017").Return(
&SampleFileShareProfiles[1], nil)
mockClient.On("DeleteProfile", c.NewAdminContext(), "2f9c0a04-66ef-11e7-ade2-43158893e017").Return(nil)
db.C = mockClient

r, _ := http.NewRequest("DELETE",
"/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", 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, 200)
})

t.Run("Should return 404 if delete profile with bad request", func(t *testing.T) {
mockClient := new(dbtest.Client)
mockClient.On("GetProfile", c.NewAdminContext(), "2f9c0a04-66ef-11e7-ade2-43158893e017").Return(
nil, errors.New("Invalid resource uuid"))
db.C = mockClient

r, _ := http.NewRequest("DELETE",
"/v1beta/profiles/2f9c0a04-66ef-11e7-ade2-43158893e017", 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)
})
}

////////////////////////////////////////////////////////////////////////////////
// Tests for profile custom properties spec //
////////////////////////////////////////////////////////////////////////////////
Expand Down
31 changes: 31 additions & 0 deletions testutils/collection/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ var (
},
}

SampleFileShareProfiles = []model.ProfileSpec{
{
BaseModel: &model.BaseModel{
Id: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
Name: "default",
Description: "default policy",
StorageType: "file",
CustomProperties: model.CustomPropertiesSpec{},
},
{
BaseModel: &model.BaseModel{
Id: "2f9c0a04-66ef-11e7-ade2-43158893e017",
},
Name: "silver",
Description: "silver policy",
StorageType: "file",
CustomProperties: model.CustomPropertiesSpec{
"dataStorage": map[string]interface{}{
"provisioningPolicy": "Thin",
"isSpaceEfficient": true,
},
"ioConnectivity": map[string]interface{}{
"accessProtocol": "NFS",
"maxIOPS": float64(5000000),
"maxBWS": float64(500),
},
},
},
}

SampleCustomProperties = model.CustomPropertiesSpec{
"dataStorage": map[string]interface{}{
"provisioningPolicy": "Thin",
Expand Down

0 comments on commit 038d649

Please sign in to comment.