config_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package chronyreceiver
  4. import (
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/receiver/scraperhelper"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/chrony"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/chronyreceiver/internal/metadata"
  17. )
  18. func TestLoadConfig(t *testing.T) {
  19. t.Parallel()
  20. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yml"))
  21. require.NoError(t, err)
  22. factory := NewFactory()
  23. cfg := factory.CreateDefaultConfig()
  24. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "custom").String())
  25. require.NoError(t, err)
  26. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  27. scs := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
  28. scs.Timeout = 10 * time.Second
  29. assert.Equal(t, &Config{
  30. ScraperControllerSettings: scs,
  31. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  32. Endpoint: "udp://localhost:3030",
  33. }, cfg)
  34. }
  35. func TestValidate(t *testing.T) {
  36. t.Parallel()
  37. tests := []struct {
  38. scenario string
  39. conf Config
  40. err error
  41. }{
  42. {
  43. scenario: "Valid udp configuration",
  44. conf: Config{
  45. Endpoint: "udp://localhost:323",
  46. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  47. CollectionInterval: time.Minute,
  48. InitialDelay: time.Second,
  49. Timeout: 10 * time.Second,
  50. },
  51. },
  52. err: nil,
  53. },
  54. {
  55. scenario: "Invalid udp hostname",
  56. conf: Config{
  57. Endpoint: "udp://:323",
  58. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  59. CollectionInterval: time.Minute,
  60. InitialDelay: time.Second,
  61. Timeout: 10 * time.Second,
  62. },
  63. },
  64. err: chrony.ErrInvalidNetwork,
  65. },
  66. {
  67. scenario: "Invalid udp port",
  68. conf: Config{
  69. Endpoint: "udp://localhost",
  70. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  71. CollectionInterval: time.Minute,
  72. InitialDelay: time.Second,
  73. Timeout: 10 * time.Second,
  74. },
  75. },
  76. err: chrony.ErrInvalidNetwork,
  77. },
  78. {
  79. scenario: "Valid unix path",
  80. conf: Config{
  81. Endpoint: fmt.Sprintf("unix://%s", t.TempDir()),
  82. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  83. CollectionInterval: time.Minute,
  84. InitialDelay: time.Second,
  85. Timeout: 10 * time.Second,
  86. },
  87. },
  88. err: nil,
  89. },
  90. {
  91. scenario: "Invalid unix path",
  92. conf: Config{
  93. Endpoint: "unix:///no/dir/to/socket",
  94. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  95. CollectionInterval: time.Minute,
  96. InitialDelay: time.Second,
  97. Timeout: 10 * time.Second,
  98. },
  99. },
  100. err: os.ErrNotExist,
  101. },
  102. {
  103. scenario: "Invalid timeout set",
  104. conf: Config{
  105. Endpoint: "unix://no/dir/to/socket",
  106. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  107. CollectionInterval: time.Minute,
  108. InitialDelay: time.Second,
  109. Timeout: 0,
  110. },
  111. },
  112. err: errInvalidValue,
  113. },
  114. }
  115. for _, tc := range tests {
  116. t.Run(tc.scenario, func(t *testing.T) {
  117. assert.ErrorIs(t, tc.conf.Validate(), tc.err, "Must match the expected error")
  118. })
  119. }
  120. }