config_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package simpleprometheusreceiver
  4. import (
  5. "net/url"
  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/config/configtls"
  14. "go.opentelemetry.io/collector/confmap/confmaptest"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/simpleprometheusreceiver/internal/metadata"
  16. )
  17. func TestLoadConfig(t *testing.T) {
  18. t.Parallel()
  19. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  20. require.NoError(t, err)
  21. tests := []struct {
  22. id component.ID
  23. expected component.Config
  24. }{
  25. {
  26. id: component.NewIDWithName(metadata.Type, ""),
  27. expected: createDefaultConfig(),
  28. },
  29. {
  30. id: component.NewIDWithName(metadata.Type, "all_settings"),
  31. expected: &Config{
  32. HTTPClientSettings: confighttp.HTTPClientSettings{
  33. Endpoint: "localhost:1234",
  34. TLSSetting: configtls.TLSClientSetting{
  35. TLSSetting: configtls.TLSSetting{
  36. CAFile: "path",
  37. CertFile: "path",
  38. KeyFile: "path",
  39. },
  40. InsecureSkipVerify: true,
  41. },
  42. },
  43. CollectionInterval: 30 * time.Second,
  44. MetricsPath: "/v2/metrics",
  45. Params: url.Values{"columns": []string{"name", "messages"}, "key": []string{"foo", "bar"}},
  46. UseServiceAccount: true,
  47. },
  48. },
  49. {
  50. id: component.NewIDWithName(metadata.Type, "partial_settings"),
  51. expected: &Config{
  52. HTTPClientSettings: confighttp.HTTPClientSettings{
  53. Endpoint: "localhost:1234",
  54. TLSSetting: configtls.TLSClientSetting{
  55. Insecure: true,
  56. },
  57. },
  58. CollectionInterval: 30 * time.Second,
  59. MetricsPath: "/metrics",
  60. },
  61. },
  62. {
  63. id: component.NewIDWithName(metadata.Type, "partial_tls_settings"),
  64. expected: &Config{
  65. HTTPClientSettings: confighttp.HTTPClientSettings{
  66. Endpoint: "localhost:1234",
  67. },
  68. CollectionInterval: 30 * time.Second,
  69. MetricsPath: "/metrics",
  70. },
  71. },
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.id.String(), func(t *testing.T) {
  75. factory := NewFactory()
  76. cfg := factory.CreateDefaultConfig()
  77. sub, err := cm.Sub(tt.id.String())
  78. require.NoError(t, err)
  79. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  80. assert.NoError(t, component.ValidateConfig(cfg))
  81. assert.Equal(t, tt.expected, cfg)
  82. })
  83. }
  84. }