@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "math/rand"
2020 "os"
21+ "path"
2122 "path/filepath"
2223 "reflect"
2324 "testing"
@@ -96,6 +97,7 @@ func TestStartStopUnit(t *testing.T) {
9697 for _ , u := range units {
9798 if u .Name == target {
9899 unit = & u
100+ break
99101 }
100102 }
101103
@@ -122,6 +124,7 @@ func TestStartStopUnit(t *testing.T) {
122124 for _ , u := range units {
123125 if u .Name == target {
124126 unit = & u
127+ break
125128 }
126129 }
127130
@@ -130,6 +133,238 @@ func TestStartStopUnit(t *testing.T) {
130133 }
131134}
132135
136+ // Ensure that ListUnitsByNames works.
137+ func TestListUnitsByNames (t * testing.T ) {
138+ target1 := "systemd-journald.service"
139+ target2 := "unexisting.service"
140+
141+ conn := setupConn (t )
142+
143+ units , err := conn .ListUnitsByNames ([]string {target1 , target2 })
144+
145+ if err != nil {
146+ t .Fatal (err )
147+ }
148+
149+ var unit * UnitStatus
150+ for _ , u := range units {
151+ if u .Name == target1 {
152+ unit = & u
153+ break
154+ }
155+ }
156+
157+ if unit == nil {
158+ t .Fatalf ("%s unit not found in list" , target1 )
159+ }
160+
161+ if unit .ActiveState != "active" {
162+ t .Fatalf ("%s unit should be active but it is %s" , target1 , unit .ActiveState )
163+ }
164+
165+ unit = nil
166+ for _ , u := range units {
167+ if u .Name == target2 {
168+ unit = & u
169+ break
170+ }
171+ }
172+
173+ if unit == nil {
174+ t .Fatalf ("Unexisting test unit not found in list" )
175+ }
176+
177+ if unit .ActiveState != "inactive" {
178+ t .Fatalf ("Test unit should be inactive" )
179+ }
180+ }
181+
182+ // Ensure that ListUnitsByPatterns works.
183+ func TestListUnitsByPatterns (t * testing.T ) {
184+ target1 := "systemd-journald.service"
185+ target2 := "unexisting.service"
186+
187+ conn := setupConn (t )
188+
189+ units , err := conn .ListUnitsByPatterns ([]string {}, []string {"systemd-journald*" , target2 })
190+
191+ if err != nil {
192+ t .Fatal (err )
193+ }
194+
195+ var unit * UnitStatus
196+ for _ , u := range units {
197+ if u .Name == target1 {
198+ unit = & u
199+ break
200+ }
201+ }
202+
203+ if unit == nil {
204+ t .Fatalf ("%s unit not found in list" , target1 )
205+ }
206+
207+ if unit .ActiveState != "active" {
208+ t .Fatalf ("Test unit should be active" )
209+ }
210+
211+ unit = nil
212+ for _ , u := range units {
213+ if u .Name == target2 {
214+ unit = & u
215+ break
216+ }
217+ }
218+
219+ if unit != nil {
220+ t .Fatalf ("Unexisting test unit found in list" )
221+ }
222+ }
223+
224+ // Ensure that ListUnitsFiltered works.
225+ func TestListUnitsFiltered (t * testing.T ) {
226+ target := "systemd-journald.service"
227+
228+ conn := setupConn (t )
229+
230+ units , err := conn .ListUnitsFiltered ([]string {"active" })
231+
232+ if err != nil {
233+ t .Fatal (err )
234+ }
235+
236+ var unit * UnitStatus
237+ for _ , u := range units {
238+ if u .Name == target {
239+ unit = & u
240+ break
241+ }
242+ }
243+
244+ if unit == nil {
245+ t .Fatalf ("%s unit not found in list" , target )
246+ }
247+
248+ if unit .ActiveState != "active" {
249+ t .Fatalf ("Test unit should be active" )
250+ }
251+
252+ units , err = conn .ListUnitsFiltered ([]string {"inactive" })
253+
254+ if err != nil {
255+ t .Fatal (err )
256+ }
257+
258+ unit = nil
259+ for _ , u := range units {
260+ if u .Name == target {
261+ unit = & u
262+ break
263+ }
264+ }
265+
266+ if unit != nil {
267+ t .Fatalf ("Inactive unit should not be found in list" )
268+ }
269+ }
270+
271+ // Ensure that ListUnitFilesByPatterns works.
272+ func TestListUnitFilesByPatterns (t * testing.T ) {
273+ target1 := "systemd-journald.service"
274+ target2 := "exit.target"
275+
276+ conn := setupConn (t )
277+
278+ units , err := conn .ListUnitFilesByPatterns ([]string {"static" }, []string {"systemd-journald*" , target2 })
279+
280+ if err != nil {
281+ t .Fatal (err )
282+ }
283+
284+ var unit * UnitFile
285+ for _ , u := range units {
286+ if path .Base (u .Path ) == target1 {
287+ unit = & u
288+ break
289+ }
290+ }
291+
292+ if unit == nil {
293+ t .Fatalf ("%s unit not found in list" , target1 )
294+ }
295+
296+ if unit .Type != "static" {
297+ t .Fatalf ("Test unit file should be static" )
298+ }
299+
300+ units , err = conn .ListUnitFilesByPatterns ([]string {"disabled" }, []string {"systemd-journald*" , target2 })
301+
302+ if err != nil {
303+ t .Fatal (err )
304+ }
305+
306+ unit = nil
307+ for _ , u := range units {
308+ if path .Base (u .Path ) == target2 {
309+ unit = & u
310+ break
311+ }
312+ }
313+
314+ if unit == nil {
315+ t .Fatalf ("%s unit not found in list" , target2 )
316+ }
317+
318+ if unit .Type != "disabled" {
319+ t .Fatalf ("%s unit file should be disabled" , target2 )
320+ }
321+ }
322+
323+ func TestListUnitFiles (t * testing.T ) {
324+ target1 := "systemd-journald.service"
325+ target2 := "exit.target"
326+
327+ conn := setupConn (t )
328+
329+ units , err := conn .ListUnitFiles ()
330+
331+ if err != nil {
332+ t .Fatal (err )
333+ }
334+
335+ var unit * UnitFile
336+ for _ , u := range units {
337+ if path .Base (u .Path ) == target1 {
338+ unit = & u
339+ break
340+ }
341+ }
342+
343+ if unit == nil {
344+ t .Fatalf ("%s unit not found in list" , target1 )
345+ }
346+
347+ if unit .Type != "static" {
348+ t .Fatalf ("Test unit file should be static" )
349+ }
350+
351+ unit = nil
352+ for _ , u := range units {
353+ if path .Base (u .Path ) == target2 {
354+ unit = & u
355+ break
356+ }
357+ }
358+
359+ if unit == nil {
360+ t .Fatalf ("%s unit not found in list" , target2 )
361+ }
362+
363+ if unit .Type != "disabled" {
364+ t .Fatalf ("%s unit file should be disabled" , target2 )
365+ }
366+ }
367+
133368// Enables a unit and then immediately tears it down
134369func TestEnableDisableUnit (t * testing.T ) {
135370 target := "enable-disable.service"
@@ -302,6 +537,7 @@ func TestStartStopTransientUnit(t *testing.T) {
302537 for _ , u := range units {
303538 if u .Name == target {
304539 unit = & u
540+ break
305541 }
306542 }
307543
@@ -328,6 +564,7 @@ func TestStartStopTransientUnit(t *testing.T) {
328564 for _ , u := range units {
329565 if u .Name == target {
330566 unit = & u
567+ break
331568 }
332569 }
333570
0 commit comments