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) {
empty := ""
config := NodeCollectorConfig{
DiskstatsDeviceFilter: DiskstatsDeviceFilterConfig{
DiskstatsDeviceExclude: &empty,
DiskstatsDeviceInclude: &empty,
OldDiskstatsDeviceExclude: &empty,
DiskstatsDeviceExclude: new(string),
DiskstatsDeviceInclude: new(string),
OldDiskstatsDeviceExclude: new(string),
},
}
sysPath := "fixtures/sys"

View File

@ -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

View File

@ -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)

View File

@ -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 != "<none>" {
args = append(args, "--collector.ipvs.backend-labels="+test.labels)
config.IPVS.Labels = &test.labels
}
if _, err := kingpin.CommandLine.Parse(args); err != nil {
t.Fatal(err)
}
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 != "<none>" {
args = append(args, "--collector.ipvs.backend-labels="+test.labels)
config.IPVS.Labels = &test.labels
}
if _, err := kingpin.CommandLine.Parse(args); err != nil {
t.Fatal(err)
}
collector, err := NewIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger())
collector, err := NewIPVSCollector(config, log.NewNopLogger())
if err != nil {
t.Fatal(err)
}

View File

@ -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),
}}
}

View File

@ -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 {

View File

@ -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)

View File

@ -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)
}