Skip to content

Commit

Permalink
Added cpu and memory components to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toberd committed Jan 11, 2021
1 parent 2020919 commit daa0c58
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 16 deletions.
46 changes: 46 additions & 0 deletions test/create_testdata/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,59 @@ func getDeviceTestData(device string) (test.DeviceTestData, error) {
checkUPSResponse = res.(*request.CheckResponse)
}

var checkCPULoadResponse *request.CheckResponse
res, err = test.ProcessRequest(
&request.CheckCPULoadRequest{
CheckDeviceRequest: request.CheckDeviceRequest{
BaseRequest: baseRequest,
CheckRequest: request.CheckRequest{},
},
},
port,
)
if err != nil {
log.Info().Err(err).Msg("check cpu load for device " + device + " failed")
} else if res.GetExitCode() == 3 {
errString, err := parser.Parse(res, "")
if err != nil {
return test.DeviceTestData{}, errors.New("failed to parse error message")
}
log.Info().Err(errors.New(string(errString))).Msg("check cpu load for device " + device + " failed")
} else {
checkCPULoadResponse = res.(*request.CheckResponse)
}

var checkMemoryUsageResponse *request.CheckResponse
res, err = test.ProcessRequest(
&request.CheckMemoryUsageRequest{
CheckDeviceRequest: request.CheckDeviceRequest{
BaseRequest: baseRequest,
CheckRequest: request.CheckRequest{},
},
},
port,
)
if err != nil {
log.Info().Err(err).Msg("check memory usage for device " + device + " failed")
} else if res.GetExitCode() == 3 {
errString, err := parser.Parse(res, "")
if err != nil {
return test.DeviceTestData{}, errors.New("failed to parse error message")
}
log.Info().Err(errors.New(string(errString))).Msg("check memory usage for device " + device + " failed")
} else {
checkMemoryUsageResponse = res.(*request.CheckResponse)
}

return test.DeviceTestData{
Type: "snmpsim",
Expectations: test.DeviceTestDataExpectations{
Identify: identifyResponse,
ReadCountInterfaces: readCountInterfacesResponse,
CheckInterfaceMetrics: checkInterfaceMetricsResponse,
CheckUPS: checkUPSResponse,
CheckCPULoad: checkCPULoadResponse,
CheckMemoryUsage: checkMemoryUsageResponse,
},
Connection: connectionData,
}, nil
Expand Down
16 changes: 16 additions & 0 deletions test/device_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type DeviceTestDataExpectations struct {
ReadCountInterfaces *request.ReadCountInterfacesResponse `json:"readCountInterfaces" mapstructure:"readCountInterfaces"`
CheckInterfaceMetrics *request.CheckResponse `json:"checkInterfaceMetrics" mapstructure:"checkInterfaceMetrics"`
CheckUPS *request.CheckResponse `json:"checkUPS" mapstructure:"checkUPS"`
CheckCPULoad *request.CheckResponse `json:"checkCPULoad" mapstructure:"checkCPULoad"`
CheckMemoryUsage *request.CheckResponse `json:"checkMemoryUsage" mapstructure:"checkMemoryUsage"`
}

// GetAvailableRequestTypes returns all available request types
Expand All @@ -48,6 +50,14 @@ func (d *DeviceTestData) GetAvailableRequestTypes() []string {
res = append(res, "check ups")
}

if d.Expectations.CheckCPULoad != nil {
res = append(res, "check cpu-load")
}

if d.Expectations.CheckMemoryUsage != nil {
res = append(res, "check memory-usage")
}

return res
}

