Skip to content

Commit

Permalink
Merge branch 'development' into controller-ut-fileshare
Browse files Browse the repository at this point in the history
  • Loading branch information
Shruthi-1MN committed May 29, 2019
2 parents 58dee1f + 038d649 commit e1fa28d
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 8 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
27 changes: 19 additions & 8 deletions pkg/controller/metrics/adapters/metrics_sender_to_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ func writeToFile(metrics *model.MetricSpec) {
// replace the last , with } to complete the set of labels
labelStr = labelStr[:len(labelStr)-1] + "}"

// form the full metric name
metricName := metrics.Job + "_" + metrics.Component + "_" + metrics.Name + "_" + metrics.Unit + "_" + metrics.AggrType
metricName := getMetricName(metrics)

finalString += metricName + labelStr + " " + strconv.FormatFloat(metrics.MetricValues[0].Value, 'f', 2, 64) + "\n"

// make a new file with current timestamp
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
// form the temp file name
tempFName := nodeExporterFolder + metrics.Name + ".prom.temp"
tempFName := nodeExporterFolder + metricName + ".prom.temp"
// form the actual file name
fName := nodeExporterFolder + metrics.Name + ".prom"
fName := nodeExporterFolder + metricName + ".prom"

// write to the temp file
f, err := os.Create(tempFName)
Expand Down Expand Up @@ -126,10 +125,22 @@ func writeToFile(metrics *model.MetricSpec) {
}
}

func getMetricName(metrics *model.MetricSpec) string {
// form the full metric name
metricName := metrics.Job + "_" + metrics.Component + "_" + metrics.Name + "_" + metrics.Unit
// is this an aggregated metric ? add the aggregation type
if metrics.AggrType != "" {
metricName = metricName + "_" + metrics.AggrType
}
return metricName
}

func sendToPushGateway(metrics *model.MetricSpec) {

metricName := getMetricName(metrics)

gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: metrics.Name,
Name: metricName,
Help: "",
})
gauge.SetToCurrentTime()
Expand All @@ -141,12 +152,12 @@ func sendToPushGateway(metrics *model.MetricSpec) {
pusher = pusher.Grouping(lKey, lValue)
}
// add the metric name here, to differentiate between various metrics
pusher = pusher.Grouping("metricname", metrics.Name)
pusher = pusher.Grouping("metricname", metricName)

if err := pusher.Push(); err != nil {
log.Errorf("error when pushing gauge for metric name=%s;timestamp=%v:value=%v to Pushgateway:%s", metrics.Name, metrics.MetricValues[0].Timestamp, metrics.MetricValues[0].Value, err)
log.Errorf("error when pushing gauge for metric name=%s;timestamp=%v:value=%v to Pushgateway:%s", metricName, metrics.MetricValues[0].Timestamp, metrics.MetricValues[0].Value, err)

}
log.Infof("completed push gauge for metric name=%s;timestamp=%v:value=%v to Pushgateway", metrics.Name, metrics.MetricValues[0].Timestamp, metrics.MetricValues[0].Value)
log.Infof("completed push gauge for metric name=%s;timestamp=%v:value=%v to Pushgateway", metricName, metrics.MetricValues[0].Timestamp, metrics.MetricValues[0].Value)

}
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 e1fa28d

Please sign in to comment.