Skip to content

Commit

Permalink
Merge pull request #46 from bcwaldon/LinkUnitFiles
Browse files Browse the repository at this point in the history
Implement LinkUnitFiles
  • Loading branch information
bcwaldon committed May 3, 2014
2 parents 2f0efc4 + 7701b63 commit f743bc1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
46 changes: 44 additions & 2 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (c *Conn) GetUnitTypeProperties(unit string, unitType string) (map[string]i
// to modify. properties are the settings to set, encoded as an array of property
// name and value pairs.
func (c *Conn) SetUnitProperties(name string, runtime bool, properties ...Property) error {
return c.sysobj.Call("SetUnitProperties", 0, name, runtime, properties).Store()
return c.sysobj.Call("org.freedesktop.systemd1.Manager.SetUnitProperties", 0, name, runtime, properties).Store()
}

func (c *Conn) GetUnitTypeProperty(unit string, unitType string, propertyName string) (*Property, error) {
Expand Down Expand Up @@ -253,6 +253,48 @@ type UnitStatus struct {
JobPath dbus.ObjectPath // The job object path
}

type LinkUnitFileChange EnableUnitFileChange

// LinkUnitFiles() links unit files (that are located outside of the
// usual unit search paths) into the unit search path.
//
// It takes a list of absolute paths to unit files to link and two
// booleans. The first boolean controls whether the unit shall be
// enabled for runtime only (true, /run), or persistently (false,
// /etc).
// The second controls whether symlinks pointing to other units shall
// be replaced if necessary.
//
// This call returns a list of the changes made. The list consists of
// structures with three strings: the type of the change (one of symlink
// or unlink), the file name of the symlink and the destination of the
// symlink.
func (c *Conn) LinkUnitFiles(files []string, runtime bool, force bool) ([]LinkUnitFileChange, error) {
result := make([][]interface{}, 0)
err := c.sysobj.Call("org.freedesktop.systemd1.Manager.LinkUnitFiles", 0, files, runtime, force).Store(&result)
if err != nil {
return nil, err
}

resultInterface := make([]interface{}, len(result))
for i := range result {
resultInterface[i] = result[i]
}

changes := make([]LinkUnitFileChange, len(result))
changesInterface := make([]interface{}, len(changes))
for i := range changes {
changesInterface[i] = &changes[i]
}

err = dbus.Store(resultInterface, changesInterface...)
if err != nil {
return nil, err
}

return changes, nil
}

// EnableUnitFiles() may be used to enable one or more units in the system (by
// creating symlinks to them in /etc or /run).
//
Expand Down Expand Up @@ -317,7 +359,7 @@ type EnableUnitFileChange struct {
// symlink.
func (c *Conn) DisableUnitFiles(files []string, runtime bool) ([]DisableUnitFileChange, error) {
result := make([][]interface{}, 0)
err := c.sysobj.Call("DisableUnitFiles", 0, files, runtime).Store(&result)
err := c.sysobj.Call("org.freedesktop.systemd1.Manager.DisableUnitFiles", 0, files, runtime).Store(&result)
if err != nil {
return nil, err
}
Expand Down
61 changes: 39 additions & 22 deletions dbus/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,38 @@ func setupConn(t *testing.T) *Conn {
return conn
}

func findFixture(target string, t *testing.T) string {
abs, err := filepath.Abs("../fixtures/" + target)
if err != nil {
t.Fatal(err)
}
return abs
}

func setupUnit(target string, conn *Conn, t *testing.T) {
// Blindly stop the unit in case it is running
conn.StopUnit(target, "replace")

// Blindly remove the symlink in case it exists
targetRun := filepath.Join("/run/systemd/system/", target)
err := os.Remove(targetRun)

// 1. Enable the unit
abs, err := filepath.Abs("../fixtures/" + target)
if err != nil {
t.Fatal(err)
}
os.Remove(targetRun)
}

func linkUnit(target string, conn *Conn, t *testing.T) {
abs := findFixture(target, t)
fixture := []string{abs}

install, changes, err := conn.EnableUnitFiles(fixture, true, true)
changes, err := conn.LinkUnitFiles(fixture, true, true)
if err != nil {
t.Fatal(err)
}

if install != false {
t.Fatal("Install was true")
}

if len(changes) < 1 {
t.Fatalf("Expected one change, got %v", changes)
}

if changes[0].Filename != targetRun {
runPath := filepath.Join("/run/systemd/system/", target)
if changes[0].Filename != runPath {
t.Fatal("Unexpected target filename")
}
}
Expand All @@ -76,6 +78,7 @@ func TestStartStopUnit(t *testing.T) {
conn := setupConn(t)

setupUnit(target, conn, t)
linkUnit(target, conn, t)

// 2. Start the unit
job, err := conn.StartUnit(target, "replace")
Expand Down Expand Up @@ -130,28 +133,41 @@ func TestEnableDisableUnit(t *testing.T) {
conn := setupConn(t)

setupUnit(target, conn, t)
abs := findFixture(target, t)
runPath := filepath.Join("/run/systemd/system/", target)

abs, err := filepath.Abs("../fixtures/" + target)
// 1. Enable the unit
install, changes, err := conn.EnableUnitFiles([]string{abs}, true, true)
if err != nil {
t.Fatal(err)
}

path := filepath.Join("/run/systemd/system/", target)
if install != false {
t.Fatal("Install was true")
}

if len(changes) < 1 {
t.Fatalf("Expected one change, got %v", changes)
}

if changes[0].Filename != runPath {
t.Fatal("Unexpected target filename")
}

// 2. Disable the unit
changes, err := conn.DisableUnitFiles([]string{abs}, true)
dChanges, err := conn.DisableUnitFiles([]string{abs}, true)
if err != nil {
t.Fatal(err)
}

if len(changes) != 1 {
t.Fatalf("Changes should include the path, %v", changes)
if len(dChanges) != 1 {
t.Fatalf("Changes should include the path, %v", dChanges)
}
if changes[0].Filename != path {
t.Fatalf("Change should include correct filename, %+v", changes[0])
if dChanges[0].Filename != runPath {
t.Fatalf("Change should include correct filename, %+v", dChanges[0])
}
if changes[0].Destination != "" {
t.Fatalf("Change destination should be empty, %+v", changes[0])
if dChanges[0].Destination != "" {
t.Fatalf("Change destination should be empty, %+v", dChanges[0])
}
}

Expand Down Expand Up @@ -295,6 +311,7 @@ func TestConnJobListener(t *testing.T) {
conn := setupConn(t)

setupUnit(target, conn, t)
linkUnit(target, conn, t)

jobSize := len(conn.jobListener.jobs)

Expand Down
1 change: 1 addition & 0 deletions dbus/subscription_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestSubscriptionSetUnit(t *testing.T) {

subSet.Add(target)
setupUnit(target, conn, t)
linkUnit(target, conn, t)

job, err := conn.StartUnit(target, "replace")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions dbus/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestSubscribeUnit(t *testing.T) {
evChan, errChan := conn.SubscribeUnits(time.Second)

setupUnit(target, conn, t)
linkUnit(target, conn, t)

job, err := conn.StartUnit(target, "replace")
if err != nil {
Expand Down

0 comments on commit f743bc1

Please sign in to comment.