123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package awscontainerinsightreceiver
- import (
- "context"
- "errors"
- "testing"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/consumer/consumertest"
- "go.opentelemetry.io/collector/pdata/pmetric"
- ci "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight"
- )
- // Mock cadvisor
- type mockCadvisor struct {
- }
- func (c *mockCadvisor) GetMetrics() []pmetric.Metrics {
- md := pmetric.NewMetrics()
- return []pmetric.Metrics{md}
- }
- func (c *mockCadvisor) Shutdown() error {
- return nil
- }
- // Mock k8sapiserver
- type mockK8sAPIServer struct {
- }
- func (m *mockK8sAPIServer) Shutdown() error {
- return nil
- }
- func (m *mockK8sAPIServer) GetMetrics() []pmetric.Metrics {
- md := pmetric.NewMetrics()
- return []pmetric.Metrics{md}
- }
- func TestReceiver(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSContainerInsightReceiver(
- componenttest.NewNopTelemetrySettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsContainerInsightReceiver)
- ctx := context.Background()
- err = r.Start(ctx, componenttest.NewNopHost())
- require.Error(t, err)
- err = r.Shutdown(ctx)
- require.NoError(t, err)
- }
- func TestReceiverForNilConsumer(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSContainerInsightReceiver(
- componenttest.NewNopTelemetrySettings(),
- cfg,
- nil,
- )
- require.NotNil(t, err)
- require.Nil(t, metricsReceiver)
- }
- func TestCollectData(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSContainerInsightReceiver(
- componenttest.NewNopTelemetrySettings(),
- cfg,
- new(consumertest.MetricsSink),
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsContainerInsightReceiver)
- _ = r.Start(context.Background(), nil)
- ctx := context.Background()
- r.k8sapiserver = &mockK8sAPIServer{}
- r.cadvisor = &mockCadvisor{}
- err = r.collectData(ctx)
- require.Nil(t, err)
- // test the case when cadvisor and k8sapiserver failed to initialize
- r.cadvisor = nil
- r.k8sapiserver = nil
- err = r.collectData(ctx)
- require.NotNil(t, err)
- }
- func TestCollectDataWithErrConsumer(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- metricsReceiver, err := newAWSContainerInsightReceiver(
- componenttest.NewNopTelemetrySettings(),
- cfg,
- consumertest.NewErr(errors.New("an error")),
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsContainerInsightReceiver)
- _ = r.Start(context.Background(), nil)
- r.cadvisor = &mockCadvisor{}
- r.k8sapiserver = &mockK8sAPIServer{}
- ctx := context.Background()
- err = r.collectData(ctx)
- require.NotNil(t, err)
- }
- func TestCollectDataWithECS(t *testing.T) {
- cfg := createDefaultConfig().(*Config)
- cfg.ContainerOrchestrator = ci.ECS
- metricsReceiver, err := newAWSContainerInsightReceiver(
- componenttest.NewNopTelemetrySettings(),
- cfg,
- new(consumertest.MetricsSink),
- )
- require.NoError(t, err)
- require.NotNil(t, metricsReceiver)
- r := metricsReceiver.(*awsContainerInsightReceiver)
- _ = r.Start(context.Background(), nil)
- ctx := context.Background()
- r.cadvisor = &mockCadvisor{}
- err = r.collectData(ctx)
- require.Nil(t, err)
- // test the case when cadvisor and k8sapiserver failed to initialize
- r.cadvisor = nil
- err = r.collectData(ctx)
- require.NotNil(t, err)
- }
|