@@ -2,6 +2,7 @@ package systemctl
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"syscall"
7
8
"testing"
@@ -58,13 +59,13 @@ func TestGetStartTime(t *testing.T) {
58
59
ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
59
60
defer cancel ()
60
61
_ , err := GetStartTime (ctx , tc .unit , tc .opts )
61
- if err != tc .err {
62
+ if ! errors . Is ( err , tc .err ) {
62
63
t .Errorf ("error is %v, but should have been %v" , err , tc .err )
63
64
}
64
65
})
65
66
}
66
67
// Prove start time changes after a restart
67
- t .Run (fmt . Sprintf ( "prove start time changes" ) , func (t * testing.T ) {
68
+ t .Run ("prove start time changes" , func (t * testing.T ) {
68
69
if userString != "root" && userString != "system" {
69
70
t .Skip ("skipping superuser test while running as user" )
70
71
}
@@ -93,12 +94,13 @@ func TestGetStartTime(t *testing.T) {
93
94
}
94
95
95
96
func TestGetNumRestarts (t * testing.T ) {
96
- testCases := [] struct {
97
+ type testCase struct {
97
98
unit string
98
99
err error
99
100
opts Options
100
101
runAsUser bool
101
- }{
102
+ }
103
+ testCases := []testCase {
102
104
// Run these tests only as a user
103
105
104
106
// try nonexistant unit in user mode as user
@@ -118,23 +120,25 @@ func TestGetNumRestarts(t *testing.T) {
118
120
{"nginx" , nil , Options {UserMode : false }, false },
119
121
}
120
122
for _ , tc := range testCases {
121
- t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
122
- t .Parallel ()
123
- if (userString == "root" || userString == "system" ) && tc .runAsUser {
124
- t .Skip ("skipping user test while running as superuser" )
125
- } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
126
- t .Skip ("skipping superuser test while running as user" )
127
- }
128
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
129
- defer cancel ()
130
- _ , err := GetNumRestarts (ctx , tc .unit , tc .opts )
131
- if err != tc .err {
132
- t .Errorf ("error is %v, but should have been %v" , err , tc .err )
133
- }
134
- })
123
+ func (tc testCase ) {
124
+ t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
125
+ t .Parallel ()
126
+ if (userString == "root" || userString == "system" ) && tc .runAsUser {
127
+ t .Skip ("skipping user test while running as superuser" )
128
+ } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
129
+ t .Skip ("skipping superuser test while running as user" )
130
+ }
131
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
132
+ defer cancel ()
133
+ _ , err := GetNumRestarts (ctx , tc .unit , tc .opts )
134
+ if ! errors .Is (err , tc .err ) {
135
+ t .Errorf ("error is %v, but should have been %v" , err , tc .err )
136
+ }
137
+ })
138
+ }(tc )
135
139
}
136
140
// Prove restart count increases by one after a restart
137
- t .Run (fmt . Sprintf ( "prove restart count increases by one after a restart" ) , func (t * testing.T ) {
141
+ t .Run ("prove restart count increases by one after a restart" , func (t * testing.T ) {
138
142
if testing .Short () {
139
143
t .Skip ("skipping in short mode" )
140
144
}
@@ -154,9 +158,9 @@ func TestGetNumRestarts(t *testing.T) {
154
158
}
155
159
syscall .Kill (pid , syscall .SIGKILL )
156
160
for {
157
- running , err := IsActive (ctx , "nginx" , Options {UserMode : false })
158
- if err != nil {
159
- t .Errorf ("error asserting nginx is up: %v" , err )
161
+ running , errIsActive := IsActive (ctx , "nginx" , Options {UserMode : false })
162
+ if errIsActive != nil {
163
+ t .Errorf ("error asserting nginx is up: %v" , errIsActive )
160
164
break
161
165
} else if running {
162
166
break
@@ -173,12 +177,13 @@ func TestGetNumRestarts(t *testing.T) {
173
177
}
174
178
175
179
func TestGetMemoryUsage (t * testing.T ) {
176
- testCases := [] struct {
180
+ type testCase struct {
177
181
unit string
178
182
err error
179
183
opts Options
180
184
runAsUser bool
181
- }{
185
+ }
186
+ testCases := []testCase {
182
187
// Run these tests only as a user
183
188
184
189
// try nonexistant unit in user mode as user
@@ -198,23 +203,25 @@ func TestGetMemoryUsage(t *testing.T) {
198
203
{"nginx" , nil , Options {UserMode : false }, false },
199
204
}
200
205
for _ , tc := range testCases {
201
- t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
202
- t .Parallel ()
203
- if (userString == "root" || userString == "system" ) && tc .runAsUser {
204
- t .Skip ("skipping user test while running as superuser" )
205
- } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
206
- t .Skip ("skipping superuser test while running as user" )
207
- }
208
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
209
- defer cancel ()
210
- _ , err := GetMemoryUsage (ctx , tc .unit , tc .opts )
211
- if err != tc .err {
212
- t .Errorf ("error is %v, but should have been %v" , err , tc .err )
213
- }
214
- })
206
+ func (tc testCase ) {
207
+ t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
208
+ t .Parallel ()
209
+ if (userString == "root" || userString == "system" ) && tc .runAsUser {
210
+ t .Skip ("skipping user test while running as superuser" )
211
+ } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
212
+ t .Skip ("skipping superuser test while running as user" )
213
+ }
214
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
215
+ defer cancel ()
216
+ _ , err := GetMemoryUsage (ctx , tc .unit , tc .opts )
217
+ if ! errors .Is (err , tc .err ) {
218
+ t .Errorf ("error is %v, but should have been %v" , err , tc .err )
219
+ }
220
+ })
221
+ }(tc )
215
222
}
216
223
// Prove memory usage values change across services
217
- t .Run (fmt . Sprintf ( "prove memory usage values change across services" ) , func (t * testing.T ) {
224
+ t .Run ("prove memory usage values change across services" , func (t * testing.T ) {
218
225
ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
219
226
defer cancel ()
220
227
bytes , err := GetMemoryUsage (ctx , "nginx" , Options {UserMode : false })
@@ -232,12 +239,14 @@ func TestGetMemoryUsage(t *testing.T) {
232
239
}
233
240
234
241
func TestGetPID (t * testing.T ) {
235
- testCases := [] struct {
242
+ type testCase struct {
236
243
unit string
237
244
err error
238
245
opts Options
239
246
runAsUser bool
240
- }{
247
+ }
248
+
249
+ testCases := []testCase {
241
250
// Run these tests only as a user
242
251
243
252
// try nonexistant unit in user mode as user
@@ -257,22 +266,24 @@ func TestGetPID(t *testing.T) {
257
266
{"nginx" , nil , Options {UserMode : false }, false },
258
267
}
259
268
for _ , tc := range testCases {
260
- t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
261
- t .Parallel ()
262
- if (userString == "root" || userString == "system" ) && tc .runAsUser {
263
- t .Skip ("skipping user test while running as superuser" )
264
- } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
265
- t .Skip ("skipping superuser test while running as user" )
266
- }
267
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
268
- defer cancel ()
269
- _ , err := GetPID (ctx , tc .unit , tc .opts )
270
- if err != tc .err {
271
- t .Errorf ("error is %v, but should have been %v" , err , tc .err )
272
- }
273
- })
269
+ func (tc testCase ) {
270
+ t .Run (fmt .Sprintf ("%s as %s" , tc .unit , userString ), func (t * testing.T ) {
271
+ t .Parallel ()
272
+ if (userString == "root" || userString == "system" ) && tc .runAsUser {
273
+ t .Skip ("skipping user test while running as superuser" )
274
+ } else if (userString != "root" && userString != "system" ) && ! tc .runAsUser {
275
+ t .Skip ("skipping superuser test while running as user" )
276
+ }
277
+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
278
+ defer cancel ()
279
+ _ , err := GetPID (ctx , tc .unit , tc .opts )
280
+ if ! errors .Is (err , tc .err ) {
281
+ t .Errorf ("error is %v, but should have been %v" , err , tc .err )
282
+ }
283
+ })
284
+ }(tc )
274
285
}
275
- t .Run (fmt . Sprintf ( "prove pid changes" ) , func (t * testing.T ) {
286
+ t .Run ("prove pid changes" , func (t * testing.T ) {
276
287
if testing .Short () {
277
288
t .Skip ("skipping in short mode" )
278
289
}
0 commit comments