hostmetrics_linux_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build linux
  4. package hostmetricsreceiver
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "github.com/shirou/gopsutil/v3/common"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/otelcol/otelcoltest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
  16. )
  17. func TestConsistentRootPaths(t *testing.T) {
  18. // use testdata because it's a directory that exists - don't actually use any files in it
  19. assert.Nil(t, testValidate("testdata"))
  20. assert.Nil(t, testValidate(""))
  21. assert.Nil(t, testValidate("/"))
  22. }
  23. func TestInconsistentRootPaths(t *testing.T) {
  24. globalRootPath = "foo"
  25. err := testValidate("testdata")
  26. assert.EqualError(t, err, "inconsistent root_path configuration detected between hostmetricsreceivers: `foo` != `testdata`")
  27. }
  28. func TestLoadConfigRootPath(t *testing.T) {
  29. t.Setenv("HOST_PROC", "testdata")
  30. factories, _ := otelcoltest.NopFactories()
  31. factory := NewFactory()
  32. factories.Receivers[metadata.Type] = factory
  33. cfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-root-path.yaml"), factories)
  34. require.NoError(t, err)
  35. globalRootPath = ""
  36. r := cfg.Receivers[component.NewID(metadata.Type)].(*Config)
  37. expectedConfig := factory.CreateDefaultConfig().(*Config)
  38. expectedConfig.RootPath = "testdata"
  39. cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
  40. cpuScraperCfg.SetRootPath("testdata")
  41. cpuScraperCfg.SetEnvMap(common.EnvMap{
  42. common.HostDevEnvKey: "testdata/dev",
  43. common.HostEtcEnvKey: "testdata/etc",
  44. common.HostRunEnvKey: "testdata/run",
  45. common.HostSysEnvKey: "testdata/sys",
  46. common.HostVarEnvKey: "testdata/var",
  47. })
  48. expectedConfig.Scrapers = map[string]internal.Config{cpuscraper.TypeStr: cpuScraperCfg}
  49. assert.Equal(t, expectedConfig, r)
  50. }
  51. func TestLoadInvalidConfig_RootPathNotExist(t *testing.T) {
  52. factories, _ := otelcoltest.NopFactories()
  53. factory := NewFactory()
  54. factories.Receivers[metadata.Type] = factory
  55. _, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-bad-root-path.yaml"), factories)
  56. assert.ErrorContains(t, err, "invalid root_path:")
  57. globalRootPath = ""
  58. }
  59. func testValidate(rootPath string) error {
  60. err := validateRootPath(rootPath)
  61. globalRootPath = ""
  62. return err
  63. }