factory_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver"
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/consumer/consumertest"
  11. "go.opentelemetry.io/collector/receiver/receivertest"
  12. "go.opentelemetry.io/collector/receiver/scraperhelper"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata"
  14. )
  15. func TestNewFactory(t *testing.T) {
  16. testCases := []struct {
  17. desc string
  18. testFunc func(*testing.T)
  19. }{
  20. {
  21. desc: "creates a new factory with correct type",
  22. testFunc: func(t *testing.T) {
  23. factory := NewFactory()
  24. require.EqualValues(t, metadata.Type, factory.Type())
  25. },
  26. },
  27. {
  28. desc: "creates a new factory with default config",
  29. testFunc: func(t *testing.T) {
  30. factory := NewFactory()
  31. var expectedCfg component.Config = &Config{
  32. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  33. CollectionInterval: 60 * time.Second,
  34. InitialDelay: time.Second,
  35. },
  36. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  37. Targets: []*targetConfig{},
  38. }
  39. require.Equal(t, expectedCfg, factory.CreateDefaultConfig())
  40. },
  41. },
  42. {
  43. desc: "creates a new factory and CreateMetricsReceiver returns no error",
  44. testFunc: func(t *testing.T) {
  45. factory := NewFactory()
  46. cfg := factory.CreateDefaultConfig()
  47. _, err := factory.CreateMetricsReceiver(
  48. context.Background(),
  49. receivertest.NewNopCreateSettings(),
  50. cfg,
  51. consumertest.NewNop(),
  52. )
  53. require.NoError(t, err)
  54. },
  55. },
  56. {
  57. desc: "creates a new factory and CreateMetricsReceiver returns error with incorrect config",
  58. testFunc: func(t *testing.T) {
  59. factory := NewFactory()
  60. _, err := factory.CreateMetricsReceiver(
  61. context.Background(),
  62. receivertest.NewNopCreateSettings(),
  63. nil,
  64. consumertest.NewNop(),
  65. )
  66. require.ErrorIs(t, err, errConfigNotHTTPCheck)
  67. },
  68. },
  69. }
  70. for _, tc := range testCases {
  71. t.Run(tc.desc, tc.testFunc)
  72. }
  73. }