factory_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/component"
  9. "go.opentelemetry.io/collector/consumer/consumertest"
  10. "go.opentelemetry.io/collector/receiver/receivertest"
  11. "go.opentelemetry.io/collector/receiver/scraperhelper"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver/internal/metadata"
  13. )
  14. func TestNewFactory(t *testing.T) {
  15. testCases := []struct {
  16. desc string
  17. testFunc func(*testing.T)
  18. }{
  19. {
  20. desc: "creates a new factory with correct type",
  21. testFunc: func(t *testing.T) {
  22. factory := NewFactory()
  23. require.EqualValues(t, metadata.Type, factory.Type())
  24. },
  25. },
  26. {
  27. desc: "creates a new factory with valid default config",
  28. testFunc: func(t *testing.T) {
  29. factory := NewFactory()
  30. var expectedCfg component.Config = &Config{
  31. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  32. CollectionInterval: defaultCollectionInterval,
  33. Timeout: defaultTimeout,
  34. },
  35. Endpoint: defaultEndpoint,
  36. Version: defaultVersion,
  37. Community: defaultCommunity,
  38. SecurityLevel: "no_auth_no_priv",
  39. AuthType: "MD5",
  40. PrivacyType: "DES",
  41. }
  42. require.Equal(t, expectedCfg, factory.CreateDefaultConfig())
  43. },
  44. },
  45. {
  46. desc: "creates a new factory and CreateMetricsReceiver returns no error",
  47. testFunc: func(t *testing.T) {
  48. factory := NewFactory()
  49. cfg := factory.CreateDefaultConfig()
  50. snmpCfg := cfg.(*Config)
  51. snmpCfg.Metrics = map[string]*MetricConfig{
  52. "m1": {
  53. Unit: "1",
  54. Gauge: &GaugeMetric{ValueType: "int"},
  55. ScalarOIDs: []ScalarOID{{
  56. OID: ".1",
  57. }},
  58. },
  59. }
  60. _, err := factory.CreateMetricsReceiver(
  61. context.Background(),
  62. receivertest.NewNopCreateSettings(),
  63. cfg,
  64. consumertest.NewNop(),
  65. )
  66. require.NoError(t, err)
  67. },
  68. },
  69. {
  70. desc: "creates a new factory and CreateMetricsReceiver returns error with incorrect config",
  71. testFunc: func(t *testing.T) {
  72. factory := NewFactory()
  73. _, err := factory.CreateMetricsReceiver(
  74. context.Background(),
  75. receivertest.NewNopCreateSettings(),
  76. nil,
  77. consumertest.NewNop(),
  78. )
  79. require.ErrorIs(t, err, errConfigNotSNMP)
  80. },
  81. },
  82. {
  83. desc: "CreateMetricsReceiver adds missing scheme to endpoint",
  84. testFunc: func(t *testing.T) {
  85. factory := NewFactory()
  86. cfg := factory.CreateDefaultConfig()
  87. snmpCfg := cfg.(*Config)
  88. snmpCfg.Endpoint = "localhost:161"
  89. snmpCfg.Metrics = map[string]*MetricConfig{
  90. "m1": {
  91. Unit: "1",
  92. Gauge: &GaugeMetric{ValueType: "int"},
  93. ScalarOIDs: []ScalarOID{{
  94. OID: ".1",
  95. }},
  96. },
  97. }
  98. _, err := factory.CreateMetricsReceiver(
  99. context.Background(),
  100. receivertest.NewNopCreateSettings(),
  101. cfg,
  102. consumertest.NewNop(),
  103. )
  104. require.NoError(t, err)
  105. require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
  106. },
  107. },
  108. {
  109. desc: "CreateMetricsReceiver adds missing port to endpoint",
  110. testFunc: func(t *testing.T) {
  111. factory := NewFactory()
  112. cfg := factory.CreateDefaultConfig()
  113. snmpCfg := cfg.(*Config)
  114. snmpCfg.Endpoint = "udp://localhost"
  115. snmpCfg.Metrics = map[string]*MetricConfig{
  116. "m1": {
  117. Unit: "1",
  118. Gauge: &GaugeMetric{ValueType: "int"},
  119. ScalarOIDs: []ScalarOID{{
  120. OID: ".1",
  121. }},
  122. },
  123. }
  124. _, err := factory.CreateMetricsReceiver(
  125. context.Background(),
  126. receivertest.NewNopCreateSettings(),
  127. cfg,
  128. consumertest.NewNop(),
  129. )
  130. require.NoError(t, err)
  131. require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
  132. },
  133. },
  134. {
  135. desc: "CreateMetricsReceiver adds missing port to endpoint with trailing colon",
  136. testFunc: func(t *testing.T) {
  137. factory := NewFactory()
  138. cfg := factory.CreateDefaultConfig()
  139. snmpCfg := cfg.(*Config)
  140. snmpCfg.Endpoint = "udp://localhost:"
  141. snmpCfg.Metrics = map[string]*MetricConfig{
  142. "m1": {
  143. Unit: "1",
  144. Gauge: &GaugeMetric{ValueType: "int"},
  145. ScalarOIDs: []ScalarOID{{
  146. OID: ".1",
  147. }},
  148. },
  149. }
  150. _, err := factory.CreateMetricsReceiver(
  151. context.Background(),
  152. receivertest.NewNopCreateSettings(),
  153. cfg,
  154. consumertest.NewNop(),
  155. )
  156. require.NoError(t, err)
  157. require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
  158. },
  159. },
  160. {
  161. desc: "CreateMetricsReceiver adds missing metric gauge value type as double",
  162. testFunc: func(t *testing.T) {
  163. factory := NewFactory()
  164. cfg := factory.CreateDefaultConfig()
  165. snmpCfg := cfg.(*Config)
  166. snmpCfg.Metrics = map[string]*MetricConfig{
  167. "m1": {
  168. Gauge: &GaugeMetric{},
  169. ScalarOIDs: []ScalarOID{{
  170. OID: ".1",
  171. }},
  172. },
  173. }
  174. _, err := factory.CreateMetricsReceiver(
  175. context.Background(),
  176. receivertest.NewNopCreateSettings(),
  177. cfg,
  178. consumertest.NewNop(),
  179. )
  180. require.NoError(t, err)
  181. require.Equal(t, "double", snmpCfg.Metrics["m1"].Gauge.ValueType)
  182. },
  183. },
  184. {
  185. desc: "CreateMetricsReceiver adds missing metric sum value type as double",
  186. testFunc: func(t *testing.T) {
  187. factory := NewFactory()
  188. cfg := factory.CreateDefaultConfig()
  189. snmpCfg := cfg.(*Config)
  190. snmpCfg.Metrics = map[string]*MetricConfig{
  191. "m1": {
  192. Sum: &SumMetric{},
  193. ScalarOIDs: []ScalarOID{{
  194. OID: ".1",
  195. }},
  196. },
  197. }
  198. _, err := factory.CreateMetricsReceiver(
  199. context.Background(),
  200. receivertest.NewNopCreateSettings(),
  201. cfg,
  202. consumertest.NewNop(),
  203. )
  204. require.NoError(t, err)
  205. require.Equal(t, "double", snmpCfg.Metrics["m1"].Sum.ValueType)
  206. },
  207. },
  208. {
  209. desc: "CreateMetricsReceiver adds missing metric sum aggregation as cumulative",
  210. testFunc: func(t *testing.T) {
  211. factory := NewFactory()
  212. cfg := factory.CreateDefaultConfig()
  213. snmpCfg := cfg.(*Config)
  214. snmpCfg.Metrics = map[string]*MetricConfig{
  215. "m1": {
  216. Sum: &SumMetric{},
  217. ScalarOIDs: []ScalarOID{{
  218. OID: ".1",
  219. }},
  220. },
  221. }
  222. _, err := factory.CreateMetricsReceiver(
  223. context.Background(),
  224. receivertest.NewNopCreateSettings(),
  225. cfg,
  226. consumertest.NewNop(),
  227. )
  228. require.NoError(t, err)
  229. require.Equal(t, "cumulative", snmpCfg.Metrics["m1"].Sum.Aggregation)
  230. },
  231. },
  232. {
  233. desc: "CreateMetricsReceiver adds missing metric unit as 1",
  234. testFunc: func(t *testing.T) {
  235. factory := NewFactory()
  236. cfg := factory.CreateDefaultConfig()
  237. snmpCfg := cfg.(*Config)
  238. snmpCfg.Metrics = map[string]*MetricConfig{
  239. "m1": {
  240. Gauge: &GaugeMetric{ValueType: "int"},
  241. ScalarOIDs: []ScalarOID{{
  242. OID: ".1",
  243. }},
  244. },
  245. }
  246. _, err := factory.CreateMetricsReceiver(
  247. context.Background(),
  248. receivertest.NewNopCreateSettings(),
  249. cfg,
  250. consumertest.NewNop(),
  251. )
  252. require.NoError(t, err)
  253. require.Equal(t, "1", snmpCfg.Metrics["m1"].Unit)
  254. },
  255. },
  256. }
  257. for _, tc := range testCases {
  258. t.Run(tc.desc, tc.testFunc)
  259. }
  260. }