config_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package lokireceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/config/configgrpc"
  11. "go.opentelemetry.io/collector/config/confighttp"
  12. "go.opentelemetry.io/collector/config/confignet"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/lokireceiver/internal/metadata"
  15. )
  16. func TestLoadConfig(t *testing.T) {
  17. t.Parallel()
  18. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  19. require.NoError(t, err)
  20. tests := []struct {
  21. id component.ID
  22. expected component.Config
  23. }{
  24. {
  25. id: component.NewIDWithName(metadata.Type, "defaults"),
  26. expected: &Config{
  27. Protocols: Protocols{
  28. GRPC: &configgrpc.GRPCServerSettings{
  29. NetAddr: confignet.NetAddr{
  30. Endpoint: "0.0.0.0:3600",
  31. Transport: "tcp",
  32. },
  33. },
  34. HTTP: &confighttp.HTTPServerSettings{
  35. Endpoint: "0.0.0.0:3500",
  36. },
  37. },
  38. },
  39. },
  40. {
  41. id: component.NewIDWithName(metadata.Type, "mixed"),
  42. expected: &Config{
  43. Protocols: Protocols{
  44. GRPC: &configgrpc.GRPCServerSettings{
  45. NetAddr: confignet.NetAddr{
  46. Endpoint: "localhost:4600",
  47. Transport: "tcp",
  48. },
  49. },
  50. HTTP: &confighttp.HTTPServerSettings{
  51. Endpoint: "localhost:4500",
  52. },
  53. },
  54. KeepTimestamp: true,
  55. },
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.id.String(), func(t *testing.T) {
  60. factory := NewFactory()
  61. cfg := factory.CreateDefaultConfig()
  62. sub, err := cm.Sub(tt.id.String())
  63. require.NoError(t, err)
  64. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  65. assert.NoError(t, component.ValidateConfig(cfg))
  66. assert.Equal(t, tt.expected, cfg)
  67. })
  68. }
  69. }
  70. func TestInvalidConfig(t *testing.T) {
  71. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  72. require.NoError(t, err)
  73. tests := []struct {
  74. id component.ID
  75. err string
  76. }{
  77. {
  78. id: component.NewIDWithName(metadata.Type, "empty"),
  79. err: "must specify at least one protocol when using the Loki receiver",
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.id.String(), func(t *testing.T) {
  84. factory := NewFactory()
  85. cfg := factory.CreateDefaultConfig()
  86. sub, err := cm.Sub(tt.id.String())
  87. require.NoError(t, err)
  88. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  89. err = component.ValidateConfig(cfg)
  90. assert.Error(t, err, tt.err)
  91. })
  92. }
  93. }
  94. func TestConfigWithUnknownKeysConfig(t *testing.T) {
  95. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  96. require.NoError(t, err)
  97. tests := []struct {
  98. id component.ID
  99. err string
  100. }{
  101. {
  102. id: component.NewIDWithName(metadata.Type, "extra_keys"),
  103. err: "'' has invalid keys: foo",
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.id.String(), func(t *testing.T) {
  108. factory := NewFactory()
  109. cfg := factory.CreateDefaultConfig()
  110. sub, err := cm.Sub(tt.id.String())
  111. require.NoError(t, err)
  112. assert.Contains(t, component.UnmarshalConfig(sub, cfg).Error(), tt.err)
  113. })
  114. }
  115. }