Repository: vmware/govmomi
Pull request #3484 (2024-07-05 14:55:43)

api: Add simulator test for GetVirtualDiskInfoByUUID

## Description

This patch adds a simulator test for GetVirtualDiskInfoByUUID. I merged https://github.com/vmware/govmomi/pull/3482 without adding these changes, but they _were_ part of my local testing recorded in that PR description, so please refer to https://github.com/vmware/govmomi/pull/3482 for a full description.

Closes: `NA`

## Type of change

Please mark options that are relevant:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

See https://github.com/vmware/govmomi/pull/3482

## Checklist:

- [x] My code follows the `CONTRIBUTION` [guidelines] of this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged

[guidelines]: https://github.com/vmware/govmomi/blob/main/CONTRIBUTING.md



Patch:
diff --git a/vmdk/disk_info.go b/vmdk/disk_info.go
index d33880fc..6c919f10 100644
--- a/vmdk/disk_info.go
+++ b/vmdk/disk_info.go
@@ -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,
@@ -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)
 		}
 	}
 


Test patch:
diff --git a/vmdk/disk_info_test.go b/vmdk/disk_info_test.go
index 7f64d51d..bcf9692d 100644
--- a/vmdk/disk_info_test.go
+++ b/vmdk/disk_info_test.go
@@ -21,6 +21,7 @@ import (
 	"context"
 	"os"
 	"path"
+	"strings"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -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"
 )
@@ -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 {
@@ -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(),
@@ -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,
@@ -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)
+}