refactor(systemdCollector): use dependency injection via Manager interface

Signed-off-by: IbraAoad <Ibrahim.Awwad@canonical.com>
This commit is contained in:
IbraAoad 2025-02-20 16:58:58 +02:00
parent 179c593272
commit 0575acc601
No known key found for this signature in database
2 changed files with 41 additions and 42 deletions

View file

@ -83,8 +83,8 @@ type systemdCollector struct {
var unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"} var unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"}
var getManagerPropertyFunc = func(conn *dbus.Conn, name string) (string, error) { type Manager interface {
return conn.GetManagerProperty(name) GetManagerProperty(prop string) (string, error)
} }
func init() { func init() {
@ -437,8 +437,8 @@ func (c *systemdCollector) collectSummaryMetrics(ch chan<- prometheus.Metric, su
} }
} }
func (c *systemdCollector) collectSystemState(conn *dbus.Conn, ch chan<- prometheus.Metric) error { func (c *systemdCollector) collectSystemState(m Manager, ch chan<- prometheus.Metric) error {
systemState, err := getManagerPropertyFunc(conn, "SystemState") systemState, err := m.GetManagerProperty("SystemState")
if err != nil { if err != nil {
return fmt.Errorf("couldn't get system state: %w", err) return fmt.Errorf("couldn't get system state: %w", err)
} }
@ -506,8 +506,8 @@ func filterUnits(units []unit, includePattern, excludePattern *regexp.Regexp, lo
return filtered return filtered
} }
func (c *systemdCollector) getSystemdVersion(conn *dbus.Conn) (float64, string) { func (c *systemdCollector) getSystemdVersion(m Manager) (float64, string) {
version, err := getManagerPropertyFunc(conn, "Version") version, err := m.GetManagerProperty("Version")
if err != nil { if err != nil {
c.logger.Debug("Unable to get systemd version property, defaulting to 0") c.logger.Debug("Unable to get systemd version property, defaulting to 0")
return 0, "" return 0, ""
@ -523,8 +523,8 @@ func (c *systemdCollector) getSystemdVersion(conn *dbus.Conn) (float64, string)
return v, version return v, version
} }
func (c *systemdCollector) getSystemdVirtualization(conn *dbus.Conn) string { func (c *systemdCollector) getSystemdVirtualization(m Manager) string {
virt, err := getManagerPropertyFunc(conn, "Virtualization") virt, err := m.GetManagerProperty("Virtualization")
if err != nil { if err != nil {
c.logger.Debug("Could not get Virtualization property", "err", err) c.logger.Debug("Could not get Virtualization property", "err", err)
return "unknown" return "unknown"

View file

@ -139,59 +139,58 @@ func testSummaryHelper(t *testing.T, state string, actual float64, expected floa
} }
} }
// fakeManager implements the Manager interface for testing.
type fakeManager struct {
result string
err error
}
// GetManagerProperty returns the controlled result for tests.
func (f *fakeManager) GetManagerProperty(prop string) (string, error) {
return f.result, f.err
}
func Test_systemdCollector_getSystemdVirtualization(t *testing.T) { func Test_systemdCollector_getSystemdVirtualization(t *testing.T) {
type fields struct { logger := slog.New(slog.NewTextHandler(io.Discard, nil))
logger *slog.Logger c, err := NewSystemdCollector(logger)
} if err != nil {
type args struct { t.Fatal(err)
conn *dbus.Conn
} }
sysdCollector := c.(*systemdCollector)
tests := []struct { tests := []struct {
name string name string
fields fields fake *fakeManager
args args want string
mock func(conn *dbus.Conn, name string) (string, error)
want string
}{ }{
{ {
name: "Error", name: "Error",
fields: fields{logger: slog.New(slog.NewTextHandler(io.Discard, nil))}, fake: &fakeManager{
args: args{conn: &dbus.Conn{}}, err: errors.New("test error"),
mock: func(conn *dbus.Conn, name string) (string, error) {
return "", errors.New("test error")
}, },
want: "unknown", want: "unknown",
}, },
{ {
name: "Empty", name: "Empty",
fields: fields{logger: slog.New(slog.NewTextHandler(io.Discard, nil))}, fake: &fakeManager{
args: args{conn: &dbus.Conn{}}, result: `""`,
mock: func(conn *dbus.Conn, name string) (string, error) {
return `""`, nil
}, },
want: "none", want: "none",
}, },
{ {
name: "Valid", name: "Valid",
fields: fields{logger: slog.New(slog.NewTextHandler(io.Discard, nil))}, fake: &fakeManager{
args: args{conn: &dbus.Conn{}}, result: `"kvm"`,
mock: func(conn *dbus.Conn, name string) (string, error) {
return `"kvm"`, nil
}, },
want: "kvm", want: "kvm",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
origFunc := getManagerPropertyFunc got := sysdCollector.getSystemdVirtualization(tt.fake)
defer func() { getManagerPropertyFunc = origFunc }() if got != tt.want {
getManagerPropertyFunc = tt.mock t.Errorf("getSystemdVirtualization() = %v, want %v", got, tt.want)
c := &systemdCollector{
logger: tt.fields.logger,
}
if got := c.getSystemdVirtualization(tt.args.conn); got != tt.want {
t.Errorf("systemdCollector.getSystemdVirtualization() = %v, want %v", got, tt.want)
} }
}) })
} }