-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers_test.go
358 lines (337 loc) · 12.1 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package systemctl
import (
"context"
"errors"
"fmt"
"syscall"
"testing"
"time"
)
// Testing assumptions
// - there's no unit installed named `nonexistant`
// - the syncthing unit is available but the binary is not on the tester's system
// this is just what was available on mine, should you want to change it,
// either to something in this repo or more common, feel free to submit a PR.
// - your 'user' isn't root
// - your user doesn't have a PolKit rule allowing access to configure nginx
func TestGetStartTime(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
testCases := []struct {
unit string
err error
opts Options
runAsUser bool
}{
// Run these tests only as a user
// try nonexistant unit in user mode as user
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
// try existing unit in user mode as user
{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
// try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true},
// Run these tests only as a superuser
// try nonexistant unit in system mode as system
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Restart(ctx, "syncthing", Options{UserMode: true})
Stop(ctx, "syncthing", Options{UserMode: true})
time.Sleep(1 * time.Second)
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s as %s, UserMode=%v", tc.unit, userString, tc.opts.UserMode), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := GetStartTime(ctx, tc.unit, tc.opts)
if !errors.Is(err, tc.err) {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}
// Prove start time changes after a restart
t.Run("prove start time changes", func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
startTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting start time of nginx: %v", err)
}
time.Sleep(1 * time.Second)
err = Restart(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue restarting nginx as %s: %v", userString, err)
}
time.Sleep(100 * time.Millisecond)
newStartTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting second start time of nginx: %v", err)
}
diff := newStartTime.Sub(startTime).Seconds()
if diff <= 0 {
t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
}
})
}
func TestGetNumRestarts(t *testing.T) {
type testCase struct {
unit string
err error
opts Options
runAsUser bool
}
testCases := []testCase{
// Run these tests only as a user
// try nonexistant unit in user mode as user
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
// try existing unit in user mode as user
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
// try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true},
// Run these tests only as a superuser
// try nonexistant unit in system mode as system
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
}
for _, tc := range testCases {
func(tc testCase) {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
t.Parallel()
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := GetNumRestarts(ctx, tc.unit, tc.opts)
if !errors.Is(err, tc.err) {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}(tc)
}
// Prove restart count increases by one after a restart
t.Run("prove restart count increases by one after a restart", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
restarts, err := GetNumRestarts(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting number of restarts for nginx: %v", err)
}
pid, err := GetPID(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting MainPID for nginx as %s: %v", userString, err)
}
syscall.Kill(pid, syscall.SIGKILL)
for {
running, errIsActive := IsActive(ctx, "nginx", Options{UserMode: false})
if errIsActive != nil {
t.Errorf("error asserting nginx is up: %v", errIsActive)
break
} else if running {
break
}
}
secondRestarts, err := GetNumRestarts(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting second reading on number of restarts for nginx: %v", err)
}
if restarts+1 != secondRestarts {
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
}
})
}
func TestGetMemoryUsage(t *testing.T) {
type testCase struct {
unit string
err error
opts Options
runAsUser bool
}
testCases := []testCase{
// Run these tests only as a user
// try nonexistant unit in user mode as user
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, true},
// try existing unit in user mode as user
{"syncthing", ErrValueNotSet, Options{UserMode: true}, true},
// try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true},
// Run these tests only as a superuser
// try nonexistant unit in system mode as system
{"nonexistant", ErrValueNotSet, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
}
for _, tc := range testCases {
func(tc testCase) {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
t.Parallel()
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := GetMemoryUsage(ctx, tc.unit, tc.opts)
if !errors.Is(err, tc.err) {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}(tc)
}
// Prove memory usage values change across services
t.Run("prove memory usage values change across services", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting memory usage of nginx: %v", err)
}
secondBytes, err := GetMemoryUsage(ctx, "user.slice", Options{UserMode: false})
if err != nil {
t.Errorf("issue getting second memory usage reading of nginx: %v", err)
}
if bytes == secondBytes {
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
}
})
}
func TestGetUnits(t *testing.T) {
type testCase struct {
err error
runAsUser bool
opts Options
}
testCases := []testCase{{
// Run these tests only as a user
runAsUser: true,
opts: Options{UserMode: true},
err: nil,
}}
for _, tc := range testCases {
t.Run(fmt.Sprintf("as %s", userString), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
units, err := GetUnits(ctx, tc.opts)
if !errors.Is(err, tc.err) {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
if len(units) == 0 {
t.Errorf("Expected at least one unit, but got none")
}
unit := units[0]
if unit.Name == "" {
t.Errorf("Expected unit name to be non-empty, but got empty")
}
if unit.Load == "" {
t.Errorf("Expected unit load state to be non-empty, but got empty")
}
if unit.Active == "" {
t.Errorf("Expected unit active state to be non-empty, but got empty")
}
if unit.Sub == "" {
t.Errorf("Expected unit sub state to be non-empty, but got empty")
}
if unit.Description == "" {
t.Errorf("Expected unit description to be non-empty, but got empty")
}
})
}
}
func TestGetPID(t *testing.T) {
type testCase struct {
unit string
err error
opts Options
runAsUser bool
}
testCases := []testCase{
// Run these tests only as a user
// try nonexistant unit in user mode as user
{"nonexistant", nil, Options{UserMode: false}, true},
// try existing unit in user mode as user
{"syncthing", nil, Options{UserMode: true}, true},
// try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true},
// Run these tests only as a superuser
// try nonexistant unit in system mode as system
{"nonexistant", nil, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
{"nginx", nil, Options{UserMode: false}, false},
}
for _, tc := range testCases {
func(tc testCase) {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
t.Parallel()
if (userString == "root" || userString == "system") && tc.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !tc.runAsUser {
t.Skip("skipping superuser test while running as user")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := GetPID(ctx, tc.unit, tc.opts)
if !errors.Is(err, tc.err) {
t.Errorf("error is %v, but should have been %v", err, tc.err)
}
})
}(tc)
}
t.Run("prove pid changes", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
unit := "nginx"
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Restart(ctx, unit, Options{UserMode: true})
pid, err := GetPID(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("issue getting MainPID for nginx as %s: %v", userString, err)
}
syscall.Kill(pid, syscall.SIGKILL)
secondPid, err := GetPID(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("issue getting second MainPID for nginx as %s: %v", userString, err)
}
if pid == secondPid {
t.Errorf("Expected pid != secondPid, but both were: %d", pid)
}
})
}