Repository: vmware/govmomi
Pull request #3488 (2024-07-05 18:01:50)

vcsim: Add support for DRS automation levels
Closes: #3485

## Description

We are using vcsim for some workflows, and we need to simulate checking the DRS automation cluster attribute.

We need the following to be supported / reflected on VCSim:

```
clusterSpec1 := types.ClusterConfigSpecEx{
			DrsConfig: &types.ClusterDrsConfigInfo{
				Enabled:           types.NewBool(true),
				DefaultVmBehavior: types.DrsBehaviorFullyAutomated, // Set DRS to fully automated
			},
			DasConfig: &types.ClusterDasConfigInfo{
				Enabled: types.NewBool(true),
			},
		}

		task1, err := cl1.Reconfigure(ctx, &clusterSpec1, false)

```

## 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?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

- [x] Unit tests

## 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/simulator/cluster_compute_resource.go b/simulator/cluster_compute_resource.go
index fdb273b1..7fbb84a5 100644
--- a/simulator/cluster_compute_resource.go
+++ b/simulator/cluster_compute_resource.go
@@ -104,6 +104,9 @@ func (c *ClusterComputeResource) update(cfg *types.ClusterConfigInfoEx, cspec *t
 		if val := cspec.DrsConfig.Enabled; val != nil {
 			cfg.DrsConfig.Enabled = val
 		}
+		if val := cspec.DrsConfig.DefaultVmBehavior; val != "" {
+			cfg.DrsConfig.DefaultVmBehavior = val
+		}
 	}
 
 	return nil


Test patch:
diff --git a/simulator/cluster_compute_resource_test.go b/simulator/cluster_compute_resource_test.go
index fc16be4b..f74eee2e 100644
--- a/simulator/cluster_compute_resource_test.go
+++ b/simulator/cluster_compute_resource_test.go
@@ -18,9 +18,13 @@ package simulator
 
 import (
 	"context"
+	"path"
 	"testing"
 
+	"github.com/stretchr/testify/require"
+
 	"github.com/vmware/govmomi"
+	"github.com/vmware/govmomi/find"
 	"github.com/vmware/govmomi/object"
 	"github.com/vmware/govmomi/simulator/esx"
 	"github.com/vmware/govmomi/simulator/vpx"
@@ -83,6 +87,35 @@ func TestClusterVC(t *testing.T) {
 		t.Fatal(err)
 	}
 
+	// Enable DRS and HA for the cluster
+	clusterSpec := types.ClusterConfigSpecEx{
+		DrsConfig: &types.ClusterDrsConfigInfo{
+			Enabled:           types.NewBool(true),
+			DefaultVmBehavior: types.DrsBehaviorFullyAutomated, // Set DRS to fully automated
+		},
+		DasConfig: &types.ClusterDasConfigInfo{
+			Enabled: types.NewBool(true),
+		},
+	}
+
+	task, err := cluster.Reconfigure(ctx, &clusterSpec, true)
+	require.NoError(t, err)
+	err = task.Wait(ctx)
+	require.NoError(t, err)
+
+	// Check if DRS and HA is set
+
+	finder := find.NewFinder(c.Client, false).SetDatacenter(dc)
+	pathname := path.Join(dc.InventoryPath, "host", "cluster1")
+	clusterComputeResource, err := finder.ClusterComputeResource(ctx, pathname)
+	require.NoError(t, err)
+	clusterComputeInfo, err := clusterComputeResource.Configuration(ctx)
+	require.NoError(t, err)
+	require.NotNil(t, clusterComputeInfo.DrsConfig.Enabled)
+	require.NotNil(t, clusterComputeInfo.DasConfig.Enabled)
+	require.True(t, *clusterComputeInfo.DrsConfig.Enabled)
+	require.True(t, *clusterComputeInfo.DasConfig.Enabled)
+	require.True(t, clusterComputeInfo.DrsConfig.DefaultVmBehavior == types.DrsBehaviorFullyAutomated)
 	_, err = folders.HostFolder.CreateCluster(ctx, "cluster1", types.ClusterConfigSpecEx{})
 	if err == nil {
 		t.Error("expected DuplicateName error")