config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package vcenterreceiver // import github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver
  4. import (
  5. "errors"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/google/go-cmp/cmp"
  10. "github.com/google/go-cmp/cmp/cmpopts"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/config/configtls"
  14. "go.opentelemetry.io/collector/confmap/confmaptest"
  15. "go.opentelemetry.io/collector/receiver/scraperhelper"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
  17. )
  18. func TestConfigValidation(t *testing.T) {
  19. cases := []struct {
  20. desc string
  21. cfg Config
  22. expectedErr error
  23. }{
  24. {
  25. desc: "empty endpoint",
  26. cfg: Config{
  27. Endpoint: "",
  28. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  29. },
  30. expectedErr: errors.New("no endpoint was provided"),
  31. },
  32. {
  33. desc: "with endpoint",
  34. cfg: Config{
  35. Endpoint: "http://vcsa.some-host",
  36. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  37. },
  38. },
  39. {
  40. desc: "not http or https",
  41. cfg: Config{
  42. Endpoint: "ws://vcsa.some-host",
  43. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  44. },
  45. expectedErr: errors.New("url scheme must be http or https"),
  46. },
  47. {
  48. desc: "unparseable URL",
  49. cfg: Config{
  50. Endpoint: "h" + string(rune(0x7f)),
  51. TLSClientSetting: configtls.TLSClientSetting{},
  52. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  53. },
  54. expectedErr: errors.New("unable to parse url"),
  55. },
  56. {
  57. desc: "no username",
  58. cfg: Config{
  59. Endpoint: "https://vcsa.some-host",
  60. Password: "otelp",
  61. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  62. },
  63. expectedErr: errors.New("username not provided"),
  64. },
  65. {
  66. desc: "no password",
  67. cfg: Config{
  68. Endpoint: "https://vcsa.some-host",
  69. Username: "otelu",
  70. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  71. },
  72. expectedErr: errors.New("password not provided"),
  73. },
  74. }
  75. for _, tc := range cases {
  76. t.Run(tc.desc, func(t *testing.T) {
  77. err := tc.cfg.Validate()
  78. if tc.expectedErr != nil {
  79. require.ErrorContains(t, err, tc.expectedErr.Error())
  80. }
  81. })
  82. }
  83. }
  84. func TestLoadConfig(t *testing.T) {
  85. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  86. require.NoError(t, err)
  87. factory := NewFactory()
  88. cfg := factory.CreateDefaultConfig()
  89. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  90. require.NoError(t, err)
  91. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  92. expected := factory.CreateDefaultConfig().(*Config)
  93. expected.Endpoint = "http://vcsa.host.localnet"
  94. expected.Username = "otelu"
  95. expected.Password = "${env:VCENTER_PASSWORD}"
  96. expected.MetricsBuilderConfig = metadata.DefaultMetricsBuilderConfig()
  97. expected.MetricsBuilderConfig.Metrics.VcenterHostCPUUtilization.Enabled = false
  98. expected.CollectionInterval = 5 * time.Minute
  99. if diff := cmp.Diff(expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{})); diff != "" {
  100. t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
  101. }
  102. }