From aa78f80cc6e5eada9a73e8572c500dc1902d4f3d Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Thu, 25 Apr 2024 12:21:08 -0500 Subject: [PATCH] Add Volume test coverage Signed-off-by: Sean McGinnis --- redfish/volume.go | 26 +++++++++-- redfish/volume_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 redfish/volume_test.go diff --git a/redfish/volume.go b/redfish/volume.go index d35b1b2a..4ecac976 100644 --- a/redfish/volume.go +++ b/redfish/volume.go @@ -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 @@ -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, diff --git a/redfish/volume_test.go b/redfish/volume_test.go new file mode 100644 index 00000000..66046052 --- /dev/null +++ b/redfish/volume_test.go @@ -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", + "InitializeType@Redfish.AllowableValues": [ + "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) + } +}