func TestEnableDisableUnit(t *testing.T) {
	target := "enable-disable.service"
	conn := setupConn(t)
	setupUnit(target, conn, t)
	abs := findFixture(target, t)
	runPath := filepath.Join("/run/systemd/system/", target)
	install, changes, err := conn.EnableUnitFiles([]string{abs}, true, true)
	if err != nil {
		t.Fatal(err)
	}
	if install != false {
		t.Log("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")
	}
	dChanges, err := conn.DisableUnitFiles([]string{target}, true)
	if err != nil {
		t.Fatal(err)
	}
	if len(dChanges) != 1 {
		t.Fatalf("Changes should include the path, %v", dChanges)
	}
	if dChanges[0].Filename != runPath {
		t.Fatalf("Change should include correct filename, %+v", dChanges[0])
	}
	if dChanges[0].Destination != "" {
		t.Fatalf("Change destination should be empty, %+v", dChanges[0])
	}
}
Enables a unit and then immediately tears it down
package dbus
import (
	"fmt"
	"math/rand"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"reflect"
	"testing"
	"github.com/godbus/dbus"
)
func setupConn(t *testing.T) *Conn {
	conn, err := New()
	if err != nil {
		t.Fatal(err)
	}
	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) {
	conn.StopUnit(target, "replace", nil)
	targetRun := filepath.Join("/run/systemd/system/", target)
	os.Remove(targetRun)
}
func linkUnit(target string, conn *Conn, t *testing.T) {
	abs := findFixture(target, t)
	fixture := []string{abs}
	changes, err := conn.LinkUnitFiles(fixture, true, true)
	if err != nil {
		t.Fatal(err)
	}
	if len(changes) < 1 {
		t.Fatalf("Expected one change, got %v", changes)
	}
	runPath := filepath.Join("/run/systemd/system/", target)
	if changes[0].Filename != runPath {
		t.Fatal("Unexpected target filename")
	}
}
func getUnitStatus(units []UnitStatus, name string) *UnitStatus {
	for _, u := range units {
		if u.Name == name {
			return &u
		}
	}
	return nil
}
func getUnitFile(units []UnitFile, name string) *UnitFile {
	for _, u := range units {
		if path.Base(u.Path) == name {
			return &u
		}
	}
	return nil
}
func TestStartStopUnit(t *testing.T) {
	target := "start-stop.service"
	conn := setupConn(t)
	setupUnit(target, conn, t)
	linkUnit(target, conn, t)
	reschan := make(chan string)
	_, err := conn.StartUnit(target, "replace", reschan)
	if err != nil {
		t.Fatal(err)
	}
	job := <-reschan
	if job != "done" {
		t.Fatal("Job is not done:", job)
	}
	units, err := conn.ListUnits()
	unit := getUnitStatus(units, target)
	if unit == nil {
		t.Fatalf("Test unit not found in list")
	} else if unit.ActiveState != "active" {
		t.Fatalf("Test unit not active")
	}
	_, err = conn.StopUnit(target, "replace", reschan)
	if err != nil {
		t.Fatal(err)
	}
	<-reschan
	units, err = conn.ListUnits()
	unit = getUnitStatus(units, target)
	if unit != nil {
		t.Fatalf("Test unit found in list, should be stopped")
	}
}
func TestListUnitsByNames(t *testing.T) {
	target1 := "systemd-journald.service"
	target2 := "unexisting.service"
	conn := setupConn(t)
	units, err := conn.ListUnitsByNames([]string{target1, target2})
	if err != nil {
		t.Skip(err)
	}
	unit := getUnitStatus(units, target1)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target1)
	} else if unit.ActiveState != "active" {
		t.Fatalf("%s unit should be active but it is %s", target1, unit.ActiveState)
	}
	unit = getUnitStatus(units, target2)
	if unit == nil {
		t.Fatalf("Unexisting test unit not found in list")
	} else if unit.ActiveState != "inactive" {
		t.Fatalf("Test unit should be inactive")
	}
}
func TestListUnitsByPatterns(t *testing.T) {
	target1 := "systemd-journald.service"
	target2 := "unexisting.service"
	conn := setupConn(t)
	units, err := conn.ListUnitsByPatterns([]string{}, []string{"systemd-journald*", target2})
	if err != nil {
		t.Skip(err)
	}
	unit := getUnitStatus(units, target1)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target1)
	} else if unit.ActiveState != "active" {
		t.Fatalf("Test unit should be active")
	}
	unit = getUnitStatus(units, target2)
	if unit != nil {
		t.Fatalf("Unexisting test unit found in list")
	}
}
func TestListUnitsFiltered(t *testing.T) {
	target := "systemd-journald.service"
	conn := setupConn(t)
	units, err := conn.ListUnitsFiltered([]string{"active"})
	if err != nil {
		t.Fatal(err)
	}
	unit := getUnitStatus(units, target)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target)
	} else if unit.ActiveState != "active" {
		t.Fatalf("Test unit should be active")
	}
	units, err = conn.ListUnitsFiltered([]string{"inactive"})
	if err != nil {
		t.Fatal(err)
	}
	unit = getUnitStatus(units, target)
	if unit != nil {
		t.Fatalf("Inactive unit should not be found in list")
	}
}
func TestListUnitFilesByPatterns(t *testing.T) {
	target1 := "systemd-journald.service"
	target2 := "exit.target"
	conn := setupConn(t)
	units, err := conn.ListUnitFilesByPatterns([]string{"static"}, []string{"systemd-journald*", target2})
	if err != nil {
		t.Skip(err)
	}
	unit := getUnitFile(units, target1)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target1)
	} else if unit.Type != "static" {
		t.Fatalf("Test unit file should be static")
	}
	units, err = conn.ListUnitFilesByPatterns([]string{"disabled"}, []string{"systemd-journald*", target2})
	if err != nil {
		t.Fatal(err)
	}
	unit = getUnitFile(units, target2)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target2)
	} else if unit.Type != "disabled" {
		t.Fatalf("%s unit file should be disabled", target2)
	}
}
func TestListUnitFiles(t *testing.T) {
	target1 := "systemd-journald.service"
	target2 := "exit.target"
	conn := setupConn(t)
	units, err := conn.ListUnitFiles()
	if err != nil {
		t.Fatal(err)
	}
	unit := getUnitFile(units, target1)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target1)
	} else if unit.Type != "static" {
		t.Fatalf("Test unit file should be static")
	}
	unit = getUnitFile(units, target2)
	if unit == nil {
		t.Fatalf("%s unit not found in list", target2)
	} else if unit.Type != "disabled" {
		t.Fatalf("%s unit file should be disabled", target2)
	}
}