config_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package signalfxreceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/config/confighttp"
  11. "go.opentelemetry.io/collector/config/configtls"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver/internal/metadata"
  15. )
  16. func TestLoadConfig(t *testing.T) {
  17. t.Parallel()
  18. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  19. require.NoError(t, err)
  20. tests := []struct {
  21. id component.ID
  22. expected component.Config
  23. }{
  24. {
  25. id: component.NewIDWithName(metadata.Type, ""),
  26. expected: createDefaultConfig(),
  27. },
  28. {
  29. id: component.NewIDWithName(metadata.Type, "allsettings"),
  30. expected: &Config{
  31. HTTPServerSettings: confighttp.HTTPServerSettings{
  32. Endpoint: "localhost:9943",
  33. },
  34. AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
  35. AccessTokenPassthrough: true,
  36. },
  37. },
  38. },
  39. {
  40. id: component.NewIDWithName(metadata.Type, "tls"),
  41. expected: &Config{
  42. HTTPServerSettings: confighttp.HTTPServerSettings{
  43. Endpoint: ":9943",
  44. TLSSetting: &configtls.TLSServerSetting{
  45. TLSSetting: configtls.TLSSetting{
  46. CertFile: "/test.crt",
  47. KeyFile: "/test.key",
  48. },
  49. },
  50. },
  51. AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
  52. AccessTokenPassthrough: false,
  53. },
  54. },
  55. },
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.id.String(), func(t *testing.T) {
  59. factory := NewFactory()
  60. cfg := factory.CreateDefaultConfig()
  61. sub, err := cm.Sub(tt.id.String())
  62. require.NoError(t, err)
  63. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  64. assert.NoError(t, component.ValidateConfig(cfg))
  65. assert.Equal(t, tt.expected, cfg)
  66. })
  67. }
  68. }
  69. func TestCreateInvalidHTTPEndpoint(t *testing.T) {
  70. factory := NewFactory()
  71. cfg := factory.CreateDefaultConfig().(*Config)
  72. cfg.Endpoint = ""
  73. err := cfg.Validate()
  74. assert.EqualError(t, err, "empty endpoint")
  75. }
  76. func TestCreateNoPortEndpoint(t *testing.T) {
  77. factory := NewFactory()
  78. cfg := factory.CreateDefaultConfig().(*Config)
  79. cfg.Endpoint = "localhost:"
  80. err := cfg.Validate()
  81. assert.EqualError(t, err, `endpoint port is not a number: strconv.ParseInt: parsing "": invalid syntax`)
  82. }
  83. func TestCreateLargePortEndpoint(t *testing.T) {
  84. factory := NewFactory()
  85. cfg := factory.CreateDefaultConfig().(*Config)
  86. cfg.Endpoint = "localhost:65536"
  87. err := cfg.Validate()
  88. assert.EqualError(t, err, "port number must be between 1 and 65535")
  89. }