diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 3b235ded48..bae0e7b70f 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -47,12 +47,11 @@ func NewTestDiskStatsCollector(config NodeCollectorConfig, logger log.Logger) (p } func TestDiskStats(t *testing.T) { - empty := "" config := NodeCollectorConfig{ DiskstatsDeviceFilter: DiskstatsDeviceFilterConfig{ - DiskstatsDeviceExclude: &empty, - DiskstatsDeviceInclude: &empty, - OldDiskstatsDeviceExclude: &empty, + DiskstatsDeviceExclude: new(string), + DiskstatsDeviceInclude: new(string), + OldDiskstatsDeviceExclude: new(string), }, } sysPath := "fixtures/sys" diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index c9c366fa6f..035adbb55f 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -282,7 +282,13 @@ func TestBuildEthtoolFQName(t *testing.T) { } func TestEthToolCollector(t *testing.T) { - config := NodeCollectorConfig{} + config := NodeCollectorConfig{ + Ethtool: EthtoolConfig{ + DeviceInclude: new(string), + DeviceExclude: new(string), + IncludedMetrics: new(string), + }, + } testcase := `# HELP node_ethtool_align_errors Network interface align_errors # TYPE node_ethtool_align_errors untyped node_ethtool_align_errors{device="eth0"} 0 diff --git a/collector/filesystem_linux_test.go b/collector/filesystem_linux_test.go index 1657f0db35..07544dfc5c 100644 --- a/collector/filesystem_linux_test.go +++ b/collector/filesystem_linux_test.go @@ -18,8 +18,6 @@ import ( "testing" "github.com/go-kit/log" - - "github.com/alecthomas/kingpin/v2" ) func Test_parseFilesystemLabelsError(t *testing.T) { @@ -45,11 +43,8 @@ func Test_parseFilesystemLabelsError(t *testing.T) { func TestMountPointDetails(t *testing.T) { path := "./fixtures/proc" - config := NodeCollectorConfig{Path: PathConfig{ProcPath: &path, SysPath: &path, RootfsPath: &path, UdevDataPath: &path}} - - // if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil { - // t.Fatal(err) - // } + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &path expected := map[string]string{ "/": "", @@ -97,11 +92,9 @@ func TestMountPointDetails(t *testing.T) { } func TestMountsFallback(t *testing.T) { - config := NodeCollectorConfig{} - - if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil { - t.Fatal(err) - } + path := "./fixtures_hidepid/proc" + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &path expected := map[string]string{ "/": "", @@ -120,11 +113,11 @@ func TestMountsFallback(t *testing.T) { } func TestPathRootfs(t *testing.T) { - config := NodeCollectorConfig{} - - if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil { - t.Fatal(err) - } + procPath := "./fixtures_bindmount/proc" + rootfsPath := "/host" + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &procPath + config.Path.RootfsPath = &rootfsPath expected := map[string]string{ // should modify these mountpoints (removes /host, see fixture proc file) diff --git a/collector/ipvs_linux_test.go b/collector/ipvs_linux_test.go index 825885327b..abb69c61f4 100644 --- a/collector/ipvs_linux_test.go +++ b/collector/ipvs_linux_test.go @@ -24,12 +24,15 @@ import ( "github.com/go-kit/log" - "github.com/alecthomas/kingpin/v2" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func TestIPVSCollector(t *testing.T) { + path := "fixtures/proc" + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &path + testcases := []struct { labels string expects []string @@ -104,14 +107,11 @@ func TestIPVSCollector(t *testing.T) { } for _, test := range testcases { t.Run(test.labels, func(t *testing.T) { - args := []string{"--path.procfs", "fixtures/proc"} + config.IPVS.Labels = new(string) if test.labels != "" { - args = append(args, "--collector.ipvs.backend-labels="+test.labels) - } - if _, err := kingpin.CommandLine.Parse(args); err != nil { - t.Fatal(err) + config.IPVS.Labels = &test.labels } - collector, err := newIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger()) + collector, err := newIPVSCollector(config, log.NewNopLogger()) if err != nil { if test.err == nil { t.Fatal(err) @@ -161,6 +161,10 @@ func (c miniCollector) Describe(ch chan<- *prometheus.Desc) { } func TestIPVSCollectorResponse(t *testing.T) { + path := "fixtures/proc" + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &path + testcases := []struct { labels string metricsFile string @@ -172,14 +176,11 @@ func TestIPVSCollectorResponse(t *testing.T) { } for _, test := range testcases { t.Run(test.labels, func(t *testing.T) { - args := []string{"--path.procfs", "fixtures/proc"} + config.IPVS.Labels = new(string) if test.labels != "" { - args = append(args, "--collector.ipvs.backend-labels="+test.labels) - } - if _, err := kingpin.CommandLine.Parse(args); err != nil { - t.Fatal(err) + config.IPVS.Labels = &test.labels } - collector, err := NewIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger()) + collector, err := NewIPVSCollector(config, log.NewNopLogger()) if err != nil { t.Fatal(err) } diff --git a/collector/paths_test.go b/collector/paths_test.go index 1e946103cb..1690ab90e2 100644 --- a/collector/paths_test.go +++ b/collector/paths_test.go @@ -16,69 +16,70 @@ package collector import ( "testing" - "github.com/alecthomas/kingpin/v2" "github.com/prometheus/procfs" ) func TestDefaultProcPath(t *testing.T) { - config := PathConfig{} + config := newNodeCollectorWithPaths() + path := procfs.DefaultMountPoint + config.Path.ProcPath = &path - if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", procfs.DefaultMountPoint}); err != nil { - t.Fatal(err) - } - - if got, want := config.procFilePath("somefile"), "/proc/somefile"; got != want { + if got, want := config.Path.procFilePath("somefile"), "/proc/somefile"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } - if got, want := config.procFilePath("some/file"), "/proc/some/file"; got != want { + if got, want := config.Path.procFilePath("some/file"), "/proc/some/file"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } } func TestCustomProcPath(t *testing.T) { - config := PathConfig{} + config := newNodeCollectorWithPaths() + path := "./../some/./place/" + config.Path.ProcPath = &path - if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./../some/./place/"}); err != nil { - t.Fatal(err) - } - - if got, want := config.procFilePath("somefile"), "../some/place/somefile"; got != want { + if got, want := config.Path.procFilePath("somefile"), "../some/place/somefile"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } - if got, want := config.procFilePath("some/file"), "../some/place/some/file"; got != want { + if got, want := config.Path.procFilePath("some/file"), "../some/place/some/file"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } } func TestDefaultSysPath(t *testing.T) { - config := PathConfig{} + config := newNodeCollectorWithPaths() + path := "/sys" + config.Path.SysPath = &path - if _, err := kingpin.CommandLine.Parse([]string{"--path.sysfs", "/sys"}); err != nil { - t.Fatal(err) - } - - if got, want := config.sysFilePath("somefile"), "/sys/somefile"; got != want { + if got, want := config.Path.sysFilePath("somefile"), "/sys/somefile"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } - if got, want := config.sysFilePath("some/file"), "/sys/some/file"; got != want { + if got, want := config.Path.sysFilePath("some/file"), "/sys/some/file"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } } func TestCustomSysPath(t *testing.T) { - config := PathConfig{} - if _, err := kingpin.CommandLine.Parse([]string{"--path.sysfs", "./../some/./place/"}); err != nil { - t.Fatal(err) - } + config := newNodeCollectorWithPaths() + path := "./../some/./place/" + config.Path.SysPath = &path - if got, want := config.sysFilePath("somefile"), "../some/place/somefile"; got != want { + if got, want := config.Path.sysFilePath("somefile"), "../some/place/somefile"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } - if got, want := config.sysFilePath("some/file"), "../some/place/some/file"; got != want { + if got, want := config.Path.sysFilePath("some/file"), "../some/place/some/file"; got != want { t.Errorf("Expected: %s, Got: %s", want, got) } } + +func newNodeCollectorWithPaths() NodeCollectorConfig { + return NodeCollectorConfig{Path: PathConfig{ + ProcPath: new(string), + SysPath: new(string), + RootfsPath: new(string), + UdevDataPath: new(string), + }} +} diff --git a/collector/processes_linux.go b/collector/processes_linux.go index 2778ebd358..8b9e3294a1 100644 --- a/collector/processes_linux.go +++ b/collector/processes_linux.go @@ -83,6 +83,7 @@ func NewProcessStatCollector(config NodeCollectorConfig, logger log.Logger) (Col "Number of max PIDs limit", nil, nil, ), logger: logger, + config: config, }, nil } func (c *processCollector) Update(ch chan<- prometheus.Metric) error { diff --git a/collector/processes_linux_test.go b/collector/processes_linux_test.go index 53e1ce5fda..e79859a6ea 100644 --- a/collector/processes_linux_test.go +++ b/collector/processes_linux_test.go @@ -19,22 +19,21 @@ package collector import ( "testing" - "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/prometheus/procfs" ) func TestReadProcessStatus(t *testing.T) { - config := NodeCollectorConfig{} - if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "fixtures/proc"}); err != nil { - t.Fatal(err) - } + path := "fixtures/proc" + config := newNodeCollectorWithPaths() + config.Path.ProcPath = &path + want := 1 fs, err := procfs.NewFS(*config.Path.ProcPath) if err != nil { t.Errorf("failed to open procfs: %v", err) } - c := processCollector{fs: fs, logger: log.NewNopLogger()} + c := processCollector{fs: fs, logger: log.NewNopLogger(), config: config} pids, states, threads, _, err := c.getAllocatedThreads() if err != nil { t.Fatalf("Cannot retrieve data from procfs getAllocatedThreads function: %v ", err) diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 9fe2147bae..bfd733a7c0 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -100,7 +100,15 @@ func TestSystemdIgnoreFilter(t *testing.T) { } func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { logger := log.NewNopLogger() - c, err := NewSystemdCollector(NodeCollectorConfig{}, logger) + defaultInclude := ".+" + defaultExclude := ".+\\.(automount|device|mount|scope|slice)" + config := NodeCollectorConfig{Systemd: SystemdConfig{ + UnitInclude: &defaultInclude, + UnitExclude: &defaultExclude, + OldUnitInclude: new(string), + OldUnitExclude: new(string), + }} + c, err := NewSystemdCollector(config, logger) if err != nil { t.Fatal(err) }