Skip to content

Commit

Permalink
Add Volume 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 04a1e35 commit aa78f80
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
26 changes: 23 additions & 3 deletions redfish/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ import (
"github.com/stmcginnis/gofish/common"
)

type InitializeMethod string

const (
// BackgroundInitializeMethod volume will be available for use immediately, with data erasure and preparation to happen as background tasks.
BackgroundInitializeMethod InitializeMethod = "Background"
// ForegroundInitializeMethod Data erasure and preparation tasks will complete before the volume is presented as available for use.
ForegroundInitializeMethod InitializeMethod = "Foreground"
// SkipInitializeMethod volume will be available for use immediately, with no preparation.
SkipInitializeMethod InitializeMethod = "Skip"
)

type InitializeType string

const (
// FastInitializeType volume is prepared for use quickly, typically by erasing just the beginning and end of the space so that partitioning can be performed.
FastInitializeType InitializeType = "Fast"
// SlowInitializeType volume is prepared for use slowly, typically by completely erasing the volume.
SlowInitializeType InitializeType = "Slow"
)

type LBAFormat struct {
// LBADataSizeBytes shall be the LBA data size reported in bytes.
LBADataSizeBytes int
Expand Down Expand Up @@ -731,13 +751,13 @@ func (volume *Volume) ForceEnable() error {
// `initializeMethod` is the Swordfish-defined InitializeMethod to be performed.
//
// `initializeType` is the Swordfish-defined InitializeType to be performed.
func (volume *Volume) Initialize(initializeMethod, initializeType string) error {
func (volume *Volume) Initialize(initializeMethod InitializeMethod, initializeType InitializeType) error {
if volume.initializeTarget == "" {
return errors.New("initialize is not supported by this volume")
}
t := struct {
InitializeMethod string
InitializeType string
InitializeMethod InitializeMethod
InitializeType InitializeType
}{
InitializeMethod: initializeMethod,
InitializeType: initializeType,
Expand Down
97 changes: 97 additions & 0 deletions redfish/volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// SPDX-License-Identifier: BSD-3-Clause
//

package redfish

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

"github.com/stmcginnis/gofish/common"
)

var volumeBody = `{
"@odata.type": "#Volume.v1_10_0.Volume",
"Id": "2",
"Name": "Virtual Disk 2",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"Encrypted": false,
"RAIDType": "RAID0",
"CapacityBytes": 107374182400,
"Identifiers": [
{
"DurableNameFormat": "UUID",
"DurableName": "0324c96c-8031-4f5e-886c-50cd90aca854"
}
],
"Links": {
"Drives": [
{
"@odata.id": "/redfish/v1/Systems/437XR1138R2/Storage/1/Drives/3D58ECBC375FD9F2"
}
]
},
"Actions": {
"#Volume.Initialize": {
"target": "/redfish/v1/Systems/3/Storage/RAIDIntegrated/Volumes/1/Actions/Volume.Initialize",
"[email protected]": [
"Fast",
"Slow"
]
}
},
"@odata.id": "/redfish/v1/Systems/437XR1138R2/Storage/1/Volumes/2"
}`

// TesVolume tests the parsing of Volume objects.
func TestVolume(t *testing.T) {
var result Volume
err := json.NewDecoder(strings.NewReader(volumeBody)).Decode(&result)

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

assertEquals(t, "2", result.ID)
assertEquals(t, "RAID0", string(result.RAIDType))
assertEquals(t, "UUID", string(result.Identifiers[0].DurableNameFormat))
assertEquals(t, "0324c96c-8031-4f5e-886c-50cd90aca854", result.Identifiers[0].DurableName)
assertEquals(t, "/redfish/v1/Systems/437XR1138R2/Storage/1/Drives/3D58ECBC375FD9F2", result.drives[0])
}

// TestVolumeInitialize tests the Volume Initialize call.
func TestVolumeInitialize(t *testing.T) {
var result Volume
err := json.NewDecoder(strings.NewReader(volumeBody)).Decode(&result)

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

testClient := &common.TestClient{}
result.SetClient(testClient)

err = result.Initialize(BackgroundInitializeMethod, FastInitializeType)
if err != nil {
t.Errorf("Error making Reset call: %s", err)
}

calls := testClient.CapturedCalls()

if len(calls) != 1 {
t.Errorf("Expected one call to be made, captured: %#v", calls)
}

if !strings.Contains(calls[0].Payload, "InitializeMethod:Background") {
t.Errorf("Expected reset type not found in payload: %s", calls[0].Payload)
}

if !strings.Contains(calls[0].Payload, "InitializeType:Fast") {
t.Errorf("Expected reset type not found in payload: %s", calls[0].Payload)
}
}

0 comments on commit aa78f80

Please sign in to comment.