receiver_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awsecscontainermetricsreceiver
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "testing"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component/componenttest"
  12. "go.opentelemetry.io/collector/consumer/consumertest"
  13. "go.uber.org/zap"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil/ecsutiltest"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver/internal/awsecscontainermetrics"
  16. )
  17. type fakeRestClient struct{ *testing.T }
  18. func (f fakeRestClient) GetResponse(path string) ([]byte, error) {
  19. if body, err := ecsutiltest.GetTestdataResponseByPath(f.T, path); body != nil || err != nil {
  20. return body, err
  21. }
  22. if path == awsecscontainermetrics.TaskStatsPath {
  23. return os.ReadFile("testdata/task_stats.json")
  24. }
  25. return nil, nil
  26. }
  27. func TestReceiver(t *testing.T) {
  28. cfg := createDefaultConfig().(*Config)
  29. metricsReceiver, err := newAWSECSContainermetrics(
  30. zap.NewNop(),
  31. cfg,
  32. consumertest.NewNop(),
  33. &fakeRestClient{t},
  34. )
  35. require.NoError(t, err)
  36. require.NotNil(t, metricsReceiver)
  37. r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
  38. ctx := context.Background()
  39. err = r.Start(ctx, componenttest.NewNopHost())
  40. require.NoError(t, err)
  41. err = r.Shutdown(ctx)
  42. require.NoError(t, err)
  43. }
  44. func TestReceiverForNilConsumer(t *testing.T) {
  45. cfg := createDefaultConfig().(*Config)
  46. metricsReceiver, err := newAWSECSContainermetrics(
  47. zap.NewNop(),
  48. cfg,
  49. nil,
  50. &fakeRestClient{},
  51. )
  52. require.NotNil(t, err)
  53. require.Nil(t, metricsReceiver)
  54. }
  55. func TestCollectDataFromEndpoint(t *testing.T) {
  56. cfg := createDefaultConfig().(*Config)
  57. metricsReceiver, err := newAWSECSContainermetrics(
  58. zap.NewNop(),
  59. cfg,
  60. new(consumertest.MetricsSink),
  61. &fakeRestClient{},
  62. )
  63. require.NoError(t, err)
  64. require.NotNil(t, metricsReceiver)
  65. r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
  66. ctx := context.Background()
  67. err = r.collectDataFromEndpoint(ctx)
  68. require.NoError(t, err)
  69. }
  70. func TestCollectDataFromEndpointWithConsumerError(t *testing.T) {
  71. cfg := createDefaultConfig().(*Config)
  72. metricsReceiver, err := newAWSECSContainermetrics(
  73. zap.NewNop(),
  74. cfg,
  75. consumertest.NewErr(errors.New("Test Error for Metrics Consumer")),
  76. &fakeRestClient{},
  77. )
  78. require.NoError(t, err)
  79. require.NotNil(t, metricsReceiver)
  80. r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
  81. ctx := context.Background()
  82. err = r.collectDataFromEndpoint(ctx)
  83. require.EqualError(t, err, "Test Error for Metrics Consumer")
  84. }
  85. type invalidFakeClient struct {
  86. }
  87. func (f invalidFakeClient) GetResponse(_ string) ([]byte, error) {
  88. return nil, fmt.Errorf("intentional error")
  89. }
  90. func TestCollectDataFromEndpointWithEndpointError(t *testing.T) {
  91. cfg := createDefaultConfig().(*Config)
  92. metricsReceiver, err := newAWSECSContainermetrics(
  93. zap.NewNop(),
  94. cfg,
  95. new(consumertest.MetricsSink),
  96. &invalidFakeClient{},
  97. )
  98. require.NoError(t, err)
  99. require.NotNil(t, metricsReceiver)
  100. r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
  101. ctx := context.Background()
  102. err = r.collectDataFromEndpoint(ctx)
  103. require.Error(t, err)
  104. }