scraper_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rabbitmqreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/mock"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component/componenttest"
  13. "go.opentelemetry.io/collector/config/confighttp"
  14. "go.opentelemetry.io/collector/config/configtls"
  15. "go.opentelemetry.io/collector/pdata/pmetric"
  16. "go.opentelemetry.io/collector/receiver/receivertest"
  17. "go.uber.org/zap"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/mocks"
  21. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/models"
  22. )
  23. func TestScraperStart(t *testing.T) {
  24. testcases := []struct {
  25. desc string
  26. scraper *rabbitmqScraper
  27. expectError bool
  28. }{
  29. {
  30. desc: "Bad Config",
  31. scraper: &rabbitmqScraper{
  32. cfg: &Config{
  33. HTTPClientSettings: confighttp.HTTPClientSettings{
  34. Endpoint: defaultEndpoint,
  35. TLSSetting: configtls.TLSClientSetting{
  36. TLSSetting: configtls.TLSSetting{
  37. CAFile: "/non/existent",
  38. },
  39. },
  40. },
  41. },
  42. settings: componenttest.NewNopTelemetrySettings(),
  43. },
  44. expectError: true,
  45. },
  46. {
  47. desc: "Valid Config",
  48. scraper: &rabbitmqScraper{
  49. cfg: &Config{
  50. HTTPClientSettings: confighttp.HTTPClientSettings{
  51. TLSSetting: configtls.TLSClientSetting{},
  52. Endpoint: defaultEndpoint,
  53. },
  54. },
  55. settings: componenttest.NewNopTelemetrySettings(),
  56. },
  57. expectError: false,
  58. },
  59. }
  60. for _, tc := range testcases {
  61. t.Run(tc.desc, func(t *testing.T) {
  62. err := tc.scraper.start(context.Background(), componenttest.NewNopHost())
  63. if tc.expectError {
  64. require.Error(t, err)
  65. } else {
  66. require.NoError(t, err)
  67. }
  68. })
  69. }
  70. }
  71. func TestScaperScrape(t *testing.T) {
  72. testCases := []struct {
  73. desc string
  74. setupMockClient func(t *testing.T) client
  75. expectedMetricGen func(t *testing.T) pmetric.Metrics
  76. expectedErr error
  77. }{
  78. {
  79. desc: "Nil client",
  80. setupMockClient: func(t *testing.T) client {
  81. return nil
  82. },
  83. expectedMetricGen: func(t *testing.T) pmetric.Metrics {
  84. return pmetric.NewMetrics()
  85. },
  86. expectedErr: errClientNotInit,
  87. },
  88. {
  89. desc: "API Call Failure",
  90. setupMockClient: func(t *testing.T) client {
  91. mockClient := mocks.MockClient{}
  92. mockClient.On("GetQueues", mock.Anything).Return(nil, errors.New("some api error"))
  93. return &mockClient
  94. },
  95. expectedMetricGen: func(t *testing.T) pmetric.Metrics {
  96. return pmetric.NewMetrics()
  97. },
  98. expectedErr: errors.New("some api error"),
  99. },
  100. {
  101. desc: "Successful Collection",
  102. setupMockClient: func(t *testing.T) client {
  103. mockClient := mocks.MockClient{}
  104. // use helper function from client tests
  105. data := loadAPIResponseData(t, queuesAPIResponseFile)
  106. var queues []*models.Queue
  107. err := json.Unmarshal(data, &queues)
  108. require.NoError(t, err)
  109. mockClient.On("GetQueues", mock.Anything).Return(queues, nil)
  110. return &mockClient
  111. },
  112. expectedMetricGen: func(t *testing.T) pmetric.Metrics {
  113. goldenPath := filepath.Join("testdata", "expected_metrics", "metrics_golden.yaml")
  114. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  115. require.NoError(t, err)
  116. return expectedMetrics
  117. },
  118. expectedErr: nil,
  119. },
  120. }
  121. for _, tc := range testCases {
  122. t.Run(tc.desc, func(t *testing.T) {
  123. scraper := newScraper(zap.NewNop(), createDefaultConfig().(*Config), receivertest.NewNopCreateSettings())
  124. scraper.client = tc.setupMockClient(t)
  125. actualMetrics, err := scraper.scrape(context.Background())
  126. if tc.expectedErr == nil {
  127. require.NoError(t, err)
  128. } else {
  129. require.EqualError(t, err, tc.expectedErr.Error())
  130. }
  131. expectedMetrics := tc.expectedMetricGen(t)
  132. require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics,
  133. pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp(),
  134. pmetrictest.IgnoreResourceMetricsOrder(),
  135. pmetrictest.IgnoreMetricDataPointsOrder(),
  136. ))
  137. })
  138. }
  139. }