Expand Down Expand Up @@ -99,6 +109,12 @@ func ProcessRequest(r request.Request, port int) (request.Response, error) {
case *request.CheckUPSRequest:
requestEndpoint = "check/ups"
response = &request.CheckResponse{}
case *request.CheckCPULoadRequest:
requestEndpoint = "check/cpu-load"
response = &request.CheckResponse{}
case *request.CheckMemoryUsageRequest:
requestEndpoint = "check/memory-usage"
response = &request.CheckResponse{}
default:
return nil, errors.New("unknown request type")
}
Expand Down
18 changes: 17 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ func (t *testDeviceInfoSNMPSim) generateRequest(requestType string) (request.Req
r := request.CheckUPSRequest{}
r.DeviceData = t.requestDeviceData
return &r, nil
case "check cpu-load":
r := request.CheckCPULoadRequest{}
r.DeviceData = t.requestDeviceData
return &r, nil
case "check memory-usage":
r := request.CheckMemoryUsageRequest{}
r.DeviceData = t.requestDeviceData
return &r, nil
default:
return nil, errors.New("unknown requestType")
}
Expand Down Expand Up @@ -282,7 +290,15 @@ func (e *DeviceTestDataExpectations) compareExpectations(response request.Respon
}
case "check ups":
if !cmp.Equal(e.CheckUPS, response, metricsTransformer(), metricsRawOutputFilter()) {
return errors.New("difference:\n" + cmp.Diff(e.CheckInterfaceMetrics, response, metricsTransformer(), metricsRawOutputFilter()))
return errors.New("difference:\n" + cmp.Diff(e.CheckUPS, response, metricsTransformer(), metricsRawOutputFilter()))
}
case "check cpu-load":
if !cmp.Equal(e.CheckCPULoad, response, metricsTransformer(), metricsRawOutputFilter()) {
return errors.New("difference:\n" + cmp.Diff(e.CheckCPULoad, response, metricsTransformer(), metricsRawOutputFilter()))
}
case "check memory-usage":
if !cmp.Equal(e.CheckMemoryUsage, response, metricsTransformer(), metricsRawOutputFilter()) {
return errors.New("difference:\n" + cmp.Diff(e.CheckMemoryUsage, response, metricsTransformer(), metricsRawOutputFilter()))
}
default:
return errors.New("unknown request type")
Expand Down
4 changes: 3 additions & 1 deletion test/testdata/devices/arista_eos/device_1/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@
}
]
},
"checkUPS": null
"checkUPS": null,
"checkCPULoad": null,
"checkMemoryUsage": null
}
}
4 changes: 3 additions & 1 deletion test/testdata/devices/comware/VSR1000/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,8 @@
}
]
},
"checkUPS": null
"checkUPS": null,
"checkCPULoad": null,
"checkMemoryUsage": null
}
}
30 changes: 19 additions & 11 deletions test/testdata/devices/ios/7206VXR/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,6 @@
"crit": null,
"min": null,
"max": null
},
{
"metric": "interface_oper_status",
"label": "Null0",
"value": 1,
"unit": "",
"warn": null,
"crit": null,
"min": null,
"max": null
}
],
"raw_output": "OK: checked\n[{\"ifIndex\":\"1\",\"ifDescr\":\"FastEthernet0/0\",\"ifName\":\"Fa0/0\",\"ifAlias\":\"\",\"ifPhysAddress\":\"CA:01:16:E4:00:00\"},{\"ifIndex\":\"2\",\"ifDescr\":\"VoIP-Null0\",\"ifName\":\"Vo0\",\"ifAlias\":\"\",\"ifPhysAddress\":\"\"},{\"ifIndex\":\"3\",\"ifDescr\":\"Null0\",\"ifName\":\"Nu0\",\"ifAlias\":\"\",\"ifPhysAddress\":\"\"}]",
Expand All @@ -626,6 +616,24 @@
}
]
},
"checkUPS": null
"checkUPS": null,
"checkCPULoad": {
"status_code": 0,
"performance_data": [
{
"metric": "cpu_load",
"label": "",
"value": "2",
"unit": "%",
"warn": null,
"crit": null,
"min": null,
"max": null
}
],
"raw_output": "OK: checked",
"messages": null
},
"checkMemoryUsage": null
}
}
4 changes: 3 additions & 1 deletion test/testdata/devices/routeros/CHR_1/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5146,6 +5146,8 @@
}
]
},
"checkUPS": null
"checkUPS": null,
"checkCPULoad": null,
"checkMemoryUsage": null
}
}
4 changes: 3 additions & 1 deletion test/testdata/devices/routeros/CHR_2/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@
}
]
},
"checkUPS": null
"checkUPS": null,
"checkCPULoad": null,
"checkMemoryUsage": null
}
}

0 comments on commit daa0c58

Please sign in to comment.