factory_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package statsdreceiver
  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. type testHost struct {
  14. component.Host
  15. t *testing.T
  16. }
  17. // ReportFatalError causes the test to be run to fail.
  18. func (h *testHost) ReportFatalError(err error) {
  19. h.t.Fatalf("receiver reported a fatal error: %v", err)
  20. }
  21. var _ component.Host = (*testHost)(nil)
  22. func TestCreateDefaultConfig(t *testing.T) {
  23. factory := NewFactory()
  24. cfg := factory.CreateDefaultConfig()
  25. assert.NotNil(t, cfg, "failed to create default config")
  26. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  27. }
  28. func TestCreateReceiver(t *testing.T) {
  29. cfg := createDefaultConfig().(*Config)
  30. cfg.NetAddr.Endpoint = "localhost:0" // Endpoint is required, not going to be used here.
  31. params := receivertest.NewNopCreateSettings()
  32. tReceiver, err := createMetricsReceiver(context.Background(), params, cfg, consumertest.NewNop())
  33. assert.NoError(t, err)
  34. assert.NotNil(t, tReceiver, "receiver creation failed")
  35. }
  36. func TestCreateMetricsReceiverWithNilConsumer(t *testing.T) {
  37. receiver, err := createMetricsReceiver(
  38. context.Background(),
  39. receivertest.NewNopCreateSettings(),
  40. createDefaultConfig(),
  41. nil,
  42. )
  43. assert.Error(t, err, "nil consumer")
  44. assert.Nil(t, receiver)
  45. }