config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "fmt"
  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/confighttp"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "go.uber.org/multierr"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver/internal/metadata"
  16. )
  17. func TestValidate(t *testing.T) {
  18. testCases := []struct {
  19. desc string
  20. cfg *Config
  21. expectedErr error
  22. }{
  23. {
  24. desc: "missing username, password and invalid endpoint",
  25. cfg: &Config{
  26. HTTPClientSettings: confighttp.HTTPClientSettings{
  27. Endpoint: "http://localhost :5984",
  28. },
  29. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  30. },
  31. expectedErr: multierr.Combine(
  32. errMissingUsername,
  33. errMissingPassword,
  34. fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
  35. ),
  36. },
  37. {
  38. desc: "missing password and invalid endpoint",
  39. cfg: &Config{
  40. HTTPClientSettings: confighttp.HTTPClientSettings{
  41. Endpoint: "http://localhost :5984",
  42. },
  43. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  44. Username: "otelu",
  45. },
  46. expectedErr: multierr.Combine(
  47. errMissingPassword,
  48. fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
  49. ),
  50. },
  51. {
  52. desc: "missing username and invalid endpoint",
  53. cfg: &Config{
  54. HTTPClientSettings: confighttp.HTTPClientSettings{
  55. Endpoint: "http://localhost :5984",
  56. },
  57. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  58. Password: "otelp",
  59. },
  60. expectedErr: multierr.Combine(
  61. errMissingUsername,
  62. fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
  63. ),
  64. },
  65. {
  66. desc: "invalid endpoint",
  67. cfg: &Config{
  68. Username: "otel",
  69. Password: "otel",
  70. HTTPClientSettings: confighttp.HTTPClientSettings{
  71. Endpoint: "http://localhost :5984",
  72. },
  73. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  74. },
  75. expectedErr: fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
  76. },
  77. {
  78. desc: "no error",
  79. cfg: &Config{
  80. Username: "otel",
  81. Password: "otel",
  82. HTTPClientSettings: confighttp.HTTPClientSettings{
  83. Endpoint: "http://localhost:5984",
  84. },
  85. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  86. },
  87. expectedErr: nil,
  88. },
  89. }
  90. for _, tc := range testCases {
  91. t.Run(tc.desc, func(t *testing.T) {
  92. actualErr := tc.cfg.Validate()
  93. require.Equal(t, tc.expectedErr, actualErr)
  94. })
  95. }
  96. }
  97. func TestLoadConfig(t *testing.T) {
  98. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  99. require.NoError(t, err)
  100. factory := NewFactory()
  101. cfg := factory.CreateDefaultConfig()
  102. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  103. require.NoError(t, err)
  104. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  105. expected := factory.CreateDefaultConfig().(*Config)
  106. expected.Endpoint = "http://localhost:5984"
  107. expected.Username = "otelu"
  108. expected.Password = "${env:COUCHDB_PASSWORD}"
  109. expected.CollectionInterval = time.Minute
  110. require.Equal(t, expected, cfg)
  111. }