config_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package saphanareceiver
  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/confmap/confmaptest"
  14. "go.uber.org/multierr"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver/internal/metadata"
  16. )
  17. func TestValidate(t *testing.T) {
  18. testCases := []struct {
  19. desc string
  20. defaultConfigModifier func(cfg *Config)
  21. expected error
  22. }{
  23. {
  24. desc: "missing username and password",
  25. defaultConfigModifier: func(cfg *Config) {},
  26. expected: multierr.Combine(
  27. errors.New(ErrNoUsername),
  28. errors.New(ErrNoPassword),
  29. ),
  30. },
  31. {
  32. desc: "missing password",
  33. defaultConfigModifier: func(cfg *Config) {
  34. cfg.Username = "otel"
  35. },
  36. expected: multierr.Combine(
  37. errors.New(ErrNoPassword),
  38. ),
  39. },
  40. {
  41. desc: "missing username",
  42. defaultConfigModifier: func(cfg *Config) {
  43. cfg.Password = "otel"
  44. },
  45. expected: multierr.Combine(
  46. errors.New(ErrNoUsername),
  47. ),
  48. },
  49. {
  50. desc: "no error",
  51. defaultConfigModifier: func(cfg *Config) {
  52. cfg.Username = "otel"
  53. cfg.Password = "otel"
  54. },
  55. expected: nil,
  56. },
  57. }
  58. for _, tC := range testCases {
  59. t.Run(tC.desc, func(t *testing.T) {
  60. factory := NewFactory()
  61. cfg := factory.CreateDefaultConfig().(*Config)
  62. tC.defaultConfigModifier(cfg)
  63. actual := component.ValidateConfig(cfg)
  64. require.Equal(t, tC.expected, actual)
  65. })
  66. }
  67. }
  68. func TestLoadConfig(t *testing.T) {
  69. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  70. require.NoError(t, err)
  71. factory := NewFactory()
  72. cfg := factory.CreateDefaultConfig()
  73. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  74. require.NoError(t, err)
  75. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  76. expected := factory.CreateDefaultConfig().(*Config)
  77. expected.MetricsBuilderConfig = metadata.DefaultMetricsBuilderConfig()
  78. expected.MetricsBuilderConfig.Metrics.SaphanaCPUUsed.Enabled = false
  79. expected.Endpoint = "example.com:30015"
  80. expected.Username = "otel"
  81. expected.Password = "password"
  82. expected.CollectionInterval = 2 * time.Minute
  83. if diff := cmp.Diff(expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{})); diff != "" {
  84. t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
  85. }
  86. }