factory_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awsecscontainermetricsreceiver
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/component/componenttest"
  9. "go.opentelemetry.io/collector/consumer/consumertest"
  10. "go.opentelemetry.io/collector/receiver/receivertest"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil/endpoints"
  12. )
  13. func TestValidConfig(t *testing.T) {
  14. err := componenttest.CheckConfigStruct(createDefaultConfig())
  15. require.NoError(t, err)
  16. }
  17. func TestCreateMetricsReceiver(t *testing.T) {
  18. metricsReceiver, err := createMetricsReceiver(
  19. context.Background(),
  20. receivertest.NewNopCreateSettings(),
  21. createDefaultConfig(),
  22. consumertest.NewNop(),
  23. )
  24. require.Error(t, err, "No Env Variable Error")
  25. require.Nil(t, metricsReceiver)
  26. }
  27. func TestCreateMetricsReceiverWithEnv(t *testing.T) {
  28. t.Setenv(endpoints.TaskMetadataEndpointV4EnvVar, "http://www.test.com")
  29. metricsReceiver, err := createMetricsReceiver(
  30. context.Background(),
  31. receivertest.NewNopCreateSettings(),
  32. createDefaultConfig(),
  33. consumertest.NewNop(),
  34. )
  35. require.NoError(t, err)
  36. require.NotNil(t, metricsReceiver)
  37. }
  38. func TestCreateMetricsReceiverWithBadUrl(t *testing.T) {
  39. t.Setenv(endpoints.TaskMetadataEndpointV4EnvVar, "bad-url-format")
  40. metricsReceiver, err := createMetricsReceiver(
  41. context.Background(),
  42. receivertest.NewNopCreateSettings(),
  43. createDefaultConfig(),
  44. consumertest.NewNop(),
  45. )
  46. require.Error(t, err)
  47. require.Nil(t, metricsReceiver)
  48. }
  49. func TestCreateMetricsReceiverWithNilConsumer(t *testing.T) {
  50. metricsReceiver, err := createMetricsReceiver(
  51. context.Background(),
  52. receivertest.NewNopCreateSettings(),
  53. createDefaultConfig(),
  54. nil,
  55. )
  56. require.Error(t, err, "Nil Comsumer")
  57. require.Nil(t, metricsReceiver)
  58. }