Skip to content

Commit

Permalink
Merge pull request #3484 from akutz/feature/disk-info-2
Browse files Browse the repository at this point in the history
api: Add simulator test for GetVirtualDiskInfoByUUID
  • Loading branch information
akutz authored Jul 5, 2024
2 parents 39d3e4d + 78f07c6 commit 769897c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vmdk/disk_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type VirtualDiskInfo struct {
// - VirtualDiskSparseVer2BackingInfo
// - VirtualDiskRawDiskVer2BackingInfo
//
// These are the only backing types that have a Uuid property for comparing the
// These are the only backing types that have a UUID property for comparing the
// provided value.
func GetVirtualDiskInfoByUUID(
ctx context.Context,
Expand Down Expand Up @@ -80,7 +80,7 @@ func GetVirtualDiskInfoByUUID(
&mo); err != nil {

return VirtualDiskInfo{},
fmt.Errorf("failed to retrieve properties %w", err)
fmt.Errorf("failed to retrieve properties: %w", err)
}
}

Expand Down
47 changes: 47 additions & 0 deletions vmdk/disk_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"os"
"path"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -29,7 +30,9 @@ import (
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vmdk"
)
Expand Down Expand Up @@ -322,6 +325,10 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
t.Run("fetch properties", func(t *testing.T) {
simulator.Test(func(ctx context.Context, c *vim25.Client) {

pc := &propertyCollectorWithFault{}
pc.Self = c.ServiceContent.PropertyCollector
simulator.Map.Put(pc)

finder := find.NewFinder(c, true)
datacenter, err := finder.DefaultDatacenter(ctx)
if err != nil {
Expand Down Expand Up @@ -413,6 +420,20 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
diskUUID: diskUUID,
err: "client is nil",
},
{
name: "failed to retrieve properties",
ctx: context.Background(),
client: c,
diskUUID: diskUUID,
mo: mo.VirtualMachine{
ManagedEntity: mo.ManagedEntity{
ExtensibleManagedObject: mo.ExtensibleManagedObject{
Self: vm.Reference(),
},
},
},
err: "failed to retrieve properties: ServerFaultCode: InvalidArgument",
},
{
name: "fetchProperties is false but cached properties are missing",
ctx: context.Background(),
Expand Down Expand Up @@ -461,6 +482,13 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
for i := range testCases {
tc := testCases[i]
t.Run(tc.name, func(t *testing.T) {
if strings.HasPrefix(tc.err, "failed to retrieve properties:") {
propertyCollectorShouldFault = true
defer func() {
propertyCollectorShouldFault = false
}()
}

dii, err := vmdk.GetVirtualDiskInfoByUUID(
tc.ctx,
tc.client,
Expand All @@ -478,3 +506,22 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
})
})
}

var propertyCollectorShouldFault bool

type propertyCollectorWithFault struct {
simulator.PropertyCollector
}

func (pc *propertyCollectorWithFault) RetrievePropertiesEx(
ctx *simulator.Context,
req *types.RetrievePropertiesEx) soap.HasFault {

if propertyCollectorShouldFault {
return &methods.RetrievePropertiesExBody{
Fault_: simulator.Fault("", &types.InvalidArgument{}),
}
}

return pc.PropertyCollector.RetrievePropertiesEx(ctx, req)
}

0 comments on commit 769897c

Please sign in to comment.