receiver_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awscontainerinsightreceiver
  4. import (
  5. "context"
  6. "errors"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component/componenttest"
  10. "go.opentelemetry.io/collector/consumer/consumertest"
  11. "go.opentelemetry.io/collector/pdata/pmetric"
  12. ci "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/containerinsight"
  13. )
  14. // Mock cadvisor
  15. type mockCadvisor struct {
  16. }
  17. func (c *mockCadvisor) GetMetrics() []pmetric.Metrics {
  18. md := pmetric.NewMetrics()
  19. return []pmetric.Metrics{md}
  20. }
  21. func (c *mockCadvisor) Shutdown() error {
  22. return nil
  23. }
  24. // Mock k8sapiserver
  25. type mockK8sAPIServer struct {
  26. }
  27. func (m *mockK8sAPIServer) Shutdown() error {
  28. return nil
  29. }
  30. func (m *mockK8sAPIServer) GetMetrics() []pmetric.Metrics {
  31. md := pmetric.NewMetrics()
  32. return []pmetric.Metrics{md}
  33. }
  34. func TestReceiver(t *testing.T) {
  35. cfg := createDefaultConfig().(*Config)
  36. metricsReceiver, err := newAWSContainerInsightReceiver(
  37. componenttest.NewNopTelemetrySettings(),
  38. cfg,
  39. consumertest.NewNop(),
  40. )
  41. require.NoError(t, err)
  42. require.NotNil(t, metricsReceiver)
  43. r := metricsReceiver.(*awsContainerInsightReceiver)
  44. ctx := context.Background()
  45. err = r.Start(ctx, componenttest.NewNopHost())
  46. require.Error(t, err)
  47. err = r.Shutdown(ctx)
  48. require.NoError(t, err)
  49. }
  50. func TestReceiverForNilConsumer(t *testing.T) {
  51. cfg := createDefaultConfig().(*Config)
  52. metricsReceiver, err := newAWSContainerInsightReceiver(
  53. componenttest.NewNopTelemetrySettings(),
  54. cfg,
  55. nil,
  56. )
  57. require.NotNil(t, err)
  58. require.Nil(t, metricsReceiver)
  59. }
  60. func TestCollectData(t *testing.T) {
  61. cfg := createDefaultConfig().(*Config)
  62. metricsReceiver, err := newAWSContainerInsightReceiver(
  63. componenttest.NewNopTelemetrySettings(),
  64. cfg,
  65. new(consumertest.MetricsSink),
  66. )
  67. require.NoError(t, err)
  68. require.NotNil(t, metricsReceiver)
  69. r := metricsReceiver.(*awsContainerInsightReceiver)
  70. _ = r.Start(context.Background(), nil)
  71. ctx := context.Background()
  72. r.k8sapiserver = &mockK8sAPIServer{}
  73. r.cadvisor = &mockCadvisor{}
  74. err = r.collectData(ctx)
  75. require.Nil(t, err)
  76. // test the case when cadvisor and k8sapiserver failed to initialize
  77. r.cadvisor = nil
  78. r.k8sapiserver = nil
  79. err = r.collectData(ctx)
  80. require.NotNil(t, err)
  81. }
  82. func TestCollectDataWithErrConsumer(t *testing.T) {
  83. cfg := createDefaultConfig().(*Config)
  84. metricsReceiver, err := newAWSContainerInsightReceiver(
  85. componenttest.NewNopTelemetrySettings(),
  86. cfg,
  87. consumertest.NewErr(errors.New("an error")),
  88. )
  89. require.NoError(t, err)
  90. require.NotNil(t, metricsReceiver)
  91. r := metricsReceiver.(*awsContainerInsightReceiver)
  92. _ = r.Start(context.Background(), nil)
  93. r.cadvisor = &mockCadvisor{}
  94. r.k8sapiserver = &mockK8sAPIServer{}
  95. ctx := context.Background()
  96. err = r.collectData(ctx)
  97. require.NotNil(t, err)
  98. }
  99. func TestCollectDataWithECS(t *testing.T) {
  100. cfg := createDefaultConfig().(*Config)
  101. cfg.ContainerOrchestrator = ci.ECS
  102. metricsReceiver, err := newAWSContainerInsightReceiver(
  103. componenttest.NewNopTelemetrySettings(),
  104. cfg,
  105. new(consumertest.MetricsSink),
  106. )
  107. require.NoError(t, err)
  108. require.NotNil(t, metricsReceiver)
  109. r := metricsReceiver.(*awsContainerInsightReceiver)
  110. _ = r.Start(context.Background(), nil)
  111. ctx := context.Background()
  112. r.cadvisor = &mockCadvisor{}
  113. err = r.collectData(ctx)
  114. require.Nil(t, err)
  115. // test the case when cadvisor and k8sapiserver failed to initialize
  116. r.cadvisor = nil
  117. err = r.collectData(ctx)
  118. require.NotNil(t, err)
  119. }