factory_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package windowsperfcountersreceiver
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "go.opentelemetry.io/collector/component"
  9. "go.opentelemetry.io/collector/component/componenttest"
  10. "go.opentelemetry.io/collector/consumer/consumertest"
  11. "go.opentelemetry.io/collector/receiver/receivertest"
  12. )
  13. var creationParams = receivertest.NewNopCreateSettings()
  14. func TestCreateDefaultConfig(t *testing.T) {
  15. factory := NewFactory()
  16. cfg := factory.CreateDefaultConfig()
  17. assert.NotNil(t, cfg, "failed to create default config")
  18. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  19. cfg.(*Config).PerfCounters = []ObjectConfig{
  20. {
  21. Object: "object",
  22. Counters: []CounterConfig{{Name: "counter", MetricRep: MetricRep{Name: "metric"}}},
  23. },
  24. }
  25. cfg.(*Config).MetricMetaData = map[string]MetricConfig{
  26. "metric": {
  27. Description: "desc",
  28. Unit: "1",
  29. Gauge: GaugeMetric{},
  30. },
  31. }
  32. assert.NoError(t, component.ValidateConfig(cfg))
  33. }
  34. func TestCreateTracesReceiver(t *testing.T) {
  35. factory := NewFactory()
  36. cfg := factory.CreateDefaultConfig()
  37. cfg.(*Config).PerfCounters = []ObjectConfig{
  38. {
  39. Object: "object",
  40. Counters: []CounterConfig{{Name: "counter", MetricRep: MetricRep{Name: "metric"}}},
  41. },
  42. }
  43. cfg.(*Config).MetricMetaData = map[string]MetricConfig{
  44. "metric": {
  45. Description: "desc",
  46. Unit: "1",
  47. Gauge: GaugeMetric{},
  48. },
  49. }
  50. tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationParams, cfg, consumertest.NewNop())
  51. assert.ErrorIs(t, err, component.ErrDataTypeIsNotSupported)
  52. assert.Nil(t, tReceiver)
  53. }
  54. func TestCreateTracesReceiverNoMetrics(t *testing.T) {
  55. factory := NewFactory()
  56. cfg := factory.CreateDefaultConfig()
  57. cfg.(*Config).PerfCounters = []ObjectConfig{
  58. {
  59. Object: "object",
  60. Counters: []CounterConfig{{Name: "counter"}},
  61. },
  62. }
  63. tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationParams, cfg, consumertest.NewNop())
  64. assert.ErrorIs(t, err, component.ErrDataTypeIsNotSupported)
  65. assert.Nil(t, tReceiver)
  66. }
  67. func TestCreateLogsReceiver(t *testing.T) {
  68. factory := NewFactory()
  69. cfg := factory.CreateDefaultConfig()
  70. cfg.(*Config).PerfCounters = []ObjectConfig{
  71. {
  72. Object: "object",
  73. Counters: []CounterConfig{{Name: "counter", MetricRep: MetricRep{Name: "metric"}}},
  74. },
  75. }
  76. cfg.(*Config).MetricMetaData = map[string]MetricConfig{
  77. "metric": {
  78. Description: "desc",
  79. Unit: "1",
  80. Gauge: GaugeMetric{},
  81. },
  82. }
  83. tReceiver, err := factory.CreateLogsReceiver(context.Background(), creationParams, cfg, consumertest.NewNop())
  84. assert.ErrorIs(t, err, component.ErrDataTypeIsNotSupported)
  85. assert.Nil(t, tReceiver)
  86. }