factory_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opencensusexporter
  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: "NoEndpoint",
  33. config: &Config{
  34. GRPCClientSettings: configgrpc.GRPCClientSettings{
  35. Endpoint: "",
  36. },
  37. NumWorkers: 3,
  38. },
  39. mustFailOnCreate: true,
  40. },
  41. {
  42. name: "ZeroNumWorkers",
  43. config: &Config{
  44. GRPCClientSettings: configgrpc.GRPCClientSettings{
  45. Endpoint: endpoint,
  46. TLSSetting: configtls.TLSClientSetting{
  47. Insecure: false,
  48. },
  49. },
  50. NumWorkers: 0,
  51. },
  52. mustFailOnCreate: true,
  53. },
  54. {
  55. name: "UseSecure",
  56. config: &Config{
  57. GRPCClientSettings: configgrpc.GRPCClientSettings{
  58. Endpoint: endpoint,
  59. TLSSetting: configtls.TLSClientSetting{
  60. Insecure: false,
  61. },
  62. },
  63. NumWorkers: 3,
  64. },
  65. },
  66. {
  67. name: "Keepalive",
  68. config: &Config{
  69. GRPCClientSettings: configgrpc.GRPCClientSettings{
  70. Endpoint: endpoint,
  71. Keepalive: &configgrpc.KeepaliveClientConfig{
  72. Time: 30 * time.Second,
  73. Timeout: 25 * time.Second,
  74. PermitWithoutStream: true,
  75. },
  76. },
  77. NumWorkers: 3,
  78. },
  79. },
  80. {
  81. name: "Compression",
  82. config: &Config{
  83. GRPCClientSettings: configgrpc.GRPCClientSettings{
  84. Endpoint: endpoint,
  85. Compression: "gzip",
  86. },
  87. NumWorkers: 3,
  88. },
  89. },
  90. {
  91. name: "Headers",
  92. config: &Config{
  93. GRPCClientSettings: configgrpc.GRPCClientSettings{
  94. Endpoint: endpoint,
  95. Headers: map[string]configopaque.String{
  96. "hdr1": "val1",
  97. "hdr2": "val2",
  98. },
  99. },
  100. NumWorkers: 3,
  101. },
  102. },
  103. {
  104. name: "CompressionError",
  105. config: &Config{
  106. GRPCClientSettings: configgrpc.GRPCClientSettings{
  107. Endpoint: endpoint,
  108. Compression: "unknown compression",
  109. },
  110. NumWorkers: 3,
  111. },
  112. mustFailOnCreate: false,
  113. mustFailOnStart: true,
  114. },
  115. {
  116. name: "CaCert",
  117. config: &Config{
  118. GRPCClientSettings: configgrpc.GRPCClientSettings{
  119. Endpoint: endpoint,
  120. TLSSetting: configtls.TLSClientSetting{
  121. TLSSetting: configtls.TLSSetting{
  122. CAFile: "testdata/test_cert.pem",
  123. },
  124. },
  125. },
  126. NumWorkers: 3,
  127. },
  128. },
  129. {
  130. name: "CertPemFileError",
  131. config: &Config{
  132. GRPCClientSettings: configgrpc.GRPCClientSettings{
  133. Endpoint: endpoint,
  134. TLSSetting: configtls.TLSClientSetting{
  135. TLSSetting: configtls.TLSSetting{
  136. CAFile: "nosuchfile",
  137. },
  138. },
  139. },
  140. NumWorkers: 3,
  141. },
  142. mustFailOnCreate: false,
  143. mustFailOnStart: true,
  144. },
  145. }
  146. for _, tt := range tests {
  147. t.Run(tt.name, func(t *testing.T) {
  148. set := exportertest.NewNopCreateSettings()
  149. tExporter, tErr := createTracesExporter(context.Background(), set, tt.config)
  150. checkErrorsAndStartAndShutdown(t, tExporter, tErr, tt.mustFailOnCreate, tt.mustFailOnStart)
  151. mExporter, mErr := createMetricsExporter(context.Background(), set, tt.config)
  152. checkErrorsAndStartAndShutdown(t, mExporter, mErr, tt.mustFailOnCreate, tt.mustFailOnStart)
  153. })
  154. }
  155. }
  156. func checkErrorsAndStartAndShutdown(t *testing.T, exporter component.Component, err error, mustFailOnCreate, mustFailOnStart bool) {
  157. if mustFailOnCreate {
  158. assert.NotNil(t, err)
  159. return
  160. }
  161. assert.NoError(t, err)
  162. assert.NotNil(t, exporter)
  163. sErr := exporter.Start(context.Background(), componenttest.NewNopHost())
  164. if mustFailOnStart {
  165. require.Error(t, sErr)
  166. return
  167. }
  168. require.NoError(t, sErr)
  169. require.NoError(t, exporter.Shutdown(context.Background()))
  170. }