@@ -71,6 +71,24 @@ func linkUnit(target string, conn *Conn, t *testing.T) {
7171 }
7272}
7373
74+ func getUnitStatus (units []UnitStatus , name string ) * UnitStatus {
75+ for _ , u := range units {
76+ if u .Name == name {
77+ return & u
78+ }
79+ }
80+ return nil
81+ }
82+
83+ func getUnitFile (units []UnitFile , name string ) * UnitFile {
84+ for _ , u := range units {
85+ if path .Base (u .Path ) == name {
86+ return & u
87+ }
88+ }
89+ return nil
90+ }
91+
7492// Ensure that basic unit starting and stopping works.
7593func TestStartStopUnit (t * testing.T ) {
7694 target := "start-stop.service"
@@ -93,19 +111,11 @@ func TestStartStopUnit(t *testing.T) {
93111
94112 units , err := conn .ListUnits ()
95113
96- var unit * UnitStatus
97- for _ , u := range units {
98- if u .Name == target {
99- unit = & u
100- break
101- }
102- }
114+ unit := getUnitStatus (units , target )
103115
104116 if unit == nil {
105117 t .Fatalf ("Test unit not found in list" )
106- }
107-
108- if unit .ActiveState != "active" {
118+ } else if unit .ActiveState != "active" {
109119 t .Fatalf ("Test unit not active" )
110120 }
111121
@@ -120,13 +130,7 @@ func TestStartStopUnit(t *testing.T) {
120130
121131 units , err = conn .ListUnits ()
122132
123- unit = nil
124- for _ , u := range units {
125- if u .Name == target {
126- unit = & u
127- break
128- }
129- }
133+ unit = getUnitStatus (units , target )
130134
131135 if unit != nil {
132136 t .Fatalf ("Test unit found in list, should be stopped" )
@@ -143,38 +147,22 @@ func TestListUnitsByNames(t *testing.T) {
143147 units , err := conn .ListUnitsByNames ([]string {target1 , target2 })
144148
145149 if err != nil {
146- t .Fatal (err )
150+ t .Skip (err )
147151 }
148152
149- var unit * UnitStatus
150- for _ , u := range units {
151- if u .Name == target1 {
152- unit = & u
153- break
154- }
155- }
153+ unit := getUnitStatus (units , target1 )
156154
157155 if unit == nil {
158156 t .Fatalf ("%s unit not found in list" , target1 )
159- }
160-
161- if unit .ActiveState != "active" {
157+ } else if unit .ActiveState != "active" {
162158 t .Fatalf ("%s unit should be active but it is %s" , target1 , unit .ActiveState )
163159 }
164160
165- unit = nil
166- for _ , u := range units {
167- if u .Name == target2 {
168- unit = & u
169- break
170- }
171- }
161+ unit = getUnitStatus (units , target2 )
172162
173163 if unit == nil {
174164 t .Fatalf ("Unexisting test unit not found in list" )
175- }
176-
177- if unit .ActiveState != "inactive" {
165+ } else if unit .ActiveState != "inactive" {
178166 t .Fatalf ("Test unit should be inactive" )
179167 }
180168}
@@ -189,32 +177,18 @@ func TestListUnitsByPatterns(t *testing.T) {
189177 units , err := conn .ListUnitsByPatterns ([]string {}, []string {"systemd-journald*" , target2 })
190178
191179 if err != nil {
192- t .Fatal (err )
180+ t .Skip (err )
193181 }
194182
195- var unit * UnitStatus
196- for _ , u := range units {
197- if u .Name == target1 {
198- unit = & u
199- break
200- }
201- }
183+ unit := getUnitStatus (units , target1 )
202184
203185 if unit == nil {
204186 t .Fatalf ("%s unit not found in list" , target1 )
205- }
206-
207- if unit .ActiveState != "active" {
187+ } else if unit .ActiveState != "active" {
208188 t .Fatalf ("Test unit should be active" )
209189 }
210190
211- unit = nil
212- for _ , u := range units {
213- if u .Name == target2 {
214- unit = & u
215- break
216- }
217- }
191+ unit = getUnitStatus (units , target2 )
218192
219193 if unit != nil {
220194 t .Fatalf ("Unexisting test unit found in list" )
@@ -233,19 +207,11 @@ func TestListUnitsFiltered(t *testing.T) {
233207 t .Fatal (err )
234208 }
235209
236- var unit * UnitStatus
237- for _ , u := range units {
238- if u .Name == target {
239- unit = & u
240- break
241- }
242- }
210+ unit := getUnitStatus (units , target )
243211
244212 if unit == nil {
245213 t .Fatalf ("%s unit not found in list" , target )
246- }
247-
248- if unit .ActiveState != "active" {
214+ } else if unit .ActiveState != "active" {
249215 t .Fatalf ("Test unit should be active" )
250216 }
251217
@@ -255,13 +221,7 @@ func TestListUnitsFiltered(t *testing.T) {
255221 t .Fatal (err )
256222 }
257223
258- unit = nil
259- for _ , u := range units {
260- if u .Name == target {
261- unit = & u
262- break
263- }
264- }
224+ unit = getUnitStatus (units , target )
265225
266226 if unit != nil {
267227 t .Fatalf ("Inactive unit should not be found in list" )
@@ -278,22 +238,14 @@ func TestListUnitFilesByPatterns(t *testing.T) {
278238 units , err := conn .ListUnitFilesByPatterns ([]string {"static" }, []string {"systemd-journald*" , target2 })
279239
280240 if err != nil {
281- t .Fatal (err )
241+ t .Skip (err )
282242 }
283243
284- var unit * UnitFile
285- for _ , u := range units {
286- if path .Base (u .Path ) == target1 {
287- unit = & u
288- break
289- }
290- }
244+ unit := getUnitFile (units , target1 )
291245
292246 if unit == nil {
293247 t .Fatalf ("%s unit not found in list" , target1 )
294- }
295-
296- if unit .Type != "static" {
248+ } else if unit .Type != "static" {
297249 t .Fatalf ("Test unit file should be static" )
298250 }
299251
@@ -303,19 +255,11 @@ func TestListUnitFilesByPatterns(t *testing.T) {
303255 t .Fatal (err )
304256 }
305257
306- unit = nil
307- for _ , u := range units {
308- if path .Base (u .Path ) == target2 {
309- unit = & u
310- break
311- }
312- }
258+ unit = getUnitFile (units , target2 )
313259
314260 if unit == nil {
315261 t .Fatalf ("%s unit not found in list" , target2 )
316- }
317-
318- if unit .Type != "disabled" {
262+ } else if unit .Type != "disabled" {
319263 t .Fatalf ("%s unit file should be disabled" , target2 )
320264 }
321265}
@@ -332,35 +276,19 @@ func TestListUnitFiles(t *testing.T) {
332276 t .Fatal (err )
333277 }
334278
335- var unit * UnitFile
336- for _ , u := range units {
337- if path .Base (u .Path ) == target1 {
338- unit = & u
339- break
340- }
341- }
279+ unit := getUnitFile (units , target1 )
342280
343281 if unit == nil {
344282 t .Fatalf ("%s unit not found in list" , target1 )
345- }
346-
347- if unit .Type != "static" {
283+ } else if unit .Type != "static" {
348284 t .Fatalf ("Test unit file should be static" )
349285 }
350286
351- unit = nil
352- for _ , u := range units {
353- if path .Base (u .Path ) == target2 {
354- unit = & u
355- break
356- }
357- }
287+ unit = getUnitFile (units , target2 )
358288
359289 if unit == nil {
360290 t .Fatalf ("%s unit not found in list" , target2 )
361- }
362-
363- if unit .Type != "disabled" {
291+ } else if unit .Type != "disabled" {
364292 t .Fatalf ("%s unit file should be disabled" , target2 )
365293 }
366294}
@@ -533,19 +461,11 @@ func TestStartStopTransientUnit(t *testing.T) {
533461
534462 units , err := conn .ListUnits ()
535463
536- var unit * UnitStatus
537- for _ , u := range units {
538- if u .Name == target {
539- unit = & u
540- break
541- }
542- }
464+ unit := getUnitStatus (units , target )
543465
544466 if unit == nil {
545467 t .Fatalf ("Test unit not found in list" )
546- }
547-
548- if unit .ActiveState != "active" {
468+ } else if unit .ActiveState != "active" {
549469 t .Fatalf ("Test unit not active" )
550470 }
551471
@@ -560,13 +480,7 @@ func TestStartStopTransientUnit(t *testing.T) {
560480
561481 units , err = conn .ListUnits ()
562482
563- unit = nil
564- for _ , u := range units {
565- if u .Name == target {
566- unit = & u
567- break
568- }
569- }
483+ unit = getUnitStatus (units , target )
570484
571485 if unit != nil {
572486 t .Fatalf ("Test unit found in list, should be stopped" )
0 commit comments