config_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mysqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver"
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/confmap/confmaptest"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver/internal/metadata"
  12. )
  13. func TestLoadConfig(t *testing.T) {
  14. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  15. require.NoError(t, err)
  16. factory := NewFactory()
  17. cfg := factory.CreateDefaultConfig()
  18. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  19. require.NoError(t, err)
  20. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  21. expected := factory.CreateDefaultConfig().(*Config)
  22. expected.Endpoint = "localhost:3306"
  23. expected.Username = "otel"
  24. expected.Password = "${env:MYSQL_PASSWORD}"
  25. expected.Database = "otel"
  26. expected.CollectionInterval = 10 * time.Second
  27. // This defaults to true when tls is omitted from the configmap.
  28. expected.TLS.Insecure = true
  29. require.Equal(t, expected, cfg)
  30. }
  31. func TestLoadConfigDefaultTLS(t *testing.T) {
  32. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  33. require.NoError(t, err)
  34. factory := NewFactory()
  35. cfg := factory.CreateDefaultConfig()
  36. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String() + "/default_tls")
  37. require.NoError(t, err)
  38. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  39. expected := factory.CreateDefaultConfig().(*Config)
  40. expected.Endpoint = "localhost:3306"
  41. expected.Username = "otel"
  42. expected.Password = "${env:MYSQL_PASSWORD}"
  43. expected.Database = "otel"
  44. expected.CollectionInterval = 10 * time.Second
  45. // This defaults to false when tls is defined in the configmap.
  46. expected.TLS.Insecure = false
  47. expected.TLS.ServerName = "localhost"
  48. require.Equal(t, expected, cfg)
  49. }