config_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package f5cloudexporter
  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/confighttp"
  13. "go.opentelemetry.io/collector/config/configopaque"
  14. "go.opentelemetry.io/collector/confmap/confmaptest"
  15. "go.opentelemetry.io/collector/exporter/exporterhelper"
  16. otlphttp "go.opentelemetry.io/collector/exporter/otlphttpexporter"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/f5cloudexporter/internal/metadata"
  18. )
  19. func TestLoadConfig(t *testing.T) {
  20. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  21. require.NoError(t, err)
  22. factory := NewFactory()
  23. cfg := factory.CreateDefaultConfig()
  24. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "allsettings").String())
  25. require.NoError(t, err)
  26. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  27. actualCfg := cfg.(*Config)
  28. expectedCfg := &Config{
  29. Config: otlphttp.Config{
  30. RetrySettings: exporterhelper.RetrySettings{
  31. Enabled: true,
  32. InitialInterval: 10 * time.Second,
  33. MaxInterval: 1 * time.Minute,
  34. MaxElapsedTime: 10 * time.Minute,
  35. RandomizationFactor: backoff.DefaultRandomizationFactor,
  36. Multiplier: backoff.DefaultMultiplier,
  37. },
  38. QueueSettings: exporterhelper.QueueSettings{
  39. Enabled: true,
  40. NumConsumers: 2,
  41. QueueSize: 10,
  42. },
  43. HTTPClientSettings: confighttp.HTTPClientSettings{
  44. Endpoint: "https://f5cloud",
  45. ReadBufferSize: 123,
  46. WriteBufferSize: 345,
  47. Timeout: time.Second * 10,
  48. Headers: map[string]configopaque.String{
  49. "User-Agent": "opentelemetry-collector-contrib {{version}}",
  50. },
  51. Compression: "gzip",
  52. },
  53. },
  54. Source: "dev",
  55. AuthConfig: AuthConfig{
  56. CredentialFile: "/etc/creds/key.json",
  57. Audience: "exampleaudience",
  58. },
  59. }
  60. // testing function equality is not supported in Go hence these will be ignored for this test
  61. expectedCfg.HTTPClientSettings.CustomRoundTripper = nil
  62. actualCfg.HTTPClientSettings.CustomRoundTripper = nil
  63. assert.Equal(t, expectedCfg, actualCfg)
  64. }
  65. func TestConfig_sanitize(t *testing.T) {
  66. const validEndpoint = "https://validendpoint.local"
  67. const validSource = "tests"
  68. type fields struct {
  69. Endpoint string
  70. Source string
  71. CredentialFile string
  72. Audience string
  73. }
  74. tests := []struct {
  75. name string
  76. fields fields
  77. errorMessage string
  78. shouldError bool
  79. }{
  80. {
  81. name: "Test missing endpoint",
  82. fields: fields{
  83. Endpoint: "",
  84. },
  85. errorMessage: "missing required \"endpoint\" setting",
  86. shouldError: true,
  87. },
  88. {
  89. name: "Test invalid endpoint",
  90. fields: fields{
  91. Endpoint: "this://is:an:invalid:endpoint.com",
  92. },
  93. errorMessage: "",
  94. shouldError: true,
  95. },
  96. {
  97. name: "Test credential file not provided",
  98. fields: fields{
  99. Endpoint: validEndpoint,
  100. Source: validSource,
  101. CredentialFile: "",
  102. },
  103. errorMessage: "missing required \"f5cloud_auth.credential_file\" setting",
  104. shouldError: true,
  105. },
  106. {
  107. name: "Test non-existent credential file",
  108. fields: fields{
  109. Endpoint: validEndpoint,
  110. Source: validSource,
  111. CredentialFile: "non-existent cred file",
  112. },
  113. errorMessage: "the provided \"f5cloud_auth.credential_file\" does not exist",
  114. shouldError: true,
  115. },
  116. {
  117. name: "Test missing source",
  118. fields: fields{
  119. Endpoint: validEndpoint,
  120. Source: "",
  121. },
  122. errorMessage: "missing required \"source\" setting",
  123. shouldError: true,
  124. },
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. factory := NewFactory()
  129. cfg := factory.CreateDefaultConfig().(*Config)
  130. cfg.Endpoint = tt.fields.Endpoint
  131. cfg.Source = tt.fields.Source
  132. cfg.AuthConfig = AuthConfig{
  133. CredentialFile: tt.fields.CredentialFile,
  134. Audience: tt.fields.Audience,
  135. }
  136. err := cfg.sanitize()
  137. if (err != nil) != tt.shouldError {
  138. t.Errorf("sanitize() error = %v, shouldError %v", err, tt.shouldError)
  139. return
  140. }
  141. if tt.shouldError {
  142. assert.Error(t, err)
  143. if len(tt.errorMessage) != 0 {
  144. assert.Equal(t, tt.errorMessage, err.Error())
  145. }
  146. }
  147. })
  148. }
  149. t.Run("Test audience is set from endpoint when not provided", func(t *testing.T) {
  150. factory := NewFactory()
  151. cfg := factory.CreateDefaultConfig().(*Config)
  152. cfg.Endpoint = validEndpoint
  153. cfg.Source = validSource
  154. cfg.AuthConfig = AuthConfig{
  155. CredentialFile: "testdata/empty_credential_file.json",
  156. Audience: "",
  157. }
  158. err := cfg.sanitize()
  159. assert.NoError(t, err)
  160. assert.Equal(t, validEndpoint, cfg.AuthConfig.Audience)
  161. })
  162. t.Run("Test audience is not set from endpoint when provided", func(t *testing.T) {
  163. factory := NewFactory()
  164. cfg := factory.CreateDefaultConfig().(*Config)
  165. cfg.Endpoint = validEndpoint
  166. cfg.Source = validSource
  167. cfg.AuthConfig = AuthConfig{
  168. CredentialFile: "testdata/empty_credential_file.json",
  169. Audience: "tests",
  170. }
  171. err := cfg.sanitize()
  172. assert.NoError(t, err)
  173. assert.Equal(t, "tests", cfg.AuthConfig.Audience)
  174. })
  175. }