From fb2a01da3177978a8060d7c10d172ec04a71a235 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Thu, 18 Apr 2024 16:28:48 -0500 Subject: [PATCH] Add test coverage for TelemetryService Also adds a workaround for a typo in Supermicro's implementation of the TelemetryService object. Signed-off-by: Sean McGinnis --- redfish/telemetryservice.go | 6 ++ redfish/telemetryservice_test.go | 119 +++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 redfish/telemetryservice_test.go diff --git a/redfish/telemetryservice.go b/redfish/telemetryservice.go index 5c3a73fe..e4118b7c 100644 --- a/redfish/telemetryservice.go +++ b/redfish/telemetryservice.go @@ -103,6 +103,8 @@ func (telemetryservice *TelemetryService) UnmarshalJSON(b []byte) error { MetricDefinitions common.Link MetricReportDefinitions common.Link MetricReports common.Link + // Bug in Supermicro implementation + SupportedCollectionFuntions []CollectionFunction } err := json.Unmarshal(b, &t) @@ -123,6 +125,10 @@ func (telemetryservice *TelemetryService) UnmarshalJSON(b []byte) error { telemetryservice.resetTriggersToDefaultsTarget = t.Actions.ResetTriggersToDefaults.Target telemetryservice.submitTestMetricReportTarget = t.Actions.SubmitTestMetricReport.Target + if len(telemetryservice.SupportedCollectionFunctions) == 0 && len(t.SupportedCollectionFuntions) > 0 { + telemetryservice.SupportedCollectionFunctions = t.SupportedCollectionFuntions + } + // This is a read/write object, so we need to save the raw object data for later telemetryservice.rawData = b diff --git a/redfish/telemetryservice_test.go b/redfish/telemetryservice_test.go new file mode 100644 index 00000000..8bf4b89b --- /dev/null +++ b/redfish/telemetryservice_test.go @@ -0,0 +1,119 @@ +// +// SPDX-License-Identifier: BSD-3-Clause +// + +package redfish + +import ( + "encoding/json" + "strings" + "testing" +) + +var tsBody = strings.NewReader( + `{ + "@odata.type": "#TelemetryService.v1_2_0.TelemetryService", + "@odata.id": "/redfish/v1/TelemetryService", + "Id": "TelemetryService", + "Name": "Telemetry Service", + "Status": { + "State": "Enabled", + "Health": "OK" + }, + "SupportedCollectionFunctions": [ + "Average", + "Minimum", + "Maximum" + ], + "MetricDefinitions": { + "@odata.id": "/redfish/v1/TelemetryService/MetricDefinitions" + }, + "MetricReportDefinitions": { + "@odata.id": "/redfish/v1/TelemetryService/MetricReportDefinitions" + }, + "MetricReports": { + "@odata.id": "/redfish/v1/TelemetryService/MetricReports" + } + }`) + +var smcBody = strings.NewReader( + `{ + "@odata.type": "#TelemetryService.v1_2_0.TelemetryService", + "@odata.id": "/redfish/v1/TelemetryService", + "Id": "TelemetryService", + "Name": "Telemetry Service", + "Status": { + "State": "Enabled", + "Health": "OK" + }, + "SupportedCollectionFuntions": [ + "Average", + "Minimum", + "Maximum" + ], + "MetricDefinitions": { + "@odata.id": "/redfish/v1/TelemetryService/MetricDefinitions" + }, + "MetricReportDefinitions": { + "@odata.id": "/redfish/v1/TelemetryService/MetricReportDefinitions" + }, + "MetricReports": { + "@odata.id": "/redfish/v1/TelemetryService/MetricReports" + } + }`) + +// TestTelemetryService tests the parsing of TelemetryService objects. +func TestTelemetryService(t *testing.T) { + var result TelemetryService + err := json.NewDecoder(tsBody).Decode(&result) + + if err != nil { + t.Errorf("Error decoding JSON: %s", err) + } + + if result.ID != "TelemetryService" { + t.Errorf("Received invalid ID: %s", result.ID) + } + + if result.Name != "Telemetry Service" { + t.Errorf("Received invalid name: %s", result.Name) + } + + if len(result.SupportedCollectionFunctions) != 3 { + t.Errorf("Expected 3 supported collection functions, got: %d", len(result.SupportedCollectionFunctions)) + } + + if result.SupportedCollectionFunctions[0] != "Average" { + t.Errorf("Unexpected collection function content: %v", result.SupportedCollectionFunctions) + } + + if TaskState(result.metricDefinitions) != "/redfish/v1/TelemetryService/MetricDefinitions" { + t.Errorf("Invalid metric definition target: %s", result.metricDefinitions) + } + + if TaskState(result.metricReportDefinitions) != "/redfish/v1/TelemetryService/MetricReportDefinitions" { + t.Errorf("Invalid metric report definition target: %s", result.metricReportDefinitions) + } + + if TaskState(result.metricReports) != "/redfish/v1/TelemetryService/MetricReports" { + t.Errorf("Invalid metric definition target: %s", result.metricReports) + } +} + +// TestTelemetryServiceSMC tests the workaround for a Supermicro typo bug in the TelemetryService. +func TestTelemetryServiceSMC(t *testing.T) { + var result TelemetryService + err := json.NewDecoder(smcBody).Decode(&result) + + if err != nil { + t.Errorf("Error decoding JSON: %s", err) + } + + if len(result.SupportedCollectionFunctions) != 3 { + t.Errorf("Expected 3 supported collection functions, got: %d", len(result.SupportedCollectionFunctions)) + } + + if result.SupportedCollectionFunctions[0] != "Average" { + t.Errorf("Unexpected collection function content: %v", result.SupportedCollectionFunctions) + } +}