config_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package skywalkingexporter
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/cenkalti/backoff/v4"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/config/configgrpc"
  13. "go.opentelemetry.io/collector/config/configopaque"
  14. "go.opentelemetry.io/collector/config/configtls"
  15. "go.opentelemetry.io/collector/confmap/confmaptest"
  16. "go.opentelemetry.io/collector/exporter/exporterhelper"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/skywalkingexporter/internal/metadata"
  18. )
  19. func TestLoadConfig(t *testing.T) {
  20. t.Parallel()
  21. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  22. require.NoError(t, err)
  23. defaultCfg := createDefaultConfig().(*Config)
  24. defaultCfg.Endpoint = "1.2.3.4:11800"
  25. tests := []struct {
  26. id component.ID
  27. expected component.Config
  28. }{
  29. {
  30. id: component.NewIDWithName(metadata.Type, ""),
  31. expected: defaultCfg,
  32. },
  33. {
  34. id: component.NewIDWithName(metadata.Type, "2"),
  35. expected: &Config{
  36. RetrySettings: exporterhelper.RetrySettings{
  37. Enabled: true,
  38. InitialInterval: 10 * time.Second,
  39. MaxInterval: 1 * time.Minute,
  40. MaxElapsedTime: 10 * time.Minute,
  41. RandomizationFactor: backoff.DefaultRandomizationFactor,
  42. Multiplier: backoff.DefaultMultiplier,
  43. },
  44. QueueSettings: exporterhelper.QueueSettings{
  45. Enabled: true,
  46. NumConsumers: 2,
  47. QueueSize: 10,
  48. },
  49. TimeoutSettings: exporterhelper.TimeoutSettings{
  50. Timeout: 10 * time.Second,
  51. },
  52. GRPCClientSettings: configgrpc.GRPCClientSettings{
  53. Headers: map[string]configopaque.String{
  54. "can you have a . here?": "F0000000-0000-0000-0000-000000000000",
  55. "header1": "234",
  56. "another": "somevalue",
  57. },
  58. Endpoint: "1.2.3.4:11800",
  59. Compression: "gzip",
  60. TLSSetting: configtls.TLSClientSetting{
  61. TLSSetting: configtls.TLSSetting{
  62. CAFile: "/var/lib/mycert.pem",
  63. },
  64. Insecure: false,
  65. },
  66. Keepalive: &configgrpc.KeepaliveClientConfig{
  67. Time: 20,
  68. PermitWithoutStream: true,
  69. Timeout: 30,
  70. },
  71. WriteBufferSize: 512 * 1024,
  72. BalancerName: "round_robin",
  73. },
  74. NumStreams: 233,
  75. },
  76. },
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.id.String(), func(t *testing.T) {
  80. factory := NewFactory()
  81. cfg := factory.CreateDefaultConfig()
  82. sub, err := cm.Sub(tt.id.String())
  83. require.NoError(t, err)
  84. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  85. assert.NoError(t, component.ValidateConfig(cfg))
  86. assert.Equal(t, tt.expected, cfg)
  87. })
  88. }
  89. }
  90. func TestValidate(t *testing.T) {
  91. c1 := &Config{
  92. GRPCClientSettings: configgrpc.GRPCClientSettings{
  93. Endpoint: "",
  94. },
  95. NumStreams: 3,
  96. }
  97. err := c1.Validate()
  98. assert.Error(t, err)
  99. c2 := &Config{
  100. GRPCClientSettings: configgrpc.GRPCClientSettings{
  101. Endpoint: "",
  102. },
  103. NumStreams: 0,
  104. }
  105. err2 := c2.Validate()
  106. assert.Error(t, err2)
  107. }