Fix tests - part 1

pull/2813/head
Marc Tuduri 2023-09-27 12:37:12 +02:00
parent 26fc637aed
commit 97ddc9b40b
No known key found for this signature in database
GPG Key ID: 761973D5AE312AF4
8 changed files with 78 additions and 70 deletions

View File

@ -47,12 +47,11 @@ func NewTestDiskStatsCollector(config NodeCollectorConfig, logger log.Logger) (p
} }
func TestDiskStats(t *testing.T) { func TestDiskStats(t *testing.T) {
empty := ""
config := NodeCollectorConfig{ config := NodeCollectorConfig{
DiskstatsDeviceFilter: DiskstatsDeviceFilterConfig{ DiskstatsDeviceFilter: DiskstatsDeviceFilterConfig{
DiskstatsDeviceExclude: &empty, DiskstatsDeviceExclude: new(string),
DiskstatsDeviceInclude: &empty, DiskstatsDeviceInclude: new(string),
OldDiskstatsDeviceExclude: &empty, OldDiskstatsDeviceExclude: new(string),
}, },
} }
sysPath := "fixtures/sys" sysPath := "fixtures/sys"

View File

@ -282,7 +282,13 @@ func TestBuildEthtoolFQName(t *testing.T) {
} }
func TestEthToolCollector(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 testcase := `# HELP node_ethtool_align_errors Network interface align_errors
# TYPE node_ethtool_align_errors untyped # TYPE node_ethtool_align_errors untyped
node_ethtool_align_errors{device="eth0"} 0 node_ethtool_align_errors{device="eth0"} 0

View File

@ -18,8 +18,6 @@ import (
"testing" "testing"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/alecthomas/kingpin/v2"
) )
func Test_parseFilesystemLabelsError(t *testing.T) { func Test_parseFilesystemLabelsError(t *testing.T) {
@ -45,11 +43,8 @@ func Test_parseFilesystemLabelsError(t *testing.T) {
func TestMountPointDetails(t *testing.T) { func TestMountPointDetails(t *testing.T) {
path := "./fixtures/proc" path := "./fixtures/proc"
config := NodeCollectorConfig{Path: PathConfig{ProcPath: &path, SysPath: &path, RootfsPath: &path, UdevDataPath: &path}} config := newNodeCollectorWithPaths()
config.Path.ProcPath = &path
// if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
// t.Fatal(err)
// }
expected := map[string]string{ expected := map[string]string{
"/": "", "/": "",
@ -97,11 +92,9 @@ func TestMountPointDetails(t *testing.T) {
} }
func TestMountsFallback(t *testing.T) { func TestMountsFallback(t *testing.T) {
config := NodeCollectorConfig{} path := "./fixtures_hidepid/proc"
config := newNodeCollectorWithPaths()
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil { config.Path.ProcPath = &path
t.Fatal(err)
}
expected := map[string]string{ expected := map[string]string{
"/": "", "/": "",
@ -120,11 +113,11 @@ func TestMountsFallback(t *testing.T) {
} }
func TestPathRootfs(t *testing.T) { func TestPathRootfs(t *testing.T) {
config := NodeCollectorConfig{} procPath := "./fixtures_bindmount/proc"
rootfsPath := "/host"
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil { config := newNodeCollectorWithPaths()
t.Fatal(err) config.Path.ProcPath = &procPath
} config.Path.RootfsPath = &rootfsPath
expected := map[string]string{ expected := map[string]string{
// should modify these mountpoints (removes /host, see fixture proc file) // should modify these mountpoints (removes /host, see fixture proc file)

View File

@ -24,12 +24,15 @@ import (
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
func TestIPVSCollector(t *testing.T) { func TestIPVSCollector(t *testing.T) {
path := "fixtures/proc"
config := newNodeCollectorWithPaths()
config.Path.ProcPath = &path
testcases := []struct { testcases := []struct {
labels string labels string
expects []string expects []string
@ -104,14 +107,11 @@ func TestIPVSCollector(t *testing.T) {
} }
for _, test := range testcases { for _, test := range testcases {
t.Run(test.labels, func(t *testing.T) { t.Run(test.labels, func(t *testing.T) {
args := []string{"--path.procfs", "fixtures/proc"} config.IPVS.Labels = new(string)
if test.labels != "<none>" { if test.labels != "<none>" {
args = append(args, "--collector.ipvs.backend-labels="+test.labels) config.IPVS.Labels = &test.labels
} }
if _, err := kingpin.CommandLine.Parse(args); err != nil { collector, err := newIPVSCollector(config, log.NewNopLogger())
t.Fatal(err)
}
collector, err := newIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger())
if err != nil { if err != nil {
if test.err == nil { if test.err == nil {
t.Fatal(err) t.Fatal(err)
@ -161,6 +161,10 @@ func (c miniCollector) Describe(ch chan<- *prometheus.Desc) {
} }
func TestIPVSCollectorResponse(t *testing.T) { func TestIPVSCollectorResponse(t *testing.T) {
path := "fixtures/proc"
config := newNodeCollectorWithPaths()
config.Path.ProcPath = &path
testcases := []struct { testcases := []struct {
labels string labels string
metricsFile string metricsFile string
@ -172,14 +176,11 @@ func TestIPVSCollectorResponse(t *testing.T) {
} }
for _, test := range testcases { for _, test := range testcases {
t.Run(test.labels, func(t *testing.T) { t.Run(test.labels, func(t *testing.T) {
args := []string{"--path.procfs", "fixtures/proc"} config.IPVS.Labels = new(string)
if test.labels != "<none>" { if test.labels != "<none>" {
args = append(args, "--collector.ipvs.backend-labels="+test.labels) config.IPVS.Labels = &test.labels
} }
if _, err := kingpin.CommandLine.Parse(args); err != nil { collector, err := NewIPVSCollector(config, log.NewNopLogger())
t.Fatal(err)
}
collector, err := NewIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -16,69 +16,70 @@ package collector
import ( import (
"testing" "testing"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/procfs" "github.com/prometheus/procfs"
) )
func TestDefaultProcPath(t *testing.T) { 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 { if got, want := config.Path.procFilePath("somefile"), "/proc/somefile"; got != want {
t.Fatal(err)
}
if got, want := config.procFilePath("somefile"), "/proc/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got) 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) t.Errorf("Expected: %s, Got: %s", want, got)
} }
} }
func TestCustomProcPath(t *testing.T) { 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 { if got, want := config.Path.procFilePath("somefile"), "../some/place/somefile"; got != want {
t.Fatal(err)
}
if got, want := config.procFilePath("somefile"), "../some/place/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got) 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) t.Errorf("Expected: %s, Got: %s", want, got)
} }
} }
func TestDefaultSysPath(t *testing.T) { 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 { if got, want := config.Path.sysFilePath("somefile"), "/sys/somefile"; got != want {
t.Fatal(err)
}
if got, want := config.sysFilePath("somefile"), "/sys/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got) 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) t.Errorf("Expected: %s, Got: %s", want, got)
} }
} }
func TestCustomSysPath(t *testing.T) { func TestCustomSysPath(t *testing.T) {
config := PathConfig{} config := newNodeCollectorWithPaths()
if _, err := kingpin.CommandLine.Parse([]string{"--path.sysfs", "./../some/./place/"}); err != nil { path := "./../some/./place/"
t.Fatal(err) 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) 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) 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),
}}
}

View File

@ -83,6 +83,7 @@ func NewProcessStatCollector(config NodeCollectorConfig, logger log.Logger) (Col
"Number of max PIDs limit", nil, nil, "Number of max PIDs limit", nil, nil,
), ),
logger: logger, logger: logger,
config: config,
}, nil }, nil
} }
func (c *processCollector) Update(ch chan<- prometheus.Metric) error { func (c *processCollector) Update(ch chan<- prometheus.Metric) error {

View File

@ -19,22 +19,21 @@ package collector
import ( import (
"testing" "testing"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/prometheus/procfs" "github.com/prometheus/procfs"
) )
func TestReadProcessStatus(t *testing.T) { func TestReadProcessStatus(t *testing.T) {
config := NodeCollectorConfig{} path := "fixtures/proc"
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "fixtures/proc"}); err != nil { config := newNodeCollectorWithPaths()
t.Fatal(err) config.Path.ProcPath = &path
}
want := 1 want := 1
fs, err := procfs.NewFS(*config.Path.ProcPath) fs, err := procfs.NewFS(*config.Path.ProcPath)
if err != nil { if err != nil {
t.Errorf("failed to open procfs: %v", err) 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() pids, states, threads, _, err := c.getAllocatedThreads()
if err != nil { if err != nil {
t.Fatalf("Cannot retrieve data from procfs getAllocatedThreads function: %v ", err) t.Fatalf("Cannot retrieve data from procfs getAllocatedThreads function: %v ", err)

View File

@ -100,7 +100,15 @@ func TestSystemdIgnoreFilter(t *testing.T) {
} }
func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) {
logger := log.NewNopLogger() 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }