Skip to content

Commit

Permalink
Add USBController test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Sean McGinnis <[email protected]>
  • Loading branch information
stmcginnis committed Apr 25, 2024
1 parent 649f156 commit 30594f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
24 changes: 4 additions & 20 deletions redfish/usbcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type USBController struct {
// PartNumber shall contain the manufacturer-provided part number for the USB controller.
PartNumber string
// Ports shall contain a link to a resource collection of type PortCollection.
ports []string
ports string
// SKU shall contain the SKU number for this USB controller.
SKU string
// SerialNumber shall contain a manufacturer-allocated number that identifies the USB controller.
Expand All @@ -59,7 +59,7 @@ func (usbcontroller *USBController) UnmarshalJSON(b []byte) error {
var t struct {
temp
Links Links
Ports common.LinksCollection
Ports common.Link
}

err := json.Unmarshal(b, &t)
Expand All @@ -72,7 +72,7 @@ func (usbcontroller *USBController) UnmarshalJSON(b []byte) error {
usbcontroller.pcieDevice = t.Links.PCIeDevice.String()
usbcontroller.processors = t.Links.Processors.ToStrings()

usbcontroller.ports = t.Ports.ToStrings()
usbcontroller.ports = t.Ports.String()

return nil
}
Expand Down Expand Up @@ -108,23 +108,7 @@ func (usbcontroller *USBController) Processors() ([]*Processor, error) {

// Ports gets the ports of the USB controller.
func (usbcontroller *USBController) Ports() ([]*Port, error) {
var result []*Port

collectionError := common.NewCollectionError()
for _, uri := range usbcontroller.ports {
item, err := GetPort(usbcontroller.GetClient(), uri)
if err != nil {
collectionError.Failures[uri] = err
} else {
result = append(result, item)
}
}

if collectionError.Empty() {
return result, nil
}

return result, collectionError
return ListReferencedPorts(usbcontroller.GetClient(), usbcontroller.ports)
}

// GetUSBController will get a USBController instance from the service.
Expand Down
55 changes: 55 additions & 0 deletions redfish/usbcontroller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// SPDX-License-Identifier: BSD-3-Clause
//

package redfish

import (
"encoding/json"
"strings"
"testing"
)

var usbControllerBody = `{
"@odata.type": "#USBController.v1_0_0.USBController",
"Id": "USB1",
"Name": "Contoso USB Controller 1",
"Manufacturer": "Contoso",
"Model": "USBv3",
"SKU": "80937",
"SerialNumber": "2M220100SL",
"PartNumber": "G37891",
"SparePartNumber": "G37890",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"Ports": {
"@odata.id": "/redfish/v1/Systems/1/USBControllers/USB1/Ports"
},
"Links": {
"Processors": [
{
"@odata.id": "/redfish/v1/Systems/1/Processors/1"
},
{
"@odata.id": "/redfish/v1/Systems/1/Processors/2"
}
]
},
"@odata.id": "/redfish/v1/Systems/1/USBControllers/USB1"
}`

// TestUSBController tests the parsing of USBController objects.
func TestUSBController(t *testing.T) {
var result USBController
err := json.NewDecoder(strings.NewReader(usbControllerBody)).Decode(&result)

if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}

assertEquals(t, "USB1", result.ID)
assertEquals(t, "/redfish/v1/Systems/1/USBControllers/USB1/Ports", result.ports)
assertEquals(t, "/redfish/v1/Systems/1/Processors/2", result.processors[1])
}

0 comments on commit 30594f5

Please sign in to comment.