Add white/blacklisting of units for systemd collector
This commit is contained in:
parent
428746c48e
commit
e890743f79
2 changed files with 87 additions and 6 deletions
|
|
@ -14,6 +14,7 @@
|
|||
package collector
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/go-systemd/dbus"
|
||||
|
|
@ -47,6 +48,30 @@ func getUnitListFixtures() [][]dbus.UnitStatus {
|
|||
JobType: "",
|
||||
JobPath: "/",
|
||||
},
|
||||
dbus.UnitStatus{
|
||||
Name: "foobar",
|
||||
Description: "bar desc",
|
||||
LoadState: "not-found",
|
||||
ActiveState: "inactive",
|
||||
SubState: "dead",
|
||||
Followed: "",
|
||||
Path: "/org/freedesktop/systemd1/unit/bar",
|
||||
JobId: 0,
|
||||
JobType: "",
|
||||
JobPath: "/",
|
||||
},
|
||||
dbus.UnitStatus{
|
||||
Name: "baz",
|
||||
Description: "bar desc",
|
||||
LoadState: "not-found",
|
||||
ActiveState: "inactive",
|
||||
SubState: "dead",
|
||||
Followed: "",
|
||||
Path: "/org/freedesktop/systemd1/unit/bar",
|
||||
JobId: 0,
|
||||
JobType: "",
|
||||
JobPath: "/",
|
||||
},
|
||||
}
|
||||
|
||||
fixture2 := []dbus.UnitStatus{}
|
||||
|
|
@ -72,3 +97,27 @@ func TestSystemdCollectorDoesntCrash(t *testing.T) {
|
|||
collector.collectUnitStatusMetrics(sink, units)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemdIgnoreFilter(t *testing.T) {
|
||||
fixtures := getUnitListFixtures()
|
||||
whitelistPattern := regexp.MustCompile("foo")
|
||||
blacklistPattern := regexp.MustCompile("bar")
|
||||
filtered := filterUnits(fixtures[0], whitelistPattern, blacklistPattern)
|
||||
for _, unit := range filtered {
|
||||
if blacklistPattern.MatchString(unit.Name) || !whitelistPattern.MatchString(unit.Name) {
|
||||
t.Error(unit.Name, "should not be in the filtered list")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) {
|
||||
c, err := NewSystemdCollector()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixtures := getUnitListFixtures()
|
||||
collector := c.(*systemdCollector)
|
||||
filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern)
|
||||
if len(filtered) != len(fixtures[0]) {
|
||||
t.Error("Default filters removed units")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue