config_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package solacereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/solacereceiver"
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/config/configtls"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/solacereceiver/internal/metadata"
  14. )
  15. func TestLoadConfig(t *testing.T) {
  16. t.Parallel()
  17. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  18. require.NoError(t, err)
  19. tests := []struct {
  20. id component.ID
  21. expected component.Config
  22. expectedErr error
  23. }{
  24. {
  25. id: component.NewIDWithName(metadata.Type, "primary"),
  26. expected: &Config{
  27. Broker: []string{"myHost:5671"},
  28. Auth: Authentication{
  29. PlainText: &SaslPlainTextConfig{
  30. Username: "otel",
  31. Password: "otel01$",
  32. },
  33. },
  34. Queue: "queue://#trace-profile123",
  35. MaxUnacked: 1234,
  36. TLS: configtls.TLSClientSetting{
  37. Insecure: false,
  38. InsecureSkipVerify: false,
  39. },
  40. Flow: FlowControl{
  41. DelayedRetry: &FlowControlDelayedRetry{
  42. Delay: 1 * time.Second,
  43. },
  44. },
  45. },
  46. },
  47. {
  48. id: component.NewIDWithName(metadata.Type, "noauth"),
  49. expectedErr: errMissingAuthDetails,
  50. },
  51. {
  52. id: component.NewIDWithName(metadata.Type, "noqueue"),
  53. expectedErr: errMissingQueueName,
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.id.String(), func(t *testing.T) {
  58. factory := NewFactory()
  59. cfg := factory.CreateDefaultConfig()
  60. sub, err := cm.Sub(tt.id.String())
  61. require.NoError(t, err)
  62. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  63. if tt.expectedErr != nil {
  64. assert.ErrorIs(t, component.ValidateConfig(cfg), tt.expectedErr)
  65. return
  66. }
  67. assert.NoError(t, component.ValidateConfig(cfg))
  68. assert.Equal(t, tt.expected, cfg)
  69. })
  70. }
  71. }
  72. func TestConfigValidateMissingAuth(t *testing.T) {
  73. cfg := createDefaultConfig().(*Config)
  74. cfg.Queue = "someQueue"
  75. err := component.ValidateConfig(cfg)
  76. assert.Equal(t, errMissingAuthDetails, err)
  77. }
  78. func TestConfigValidateMissingQueue(t *testing.T) {
  79. cfg := createDefaultConfig().(*Config)
  80. cfg.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
  81. err := component.ValidateConfig(cfg)
  82. assert.Equal(t, errMissingQueueName, err)
  83. }
  84. func TestConfigValidateMissingFlowControl(t *testing.T) {
  85. cfg := createDefaultConfig().(*Config)
  86. cfg.Queue = "someQueue"
  87. cfg.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
  88. // this should never happen in reality, test validation anyway
  89. cfg.Flow.DelayedRetry = nil
  90. err := cfg.Validate()
  91. assert.Equal(t, errMissingFlowControl, err)
  92. }
  93. func TestConfigValidateInvalidFlowControlDelayedRetryDelay(t *testing.T) {
  94. cfg := createDefaultConfig().(*Config)
  95. cfg.Queue = "someQueue"
  96. cfg.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
  97. cfg.Flow.DelayedRetry = &FlowControlDelayedRetry{
  98. Delay: -30 * time.Second,
  99. }
  100. err := cfg.Validate()
  101. assert.Equal(t, errInvalidDelayedRetryDelay, err)
  102. }
  103. func TestConfigValidateSuccess(t *testing.T) {
  104. successCases := map[string]func(*Config){
  105. "With Plaintext Auth": func(c *Config) {
  106. c.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
  107. },
  108. "With XAuth2 Auth": func(c *Config) {
  109. c.Auth.XAuth2 = &SaslXAuth2Config{
  110. Username: "Username",
  111. Bearer: "Bearer",
  112. }
  113. },
  114. "With External Auth": func(c *Config) {
  115. c.Auth.External = &SaslExternalConfig{}
  116. },
  117. }
  118. for caseName, configure := range successCases {
  119. t.Run(caseName, func(t *testing.T) {
  120. cfg := createDefaultConfig().(*Config)
  121. cfg.Queue = "someQueue"
  122. configure(cfg)
  123. err := component.ValidateConfig(cfg)
  124. assert.NoError(t, err)
  125. })
  126. }
  127. }