adding tests for Smart and parser.

pull/222/head
Jason Kulatunga 3 years ago
parent 2b5c864a74
commit 5ed69d7fc4

@ -109,6 +109,9 @@ func (sm *Smart) FromCollectorSmartInfo(wwn string, info collector.SmartInfo) er
sm.Temp = info.Temperature.Current sm.Temp = info.Temperature.Current
sm.PowerCycleCount = info.PowerCycleCount sm.PowerCycleCount = info.PowerCycleCount
sm.PowerOnHours = info.PowerOnTime.Hours sm.PowerOnHours = info.PowerOnTime.Hours
if !info.SmartStatus.Passed {
sm.Status = pkg.DeviceStatusFailedSmart
}
sm.DeviceProtocol = info.Device.Protocol sm.DeviceProtocol = info.Device.Protocol
// process ATA/NVME/SCSI protocol data // process ATA/NVME/SCSI protocol data
@ -126,7 +129,6 @@ func (sm *Smart) FromCollectorSmartInfo(wwn string, info collector.SmartInfo) er
//generate SmartAtaAttribute entries from Scrutiny Collector Smart data. //generate SmartAtaAttribute entries from Scrutiny Collector Smart data.
func (sm *Smart) ProcessAtaSmartInfo(tableItems []collector.AtaSmartAttributesTableItem) { func (sm *Smart) ProcessAtaSmartInfo(tableItems []collector.AtaSmartAttributesTableItem) {
sm.Status = pkg.DeviceStatusPassed
for _, collectorAttr := range tableItems { for _, collectorAttr := range tableItems {
attrModel := SmartAtaAttribute{ attrModel := SmartAtaAttribute{
AttributeId: collectorAttr.ID, AttributeId: collectorAttr.ID,
@ -147,7 +149,7 @@ func (sm *Smart) ProcessAtaSmartInfo(tableItems []collector.AtaSmartAttributesTa
attrModel.PopulateAttributeStatus() attrModel.PopulateAttributeStatus()
sm.Attributes[strconv.Itoa(collectorAttr.ID)] = &attrModel sm.Attributes[strconv.Itoa(collectorAttr.ID)] = &attrModel
if attrModel.Status == pkg.SmartAttributeStatusFailed { if attrModel.Status == pkg.SmartAttributeStatusFailed {
sm.Status = pkg.DeviceStatusFailedScrutiny sm.Status = pkg.Set(sm.Status, pkg.DeviceStatusFailedScrutiny)
} }
} }
} }
@ -177,7 +179,7 @@ func (sm *Smart) ProcessNvmeSmartInfo(nvmeSmartHealthInformationLog collector.Nv
//find analyzed attribute status //find analyzed attribute status
for _, val := range sm.Attributes { for _, val := range sm.Attributes {
if val.GetStatus() == pkg.SmartAttributeStatusFailed { if val.GetStatus() == pkg.SmartAttributeStatusFailed {
sm.Status = pkg.DeviceStatusFailedScrutiny sm.Status = pkg.Set(sm.Status, pkg.DeviceStatusFailedScrutiny)
} }
} }
} }
@ -203,7 +205,7 @@ func (sm *Smart) ProcessScsiSmartInfo(defectGrownList int64, scsiErrorCounterLog
//find analyzed attribute status //find analyzed attribute status
for _, val := range sm.Attributes { for _, val := range sm.Attributes {
if val.GetStatus() == pkg.SmartAttributeStatusFailed { if val.GetStatus() == pkg.SmartAttributeStatusFailed {
sm.Status = pkg.DeviceStatusFailedScrutiny sm.Status = pkg.Set(sm.Status, pkg.DeviceStatusFailedScrutiny)
} }
} }
} }

@ -1,141 +1,436 @@
package measurements_test package measurements_test
//func TestFromCollectorSmartInfo(t *testing.T) { import (
// //setup "encoding/json"
// smartDataFile, err := os.Open("../testdata/smart-ata.json") "github.com/analogj/scrutiny/webapp/backend/pkg"
// require.NoError(t, err) "github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
// defer smartDataFile.Close() "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
// "github.com/stretchr/testify/require"
// var smartJson collector.SmartInfo "io/ioutil"
// "os"
// smartDataBytes, err := ioutil.ReadAll(smartDataFile) "testing"
// require.NoError(t, err) "time"
// err = json.Unmarshal(smartDataBytes, &smartJson) )
// require.NoError(t, err)
// func TestSmart_Flatten(t *testing.T) {
// //test //setup
// smartMdl := db.Smart{} timeNow := time.Now()
// err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson) smart := measurements.Smart{
// Date: timeNow,
// //assert DeviceWWN: "test-wwn",
// require.NoError(t, err) DeviceProtocol: pkg.DeviceProtocolAta,
// require.Equal(t, "WWN-test", smartMdl.DeviceWWN) Temp: 50,
// require.Equal(t, "passed", smartMdl.SmartStatus) PowerOnHours: 10,
// require.Equal(t, 18, len(smartMdl.Attributes)) PowerCycleCount: 10,
// Attributes: nil,
// //check that temperature was correctly parsed Status: 0,
// for _, attr := range smartMdl.Attributes { }
// if attr.AttributeId == 194 {
// require.Equal(t, int64(163210330144), attr.RawValue) //test
// require.Equal(t, int64(32), attr.TransformedValue) tags, fields := smart.Flatten()
// }
// } //assert
//} require.Equal(t, map[string]string{"device_protocol": "ATA", "device_wwn": "test-wwn"}, tags)
// require.Equal(t, map[string]interface{}{"power_cycle_count": int64(10), "power_on_hours": int64(10), "temp": int64(50)}, fields)
//func TestFromCollectorSmartInfo_Fail(t *testing.T) { }
// //setup
// smartDataFile, err := os.Open("../testdata/smart-fail.json") func TestSmart_Flatten_ATA(t *testing.T) {
// require.NoError(t, err) //setup
// defer smartDataFile.Close() timeNow := time.Now()
// smart := measurements.Smart{
// var smartJson collector.SmartInfo Date: timeNow,
// DeviceWWN: "test-wwn",
// smartDataBytes, err := ioutil.ReadAll(smartDataFile) DeviceProtocol: pkg.DeviceProtocolAta,
// require.NoError(t, err) Temp: 50,
// err = json.Unmarshal(smartDataBytes, &smartJson) PowerOnHours: 10,
// require.NoError(t, err) PowerCycleCount: 10,
// Status: 0,
// //test Attributes: map[string]measurements.SmartAttribute{
// smartMdl := db.Smart{} "1": &measurements.SmartAtaAttribute{
// err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson) AttributeId: 1,
// Value: 100,
// //assert Threshold: 1,
// require.NoError(t, err) Worst: 100,
// require.Equal(t, "WWN-test", smartMdl.DeviceWWN) RawValue: 0,
// require.Equal(t, "failed", smartMdl.SmartStatus) RawString: "0",
// require.Equal(t, 0, len(smartMdl.AtaAttributes)) WhenFailed: "",
// require.Equal(t, 0, len(smartMdl.NvmeAttributes)) },
// require.Equal(t, 0, len(smartMdl.ScsiAttributes)) "2": &measurements.SmartAtaAttribute{
//} AttributeId: 2,
// Value: 135,
//func TestFromCollectorSmartInfo_Fail2(t *testing.T) { Threshold: 54,
// //setup Worst: 135,
// smartDataFile, err := os.Open("../testdata/smart-fail2.json") RawValue: 108,
// require.NoError(t, err) RawString: "108",
// defer smartDataFile.Close() WhenFailed: "",
// },
// var smartJson collector.SmartInfo },
// }
// smartDataBytes, err := ioutil.ReadAll(smartDataFile)
// require.NoError(t, err) //test
// err = json.Unmarshal(smartDataBytes, &smartJson) tags, fields := smart.Flatten()
// require.NoError(t, err)
// //assert
// //test require.Equal(t, map[string]string{"device_protocol": "ATA", "device_wwn": "test-wwn"}, tags)
// smartMdl := db.Smart{} require.Equal(t, map[string]interface{}{
// err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson) "attr.1.attribute_id": "1",
// "attr.1.failure_rate": float64(0),
// //assert "attr.1.raw_string": "0",
// require.NoError(t, err) "attr.1.raw_value": int64(0),
// require.Equal(t, "WWN-test", smartMdl.DeviceWWN) "attr.1.status": int64(0),
// require.Equal(t, "failed", smartMdl.SmartStatus) "attr.1.status_reason": "",
// require.Equal(t, 17, len(smartMdl.Attributes)) "attr.1.thresh": int64(1),
//} "attr.1.transformed_value": int64(0),
// "attr.1.value": int64(100),
//func TestFromCollectorSmartInfo_Nvme(t *testing.T) { "attr.1.when_failed": "",
// //setup "attr.1.worst": int64(100),
// smartDataFile, err := os.Open("../testdata/smart-nvme.json")
// require.NoError(t, err) "attr.2.attribute_id": "2",
// defer smartDataFile.Close() "attr.2.failure_rate": float64(0),
// "attr.2.raw_string": "108",
// var smartJson collector.SmartInfo "attr.2.raw_value": int64(108),
// "attr.2.status": int64(0),
// smartDataBytes, err := ioutil.ReadAll(smartDataFile) "attr.2.status_reason": "",
// require.NoError(t, err) "attr.2.thresh": int64(54),
// err = json.Unmarshal(smartDataBytes, &smartJson) "attr.2.transformed_value": int64(0),
// require.NoError(t, err) "attr.2.value": int64(135),
// "attr.2.when_failed": "",
// //test "attr.2.worst": int64(135),
// smartMdl := db.Smart{}
// err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson) "power_cycle_count": int64(10),
// "power_on_hours": int64(10),
// //assert "temp": int64(50),
// require.NoError(t, err) }, fields)
// require.Equal(t, "WWN-test", smartMdl.DeviceWWN) }
// require.Equal(t, "passed", smartMdl.SmartStatus)
// require.Equal(t, 0, len(smartMdl.AtaAttributes)) func TestSmart_Flatten_SCSI(t *testing.T) {
// require.Equal(t, 16, len(smartMdl.NvmeAttributes)) //setup
// require.Equal(t, 0, len(smartMdl.ScsiAttributes)) timeNow := time.Now()
// smart := measurements.Smart{
// require.Equal(t, 111303174, smartMdl.NvmeAttributes[6].Value) Date: timeNow,
// require.Equal(t, 83170961, smartMdl.NvmeAttributes[7].Value) DeviceWWN: "test-wwn",
//} DeviceProtocol: pkg.DeviceProtocolScsi,
// Temp: 50,
//func TestFromCollectorSmartInfo_Scsi(t *testing.T) { PowerOnHours: 10,
// //setup PowerCycleCount: 10,
// smartDataFile, err := os.Open("../testdata/smart-scsi.json") Status: 0,
// require.NoError(t, err) Attributes: map[string]measurements.SmartAttribute{
// defer smartDataFile.Close() "read_errors_corrected_by_eccfast": &measurements.SmartScsiAttribute{
// AttributeId: "read_errors_corrected_by_eccfast",
// var smartJson collector.SmartInfo Value: int64(300357663),
// },
// smartDataBytes, err := ioutil.ReadAll(smartDataFile) },
// require.NoError(t, err) }
// err = json.Unmarshal(smartDataBytes, &smartJson)
// require.NoError(t, err) //test
// tags, fields := smart.Flatten()
// //test
// smartMdl := db.Smart{} //assert
// err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson) require.Equal(t, map[string]string{"device_protocol": "SCSI", "device_wwn": "test-wwn"}, tags)
// require.Equal(t, map[string]interface{}{
// //assert "attr.read_errors_corrected_by_eccfast.attribute_id": "read_errors_corrected_by_eccfast",
// require.NoError(t, err) "attr.read_errors_corrected_by_eccfast.failure_rate": float64(0),
// require.Equal(t, "WWN-test", smartMdl.DeviceWWN) "attr.read_errors_corrected_by_eccfast.status": int64(0),
// require.Equal(t, "passed", smartMdl.SmartStatus) "attr.read_errors_corrected_by_eccfast.status_reason": "",
// require.Equal(t, 0, len(smartMdl.AtaAttributes)) "attr.read_errors_corrected_by_eccfast.thresh": int64(0),
// require.Equal(t, 0, len(smartMdl.NvmeAttributes)) "attr.read_errors_corrected_by_eccfast.transformed_value": int64(0),
// require.Equal(t, 13, len(smartMdl.ScsiAttributes)) "attr.read_errors_corrected_by_eccfast.value": int64(300357663),
// "power_cycle_count": int64(10),
// require.Equal(t, 56, smartMdl.ScsiAttributes[0].Value) "power_on_hours": int64(10),
// require.Equal(t, 300357663, smartMdl.ScsiAttributes[4].Value) //total_errors_corrected "temp": int64(50)},
//} fields)
}
func TestSmart_Flatten_NVMe(t *testing.T) {
//setup
timeNow := time.Now()
smart := measurements.Smart{
Date: timeNow,
DeviceWWN: "test-wwn",
DeviceProtocol: pkg.DeviceProtocolNvme,
Temp: 50,
PowerOnHours: 10,
PowerCycleCount: 10,
Status: 0,
Attributes: map[string]measurements.SmartAttribute{
"available_spare": &measurements.SmartNvmeAttribute{
AttributeId: "available_spare",
Value: int64(100),
},
},
}
//test
tags, fields := smart.Flatten()
//assert
require.Equal(t, map[string]string{"device_protocol": "NVMe", "device_wwn": "test-wwn"}, tags)
require.Equal(t, map[string]interface{}{
"attr.available_spare.attribute_id": "available_spare",
"attr.available_spare.failure_rate": float64(0),
"attr.available_spare.status": int64(0),
"attr.available_spare.status_reason": "",
"attr.available_spare.thresh": int64(0),
"attr.available_spare.transformed_value": int64(0),
"attr.available_spare.value": int64(100),
"power_cycle_count": int64(10),
"power_on_hours": int64(10),
"temp": int64(50)}, fields)
}
func TestNewSmartFromInfluxDB_ATA(t *testing.T) {
//setup
timeNow := time.Now()
attrs := map[string]interface{}{
"_time": timeNow,
"device_wwn": "test-wwn",
"device_protocol": pkg.DeviceProtocolAta,
"attr.1.attribute_id": "1",
"attr.1.failure_rate": float64(0),
"attr.1.raw_string": "108",
"attr.1.raw_value": int64(108),
"attr.1.status": int64(0),
"attr.1.status_reason": "",
"attr.1.thresh": int64(54),
"attr.1.transformed_value": int64(0),
"attr.1.value": int64(135),
"attr.1.when_failed": "",
"attr.1.worst": int64(135),
"power_cycle_count": int64(10),
"power_on_hours": int64(10),
"temp": int64(50),
}
//test
smart, err := measurements.NewSmartFromInfluxDB(attrs)
//assert
require.NoError(t, err)
require.Equal(t, &measurements.Smart{
Date: timeNow,
DeviceWWN: "test-wwn",
DeviceProtocol: "ATA",
Temp: 50,
PowerOnHours: 10,
PowerCycleCount: 10,
Attributes: map[string]measurements.SmartAttribute{
"1": &measurements.SmartAtaAttribute{
AttributeId: 1,
Value: 135,
Threshold: 54,
Worst: 135,
RawValue: 108,
RawString: "108",
WhenFailed: "",
},
}, Status: 0}, smart)
}
func TestNewSmartFromInfluxDB_NVMe(t *testing.T) {
//setup
timeNow := time.Now()
attrs := map[string]interface{}{
"_time": timeNow,
"device_wwn": "test-wwn",
"device_protocol": pkg.DeviceProtocolNvme,
"attr.available_spare.attribute_id": "available_spare",
"attr.available_spare.failure_rate": float64(0),
"attr.available_spare.status": int64(0),
"attr.available_spare.status_reason": "",
"attr.available_spare.thresh": int64(0),
"attr.available_spare.transformed_value": int64(0),
"attr.available_spare.value": int64(100),
"power_cycle_count": int64(10),
"power_on_hours": int64(10),
"temp": int64(50),
}
//test
smart, err := measurements.NewSmartFromInfluxDB(attrs)
//assert
require.NoError(t, err)
require.Equal(t, &measurements.Smart{
Date: timeNow,
DeviceWWN: "test-wwn",
DeviceProtocol: "NVMe",
Temp: 50,
PowerOnHours: 10,
PowerCycleCount: 10,
Attributes: map[string]measurements.SmartAttribute{
"available_spare": &measurements.SmartNvmeAttribute{
AttributeId: "available_spare",
Value: int64(100),
},
}, Status: 0}, smart)
}
func TestNewSmartFromInfluxDB_SCSI(t *testing.T) {
//setup
timeNow := time.Now()
attrs := map[string]interface{}{
"_time": timeNow,
"device_wwn": "test-wwn",
"device_protocol": pkg.DeviceProtocolScsi,
"attr.read_errors_corrected_by_eccfast.attribute_id": "read_errors_corrected_by_eccfast",
"attr.read_errors_corrected_by_eccfast.failure_rate": float64(0),
"attr.read_errors_corrected_by_eccfast.status": int64(0),
"attr.read_errors_corrected_by_eccfast.status_reason": "",
"attr.read_errors_corrected_by_eccfast.thresh": int64(0),
"attr.read_errors_corrected_by_eccfast.transformed_value": int64(0),
"attr.read_errors_corrected_by_eccfast.value": int64(300357663),
"power_cycle_count": int64(10),
"power_on_hours": int64(10),
"temp": int64(50),
}
//test
smart, err := measurements.NewSmartFromInfluxDB(attrs)
//assert
require.NoError(t, err)
require.Equal(t, &measurements.Smart{
Date: timeNow,
DeviceWWN: "test-wwn",
DeviceProtocol: "SCSI",
Temp: 50,
PowerOnHours: 10,
PowerCycleCount: 10,
Attributes: map[string]measurements.SmartAttribute{
"read_errors_corrected_by_eccfast": &measurements.SmartScsiAttribute{
AttributeId: "read_errors_corrected_by_eccfast",
Value: int64(300357663),
},
}, Status: 0}, smart)
}
func TestFromCollectorSmartInfo(t *testing.T) {
//setup
smartDataFile, err := os.Open("../testdata/smart-ata.json")
require.NoError(t, err)
defer smartDataFile.Close()
var smartJson collector.SmartInfo
smartDataBytes, err := ioutil.ReadAll(smartDataFile)
require.NoError(t, err)
err = json.Unmarshal(smartDataBytes, &smartJson)
require.NoError(t, err)
//test
smartMdl := measurements.Smart{}
err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson)
//assert
require.NoError(t, err)
require.Equal(t, "WWN-test", smartMdl.DeviceWWN)
require.Equal(t, pkg.DeviceStatusPassed, smartMdl.Status)
require.Equal(t, 18, len(smartMdl.Attributes))
//check that temperature was correctly parsed
require.Equal(t, int64(163210330144), smartMdl.Attributes["194"].(*measurements.SmartAtaAttribute).RawValue)
require.Equal(t, int64(32), smartMdl.Attributes["194"].(*measurements.SmartAtaAttribute).TransformedValue)
}
func TestFromCollectorSmartInfo_Fail_Smart(t *testing.T) {
//setup
smartDataFile, err := os.Open("../testdata/smart-fail.json")
require.NoError(t, err)
defer smartDataFile.Close()
var smartJson collector.SmartInfo
smartDataBytes, err := ioutil.ReadAll(smartDataFile)
require.NoError(t, err)
err = json.Unmarshal(smartDataBytes, &smartJson)
require.NoError(t, err)
//test
smartMdl := measurements.Smart{}
err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson)
//assert
require.NoError(t, err)
require.Equal(t, "WWN-test", smartMdl.DeviceWWN)
require.Equal(t, pkg.DeviceStatusFailedSmart, smartMdl.Status)
require.Equal(t, 0, len(smartMdl.Attributes))
}
func TestFromCollectorSmartInfo_Fail_ScrutinySmart(t *testing.T) {
//setup
smartDataFile, err := os.Open("../testdata/smart-fail2.json")
require.NoError(t, err)
defer smartDataFile.Close()
var smartJson collector.SmartInfo
smartDataBytes, err := ioutil.ReadAll(smartDataFile)
require.NoError(t, err)
err = json.Unmarshal(smartDataBytes, &smartJson)
require.NoError(t, err)
//test
smartMdl := measurements.Smart{}
err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson)
//assert
require.NoError(t, err)
require.Equal(t, "WWN-test", smartMdl.DeviceWWN)
require.Equal(t, pkg.DeviceStatusFailedScrutiny|pkg.DeviceStatusFailedSmart, smartMdl.Status)
require.Equal(t, 17, len(smartMdl.Attributes))
}
func TestFromCollectorSmartInfo_Nvme(t *testing.T) {
//setup
smartDataFile, err := os.Open("../testdata/smart-nvme.json")
require.NoError(t, err)
defer smartDataFile.Close()
var smartJson collector.SmartInfo
smartDataBytes, err := ioutil.ReadAll(smartDataFile)
require.NoError(t, err)
err = json.Unmarshal(smartDataBytes, &smartJson)
require.NoError(t, err)
//test
smartMdl := measurements.Smart{}
err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson)
//assert
require.NoError(t, err)
require.Equal(t, "WWN-test", smartMdl.DeviceWWN)
require.Equal(t, pkg.DeviceStatusPassed, smartMdl.Status)
require.Equal(t, 16, len(smartMdl.Attributes))
require.Equal(t, int64(111303174), smartMdl.Attributes["host_reads"].(*measurements.SmartNvmeAttribute).Value)
require.Equal(t, int64(83170961), smartMdl.Attributes["host_writes"].(*measurements.SmartNvmeAttribute).Value)
}
func TestFromCollectorSmartInfo_Scsi(t *testing.T) {
//setup
smartDataFile, err := os.Open("../testdata/smart-scsi.json")
require.NoError(t, err)
defer smartDataFile.Close()
var smartJson collector.SmartInfo
smartDataBytes, err := ioutil.ReadAll(smartDataFile)
require.NoError(t, err)
err = json.Unmarshal(smartDataBytes, &smartJson)
require.NoError(t, err)
//test
smartMdl := measurements.Smart{}
err = smartMdl.FromCollectorSmartInfo("WWN-test", smartJson)
//assert
require.NoError(t, err)
require.Equal(t, "WWN-test", smartMdl.DeviceWWN)
require.Equal(t, pkg.DeviceStatusPassed, smartMdl.Status)
require.Equal(t, 13, len(smartMdl.Attributes))
require.Equal(t, int64(56), smartMdl.Attributes["scsi_grown_defect_list"].(*measurements.SmartScsiAttribute).Value)
require.Equal(t, int64(300357663), smartMdl.Attributes["read_errors_corrected_by_eccfast"].(*measurements.SmartScsiAttribute).Value) //total_errors_corrected
}

Loading…
Cancel
Save