config_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package bigipreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver"
  4. import (
  5. "fmt"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/config/confighttp"
  11. "go.opentelemetry.io/collector/confmap/confmaptest"
  12. "go.opentelemetry.io/collector/receiver/scraperhelper"
  13. "go.uber.org/multierr"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver/internal/metadata"
  15. )
  16. func TestValidate(t *testing.T) {
  17. defaultConfig := createDefaultConfig().(*Config)
  18. defaultConfig.Username = "otelu"
  19. defaultConfig.Password = "otelp"
  20. testCases := []struct {
  21. desc string
  22. cfg *Config
  23. expectedErr error
  24. }{
  25. {
  26. desc: "missing username, password, and invalid endpoint",
  27. cfg: &Config{
  28. HTTPClientSettings: confighttp.HTTPClientSettings{
  29. Endpoint: "invalid://endpoint: 12efg",
  30. },
  31. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  32. },
  33. expectedErr: multierr.Combine(
  34. errMissingUsername,
  35. errMissingPassword,
  36. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  37. ),
  38. },
  39. {
  40. desc: "missing password and invalid endpoint",
  41. cfg: &Config{
  42. Username: "otelu",
  43. HTTPClientSettings: confighttp.HTTPClientSettings{
  44. Endpoint: "invalid://endpoint: 12efg",
  45. }, ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  46. },
  47. expectedErr: multierr.Combine(
  48. errMissingPassword,
  49. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  50. ),
  51. },
  52. {
  53. desc: "missing username and invalid endpoint",
  54. cfg: &Config{
  55. Password: "otelp",
  56. HTTPClientSettings: confighttp.HTTPClientSettings{
  57. Endpoint: "invalid://endpoint: 12efg",
  58. },
  59. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  60. },
  61. expectedErr: multierr.Combine(
  62. errMissingUsername,
  63. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  64. ),
  65. },
  66. {
  67. desc: "invalid endpoint",
  68. cfg: &Config{
  69. Username: "otelu",
  70. Password: "otelp",
  71. HTTPClientSettings: confighttp.HTTPClientSettings{
  72. Endpoint: "invalid://endpoint: 12efg",
  73. },
  74. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  75. },
  76. expectedErr: multierr.Combine(
  77. fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
  78. ),
  79. },
  80. {
  81. desc: "valid config",
  82. cfg: &Config{
  83. Username: "otelu",
  84. Password: "otelp",
  85. HTTPClientSettings: confighttp.HTTPClientSettings{
  86. Endpoint: defaultEndpoint,
  87. },
  88. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  89. },
  90. expectedErr: nil,
  91. },
  92. {
  93. desc: "invalid default config",
  94. cfg: createDefaultConfig().(*Config),
  95. expectedErr: multierr.Combine(
  96. errMissingUsername,
  97. errMissingPassword,
  98. ),
  99. },
  100. {
  101. desc: "valid default config with supplied username/password",
  102. cfg: defaultConfig,
  103. expectedErr: nil,
  104. },
  105. }
  106. for _, tc := range testCases {
  107. t.Run(tc.desc, func(t *testing.T) {
  108. actualErr := tc.cfg.Validate()
  109. if tc.expectedErr != nil {
  110. require.EqualError(t, actualErr, tc.expectedErr.Error())
  111. } else {
  112. require.NoError(t, actualErr)
  113. }
  114. })
  115. }
  116. }
  117. func TestLoadConfig(t *testing.T) {
  118. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  119. require.NoError(t, err)
  120. factory := NewFactory()
  121. cfg := factory.CreateDefaultConfig()
  122. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  123. require.NoError(t, err)
  124. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  125. expected := factory.CreateDefaultConfig().(*Config)
  126. expected.Username = "otelu"
  127. expected.Password = "${env:BIGIP_PASSWORD}"
  128. expected.TLSSetting.InsecureSkipVerify = true
  129. require.Equal(t, expected, cfg)
  130. }