client_mock.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mockserver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/mockserver"
  4. import (
  5. "encoding/json"
  6. "errors"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "path/filepath"
  11. "testing"
  12. xj "github.com/basgys/goxml2json"
  13. "github.com/stretchr/testify/require"
  14. )
  15. const (
  16. // MockUsername is the correct user for authentication to the Mock Server
  17. MockUsername = "otelu"
  18. // MockPassword is the correct password for authentication to the Mock Server
  19. MockPassword = "otelp"
  20. )
  21. var errNotFound = errors.New("not found")
  22. type soapRequest struct {
  23. Envelope soapEnvelope `json:"Envelope"`
  24. }
  25. type soapEnvelope struct {
  26. Body map[string]any `json:"Body"`
  27. }
  28. // MockServer has access to recorded SOAP responses and will serve them over http based off the scraper's API calls
  29. func MockServer(t *testing.T, useTLS bool) *httptest.Server {
  30. handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  31. // converting to JSON in order to iterate over map keys
  32. jsonified, err := xj.Convert(r.Body)
  33. require.NoError(t, err)
  34. sr := &soapRequest{}
  35. err = json.Unmarshal(jsonified.Bytes(), sr)
  36. require.NoError(t, err)
  37. require.Len(t, sr.Envelope.Body, 1)
  38. var requestType string
  39. for k := range sr.Envelope.Body {
  40. requestType = k
  41. }
  42. require.NotEmpty(t, requestType)
  43. body, err := routeBody(t, requestType, sr.Envelope.Body)
  44. if errors.Is(err, errNotFound) {
  45. w.WriteHeader(404)
  46. return
  47. }
  48. w.WriteHeader(http.StatusOK)
  49. w.Header().Set("Content-Type", "text/xml")
  50. _, _ = w.Write(body)
  51. })
  52. if useTLS {
  53. return httptest.NewTLSServer(handlerFunc)
  54. }
  55. return httptest.NewServer(handlerFunc)
  56. }
  57. func routeBody(t *testing.T, requestType string, body map[string]any) ([]byte, error) {
  58. switch requestType {
  59. case "RetrieveServiceContent":
  60. return loadResponse("service-content.xml")
  61. case "Login":
  62. return loadResponse("login.xml")
  63. case "Logout":
  64. return loadResponse("logout.xml")
  65. case "RetrieveProperties":
  66. return routeRetreiveProperties(t, body)
  67. case "QueryPerf":
  68. return routePerformanceQuery(t, body)
  69. }
  70. return []byte{}, errNotFound
  71. }
  72. func routeRetreiveProperties(t *testing.T, body map[string]any) ([]byte, error) {
  73. rp, ok := body["RetrieveProperties"].(map[string]any)
  74. require.True(t, ok)
  75. specSet := rp["specSet"].(map[string]any)
  76. var objectSetArray = false
  77. objectSet, ok := specSet["objectSet"].(map[string]any)
  78. if !ok {
  79. objectSetArray = true
  80. }
  81. var propSetArray = false
  82. propSet, ok := specSet["propSet"].(map[string]any)
  83. if !ok {
  84. propSetArray = true
  85. }
  86. var obj map[string]any
  87. var content string
  88. var contentType string
  89. if !objectSetArray {
  90. obj = objectSet["obj"].(map[string]any)
  91. content = obj["#content"].(string)
  92. contentType = obj["-type"].(string)
  93. }
  94. switch {
  95. case content == "group-d1" && contentType == "Folder":
  96. return loadResponse("datacenter.xml")
  97. case content == "datacenter-3" && contentType == "Datacenter":
  98. return loadResponse("datacenter-properties.xml")
  99. case content == "domain-c8" && contentType == "ClusterComputeResource":
  100. if propSetArray {
  101. pSet := specSet["propSet"].([]any)
  102. for _, prop := range pSet {
  103. spec := prop.(map[string]any)
  104. specType := spec["type"].(string)
  105. if specType == "ResourcePool" {
  106. return loadResponse("resource-pool.xml")
  107. }
  108. }
  109. }
  110. path := propSet["pathSet"].(string)
  111. switch path {
  112. case "datastore":
  113. return loadResponse("cluster-datastore.xml")
  114. case "summary":
  115. return loadResponse("cluster-summary.xml")
  116. case "host":
  117. return loadResponse("host-list.xml")
  118. }
  119. case content == "PerfMgr" && contentType == "PerformanceManager":
  120. return loadResponse("perf-manager.xml")
  121. case content == "group-h5" && contentType == "Folder":
  122. if propSetArray {
  123. arr := specSet["propSet"].([]any)
  124. for _, i := range arr {
  125. m, ok := i.(map[string]any)
  126. require.True(t, ok)
  127. if m["type"] == "ClusterComputeResource" {
  128. return loadResponse("host-cluster.xml")
  129. }
  130. }
  131. }
  132. return loadResponse("host-parent.xml")
  133. case content == "datastore-1003" && contentType == "Datastore":
  134. if objectSetArray {
  135. return loadResponse("datastore-list.xml")
  136. }
  137. return loadResponse("datastore-summary.xml")
  138. case contentType == "HostSystem":
  139. if ps, ok := propSet["pathSet"].([]any); ok {
  140. for _, v := range ps {
  141. if v == "summary.hardware" {
  142. return loadResponse("host-properties.xml")
  143. }
  144. }
  145. } else {
  146. ps, ok := propSet["pathSet"].(string)
  147. require.True(t, ok)
  148. if ps == "name" {
  149. return loadResponse("host-names.xml")
  150. }
  151. if ps == "summary.hardware" {
  152. return loadResponse("host-properties.xml")
  153. }
  154. }
  155. case content == "group-v4" && contentType == "Folder":
  156. if propSetArray {
  157. return loadResponse("vm-group.xml")
  158. }
  159. if propSet == nil {
  160. return loadResponse("vm-folder.xml")
  161. }
  162. return loadResponse("vm-folder-parent.xml")
  163. case content == "vm-1040" && contentType == "VirtualMachine":
  164. if propSet["pathSet"] == "summary.runtime.host" {
  165. return loadResponse("vm-host.xml")
  166. }
  167. return loadResponse("vm-properties.xml")
  168. case (content == "group-v1034" || content == "group-v1001") && contentType == "Folder":
  169. return loadResponse("vm-empty-folder.xml")
  170. case contentType == "ResourcePool":
  171. if ps, ok := propSet["pathSet"].([]any); ok {
  172. for _, prop := range ps {
  173. if prop == "summary" {
  174. return loadResponse("resource-pool-summary.xml")
  175. }
  176. }
  177. }
  178. if ss, ok := objectSet["selectSet"].(map[string]any); ok && ss["path"] == "resourcePool" {
  179. return loadResponse("resource-pool-group.xml")
  180. }
  181. case objectSetArray:
  182. objectArray := specSet["objectSet"].([]any)
  183. for _, i := range objectArray {
  184. m, ok := i.(map[string]any)
  185. require.True(t, ok)
  186. mObj := m["obj"].(map[string](any))
  187. typeString := mObj["-type"]
  188. if typeString == "HostSystem" {
  189. return loadResponse("host-names.xml")
  190. }
  191. }
  192. }
  193. return []byte{}, errNotFound
  194. }
  195. func routePerformanceQuery(t *testing.T, body map[string]any) ([]byte, error) {
  196. queryPerf := body["QueryPerf"].(map[string]any)
  197. require.NotNil(t, queryPerf)
  198. querySpec := queryPerf["querySpec"].(map[string]any)
  199. entity := querySpec["entity"].(map[string]any)
  200. switch entity["-type"] {
  201. case "HostSystem":
  202. return loadResponse("host-performance-counters.xml")
  203. case "VirtualMachine":
  204. return loadResponse("vm-performance-counters.xml")
  205. }
  206. return []byte{}, errNotFound
  207. }
  208. func loadResponse(filename string) ([]byte, error) {
  209. return os.ReadFile(filepath.Join("internal", "mockserver", "responses", filename))
  210. }