config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package logicmonitorexporter
  4. import (
  5. "errors"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/config/confighttp"
  12. "go.opentelemetry.io/collector/config/configopaque"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/exporter/exporterhelper"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logicmonitorexporter/internal/metadata"
  16. )
  17. func TestConfigValidation(t *testing.T) {
  18. testcases := []struct {
  19. name string
  20. cfg *Config
  21. wantErr bool
  22. errorMessage string
  23. }{
  24. {
  25. name: "empty endpoint",
  26. cfg: &Config{
  27. HTTPClientSettings: confighttp.HTTPClientSettings{
  28. Endpoint: "",
  29. },
  30. },
  31. wantErr: true,
  32. errorMessage: "Endpoint should not be empty",
  33. },
  34. {
  35. name: "missing http scheme",
  36. cfg: &Config{
  37. HTTPClientSettings: confighttp.HTTPClientSettings{
  38. Endpoint: "test.com/dummy",
  39. },
  40. },
  41. wantErr: true,
  42. errorMessage: "Endpoint must be valid",
  43. },
  44. {
  45. name: "invalid endpoint format",
  46. cfg: &Config{
  47. HTTPClientSettings: confighttp.HTTPClientSettings{
  48. Endpoint: "invalid.com@#$%",
  49. },
  50. },
  51. wantErr: true,
  52. errorMessage: "Endpoint must be valid",
  53. },
  54. {
  55. name: "valid config",
  56. cfg: &Config{
  57. HTTPClientSettings: confighttp.HTTPClientSettings{
  58. Endpoint: "http://validurl.com/rest",
  59. },
  60. },
  61. wantErr: false,
  62. errorMessage: "",
  63. },
  64. }
  65. for _, tc := range testcases {
  66. t.Run(tc.name, func(t *testing.T) {
  67. err := tc.cfg.Validate()
  68. if (err != nil) != tc.wantErr {
  69. t.Errorf("config validation failed: error = %v, wantErr %v", err, tc.wantErr)
  70. return
  71. }
  72. if tc.wantErr {
  73. assert.Error(t, err)
  74. if len(tc.errorMessage) != 0 {
  75. assert.Equal(t, errors.New(tc.errorMessage), err, "Error messages must match")
  76. }
  77. return
  78. }
  79. assert.NoError(t, err)
  80. })
  81. }
  82. }
  83. func TestLoadConfig(t *testing.T) {
  84. t.Parallel()
  85. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  86. require.NoError(t, err)
  87. tests := []struct {
  88. id component.ID
  89. expected component.Config
  90. }{
  91. {
  92. id: component.NewIDWithName(metadata.Type, "apitoken"),
  93. expected: &Config{
  94. RetrySettings: exporterhelper.NewDefaultRetrySettings(),
  95. QueueSettings: exporterhelper.NewDefaultQueueSettings(),
  96. HTTPClientSettings: confighttp.HTTPClientSettings{
  97. Endpoint: "https://company.logicmonitor.com/rest",
  98. },
  99. APIToken: APIToken{
  100. AccessID: "accessid",
  101. AccessKey: "accesskey",
  102. },
  103. },
  104. },
  105. {
  106. id: component.NewIDWithName(metadata.Type, "bearertoken"),
  107. expected: &Config{
  108. RetrySettings: exporterhelper.NewDefaultRetrySettings(),
  109. QueueSettings: exporterhelper.NewDefaultQueueSettings(),
  110. HTTPClientSettings: confighttp.HTTPClientSettings{
  111. Endpoint: "https://company.logicmonitor.com/rest",
  112. Headers: map[string]configopaque.String{
  113. "Authorization": "Bearer <token>",
  114. },
  115. },
  116. },
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.id.String(), func(t *testing.T) {
  121. factory := NewFactory()
  122. cfg := factory.CreateDefaultConfig()
  123. sub, err := cm.Sub(tt.id.String())
  124. require.NoError(t, err)
  125. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  126. assert.NoError(t, component.ValidateConfig(cfg))
  127. assert.Equal(t, tt.expected, cfg)
  128. })
  129. }
  130. }