config_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mongodbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver"
  4. import (
  5. "errors"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/config/confignet"
  12. "go.opentelemetry.io/collector/config/configopaque"
  13. "go.opentelemetry.io/collector/config/configtls"
  14. "go.opentelemetry.io/collector/confmap/confmaptest"
  15. "go.opentelemetry.io/collector/receiver/scraperhelper"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver/internal/metadata"
  17. )
  18. func TestValidate(t *testing.T) {
  19. testCases := []struct {
  20. endpoints []string
  21. desc string
  22. username string
  23. password string
  24. expected error
  25. }{
  26. {
  27. desc: "no username, no password",
  28. endpoints: []string{"localhost:27107"},
  29. username: "",
  30. password: "",
  31. expected: nil,
  32. },
  33. {
  34. desc: "no username, with password",
  35. endpoints: []string{"localhost:27107"},
  36. username: "",
  37. password: "pass",
  38. expected: errors.New("password provided without user"),
  39. },
  40. {
  41. desc: "with username, no password",
  42. endpoints: []string{"localhost:27107"},
  43. username: "user",
  44. password: "",
  45. expected: errors.New("username provided without password"),
  46. },
  47. {
  48. desc: "with username and password",
  49. endpoints: []string{"localhost:27107"},
  50. username: "user",
  51. password: "pass",
  52. expected: nil,
  53. },
  54. {
  55. desc: "no hosts",
  56. username: "user",
  57. password: "pass",
  58. expected: errors.New("no hosts were specified in the config"),
  59. },
  60. {
  61. desc: "valid hostname",
  62. endpoints: []string{"localhost"},
  63. expected: nil,
  64. },
  65. {
  66. desc: "empty host",
  67. username: "user",
  68. endpoints: []string{""},
  69. expected: errors.New("no endpoint specified for one of the hosts"),
  70. },
  71. }
  72. for _, tc := range testCases {
  73. t.Run(tc.desc, func(t *testing.T) {
  74. var hosts []confignet.NetAddr
  75. for _, ep := range tc.endpoints {
  76. hosts = append(hosts, confignet.NetAddr{
  77. Endpoint: ep,
  78. })
  79. }
  80. cfg := &Config{
  81. Username: tc.username,
  82. Password: configopaque.String(tc.password),
  83. Hosts: hosts,
  84. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  85. }
  86. err := component.ValidateConfig(cfg)
  87. if tc.expected == nil {
  88. require.Nil(t, err)
  89. } else {
  90. require.Contains(t, err.Error(), tc.expected.Error())
  91. }
  92. })
  93. }
  94. }
  95. func TestBadTLSConfigs(t *testing.T) {
  96. testCases := []struct {
  97. desc string
  98. tlsConfig configtls.TLSClientSetting
  99. expectError bool
  100. }{
  101. {
  102. desc: "CA file not found",
  103. tlsConfig: configtls.TLSClientSetting{
  104. TLSSetting: configtls.TLSSetting{
  105. CAFile: "not/a/real/file.pem",
  106. },
  107. Insecure: false,
  108. InsecureSkipVerify: false,
  109. ServerName: "",
  110. },
  111. expectError: true,
  112. },
  113. {
  114. desc: "no issues",
  115. tlsConfig: configtls.TLSClientSetting{
  116. TLSSetting: configtls.TLSSetting{},
  117. Insecure: false,
  118. InsecureSkipVerify: false,
  119. ServerName: "",
  120. },
  121. expectError: false,
  122. },
  123. }
  124. for _, tc := range testCases {
  125. t.Run(tc.desc, func(t *testing.T) {
  126. cfg := &Config{
  127. Username: "otel",
  128. Password: "pword",
  129. Hosts: []confignet.NetAddr{
  130. {
  131. Endpoint: "localhost:27017",
  132. },
  133. },
  134. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  135. TLSClientSetting: tc.tlsConfig,
  136. }
  137. err := component.ValidateConfig(cfg)
  138. if tc.expectError {
  139. require.Error(t, err)
  140. } else {
  141. require.NoError(t, err)
  142. }
  143. })
  144. }
  145. }
  146. func TestOptions(t *testing.T) {
  147. cfg := &Config{
  148. Hosts: []confignet.NetAddr{
  149. {
  150. Endpoint: "localhost:27017",
  151. },
  152. },
  153. Username: "uname",
  154. Password: "password",
  155. Timeout: 2 * time.Minute,
  156. ReplicaSet: "rs-1",
  157. }
  158. clientOptions := cfg.ClientOptions()
  159. require.Equal(t, clientOptions.Auth.Username, cfg.Username)
  160. require.Equal(t,
  161. clientOptions.ConnectTimeout.Milliseconds(),
  162. (2 * time.Minute).Milliseconds(),
  163. )
  164. require.Equal(t, "rs-1", *clientOptions.ReplicaSet)
  165. }
  166. func TestOptionsTLS(t *testing.T) {
  167. // loading valid ca file
  168. caFile := filepath.Join("testdata", "certs", "ca.crt")
  169. cfg := &Config{
  170. Hosts: []confignet.NetAddr{
  171. {
  172. Endpoint: "localhost:27017",
  173. },
  174. },
  175. TLSClientSetting: configtls.TLSClientSetting{
  176. Insecure: false,
  177. TLSSetting: configtls.TLSSetting{
  178. CAFile: caFile,
  179. },
  180. },
  181. }
  182. opts := cfg.ClientOptions()
  183. require.NotNil(t, opts.TLSConfig)
  184. }
  185. func TestLoadConfig(t *testing.T) {
  186. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  187. require.NoError(t, err)
  188. factory := NewFactory()
  189. cfg := factory.CreateDefaultConfig()
  190. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  191. require.NoError(t, err)
  192. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  193. expected := factory.CreateDefaultConfig().(*Config)
  194. expected.Hosts = []confignet.NetAddr{
  195. {
  196. Endpoint: "localhost:27017",
  197. },
  198. }
  199. expected.Username = "otel"
  200. expected.Password = "${env:MONGO_PASSWORD}"
  201. expected.CollectionInterval = time.Minute
  202. require.Equal(t, expected, cfg)
  203. }