config_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package instanaexporter
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "go.opentelemetry.io/collector/config/confighttp"
  8. "go.opentelemetry.io/collector/config/configtls"
  9. )
  10. func TestConfigValidate(t *testing.T) {
  11. t.Run("Empty configuration", func(t *testing.T) {
  12. c := &Config{}
  13. err := c.Validate()
  14. assert.Error(t, err)
  15. })
  16. t.Run("Valid configuration", func(t *testing.T) {
  17. c := &Config{Endpoint: "https://example.com/", AgentKey: "key1"}
  18. err := c.Validate()
  19. assert.NoError(t, err)
  20. assert.Equal(t, "https://example.com/", c.Endpoint, "no Instana endpoint set")
  21. assert.Equal(t, "", c.TLSSetting.CAFile, "optional ca_file property not set")
  22. })
  23. t.Run("Valid configuration with ca_file", func(t *testing.T) {
  24. c := &Config{Endpoint: "https://example.com/", AgentKey: "key1", HTTPClientSettings: confighttp.HTTPClientSettings{
  25. TLSSetting: configtls.TLSClientSetting{
  26. TLSSetting: configtls.TLSSetting{
  27. CAFile: "ca.crt",
  28. },
  29. },
  30. }}
  31. err := c.Validate()
  32. assert.NoError(t, err)
  33. assert.Equal(t, "https://example.com/", c.Endpoint, "no Instana endpoint set")
  34. assert.Equal(t, "ca.crt", c.TLSSetting.CAFile, "optional ca_file property set")
  35. })
  36. t.Run("Invalid Endpoint Invalid URL", func(t *testing.T) {
  37. c := &Config{Endpoint: "https://example.}~", AgentKey: "key1"}
  38. err := c.Validate()
  39. assert.Error(t, err)
  40. })
  41. t.Run("Invalid Endpoint No Protocol", func(t *testing.T) {
  42. c := &Config{Endpoint: "example.com", AgentKey: "key1"}
  43. err := c.Validate()
  44. assert.Error(t, err)
  45. })
  46. t.Run("Invalid Endpoint No https:// Protocol", func(t *testing.T) {
  47. c := &Config{Endpoint: "http://example.com", AgentKey: "key1"}
  48. err := c.Validate()
  49. assert.Error(t, err, "endpoint must start with https://")
  50. })
  51. t.Run("No Agent key", func(t *testing.T) {
  52. c := &Config{Endpoint: "https://example.com/"}
  53. err := c.Validate()
  54. assert.Error(t, err)
  55. })
  56. }