client_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "github.com/stretchr/testify/require"
  14. "go.opentelemetry.io/collector/component"
  15. "go.opentelemetry.io/collector/component/componenttest"
  16. "go.opentelemetry.io/collector/config/confighttp"
  17. "go.opentelemetry.io/collector/config/configtls"
  18. "go.uber.org/zap"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/models"
  20. )
  21. const (
  22. queuesAPIResponseFile = "get_queues_response.json"
  23. )
  24. func TestNewClient(t *testing.T) {
  25. testCase := []struct {
  26. desc string
  27. cfg *Config
  28. host component.Host
  29. settings component.TelemetrySettings
  30. logger *zap.Logger
  31. expectError error
  32. }{
  33. {
  34. desc: "Invalid HTTP config",
  35. cfg: &Config{
  36. HTTPClientSettings: confighttp.HTTPClientSettings{
  37. Endpoint: defaultEndpoint,
  38. TLSSetting: configtls.TLSClientSetting{
  39. TLSSetting: configtls.TLSSetting{
  40. CAFile: "/non/existent",
  41. },
  42. },
  43. },
  44. },
  45. host: componenttest.NewNopHost(),
  46. settings: componenttest.NewNopTelemetrySettings(),
  47. logger: zap.NewNop(),
  48. expectError: errors.New("failed to create HTTP Client"),
  49. },
  50. {
  51. desc: "Valid Configuration",
  52. cfg: &Config{
  53. HTTPClientSettings: confighttp.HTTPClientSettings{
  54. TLSSetting: configtls.TLSClientSetting{},
  55. Endpoint: defaultEndpoint,
  56. },
  57. },
  58. host: componenttest.NewNopHost(),
  59. settings: componenttest.NewNopTelemetrySettings(),
  60. logger: zap.NewNop(),
  61. expectError: nil,
  62. },
  63. }
  64. for _, tc := range testCase {
  65. t.Run(tc.desc, func(t *testing.T) {
  66. ac, err := newClient(tc.cfg, tc.host, tc.settings, tc.logger)
  67. if tc.expectError != nil {
  68. require.Nil(t, ac)
  69. require.Contains(t, err.Error(), tc.expectError.Error())
  70. } else {
  71. require.NoError(t, err)
  72. actualClient, ok := ac.(*rabbitmqClient)
  73. require.True(t, ok)
  74. require.Equal(t, tc.cfg.Username, actualClient.creds.username)
  75. require.Equal(t, string(tc.cfg.Password), actualClient.creds.password)
  76. require.Equal(t, tc.cfg.Endpoint, actualClient.hostEndpoint)
  77. require.Equal(t, tc.logger, actualClient.logger)
  78. require.NotNil(t, actualClient.client)
  79. }
  80. })
  81. }
  82. }
  83. func TestGetQueuesDetails(t *testing.T) {
  84. testCases := []struct {
  85. desc string
  86. testFunc func(*testing.T)
  87. }{
  88. {
  89. desc: "Non-200 Response",
  90. testFunc: func(t *testing.T) {
  91. // Setup test server
  92. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  93. w.WriteHeader(http.StatusUnauthorized)
  94. }))
  95. defer ts.Close()
  96. tc := createTestClient(t, ts.URL)
  97. clusters, err := tc.GetQueues(context.Background())
  98. require.Nil(t, clusters)
  99. require.EqualError(t, err, "non 200 code returned 401")
  100. },
  101. },
  102. {
  103. desc: "Bad payload returned",
  104. testFunc: func(t *testing.T) {
  105. // Setup test server
  106. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  107. _, err := w.Write([]byte("{}"))
  108. require.NoError(t, err)
  109. }))
  110. defer ts.Close()
  111. tc := createTestClient(t, ts.URL)
  112. clusters, err := tc.GetQueues(context.Background())
  113. require.Nil(t, clusters)
  114. require.Contains(t, err.Error(), "failed to decode response payload")
  115. },
  116. },
  117. {
  118. desc: "Successful call",
  119. testFunc: func(t *testing.T) {
  120. data := loadAPIResponseData(t, queuesAPIResponseFile)
  121. // Setup test server
  122. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  123. _, err := w.Write(data)
  124. require.NoError(t, err)
  125. }))
  126. defer ts.Close()
  127. tc := createTestClient(t, ts.URL)
  128. // Load the valid data into a struct to compare
  129. var expected []*models.Queue
  130. err := json.Unmarshal(data, &expected)
  131. require.NoError(t, err)
  132. clusters, err := tc.GetQueues(context.Background())
  133. require.NoError(t, err)
  134. require.Equal(t, expected, clusters)
  135. },
  136. },
  137. }
  138. for _, tc := range testCases {
  139. t.Run(tc.desc, tc.testFunc)
  140. }
  141. }
  142. func createTestClient(t *testing.T, baseEndpoint string) client {
  143. t.Helper()
  144. cfg := createDefaultConfig().(*Config)
  145. cfg.Endpoint = baseEndpoint
  146. testClient, err := newClient(cfg, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), zap.NewNop())
  147. require.NoError(t, err)
  148. return testClient
  149. }
  150. func loadAPIResponseData(t *testing.T, fileName string) []byte {
  151. t.Helper()
  152. fullPath := filepath.Join("testdata", "apiresponses", fileName)
  153. data, err := os.ReadFile(fullPath)
  154. require.NoError(t, err)
  155. return data
  156. }