factory_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package prometheusreceiver
  4. import (
  5. "context"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/component/componenttest"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "go.opentelemetry.io/collector/receiver/receivertest"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal/metadata"
  15. )
  16. func TestCreateDefaultConfig(t *testing.T) {
  17. cfg := createDefaultConfig()
  18. assert.NotNil(t, cfg, "failed to create default config")
  19. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  20. }
  21. func TestCreateReceiver(t *testing.T) {
  22. cfg := createDefaultConfig()
  23. // The default config does not provide scrape_config so we expect that metrics receiver
  24. // creation must also fail.
  25. creationSet := receivertest.NewNopCreateSettings()
  26. mReceiver, _ := createMetricsReceiver(context.Background(), creationSet, cfg, nil)
  27. assert.NotNil(t, mReceiver)
  28. assert.NotNil(t, mReceiver.(*pReceiver).cfg.PrometheusConfig.GlobalConfig)
  29. }
  30. func TestFactoryCanParseServiceDiscoveryConfigs(t *testing.T) {
  31. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config_sd.yaml"))
  32. require.NoError(t, err)
  33. factory := NewFactory()
  34. cfg := factory.CreateDefaultConfig()
  35. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  36. require.NoError(t, err)
  37. assert.NoError(t, component.UnmarshalConfig(sub, cfg))
  38. }