Skip to content

Commit

Permalink
dbus: add GetUnitFileState method
Browse files Browse the repository at this point in the history
  • Loading branch information
vaspahomov committed Dec 15, 2020
1 parent 87511f3 commit 3ef922d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,18 @@ func (c *Conn) listJobsInternal(ctx context.Context) ([]JobStatus, error) {

return status, nil
}

// GetUnitFileStateContext returns UnitFileState
func (c *Conn) GetUnitFileStateContext(ctx context.Context, name string) (string, error) {
var prop dbus.Variant
obj := c.sysconn.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
err := obj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.GetUnitFileState", 0, name).Store(&prop)
if err != nil {
return "", err
}
state, ok := prop.Value().(string)
if !ok {
return "", fmt.Errorf("failed to cast UnitFileState prop to string")
}
return state, nil
}
26 changes: 26 additions & 0 deletions dbus/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package dbus

import (
"context"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -1600,3 +1602,27 @@ func TestUnitName(t *testing.T) {
}
}
}

// TestGetUnitFileState reads the `systemd-udevd.service` which should exist on all systemd
// systems and ensures that UnitFileState property is valid.
func TestGetUnitFileState(t *testing.T) {
conn := setupConn(t)
defer conn.Close()
service := "systemd-udevd.service"
got, err := conn.GetUnitFileStateContext(context.Background(), service)
if err != nil {
t.Fatal(err)
}
ok := false
// valid UnitFileState values
validUnitFileStates := []string{"enabled", "disabled", "static", "bad"}
for _, v := range validUnitFileStates {
if got == v {
ok = true
break
}
}
if !ok {
t.Errorf("invalid UnitFileState returned from GetUnitFileState(%s): got %s, want one of [%s]", service, got, strings.Join(validUnitFileStates, ", "))
}
}

0 comments on commit 3ef922d

Please sign in to comment.