config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudpubsubreceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/confmap/confmaptest"
  12. "go.opentelemetry.io/collector/exporter/exporterhelper"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal/metadata"
  14. )
  15. func TestLoadConfig(t *testing.T) {
  16. t.Parallel()
  17. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  18. require.NoError(t, err)
  19. tests := []struct {
  20. id component.ID
  21. expected component.Config
  22. expectedErr error
  23. }{
  24. {
  25. id: component.NewIDWithName(metadata.Type, ""),
  26. expected: &Config{},
  27. },
  28. {
  29. id: component.NewIDWithName(metadata.Type, "customname"),
  30. expected: &Config{
  31. ProjectID: "my-project",
  32. UserAgent: "opentelemetry-collector-contrib {{version}}",
  33. TimeoutSettings: exporterhelper.TimeoutSettings{
  34. Timeout: 20 * time.Second,
  35. },
  36. Subscription: "projects/my-project/subscriptions/otlp-subscription",
  37. },
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.id.String(), func(t *testing.T) {
  42. factory := NewFactory()
  43. cfg := factory.CreateDefaultConfig()
  44. sub, err := cm.Sub(tt.id.String())
  45. require.NoError(t, err)
  46. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  47. assert.NoError(t, component.ValidateConfig(cfg))
  48. assert.Equal(t, tt.expected, cfg)
  49. })
  50. }
  51. }
  52. func TestConfigValidation(t *testing.T) {
  53. factory := NewFactory()
  54. c := factory.CreateDefaultConfig().(*Config)
  55. assert.Error(t, c.validateForTrace())
  56. assert.Error(t, c.validateForLog())
  57. assert.Error(t, c.validateForMetric())
  58. c.Subscription = "projects/000project/subscriptions/my-subscription"
  59. assert.Error(t, c.validate())
  60. c.Subscription = "projects/my-project/topics/my-topic"
  61. assert.Error(t, c.validate())
  62. c.Subscription = "projects/my-project/subscriptions/my-subscription"
  63. assert.NoError(t, c.validate())
  64. }
  65. func TestTraceConfigValidation(t *testing.T) {
  66. factory := NewFactory()
  67. c := factory.CreateDefaultConfig().(*Config)
  68. c.Subscription = "projects/my-project/subscriptions/my-subscription"
  69. assert.NoError(t, c.validateForTrace())
  70. c.Encoding = "otlp_proto_metric"
  71. assert.Error(t, c.validateForTrace())
  72. c.Encoding = "otlp_proto_log"
  73. assert.Error(t, c.validateForTrace())
  74. c.Encoding = "raw_text"
  75. assert.Error(t, c.validateForTrace())
  76. c.Encoding = "raw_json"
  77. assert.Error(t, c.validateForTrace())
  78. c.Encoding = "otlp_proto_trace"
  79. assert.NoError(t, c.validateForTrace())
  80. }
  81. func TestMetricConfigValidation(t *testing.T) {
  82. factory := NewFactory()
  83. c := factory.CreateDefaultConfig().(*Config)
  84. c.Subscription = "projects/my-project/subscriptions/my-subscription"
  85. assert.NoError(t, c.validateForMetric())
  86. c.Encoding = "otlp_proto_trace"
  87. assert.Error(t, c.validateForMetric())
  88. c.Encoding = "otlp_proto_log"
  89. assert.Error(t, c.validateForMetric())
  90. c.Encoding = "raw_text"
  91. assert.Error(t, c.validateForMetric())
  92. c.Encoding = "raw_json"
  93. assert.Error(t, c.validateForMetric())
  94. c.Encoding = "otlp_proto_metric"
  95. assert.NoError(t, c.validateForMetric())
  96. }
  97. func TestLogConfigValidation(t *testing.T) {
  98. factory := NewFactory()
  99. c := factory.CreateDefaultConfig().(*Config)
  100. c.Subscription = "projects/my-project/subscriptions/my-subscription"
  101. assert.NoError(t, c.validateForLog())
  102. c.Encoding = "otlp_proto_trace"
  103. assert.Error(t, c.validateForLog())
  104. c.Encoding = "otlp_proto_metric"
  105. assert.Error(t, c.validateForLog())
  106. c.Encoding = "raw_text"
  107. assert.NoError(t, c.validateForLog())
  108. c.Encoding = "raw_json"
  109. assert.NoError(t, c.validateForLog())
  110. c.Encoding = "otlp_proto_log"
  111. assert.NoError(t, c.validateForLog())
  112. }