config_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snowflakereceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/google/go-cmp/cmp"
  9. "github.com/google/go-cmp/cmp/cmpopts"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/receiver/scraperhelper"
  15. "go.uber.org/multierr"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snowflakereceiver/internal/metadata"
  17. )
  18. func TestValidateConfig(t *testing.T) {
  19. t.Parallel()
  20. var multierror error
  21. multierror = multierr.Append(multierror, errMissingPassword)
  22. multierror = multierr.Append(multierror, errMissingWarehouse)
  23. tests := []struct {
  24. desc string
  25. expect error
  26. conf Config
  27. }{
  28. {
  29. desc: "Missing username all else present",
  30. expect: errMissingUsername,
  31. conf: Config{
  32. Username: "",
  33. Password: "password",
  34. Account: "account",
  35. Warehouse: "warehouse",
  36. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  37. },
  38. },
  39. {
  40. desc: "Missing password all else present",
  41. expect: errMissingPassword,
  42. conf: Config{
  43. Username: "username",
  44. Password: "",
  45. Account: "account",
  46. Warehouse: "warehouse",
  47. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  48. },
  49. },
  50. {
  51. desc: "Missing account all else present",
  52. expect: errMissingAccount,
  53. conf: Config{
  54. Username: "username",
  55. Password: "password",
  56. Account: "",
  57. Warehouse: "warehouse",
  58. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  59. },
  60. },
  61. {
  62. desc: "Missing warehouse all else present",
  63. expect: errMissingWarehouse,
  64. conf: Config{
  65. Username: "username",
  66. Password: "password",
  67. Account: "account",
  68. Warehouse: "",
  69. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  70. },
  71. },
  72. {
  73. desc: "Missing multiple check multierror",
  74. expect: multierror,
  75. conf: Config{
  76. Username: "username",
  77. Password: "",
  78. Account: "account",
  79. Warehouse: "",
  80. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  81. },
  82. },
  83. }
  84. for i := range tests {
  85. test := tests[i]
  86. t.Run(test.desc, func(t *testing.T) {
  87. t.Parallel()
  88. err := test.conf.Validate()
  89. require.Error(t, err)
  90. require.Contains(t, err.Error(), test.expect.Error())
  91. })
  92. }
  93. }
  94. func TestLoadConfig(t *testing.T) {
  95. t.Parallel()
  96. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  97. require.NoError(t, err)
  98. // LoadConf includes the TypeStr which NewFactory does not set
  99. id := component.NewIDWithName(metadata.Type, "")
  100. cmNoStr, err := cm.Sub(id.String())
  101. require.NoError(t, err)
  102. testMetrics := metadata.DefaultMetricsBuilderConfig()
  103. testMetrics.Metrics.SnowflakeDatabaseBytesScannedAvg.Enabled = true
  104. testMetrics.Metrics.SnowflakeQueryBytesDeletedAvg.Enabled = false
  105. expected := &Config{
  106. Username: "snowflakeuser",
  107. Password: "securepassword",
  108. Account: "bigbusinessaccount",
  109. Warehouse: "metricWarehouse",
  110. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  111. CollectionInterval: 18 * time.Minute,
  112. InitialDelay: time.Second,
  113. },
  114. Role: "customMonitoringRole",
  115. Database: "SNOWFLAKE",
  116. Schema: "ACCOUNT_USAGE",
  117. MetricsBuilderConfig: testMetrics,
  118. }
  119. factory := NewFactory()
  120. cfg := factory.CreateDefaultConfig()
  121. require.NoError(t, component.UnmarshalConfig(cmNoStr, cfg))
  122. assert.NoError(t, component.ValidateConfig(cfg))
  123. diff := cmp.Diff(expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{}))
  124. if diff != "" {
  125. t.Errorf("config mismatch (-expected / +actual)\n%s", diff)
  126. }
  127. }