Skip to content

Commit

Permalink
[chore] vcenterreceiver Updates Scraper Tests To Represent More Diver…
Browse files Browse the repository at this point in the history
…se Environment (#32607)

**Description:** <Describe what has changed.>
No user facing changes.

Updated the scraper unit tests of the vcenterreceiver to represent a
more interesting environment (Multiple hosts, multiple VMs, a vApp).
This will change the expected results, but will also highlight some of
the odd issues that I've been noticing (and hopefully will make future
PRs to fix some of these issues a little more painless).

Also cleaned up the mock server and XML response files in general so
they were a little easier to follow.

**Link to tracking Issue:** <Issue number if applicable>
N/A

**Testing:** <Describe what testing was performed and which tests were
added.>
Unit tests passing with updated (but expected) results.

**Documentation:** <Describe the documentation added.>
None needed.
  • Loading branch information
StefanKurek authored Apr 23, 2024
1 parent 27840d5 commit 17ebc7b
Show file tree
Hide file tree
Showing 29 changed files with 35,003 additions and 145 deletions.
175 changes: 116 additions & 59 deletions receiver/vcenterreceiver/internal/mockserver/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func routeRetreiveProperties(t *testing.T, body map[string]any) ([]byte, error)
objectSetArray = true
}

var propSetArray = false
propSet, ok := specSet["propSet"].(map[string]any)
var propSetArray []any
if !ok {
propSetArray = true
propSetArray = specSet["propSet"].([]any)
}

var obj map[string]any
Expand All @@ -119,15 +119,18 @@ func routeRetreiveProperties(t *testing.T, body map[string]any) ([]byte, error)
case content == "datacenter-3" && contentType == "Datacenter":
return loadResponse("datacenter-properties.xml")

case content == "datastore-1003" && contentType == "Datastore":
if objectSetArray {
return loadResponse("datastore-list.xml")
}
return loadResponse("datastore-properties.xml")

