hostid_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package splunk
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "go.opentelemetry.io/collector/pdata/pcommon"
  8. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  9. )
  10. var (
  11. ec2Resource = func() pcommon.Resource {
  12. res := pcommon.NewResource()
  13. attr := res.Attributes()
  14. attr.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
  15. attr.PutStr(conventions.AttributeCloudAccountID, "1234")
  16. attr.PutStr(conventions.AttributeCloudRegion, "us-west-2")
  17. attr.PutStr(conventions.AttributeHostID, "i-abcd")
  18. return res
  19. }()
  20. ec2WithHost = func() pcommon.Resource {
  21. res := pcommon.NewResource()
  22. attr := res.Attributes()
  23. attr.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
  24. attr.PutStr(conventions.AttributeCloudAccountID, "1234")
  25. attr.PutStr(conventions.AttributeCloudRegion, "us-west-2")
  26. attr.PutStr(conventions.AttributeHostID, "i-abcd")
  27. attr.PutStr(conventions.AttributeHostName, "localhost")
  28. return res
  29. }()
  30. ec2PartialResource = func() pcommon.Resource {
  31. res := pcommon.NewResource()
  32. attr := res.Attributes()
  33. attr.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
  34. attr.PutStr(conventions.AttributeHostID, "i-abcd")
  35. return res
  36. }()
  37. gcpResource = func() pcommon.Resource {
  38. res := pcommon.NewResource()
  39. attr := res.Attributes()
  40. attr.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)
  41. attr.PutStr(conventions.AttributeCloudAccountID, "1234")
  42. attr.PutStr(conventions.AttributeHostID, "i-abcd")
  43. return res
  44. }()
  45. gcpPartialResource = func() pcommon.Resource {
  46. res := pcommon.NewResource()
  47. attr := res.Attributes()
  48. attr.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)
  49. attr.PutStr(conventions.AttributeCloudAccountID, "1234")
  50. return res
  51. }()
  52. azureResource = func() pcommon.Resource {
  53. res := pcommon.NewResource()
  54. attrs := res.Attributes()
  55. attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
  56. attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureVM)
  57. attrs.PutStr(conventions.AttributeHostName, "myHostName")
  58. attrs.PutStr(conventions.AttributeCloudRegion, "myCloudRegion")
  59. attrs.PutStr(conventions.AttributeHostID, "myHostID")
  60. attrs.PutStr(conventions.AttributeCloudAccountID, "myCloudAccount")
  61. attrs.PutStr("azure.vm.name", "myVMName")
  62. attrs.PutStr("azure.vm.size", "42")
  63. attrs.PutStr("azure.resourcegroup.name", "myResourcegroupName")
  64. return res
  65. }()
  66. azureScalesetResource = func() pcommon.Resource {
  67. res := pcommon.NewResource()
  68. attrs := res.Attributes()
  69. attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
  70. attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureVM)
  71. attrs.PutStr(conventions.AttributeHostName, "my.fq.host.name")
  72. attrs.PutStr(conventions.AttributeCloudRegion, "myCloudRegion")
  73. attrs.PutStr(conventions.AttributeHostID, "myHostID")
  74. attrs.PutStr(conventions.AttributeCloudAccountID, "myCloudAccount")
  75. attrs.PutStr("azure.vm.name", "myVMScalesetName_1")
  76. attrs.PutStr("azure.vm.size", "42")
  77. attrs.PutStr("azure.vm.scaleset.name", "myVMScalesetName")
  78. attrs.PutStr("azure.resourcegroup.name", "myResourcegroupName")
  79. return res
  80. }()
  81. azureMissingCloudAcct = func() pcommon.Resource {
  82. res := pcommon.NewResource()
  83. attrs := res.Attributes()
  84. attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
  85. attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureVM)
  86. attrs.PutStr(conventions.AttributeCloudRegion, "myCloudRegion")
  87. attrs.PutStr(conventions.AttributeHostID, "myHostID")
  88. attrs.PutStr("azure.vm.size", "42")
  89. attrs.PutStr("azure.resourcegroup.name", "myResourcegroupName")
  90. return res
  91. }()
  92. azureMissingResourceGroup = func() pcommon.Resource {
  93. res := pcommon.NewResource()
  94. attrs := res.Attributes()
  95. attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
  96. attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureVM)
  97. attrs.PutStr(conventions.AttributeCloudRegion, "myCloudRegion")
  98. attrs.PutStr(conventions.AttributeHostID, "myHostID")
  99. attrs.PutStr(conventions.AttributeCloudAccountID, "myCloudAccount")
  100. attrs.PutStr("azure.vm.size", "42")
  101. return res
  102. }()
  103. azureMissingHostName = func() pcommon.Resource {
  104. res := pcommon.NewResource()
  105. attrs := res.Attributes()
  106. attrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
  107. attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureVM)
  108. attrs.PutStr(conventions.AttributeCloudRegion, "myCloudRegion")
  109. attrs.PutStr(conventions.AttributeHostID, "myHostID")
  110. attrs.PutStr(conventions.AttributeCloudAccountID, "myCloudAccount")
  111. attrs.PutStr("azure.resourcegroup.name", "myResourcegroupName")
  112. attrs.PutStr("azure.vm.size", "42")
  113. return res
  114. }()
  115. hostResource = func() pcommon.Resource {
  116. res := pcommon.NewResource()
  117. attr := res.Attributes()
  118. attr.PutStr(conventions.AttributeHostName, "localhost")
  119. return res
  120. }()
  121. unknownResource = func() pcommon.Resource {
  122. res := pcommon.NewResource()
  123. attr := res.Attributes()
  124. attr.PutStr(conventions.AttributeCloudProvider, "unknown")
  125. attr.PutStr(conventions.AttributeCloudAccountID, "1234")
  126. attr.PutStr(conventions.AttributeHostID, "i-abcd")
  127. return res
  128. }()
  129. )
  130. func TestResourceToHostID(t *testing.T) {
  131. type args struct {
  132. res pcommon.Resource
  133. }
  134. tests := []struct {
  135. name string
  136. args args
  137. want HostID
  138. ok bool
  139. }{
  140. {
  141. name: "nil resource",
  142. args: args{pcommon.NewResource()},
  143. want: HostID{},
  144. ok: false,
  145. },
  146. {
  147. name: "ec2",
  148. args: args{ec2Resource},
  149. want: HostID{
  150. Key: "AWSUniqueId",
  151. ID: "i-abcd_us-west-2_1234",
  152. },
  153. ok: true,
  154. },
  155. {
  156. name: "ec2 with hostname prefers ec2",
  157. args: args{ec2WithHost},
  158. want: HostID{
  159. Key: "AWSUniqueId",
  160. ID: "i-abcd_us-west-2_1234",
  161. },
  162. ok: true,
  163. },
  164. {
  165. name: "gcp",
  166. args: args{gcpResource},
  167. want: HostID{
  168. Key: "gcp_id",
  169. ID: "1234_i-abcd",
  170. },
  171. ok: true,
  172. },
  173. {
  174. name: "azure",
  175. args: args{azureResource},
  176. want: HostID{
  177. Key: "azure_resource_id",
  178. ID: "mycloudaccount/myresourcegroupname/microsoft.compute/virtualmachines/myvmname",
  179. },
  180. ok: true,
  181. },
  182. {
  183. name: "azure scaleset",
  184. args: args{azureScalesetResource},
  185. want: HostID{
  186. Key: "azure_resource_id",
  187. ID: "mycloudaccount/myresourcegroupname/microsoft.compute/virtualmachinescalesets/myvmscalesetname/virtualmachines/1",
  188. },
  189. ok: true,
  190. },
  191. {
  192. name: "azure cloud account missing",
  193. args: args{azureMissingCloudAcct},
  194. want: HostID{},
  195. ok: false,
  196. },
  197. {
  198. name: "azure resource group missing",
  199. args: args{azureMissingResourceGroup},
  200. want: HostID{},
  201. ok: false,
  202. },
  203. {
  204. name: "azure hostname missing",
  205. args: args{azureMissingHostName},
  206. want: HostID{},
  207. ok: false,
  208. },
  209. {
  210. name: "ec2 attributes missing",
  211. args: args{ec2PartialResource},
  212. want: HostID{},
  213. ok: false,
  214. },
  215. {
  216. name: "gcp attributes missing",
  217. args: args{gcpPartialResource},
  218. want: HostID{},
  219. ok: false,
  220. },
  221. {
  222. name: "unknown provider",
  223. args: args{unknownResource},
  224. want: HostID{},
  225. ok: false,
  226. },
  227. {
  228. name: "host provider",
  229. args: args{hostResource},
  230. want: HostID{
  231. Key: "host.name",
  232. ID: "localhost",
  233. },
  234. ok: true,
  235. },
  236. }
  237. for _, tt := range tests {
  238. t.Run(tt.name, func(t *testing.T) {
  239. hostID, ok := ResourceToHostID(tt.args.res)
  240. assert.Equal(t, tt.ok, ok)
  241. assert.Equal(t, tt.want, hostID)
  242. })
  243. }
  244. }