config_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package collectdreceiver
  4. import (
  5. "errors"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/config/confighttp"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver/internal/metadata"
  15. )
  16. func TestLoadConfig(t *testing.T) {
  17. t.Parallel()
  18. tests := []struct {
  19. id component.ID
  20. expected component.Config
  21. wantErr error
  22. }{
  23. {
  24. id: component.NewIDWithName(metadata.Type, ""),
  25. expected: createDefaultConfig(),
  26. },
  27. {
  28. id: component.NewIDWithName(metadata.Type, "one"),
  29. expected: &Config{
  30. HTTPServerSettings: confighttp.HTTPServerSettings{
  31. Endpoint: "localhost:12345",
  32. },
  33. Timeout: 50 * time.Second,
  34. AttributesPrefix: "dap_",
  35. Encoding: "command",
  36. },
  37. wantErr: errors.New("CollectD only support JSON encoding format. command is not supported"),
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.id.String(), func(t *testing.T) {
  42. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  43. require.NoError(t, err)
  44. factory := NewFactory()
  45. cfg := factory.CreateDefaultConfig()
  46. sub, err := cm.Sub(tt.id.String())
  47. require.NoError(t, err)
  48. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  49. if tt.wantErr == nil {
  50. assert.NoError(t, component.ValidateConfig(cfg))
  51. } else {
  52. assert.Equal(t, tt.wantErr, component.ValidateConfig(cfg))
  53. }
  54. assert.Equal(t, tt.expected, cfg)
  55. })
  56. }
  57. }