factory_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudpubsubreceiver
  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. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal/metadata"
  13. )
  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. }
  20. func TestType(t *testing.T) {
  21. factory := NewFactory()
  22. assert.Equal(t, component.Type(metadata.Type), factory.Type())
  23. }
  24. func TestCreateTracesReceiver(t *testing.T) {
  25. factory := NewFactory()
  26. cfg := factory.CreateDefaultConfig()
  27. cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription"
  28. params := receivertest.NewNopCreateSettings()
  29. tReceiver, err := factory.CreateTracesReceiver(context.Background(), params, cfg, consumertest.NewNop())
  30. assert.NoError(t, err)
  31. assert.NotNil(t, tReceiver, "traces receiver creation failed")
  32. _, err = factory.CreateTracesReceiver(context.Background(), params, cfg, nil)
  33. assert.Error(t, err)
  34. }
  35. func TestCreateMetricsReceiver(t *testing.T) {
  36. factory := NewFactory()
  37. cfg := factory.CreateDefaultConfig()
  38. cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription"
  39. params := receivertest.NewNopCreateSettings()
  40. tReceiver, err := factory.CreateMetricsReceiver(context.Background(), params, cfg, consumertest.NewNop())
  41. assert.NoError(t, err)
  42. assert.NotNil(t, tReceiver, "metrics receiver creation failed")
  43. _, err = factory.CreateMetricsReceiver(context.Background(), params, cfg, nil)
  44. assert.Error(t, err)
  45. }
  46. func TestCreateLogsReceiver(t *testing.T) {
  47. factory := NewFactory()
  48. cfg := factory.CreateDefaultConfig()
  49. cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription"
  50. params := receivertest.NewNopCreateSettings()
  51. tReceiver, err := factory.CreateLogsReceiver(context.Background(), params, cfg, consumertest.NewNop())
  52. assert.NoError(t, err)
  53. assert.NotNil(t, tReceiver, "logs receiver creation failed")
  54. _, err = factory.CreateLogsReceiver(context.Background(), params, cfg, nil)
  55. assert.Error(t, err)
  56. }
  57. func TestEnsureReceiver(t *testing.T) {
  58. factory := NewFactory()
  59. cfg := factory.CreateDefaultConfig()
  60. cfg.(*Config).Subscription = "projects/my-project/subscriptions/my-subscription"
  61. tReceiver, err := factory.CreateTracesReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
  62. assert.NoError(t, err)
  63. mReceiver, err := factory.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
  64. assert.NoError(t, err)
  65. assert.Equal(t, tReceiver, mReceiver)
  66. lReceiver, err := factory.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
  67. assert.NoError(t, err)
  68. assert.Equal(t, mReceiver, lReceiver)
  69. }