case content == "domain-c8" && contentType == "ClusterComputeResource":
if propSetArray {
pSet := specSet["propSet"].([]any)
for _, prop := range pSet {
spec := prop.(map[string]any)
specType := spec["type"].(string)
if specType == "ResourcePool" {
return loadResponse("resource-pool.xml")
}
for _, prop := range propSetArray {
spec := prop.(map[string]any)
specType := spec["type"].(string)
if specType == "ResourcePool" {
return loadResponse("cluster-children.xml")
}
}
path := propSet["pathSet"].(string)
Expand All @@ -137,83 +140,132 @@ func routeRetreiveProperties(t *testing.T, body map[string]any) ([]byte, error)
case "summary":
return loadResponse("cluster-summary.xml")
case "host":
return loadResponse("host-list.xml")
return loadResponse("cluster-host.xml")
}

case content == "PerfMgr" && contentType == "PerformanceManager":
return loadResponse("perf-manager.xml")
case content == "domain-c9" && contentType == "ComputeResource":
for _, prop := range propSetArray {
spec := prop.(map[string]any)
specType := spec["type"].(string)
if specType == "ResourcePool" {
return loadResponse("compute-children.xml")
}
}
path := propSet["pathSet"].(string)
switch path {
case "datastore":
return loadResponse("compute-datastore.xml")
case "host":
return loadResponse("compute-host.xml")
}

case content == "group-h5" && contentType == "Folder":
if propSetArray {
arr := specSet["propSet"].([]any)
for _, i := range arr {
m, ok := i.(map[string]any)
require.True(t, ok)
if m["type"] == "ClusterComputeResource" {
return loadResponse("host-cluster.xml")
case contentType == "ResourcePool":
if ps, ok := propSet["pathSet"].([]any); ok {
for _, prop := range ps {
if prop == "summary" {
if content == "resgroup-9" {
return loadResponse("cluster-resource-pool-properties.xml")
}
if content == "resgroup-10" {
return loadResponse("compute-resource-pool-properties.xml")
}
}
}
}
if ps, ok := propSet["pathSet"].(string); ok {
if ps == "owner" {
if content == "resgroup-9" {
return loadResponse("cluster-resource-pool-owner.xml")
}
if content == "resgroup-10" {
return loadResponse("compute-resource-pool-owner.xml")
}
}
}
return loadResponse("host-parent.xml")
if ss, ok := objectSet["selectSet"].(map[string]any); ok && ss["path"] == "resourcePool" {
if content == "resgroup-9" {
return loadResponse("retrieve-properties-empty.xml")
}
if content == "resgroup-10" {
return loadResponse("retrieve-properties-empty.xml")
}
}

case content == "datastore-1003" && contentType == "Datastore":
if objectSetArray {
return loadResponse("datastore-list.xml")
case content == "resgroup-v10" && contentType == "VirtualApp":
for _, prop := range propSetArray {
innerPropSet, ok := prop.(map[string]any)
require.True(t, ok)
if innerPropSet["type"] == "VirtualMachine" {
return loadResponse("virtual-app-children.xml")
}
}
if ps, ok := propSet["pathSet"].(string); ok {
if ps == "owner" {
return loadResponse("virtual-app-owner.xml")
}
}

case content == "group-h5" && contentType == "Folder":
for _, i := range propSetArray {
m, ok := i.(map[string]any)
require.True(t, ok)
if m["type"] == "ClusterComputeResource" {
return loadResponse("host-folder-children.xml")
}
}
return loadResponse("datastore-summary.xml")
return loadResponse("host-folder-parent.xml")

case contentType == "HostSystem":
if ps, ok := propSet["pathSet"].([]any); ok {
for _, v := range ps {
if v == "summary.hardware" || v == "summary.hardware.cpuMhz" {
return loadResponse("host-properties.xml")
if content == "host-1002" {
return loadResponse("cluster-host-properties.xml")
}
if content == "host-1003" {
return loadResponse("compute-host-properties.xml")
}
}
}
} else {
ps, ok := propSet["pathSet"].(string)
require.True(t, ok)
if ps == "name" {
return loadResponse("host-names.xml")
if content == "host-1002" {
return loadResponse("cluster-host-name.xml")
}
if content == "host-1003" {
return loadResponse("compute-host-name.xml")
}
}
if ps == "summary.hardware" {
return loadResponse("host-properties.xml")
}

}

case contentType == "ContainerView" && propSet["type"] == "VirtualMachine":
return loadResponse("vm-default-properties.xml")

case contentType == "ResourcePool":
if ps, ok := propSet["pathSet"].([]any); ok {
for _, prop := range ps {
if prop == "summary" {
return loadResponse("resource-pool-summary.xml")
if content == "host-1002" {
return loadResponse("cluster-host-properties.xml")
}
if content == "host-1003" {
return loadResponse("compute-host-properties.xml")
}
}
}

if ps, ok := propSet["pathSet"].(string); ok {
if ps == "owner" {
return loadResponse("resource-pool-single.xml")
}
}

if ss, ok := objectSet["selectSet"].(map[string]any); ok && ss["path"] == "resourcePool" {
return loadResponse("resource-pool-group.xml")
}

case objectSetArray:
objectArray := specSet["objectSet"].([]any)
for _, i := range objectArray {
case content == "group-v4" && contentType == "Folder":
for _, i := range propSetArray {
m, ok := i.(map[string]any)
require.True(t, ok)
mObj := m["obj"].(map[string](any))
typeString := mObj["-type"]
if typeString == "HostSystem" {
return loadResponse("host-names.xml")
if m["pathSet"] == "parentVApp" && m["type"] == "VirtualMachine" {
return loadResponse("vm-folder-parents.xml")
}
}
return loadResponse("vm-folder-children.xml")

case (content == "group-v1034" || content == "group-v1001") && contentType == "Folder":
return loadResponse("retrieve-properties-empty.xml")

case contentType == "ContainerView" && propSet["type"] == "VirtualMachine":
return loadResponse("vm-default-properties.xml")

case content == "PerfMgr" && contentType == "PerformanceManager":
return loadResponse("perf-manager.xml")
}

return []byte{}, errNotFound
Expand All @@ -230,7 +282,12 @@ func routePerformanceQuery(t *testing.T, body map[string]any) ([]byte, error) {
entity := querySpec["entity"].(map[string]any)
switch entity["-type"] {
case "HostSystem":
return loadResponse("host-performance-counters.xml")
if entity["#content"] == "host-1002" {
return loadResponse("cluster-host-perf-counters.xml")
}
if entity["#content"] == "host-1003" {
return loadResponse("compute-host-perf-counters.xml")
}
case "VirtualMachine":
return loadResponse("vm-performance-counters.xml")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<name>summary.hardware.cpuMhz</name>
<val xsi:type="xsd:int">2593</val>
</propSet>
<propSet>
<name>vm</name>
<val xsi:type="ArrayOfManagedObjectReference">
<ManagedObjectReference type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1040</ManagedObjectReference>
<ManagedObjectReference type="VirtualMachine" xsi:type="ManagedObjectReference">vm-6004</ManagedObjectReference>
</val>
</propSet>
<propSet>
<name>config</name>
<val xsi:type="HostConfigInfo">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RetrievePropertiesResponse xmlns="urn:vim25">
<returnval>
<obj type="VirtualApp">resgroup-v10</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">v-app-1</val>
</propSet>
</returnval>
</RetrievePropertiesResponse>
</soapenv:Body>
</soapenv:Envelope>
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@
<val xsi:type="ArrayOfManagedObjectReference">
<ManagedObjectReference type="VirtualMachine"
xsi:type="ManagedObjectReference">vm-1040</ManagedObjectReference>
<ManagedObjectReference type="VirtualMachine"
xsi:type="ManagedObjectReference">vm-6005</ManagedObjectReference>
</val>
</propSet>
</returnval>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
<soapenv:Body>
<RetrievePropertiesResponse xmlns="urn:vim25">
<returnval>
<obj type="ClusterComputeResource">domain-c8</obj>
<obj type="ResourcePool">resgroup-10</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">Cluster</val>
<val xsi:type="xsd:string">Resources</val>
</propSet>
</returnval>
<returnval>
<obj type="HostSystem">host-1003</obj>
<propSet>
<name>resourcePool</name>
<val type="ResourcePool" xsi:type="ManagedObjectReference">resgroup-9</val>
<name>name</name>
<val xsi:type="xsd:string">esxi-111.europe-southeast1.gve.goog</val>
</propSet>
</returnval>
</RetrievePropertiesResponse>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RetrievePropertiesResponse xmlns="urn:vim25">
<returnval>
<obj type="ComputeResource">domain-c9</obj>
<propSet>
<name>datastore</name>
<val xsi:type="ArrayOfManagedObjectReference">
<ManagedObjectReference type="Datastore" xsi:type="ManagedObjectReference">datastore-1003</ManagedObjectReference>
</val>
</propSet>
</returnval>
</RetrievePropertiesResponse>
</soapenv:Body>
</soapenv:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RetrievePropertiesResponse xmlns="urn:vim25">
<returnval>
<obj type="HostSystem">host-1003</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">esxi-111.europe-southeast1.gve.goog</val>
</propSet>
</returnval>
</RetrievePropertiesResponse>
</soapenv:Body>
</soapenv:Envelope>
Loading

0 comments on commit 17ebc7b

Please sign in to comment.