receiver_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build !windows
  4. // +build !windows
  5. package podmanreceiver
  6. import (
  7. "context"
  8. "errors"
  9. "net/url"
  10. "testing"
  11. "time"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "go.opentelemetry.io/collector/component/componenttest"
  15. "go.opentelemetry.io/collector/consumer"
  16. "go.opentelemetry.io/collector/consumer/consumertest"
  17. "go.opentelemetry.io/collector/pdata/pmetric"
  18. "go.opentelemetry.io/collector/receiver/receivertest"
  19. "go.opentelemetry.io/collector/receiver/scraperhelper"
  20. "go.uber.org/zap"
  21. )
  22. func TestNewReceiver(t *testing.T) {
  23. config := &Config{
  24. Endpoint: "unix:///run/some.sock",
  25. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  26. CollectionInterval: 1 * time.Second,
  27. InitialDelay: time.Second,
  28. },
  29. }
  30. nextConsumer := consumertest.NewNop()
  31. mr, err := newMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), config, nextConsumer, nil)
  32. assert.NotNil(t, mr)
  33. assert.Nil(t, err)
  34. }
  35. func TestNewReceiverErrors(t *testing.T) {
  36. r, err := newMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), &Config{}, consumertest.NewNop(), nil)
  37. assert.Nil(t, r)
  38. require.Error(t, err)
  39. assert.Equal(t, "config.Endpoint must be specified", err.Error())
  40. r, err = newMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), &Config{Endpoint: "someEndpoint"}, consumertest.NewNop(), nil)
  41. assert.Nil(t, r)
  42. require.Error(t, err)
  43. assert.Equal(t, "config.CollectionInterval must be specified", err.Error())
  44. }
  45. func TestScraperLoop(t *testing.T) {
  46. cfg := createDefaultConfig()
  47. cfg.CollectionInterval = 100 * time.Millisecond
  48. client := make(mockClient)
  49. consumer := make(mockConsumer)
  50. r, err := newMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumer, client.factory)
  51. require.NoError(t, err)
  52. assert.NotNil(t, r)
  53. go func() {
  54. client <- containerStatsReport{
  55. Stats: []containerStats{{
  56. ContainerID: "c1",
  57. }},
  58. Error: containerStatsReportError{},
  59. }
  60. }()
  61. assert.NoError(t, r.Start(context.Background(), componenttest.NewNopHost()))
  62. md := <-consumer
  63. assert.Equal(t, md.ResourceMetrics().Len(), 1)
  64. assert.NoError(t, r.Shutdown(context.Background()))
  65. }
  66. type mockClient chan containerStatsReport
  67. func (c mockClient) factory(_ *zap.Logger, _ *Config) (PodmanClient, error) {
  68. return c, nil
  69. }
  70. func (c mockClient) stats(context.Context, url.Values) ([]containerStats, error) {
  71. report := <-c
  72. if report.Error.Message != "" {
  73. return nil, errors.New(report.Error.Message)
  74. }
  75. return report.Stats, nil
  76. }
  77. func (c mockClient) ping(context.Context) error {
  78. return nil
  79. }
  80. type mockConsumer chan pmetric.Metrics
  81. func (c mockClient) list(context.Context, url.Values) ([]container, error) {
  82. return []container{{ID: "c1"}}, nil
  83. }
  84. func (c mockClient) events(context.Context, url.Values) (<-chan event, <-chan error) {
  85. return nil, nil
  86. }
  87. func (m mockConsumer) Capabilities() consumer.Capabilities {
  88. return consumer.Capabilities{}
  89. }
  90. func (m mockConsumer) ConsumeMetrics(_ context.Context, md pmetric.Metrics) error {
  91. m <- md
  92. return nil
  93. }