@@ -17,17 +17,109 @@ limitations under the License.
1717package dbus
1818
1919import (
20+ "os"
21+ "path/filepath"
2022 "testing"
2123)
2224
23- // TestActivation forks out a copy of activation.go example and reads back two
24- // strings from the pipes that are passed in.
25- func TestGetUnitProperties (t * testing.T ) {
25+ func setupConn (t * testing.T ) * Conn {
2626 conn , err := New ()
2727 if err != nil {
2828 t .Fatal (err )
2929 }
30- //defer conn.Close()
30+
31+ return conn
32+ }
33+
34+ func setupUnit (target string , conn * Conn , t * testing.T ) {
35+ // Blindly stop the unit in case it is running
36+ conn .StopUnit (target , "replace" )
37+
38+ // Blindly remove the symlink in case it exists
39+ targetRun := filepath .Join ("/run/systemd/system/" , target )
40+ err := os .Remove (targetRun )
41+
42+ // 1. Enable the unit
43+ abs , err := filepath .Abs ("../fixtures/" + target )
44+ if err != nil {
45+ t .Fatal (err )
46+ }
47+
48+ fixture := []string {abs }
49+
50+ install , changes , err := conn .EnableUnitFiles (fixture , true , true )
51+
52+ if install != false {
53+ t .Fatal ("Install was true" )
54+ }
55+
56+ if len (changes ) < 1 {
57+ t .Fatal ("Expected one change, got %v" , changes )
58+ }
59+
60+ if changes [0 ].Filename != targetRun {
61+ t .Fatal ("Unexpected target filename" )
62+ }
63+ }
64+
65+ // Ensure that basic unit starting and stopping works.
66+ func TestStartStopUnit (t * testing.T ) {
67+ target := "start-stop.service"
68+ conn := setupConn (t )
69+
70+ setupUnit (target , conn , t )
71+
72+ // 2. Start the unit
73+ job , err := conn .StartUnit (target , "replace" )
74+ if err != nil {
75+ t .Fatal (err )
76+ }
77+
78+ if job != "done" {
79+ t .Fatal ("Job is not done, %v" , job )
80+ }
81+
82+ units , err := conn .ListUnits ()
83+
84+ var unit * UnitStatus
85+ for _ , u := range units {
86+ if u .Name == target {
87+ unit = & u
88+ }
89+ }
90+
91+ if unit == nil {
92+ t .Fatalf ("Test unit not found in list" )
93+ }
94+
95+ if unit .ActiveState != "active" {
96+ t .Fatalf ("Test unit not active" )
97+ }
98+
99+ // 3. Stop the unit
100+ job , err = conn .StopUnit (target , "replace" )
101+ if err != nil {
102+ t .Fatal (err )
103+ }
104+
105+ units , err = conn .ListUnits ()
106+
107+ unit = nil
108+ for _ , u := range units {
109+ if u .Name == target {
110+ unit = & u
111+ }
112+ }
113+
114+ if unit != nil {
115+ t .Fatalf ("Test unit found in list, should be stopped" )
116+ }
117+ }
118+
119+ // TestGetUnitProperties reads the `-.mount` which should exist on all systemd
120+ // systems and ensures that one of its properties is valid.
121+ func TestGetUnitProperties (t * testing.T ) {
122+ conn := setupConn (t )
31123
32124 unit := "-.mount"
33125
0 commit comments