integration_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build integration
  4. // +build integration
  5. package hostmetricsreceiver
  6. import (
  7. "os/exec"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
  18. )
  19. func Test_ProcessScrape(t *testing.T) {
  20. expectedFile := filepath.Join("testdata", "e2e", "expected_process.yaml")
  21. cmd := exec.Command("/bin/sleep", "300")
  22. require.NoError(t, cmd.Start())
  23. defer func() {
  24. require.NoError(t, cmd.Process.Kill())
  25. }()
  26. scraperinttest.NewIntegrationTest(
  27. NewFactory(),
  28. scraperinttest.WithCustomConfig(
  29. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  30. rCfg := cfg.(*Config)
  31. rCfg.CollectionInterval = time.Second
  32. pCfg := (&processscraper.Factory{}).CreateDefaultConfig().(*processscraper.Config)
  33. pCfg.Include = processscraper.MatchConfig{
  34. Config: filterset.Config{MatchType: filterset.Regexp},
  35. Names: []string{"sleep"},
  36. }
  37. rCfg.Scrapers = map[string]internal.Config{
  38. "process": pCfg,
  39. }
  40. }),
  41. scraperinttest.WithExpectedFile(expectedFile),
  42. scraperinttest.WithCompareOptions(
  43. pmetrictest.IgnoreResourceMetricsOrder(),
  44. pmetrictest.IgnoreMetricValues(),
  45. pmetrictest.IgnoreMetricDataPointsOrder(),
  46. pmetrictest.IgnoreStartTimestamp(),
  47. pmetrictest.IgnoreTimestamp(),
  48. ),
  49. ).Run(t)
  50. }
  51. func Test_ProcessScrapeWithCustomRootPath(t *testing.T) {
  52. expectedFile := filepath.Join("testdata", "e2e", "expected_process_separate_proc.yaml")
  53. scraperinttest.NewIntegrationTest(
  54. NewFactory(),
  55. scraperinttest.WithCustomConfig(
  56. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  57. rCfg := cfg.(*Config)
  58. rCfg.CollectionInterval = time.Second
  59. pCfg := (&processscraper.Factory{}).CreateDefaultConfig().(*processscraper.Config)
  60. pCfg.Include = processscraper.MatchConfig{
  61. Config: filterset.Config{MatchType: filterset.Regexp},
  62. Names: []string{"sleep"},
  63. }
  64. rCfg.Scrapers = map[string]internal.Config{
  65. "process": pCfg,
  66. }
  67. rCfg.RootPath = filepath.Join("testdata", "e2e")
  68. }),
  69. scraperinttest.WithExpectedFile(expectedFile),
  70. scraperinttest.WithCompareOptions(
  71. pmetrictest.IgnoreResourceMetricsOrder(),
  72. pmetrictest.IgnoreMetricValues(),
  73. pmetrictest.IgnoreMetricDataPointsOrder(),
  74. pmetrictest.IgnoreStartTimestamp(),
  75. pmetrictest.IgnoreTimestamp(),
  76. ),
  77. ).Run(t)
  78. }
  79. func Test_ProcessScrapeWithBadRootPathAndEnvVar(t *testing.T) {
  80. expectedFile := filepath.Join("testdata", "e2e", "expected_process_separate_proc.yaml")
  81. t.Setenv("HOST_PROC", filepath.Join("testdata", "e2e", "proc"))
  82. scraperinttest.NewIntegrationTest(
  83. NewFactory(),
  84. scraperinttest.WithCustomConfig(
  85. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  86. rCfg := cfg.(*Config)
  87. rCfg.CollectionInterval = time.Second
  88. pCfg := (&processscraper.Factory{}).CreateDefaultConfig().(*processscraper.Config)
  89. pCfg.Include = processscraper.MatchConfig{
  90. Config: filterset.Config{MatchType: filterset.Regexp},
  91. Names: []string{"sleep"},
  92. }
  93. rCfg.Scrapers = map[string]internal.Config{
  94. "process": pCfg,
  95. }
  96. rCfg.RootPath = filepath.Join("testdata", "NOT A VALID FOLDER")
  97. }),
  98. scraperinttest.WithExpectedFile(expectedFile),
  99. scraperinttest.WithCompareOptions(
  100. pmetrictest.IgnoreResourceMetricsOrder(),
  101. pmetrictest.IgnoreMetricValues(),
  102. pmetrictest.IgnoreMetricDataPointsOrder(),
  103. pmetrictest.IgnoreStartTimestamp(),
  104. pmetrictest.IgnoreTimestamp(),
  105. ),
  106. ).Run(t)
  107. }