client_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package couchdbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver"
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component/componenttest"
  11. "go.opentelemetry.io/collector/config/confighttp"
  12. "go.opentelemetry.io/collector/config/configtls"
  13. "go.uber.org/zap"
  14. )
  15. func defaultClient(t *testing.T, endpoint string) client {
  16. cfg := createDefaultConfig().(*Config)
  17. cfg.Endpoint = endpoint
  18. couchdbClient, err := newCouchDBClient(
  19. cfg,
  20. componenttest.NewNopHost(),
  21. componenttest.NewNopTelemetrySettings())
  22. require.Nil(t, err)
  23. require.NotNil(t, couchdbClient)
  24. return couchdbClient
  25. }
  26. func TestNewCouchDBClient(t *testing.T) {
  27. t.Run("Invalid config", func(t *testing.T) {
  28. couchdbClient, err := newCouchDBClient(
  29. &Config{
  30. HTTPClientSettings: confighttp.HTTPClientSettings{
  31. Endpoint: defaultEndpoint,
  32. TLSSetting: configtls.TLSClientSetting{
  33. TLSSetting: configtls.TLSSetting{
  34. CAFile: "/non/existent",
  35. },
  36. },
  37. }},
  38. componenttest.NewNopHost(),
  39. componenttest.NewNopTelemetrySettings())
  40. require.Error(t, err)
  41. require.Contains(t, err.Error(), "failed to create HTTP Client: ")
  42. require.Nil(t, couchdbClient)
  43. })
  44. t.Run("no error", func(t *testing.T) {
  45. client := defaultClient(t, defaultEndpoint)
  46. require.NotNil(t, client)
  47. })
  48. }
  49. func TestGet(t *testing.T) {
  50. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. u, p, _ := r.BasicAuth()
  52. if u == "unauthorized" || p == "unauthorized" {
  53. w.WriteHeader(401)
  54. return
  55. }
  56. if strings.Contains(r.URL.Path, "/_stats/couchdb") {
  57. w.WriteHeader(200)
  58. return
  59. }
  60. if strings.Contains(r.URL.Path, "/invalid_endpoint") {
  61. w.WriteHeader(404)
  62. return
  63. }
  64. if strings.Contains(r.URL.Path, "/invalid_body") {
  65. w.Header().Set("Content-Length", "1")
  66. return
  67. }
  68. w.WriteHeader(404)
  69. }))
  70. defer ts.Close()
  71. t.Run("invalid url request", func(t *testing.T) {
  72. url := ts.URL + " /space"
  73. couchdbClient := defaultClient(t, url)
  74. result, err := couchdbClient.Get(url)
  75. require.NotNil(t, err)
  76. require.Nil(t, result)
  77. require.Contains(t, err.Error(), "invalid port ")
  78. })
  79. t.Run("invalid endpoint", func(t *testing.T) {
  80. url := ts.URL + "/invalid_endpoint"
  81. couchdbClient := defaultClient(t, url)
  82. result, err := couchdbClient.Get(url)
  83. require.NotNil(t, err)
  84. require.Nil(t, result)
  85. require.Contains(t, err.Error(), "404 Not Found")
  86. })
  87. t.Run("invalid body", func(t *testing.T) {
  88. url := ts.URL + "/invalid_body"
  89. couchdbClient := defaultClient(t, url)
  90. result, err := couchdbClient.Get(url)
  91. require.NotNil(t, err)
  92. require.Nil(t, result)
  93. require.Contains(t, err.Error(), "failed to read response body ")
  94. })
  95. t.Run("401 Unauthorized", func(t *testing.T) {
  96. url := ts.URL + "/_node/_local/_stats/couchdb"
  97. couchdbClient, err := newCouchDBClient(
  98. &Config{
  99. HTTPClientSettings: confighttp.HTTPClientSettings{
  100. Endpoint: url,
  101. },
  102. Username: "unauthorized",
  103. Password: "unauthorized",
  104. },
  105. componenttest.NewNopHost(),
  106. componenttest.NewNopTelemetrySettings())
  107. require.Nil(t, err)
  108. require.NotNil(t, couchdbClient)
  109. result, err := couchdbClient.Get(url)
  110. require.Nil(t, result)
  111. require.NotNil(t, err)
  112. require.Contains(t, err.Error(), "401 Unauthorized")
  113. })
  114. t.Run("no error", func(t *testing.T) {
  115. url := ts.URL + "/_node/_local/_stats/couchdb"
  116. couchdbClient := defaultClient(t, url)
  117. result, err := couchdbClient.Get(url)
  118. require.Nil(t, err)
  119. require.NotNil(t, result)
  120. })
  121. }
  122. func TestGetNodeStats(t *testing.T) {
  123. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  124. if strings.Contains(r.URL.Path, "/invalid_json") {
  125. w.WriteHeader(200)
  126. _, err := w.Write([]byte(`{"}`))
  127. require.NoError(t, err)
  128. return
  129. }
  130. if strings.Contains(r.URL.Path, "/_stats/couchdb") {
  131. w.WriteHeader(200)
  132. _, err := w.Write([]byte(`{"key":["value"]}`))
  133. require.NoError(t, err)
  134. return
  135. }
  136. w.WriteHeader(404)
  137. }))
  138. defer ts.Close()
  139. t.Run("invalid endpoint", func(t *testing.T) {
  140. couchdbClient := defaultClient(t, "invalid")
  141. actualStats, err := couchdbClient.GetStats("_local")
  142. require.NotNil(t, err)
  143. require.Nil(t, actualStats)
  144. })
  145. t.Run("invalid json", func(t *testing.T) {
  146. couchdbClient := defaultClient(t, ts.URL+"/invalid_json")
  147. actualStats, err := couchdbClient.GetStats("_local")
  148. require.NotNil(t, err)
  149. require.Nil(t, actualStats)
  150. })
  151. t.Run("no error", func(t *testing.T) {
  152. expectedStats := map[string]any{"key": []any{"value"}}
  153. couchdbClient := defaultClient(t, ts.URL)
  154. actualStats, err := couchdbClient.GetStats("_local")
  155. require.Nil(t, err)
  156. require.EqualValues(t, expectedStats, actualStats)
  157. })
  158. }
  159. func TestBuildReq(t *testing.T) {
  160. couchdbClient := couchDBClient{
  161. client: &http.Client{},
  162. cfg: &Config{
  163. HTTPClientSettings: confighttp.HTTPClientSettings{
  164. Endpoint: defaultEndpoint,
  165. },
  166. Username: "otelu",
  167. Password: "otelp",
  168. },
  169. logger: zap.NewNop(),
  170. }
  171. path := "/_node/_local/_stats/couchdb"
  172. req, err := couchdbClient.buildReq(path)
  173. require.NoError(t, err)
  174. require.NotNil(t, req)
  175. require.Equal(t, "application/json", req.Header["Content-Type"][0])
  176. require.Equal(t, []string{"Basic b3RlbHU6b3RlbHA="}, req.Header["Authorization"])
  177. require.Equal(t, defaultEndpoint+path, req.URL.String())
  178. }
  179. func TestBuildBadReq(t *testing.T) {
  180. couchdbClient := couchDBClient{
  181. client: &http.Client{},
  182. cfg: &Config{
  183. HTTPClientSettings: confighttp.HTTPClientSettings{
  184. Endpoint: defaultEndpoint,
  185. },
  186. },
  187. logger: zap.NewNop(),
  188. }
  189. _, err := couchdbClient.buildReq(" ")
  190. require.Error(t, err)
  191. }