config_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/config/confighttp"
  9. "go.opentelemetry.io/collector/receiver/scraperhelper"
  10. "go.uber.org/multierr"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/metadata"
  12. )
  13. func TestValidate(t *testing.T) {
  14. testCases := []struct {
  15. desc string
  16. cfg *Config
  17. expectedErr error
  18. }{
  19. {
  20. desc: "missing username, password, and invalid endpoint",
  21. cfg: &Config{
  22. HTTPClientSettings: confighttp.HTTPClientSettings{
  23. Endpoint: "invalid://endpoint: 12efg",
  24. },
  25. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  26. },
  27. expectedErr: multierr.Combine(
  28. errMissingUsername,
  29. errMissingPassword,
  30. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  31. ),
  32. },
  33. {
  34. desc: "missing password and invalid endpoint",
  35. cfg: &Config{
  36. Username: "otelu",
  37. HTTPClientSettings: confighttp.HTTPClientSettings{
  38. Endpoint: "invalid://endpoint: 12efg",
  39. },
  40. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  41. },
  42. expectedErr: multierr.Combine(
  43. errMissingPassword,
  44. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  45. ),
  46. },
  47. {
  48. desc: "missing username and invalid endpoint",
  49. cfg: &Config{
  50. Password: "otelp",
  51. HTTPClientSettings: confighttp.HTTPClientSettings{
  52. Endpoint: "invalid://endpoint: 12efg",
  53. },
  54. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  55. },
  56. expectedErr: multierr.Combine(
  57. errMissingUsername,
  58. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  59. ),
  60. },
  61. {
  62. desc: "invalid endpoint",
  63. cfg: &Config{
  64. Username: "otelu",
  65. Password: "otelp",
  66. HTTPClientSettings: confighttp.HTTPClientSettings{
  67. Endpoint: "invalid://endpoint: 12efg",
  68. },
  69. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  70. },
  71. expectedErr: multierr.Combine(
  72. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  73. ),
  74. },
  75. {
  76. desc: "valid config",
  77. cfg: &Config{
  78. Username: "otelu",
  79. Password: "otelp",
  80. HTTPClientSettings: confighttp.HTTPClientSettings{
  81. Endpoint: defaultEndpoint,
  82. },
  83. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  84. },
  85. expectedErr: nil,
  86. },
  87. }
  88. for _, tc := range testCases {
  89. t.Run(tc.desc, func(t *testing.T) {
  90. actualErr := tc.cfg.Validate()
  91. if tc.expectedErr != nil {
  92. require.EqualError(t, actualErr, tc.expectedErr.Error())
  93. } else {
  94. require.NoError(t, actualErr)
  95. }
  96. })
  97. }
  98. }