config_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "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.uber.org/multierr"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqreceiver/internal/metadata"
  15. )
  16. func TestValidate(t *testing.T) {
  17. testCases := []struct {
  18. desc string
  19. cfg *Config
  20. expectedErr error
  21. }{
  22. {
  23. desc: "missing username, password, and invalid endpoint",
  24. cfg: &Config{
  25. HTTPClientSettings: confighttp.HTTPClientSettings{
  26. Endpoint: "invalid://endpoint: 12efg",
  27. },
  28. },
  29. expectedErr: multierr.Combine(
  30. errMissingUsername,
  31. errMissingPassword,
  32. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  33. ),
  34. },
  35. {
  36. desc: "missing password and invalid endpoint",
  37. cfg: &Config{
  38. Username: "otelu",
  39. HTTPClientSettings: confighttp.HTTPClientSettings{
  40. Endpoint: "invalid://endpoint: 12efg",
  41. },
  42. },
  43. expectedErr: multierr.Combine(
  44. errMissingPassword,
  45. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  46. ),
  47. },
  48. {
  49. desc: "missing username and invalid endpoint",
  50. cfg: &Config{
  51. Password: "otelp",
  52. HTTPClientSettings: confighttp.HTTPClientSettings{
  53. Endpoint: "invalid://endpoint: 12efg",
  54. },
  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. },
  70. expectedErr: multierr.Combine(
  71. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  72. ),
  73. },
  74. {
  75. desc: "valid config",
  76. cfg: &Config{
  77. Username: "otelu",
  78. Password: "otelp",
  79. HTTPClientSettings: confighttp.HTTPClientSettings{
  80. Endpoint: defaultEndpoint,
  81. },
  82. },
  83. expectedErr: nil,
  84. },
  85. }
  86. for _, tc := range testCases {
  87. t.Run(tc.desc, func(t *testing.T) {
  88. actualErr := tc.cfg.Validate()
  89. if tc.expectedErr != nil {
  90. require.EqualError(t, actualErr, tc.expectedErr.Error())
  91. } else {
  92. require.NoError(t, actualErr)
  93. }
  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:15672"
  107. expected.Username = "otelu"
  108. expected.Password = "${env:RABBITMQ_PASSWORD}"
  109. expected.CollectionInterval = 10 * time.Second
  110. require.Equal(t, expected, cfg)
  111. }