factory_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package f5cloudexporter
  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/configopaque"
  13. "go.opentelemetry.io/collector/exporter/exportertest"
  14. "golang.org/x/oauth2"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/f5cloudexporter/internal/metadata"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
  17. )
  18. func TestFactory_TestType(t *testing.T) {
  19. f := NewFactory()
  20. assert.Equal(t, f.Type(), component.Type(metadata.Type))
  21. }
  22. func TestFactory_CreateDefaultConfig(t *testing.T) {
  23. factory := NewFactory()
  24. cfg := factory.CreateDefaultConfig()
  25. assert.NotNil(t, cfg, "failed to create default config")
  26. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  27. ocfg, ok := factory.CreateDefaultConfig().(*Config)
  28. assert.True(t, ok)
  29. assert.Equal(t, ocfg.HTTPClientSettings.Endpoint, "")
  30. assert.Equal(t, ocfg.HTTPClientSettings.Timeout, 30*time.Second, "default timeout is 30 seconds")
  31. assert.Equal(t, ocfg.RetrySettings.Enabled, true, "default retry is enabled")
  32. assert.Equal(t, ocfg.RetrySettings.MaxElapsedTime, 300*time.Second, "default retry MaxElapsedTime")
  33. assert.Equal(t, ocfg.RetrySettings.InitialInterval, 5*time.Second, "default retry InitialInterval")
  34. assert.Equal(t, ocfg.RetrySettings.MaxInterval, 30*time.Second, "default retry MaxInterval")
  35. assert.Equal(t, ocfg.QueueSettings.Enabled, true, "default sending queue is enabled")
  36. }
  37. func TestFactory_CreateMetricsExporter(t *testing.T) {
  38. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  39. cfg := factory.CreateDefaultConfig().(*Config)
  40. cfg.HTTPClientSettings.Endpoint = "https://" + testutil.GetAvailableLocalAddress(t)
  41. cfg.Source = "tests"
  42. cfg.AuthConfig = AuthConfig{
  43. CredentialFile: "testdata/empty_credential_file.json",
  44. Audience: "tests",
  45. }
  46. creationParams := exportertest.NewNopCreateSettings()
  47. creationParams.BuildInfo = component.BuildInfo{
  48. Version: "0.0.0",
  49. }
  50. oexp, err := factory.CreateMetricsExporter(context.Background(), creationParams, cfg)
  51. require.Nil(t, err)
  52. require.NotNil(t, oexp)
  53. require.Equal(t, configopaque.String("opentelemetry-collector-contrib 0.0.0"), cfg.Headers["User-Agent"])
  54. }
  55. func TestFactory_CreateMetricsExporterInvalidConfig(t *testing.T) {
  56. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  57. cfg := factory.CreateDefaultConfig().(*Config)
  58. creationParams := exportertest.NewNopCreateSettings()
  59. oexp, err := factory.CreateMetricsExporter(context.Background(), creationParams, cfg)
  60. require.Error(t, err)
  61. require.Nil(t, oexp)
  62. }
  63. func TestFactory_CreateTracesExporter(t *testing.T) {
  64. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  65. cfg := factory.CreateDefaultConfig().(*Config)
  66. cfg.HTTPClientSettings.Endpoint = "https://" + testutil.GetAvailableLocalAddress(t)
  67. cfg.Source = "tests"
  68. cfg.AuthConfig = AuthConfig{
  69. CredentialFile: "testdata/empty_credential_file.json",
  70. Audience: "tests",
  71. }
  72. creationParams := exportertest.NewNopCreateSettings()
  73. creationParams.BuildInfo = component.BuildInfo{
  74. Version: "0.0.0",
  75. }
  76. oexp, err := factory.CreateTracesExporter(context.Background(), creationParams, cfg)
  77. require.Nil(t, err)
  78. require.NotNil(t, oexp)
  79. require.Equal(t, configopaque.String("opentelemetry-collector-contrib 0.0.0"), cfg.Headers["User-Agent"])
  80. }
  81. func Test_Factory_CreateTracesExporterInvalidConfig(t *testing.T) {
  82. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  83. cfg := factory.CreateDefaultConfig().(*Config)
  84. creationParams := exportertest.NewNopCreateSettings()
  85. oexp, err := factory.CreateTracesExporter(context.Background(), creationParams, cfg)
  86. require.Error(t, err)
  87. require.Nil(t, oexp)
  88. }
  89. func TestFactory_CreateLogsExporter(t *testing.T) {
  90. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  91. cfg := factory.CreateDefaultConfig().(*Config)
  92. cfg.HTTPClientSettings.Endpoint = "https://" + testutil.GetAvailableLocalAddress(t)
  93. cfg.Source = "tests"
  94. cfg.AuthConfig = AuthConfig{
  95. CredentialFile: "testdata/empty_credential_file.json",
  96. Audience: "tests",
  97. }
  98. creationParams := exportertest.NewNopCreateSettings()
  99. creationParams.BuildInfo = component.BuildInfo{
  100. Version: "0.0.0",
  101. }
  102. oexp, err := factory.CreateLogsExporter(context.Background(), creationParams, cfg)
  103. require.Nil(t, err)
  104. require.NotNil(t, oexp)
  105. require.Equal(t, configopaque.String("opentelemetry-collector-contrib 0.0.0"), cfg.Headers["User-Agent"])
  106. }
  107. func TestFactory_CreateLogsExporterInvalidConfig(t *testing.T) {
  108. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  109. cfg := factory.CreateDefaultConfig().(*Config)
  110. creationParams := exportertest.NewNopCreateSettings()
  111. oexp, err := factory.CreateLogsExporter(context.Background(), creationParams, cfg)
  112. require.Error(t, err)
  113. require.Nil(t, oexp)
  114. }
  115. func TestFactory_getTokenSourceFromConfig(t *testing.T) {
  116. factory := newFactoryWithTokenSourceGetter(mockTokenSourceGetter)
  117. cfg := factory.CreateDefaultConfig().(*Config)
  118. cfg.HTTPClientSettings.Endpoint = "https://" + testutil.GetAvailableLocalAddress(t)
  119. cfg.Source = "tests"
  120. cfg.AuthConfig = AuthConfig{
  121. CredentialFile: "testdata/empty_credential_file.json",
  122. Audience: "tests",
  123. }
  124. ts, err := getTokenSourceFromConfig(cfg)
  125. assert.Error(t, err)
  126. assert.Nil(t, ts)
  127. }
  128. func mockTokenSourceGetter(_ *Config) (oauth2.TokenSource, error) {
  129. tkn := &oauth2.Token{
  130. AccessToken: "",
  131. TokenType: "",
  132. RefreshToken: "",
  133. Expiry: time.Time{},
  134. }
  135. return oauth2.StaticTokenSource(tkn), nil
  136. }