factory_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package hostmetricsreceiver
  4. import (
  5. "context"
  6. "fmt"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/component/componenttest"
  11. "go.opentelemetry.io/collector/consumer/consumertest"
  12. "go.opentelemetry.io/collector/receiver/receivertest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
  14. )
  15. var creationSet = receivertest.NewNopCreateSettings()
  16. func TestCreateDefaultConfig(t *testing.T) {
  17. factory := NewFactory()
  18. cfg := factory.CreateDefaultConfig()
  19. assert.NotNil(t, cfg, "failed to create default config")
  20. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  21. }
  22. func TestCreateReceiver(t *testing.T) {
  23. factory := NewFactory()
  24. cfg := factory.CreateDefaultConfig()
  25. tReceiver, err := factory.CreateTracesReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
  26. assert.Equal(t, err, component.ErrDataTypeIsNotSupported)
  27. assert.Nil(t, tReceiver)
  28. mReceiver, err := factory.CreateMetricsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
  29. assert.NoError(t, err)
  30. assert.NotNil(t, mReceiver)
  31. tLogs, err := factory.CreateLogsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
  32. assert.Equal(t, err, component.ErrDataTypeIsNotSupported)
  33. assert.Nil(t, tLogs)
  34. }
  35. func TestCreateReceiver_ScraperKeyConfigError(t *testing.T) {
  36. const errorKey string = "error"
  37. factory := NewFactory()
  38. cfg := &Config{Scrapers: map[string]internal.Config{errorKey: &mockConfig{}}}
  39. _, err := factory.CreateMetricsReceiver(context.Background(), creationSet, cfg, consumertest.NewNop())
  40. assert.EqualError(t, err, fmt.Sprintf("host metrics scraper factory not found for key: %q", errorKey))
  41. }