config_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package dockerstatsreceiver
  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. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver/internal/metadata"
  16. )
  17. func TestLoadConfig(t *testing.T) {
  18. t.Parallel()
  19. tests := []struct {
  20. id component.ID
  21. expected component.Config
  22. }{
  23. {
  24. id: component.NewIDWithName(metadata.Type, ""),
  25. expected: createDefaultConfig(),
  26. },
  27. {
  28. id: component.NewIDWithName(metadata.Type, "allsettings"),
  29. expected: &Config{
  30. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  31. CollectionInterval: 2 * time.Second,
  32. InitialDelay: time.Second,
  33. Timeout: 20 * time.Second,
  34. },
  35. Endpoint: "http://example.com/",
  36. DockerAPIVersion: 1.25,
  37. ExcludedImages: []string{
  38. "undesired-container",
  39. "another-*-container",
  40. },
  41. ContainerLabelsToMetricLabels: map[string]string{
  42. "my.container.label": "my-metric-label",
  43. "my.other.container.label": "my-other-metric-label",
  44. },
  45. EnvVarsToMetricLabels: map[string]string{
  46. "MY_ENVIRONMENT_VARIABLE": "my-metric-label",
  47. "MY_OTHER_ENVIRONMENT_VARIABLE": "my-other-metric-label",
  48. },
  49. MetricsBuilderConfig: func() metadata.MetricsBuilderConfig {
  50. m := metadata.DefaultMetricsBuilderConfig()
  51. m.Metrics.ContainerCPUUsageSystem = metadata.MetricConfig{
  52. Enabled: false,
  53. }
  54. m.Metrics.ContainerMemoryTotalRss = metadata.MetricConfig{
  55. Enabled: true,
  56. }
  57. return m
  58. }(),
  59. },
  60. },
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.id.String(), func(t *testing.T) {
  64. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  65. require.NoError(t, err)
  66. factory := NewFactory()
  67. cfg := factory.CreateDefaultConfig()
  68. sub, err := cm.Sub(tt.id.String())
  69. require.NoError(t, err)
  70. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  71. assert.NoError(t, component.ValidateConfig(cfg))
  72. if diff := cmp.Diff(tt.expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{})); diff != "" {
  73. t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
  74. }
  75. })
  76. }
  77. }
  78. func TestValidateErrors(t *testing.T) {
  79. cfg := &Config{ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)}
  80. assert.Equal(t, "endpoint must be specified", component.ValidateConfig(cfg).Error())
  81. cfg = &Config{ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: 1 * time.Second}, Endpoint: "someEndpoint", DockerAPIVersion: 1.21}
  82. assert.Equal(t, "api_version must be at least 1.25", component.ValidateConfig(cfg).Error())
  83. }