factory_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package skywalkingexporter
  4. import (
  5. "context"
  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/component/componenttest"
  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/exporter/exportertest"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
  17. )
  18. func TestCreateDefaultConfig(t *testing.T) {
  19. cfg := createDefaultConfig()
  20. assert.NotNil(t, cfg, "failed to create default config")
  21. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  22. }
  23. func TestCreateTracesExporter(t *testing.T) {
  24. endpoint := testutil.GetAvailableLocalAddress(t)
  25. tests := []struct {
  26. name string
  27. config *Config
  28. mustFailOnCreate bool
  29. mustFailOnStart bool
  30. }{
  31. {
  32. name: "UseSecure",
  33. config: &Config{
  34. GRPCClientSettings: configgrpc.GRPCClientSettings{
  35. Endpoint: endpoint,
  36. TLSSetting: configtls.TLSClientSetting{
  37. Insecure: false,
  38. },
  39. },
  40. NumStreams: 3,
  41. },
  42. },
  43. {
  44. name: "Keepalive",
  45. config: &Config{
  46. GRPCClientSettings: configgrpc.GRPCClientSettings{
  47. Endpoint: endpoint,
  48. Keepalive: &configgrpc.KeepaliveClientConfig{
  49. Time: 30 * time.Second,
  50. Timeout: 25 * time.Second,
  51. PermitWithoutStream: true,
  52. },
  53. },
  54. NumStreams: 3,
  55. },
  56. },
  57. {
  58. name: "Compression",
  59. config: &Config{
  60. GRPCClientSettings: configgrpc.GRPCClientSettings{
  61. Endpoint: endpoint,
  62. Compression: "gzip",
  63. },
  64. NumStreams: 3,
  65. },
  66. },
  67. {
  68. name: "Headers",
  69. config: &Config{
  70. GRPCClientSettings: configgrpc.GRPCClientSettings{
  71. Endpoint: endpoint,
  72. Headers: map[string]configopaque.String{
  73. "hdr1": "val1",
  74. "hdr2": "val2",
  75. },
  76. },
  77. NumStreams: 3,
  78. },
  79. },
  80. {
  81. name: "CompressionError",
  82. config: &Config{
  83. GRPCClientSettings: configgrpc.GRPCClientSettings{
  84. Endpoint: endpoint,
  85. Compression: "unknown compression",
  86. },
  87. NumStreams: 3,
  88. },
  89. mustFailOnCreate: false,
  90. mustFailOnStart: true,
  91. },
  92. {
  93. name: "CaCert",
  94. config: &Config{
  95. GRPCClientSettings: configgrpc.GRPCClientSettings{
  96. Endpoint: endpoint,
  97. TLSSetting: configtls.TLSClientSetting{
  98. TLSSetting: configtls.TLSSetting{
  99. CAFile: "testdata/test_cert.pem",
  100. },
  101. Insecure: false,
  102. },
  103. },
  104. NumStreams: 3,
  105. },
  106. },
  107. {
  108. name: "CertPemFileError",
  109. config: &Config{
  110. GRPCClientSettings: configgrpc.GRPCClientSettings{
  111. Endpoint: endpoint,
  112. TLSSetting: configtls.TLSClientSetting{
  113. TLSSetting: configtls.TLSSetting{
  114. CAFile: "nosuchfile",
  115. },
  116. },
  117. },
  118. NumStreams: 3,
  119. },
  120. mustFailOnCreate: false,
  121. mustFailOnStart: true,
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. set := exportertest.NewNopCreateSettings()
  127. tExporter, tErr := createLogsExporter(context.Background(), set, tt.config)
  128. checkErrorsAndStartAndShutdown(t, tExporter, tErr, tt.mustFailOnCreate, tt.mustFailOnStart)
  129. tExporter2, tErr2 := createMetricsExporter(context.Background(), set, tt.config)
  130. checkErrorsAndStartAndShutdown(t, tExporter2, tErr2, tt.mustFailOnCreate, tt.mustFailOnStart)
  131. })
  132. }
  133. }
  134. func checkErrorsAndStartAndShutdown(t *testing.T, exporter component.Component, err error, mustFailOnCreate, mustFailOnStart bool) {
  135. if mustFailOnCreate {
  136. assert.NotNil(t, err)
  137. return
  138. }
  139. assert.NoError(t, err)
  140. assert.NotNil(t, exporter)
  141. sErr := exporter.Start(context.Background(), componenttest.NewNopHost())
  142. if mustFailOnStart {
  143. require.Error(t, sErr)
  144. return
  145. }
  146. require.NoError(t, sErr)
  147. require.NoError(t, exporter.Shutdown(context.Background()))
  148. }