123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package awsecscontainermetricsreceiver
- import (
- "context"
- "errors"
- "fmt"
- "os"
- "testing"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/consumer/consumertest"
- "go.uber.org/zap"
- "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil/ecsutiltest"
- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver/internal/awsecscontainermetrics"
- )
- type fakeRestClient struct{ *testing.T }
- func (f fakeRestClient) GetResponse(path string) ([]byte, error) {
- if body, err := ecsutiltest.GetTestdataResponseByPath(f.T, path); body != nil || err != nil {
- return body, err
- }
- if path == awsecscontainermetrics.TaskStatsPath {
- return os.ReadFile("testdata/task_stats.json")
- }
- return nil, nil
- }
- func TestReceiver(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSECSContainermetrics(
- zap.NewNop(),
- cfg,
- consumertest.NewNop(),
- &fakeRestClient{t},
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
- ctx := context.Background()
- err = r.Start(ctx, componenttest.NewNopHost())
- require.NoError(t, err)
- err = r.Shutdown(ctx)
- require.NoError(t, err)
- }
- func TestReceiverForNilConsumer(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSECSContainermetrics(
- zap.NewNop(),
- cfg,
- nil,
- &fakeRestClient{},
- )
- require.NotNil(t, err)
- require.Nil(t, metricsReceiver)
- }
- func TestCollectDataFromEndpoint(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSECSContainermetrics(
- zap.NewNop(),
- cfg,
- new(consumertest.MetricsSink),
- &fakeRestClient{},
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
- ctx := context.Background()
- err = r.collectDataFromEndpoint(ctx)
- require.NoError(t, err)
- }
- func TestCollectDataFromEndpointWithConsumerError(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSECSContainermetrics(
- zap.NewNop(),
- cfg,
- consumertest.NewErr(errors.New("Test Error for Metrics Consumer")),
- &fakeRestClient{},
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
- ctx := context.Background()
- err = r.collectDataFromEndpoint(ctx)
- require.EqualError(t, err, "Test Error for Metrics Consumer")
- }
- type invalidFakeClient struct {
- }
- func (f invalidFakeClient) GetResponse(_ string) ([]byte, error) {
- return nil, fmt.Errorf("intentional error")
- }
- func TestCollectDataFromEndpointWithEndpointError(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSECSContainermetrics(
- zap.NewNop(),
- cfg,
- new(consumertest.MetricsSink),
- &invalidFakeClient{},
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsEcsContainerMetricsReceiver)
- ctx := context.Background()
- err = r.collectDataFromEndpoint(ctx)
- require.Error(t, err)
- }
|