Skip to content

Commit

Permalink
Fix tests - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
marctc committed Sep 27, 2023
1 parent 26fc637 commit 97ddc9b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 70 deletions.
7 changes: 3 additions & 4 deletions collector/diskstats_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion collector/ethtool_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 10 additions & 17 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"testing"

"github.com/go-kit/log"

"github.com/alecthomas/kingpin/v2"
)

func Test_parseFilesystemLabelsError(t *testing.T) {
Expand All @@ -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{
"/": "",
Expand Down Expand Up @@ -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{
"/": "",
Expand All @@ -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)
Expand Down
27 changes: 14 additions & 13 deletions collector/ipvs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 != "<none>" {
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)
Expand Down Expand Up @@ -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
Expand All @@ -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 != "<none>" {
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)
}
Expand Down
57 changes: 29 additions & 28 deletions collector/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}}
}
1 change: 1 addition & 0 deletions collector/processes_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions collector/processes_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion collector/systemd_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 97ddc9b

Please sign in to comment.