Skip to content

Commit

Permalink
[chore][receiver/vcenter] Reworked Integration test for vCenter (#34201)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
The following PR works to make the integration tests a bit more
deterministic by taking advantage of the govmomi simulator. Previously
the integration test had the following line when comparing
```
pmetrictest.IgnoreResourceAttributeValue("vcenter.host.name")
```
Which is a bit of a hacky fix that addressed an issue where the
simulator had inconsistent VM to Host assignments between test runs.

Although ignoring the resource attribute value currently works, it
breaks once datacenter metrics are enabled by default

This PR hopes to make the integration tests more consistent and accurate
going forward.

**Link to tracking Issue:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>
- Regenerated the expected.yaml using `scraperinttest.WriteExpected()`
- Passes Integration test

**Documentation:** <Describe the documentation added.>
  • Loading branch information
BominRahmani authored Jul 23, 2024
1 parent e32faef commit c71919f
Show file tree
Hide file tree
Showing 2 changed files with 486 additions and 440 deletions.
52 changes: 49 additions & 3 deletions receiver/vcenterreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/types"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtls"
Expand All @@ -26,7 +27,52 @@ import (
)

func TestIntegration(t *testing.T) {
simulator.Test(func(_ context.Context, c *vim25.Client) {
model := simulator.VPX()

model.Host = 2
model.Machine = 2

err := model.Create()
require.NoError(t, err)

simulator.Test(func(ctx context.Context, c *vim25.Client) {
finder := find.NewFinder(c)

hosts, err := finder.HostSystemList(ctx, "/DC0/host/*")
require.NoError(t, err)
for i, host := range hosts {
newName := fmt.Sprintf("H%d", i)
simulator.Map.Get(host.Reference()).(*simulator.HostSystem).Name = newName
}

vms, err := finder.VirtualMachineList(ctx, "/DC0/vm/*")
require.NoError(t, err)
for i, vm := range vms {
newName := fmt.Sprintf("VM%d", i)
newUUID := fmt.Sprintf("vm-uuid-%d", i)
simVM := simulator.Map.Get(vm.Reference()).(*simulator.VirtualMachine)
simVM.Name = newName
simVM.Config.Uuid = newUUID

// Explicitly assign VM to a specific host
hostIndex := i % len(hosts) // This will alternate VMs between hosts
host := hosts[hostIndex]
hostRef := host.Reference()
relocateSpec := types.VirtualMachineRelocateSpec{
Host: &hostRef,
}
task, err := vm.Relocate(ctx, relocateSpec, types.VirtualMachineMovePriorityDefaultPriority)
require.NoError(t, err)
require.NoError(t, task.Wait(ctx))

// Reconfigure the VM to apply name and UUID changes
task, err = vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
Name: newName,
Uuid: newUUID,
})
require.NoError(t, err)
require.NoError(t, task.Wait(ctx))
}
pw, set := simulator.DefaultLogin.Password()
require.True(t, set)

Expand All @@ -51,7 +97,6 @@ func TestIntegration(t *testing.T) {
defer func() {
newVcenterClient = defaultNewVcenterClient
}()

scraperinttest.NewIntegrationTest(
NewFactory(),
scraperinttest.WithCustomConfig(
Expand All @@ -66,9 +111,10 @@ func TestIntegration(t *testing.T) {
}
}),
scraperinttest.WithCompareOptions(
pmetrictest.IgnoreResourceAttributeValue("vcenter.host.name"),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreDatapointAttributesOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreMetricValues(),
Expand Down
Loading

0 comments on commit c71919f

Please sign in to comment.