Skip to content

Commit 0075dc6

Browse files
committed
update to fix some tests, remove panics, and wrap errors
1 parent 5da4924 commit 0075dc6

File tree

7 files changed

+197
-205
lines changed

7 files changed

+197
-205
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This library aims at providing idiomatic `systemctl` bindings for go developers,
55
This tool tries to take guesswork out of arbitrarily shelling out to `systemctl` by providing a structured, thoroughly-tested wrapper for the `systemctl` functions most-likely to be used in a system program.
66

77
If your system isn't running (or targeting another system running) `systemctl`, this library will be of little use to you.
8-
In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
98

109
## What is systemctl
1110

errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ var (
2121
// Masked units can only be unmasked, but something else was attempted
2222
// Unmask the unit before enabling or disabling it
2323
ErrMasked = errors.New("unit masked")
24-
// If this error occurs, the library isn't entirely useful, as it causes a panic
2524
// Make sure systemctl is in the PATH before calling again
2625
ErrNotInstalled = errors.New("systemctl not in $PATH")
2726
// A unit was expected to be running but was found inactive

errors_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package systemctl
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"runtime"
@@ -63,7 +64,7 @@ func TestErrorFuncs(t *testing.T) {
6364
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
6465
defer cancel()
6566
err := f(ctx, tc.unit, tc.opts)
66-
if err != tc.err {
67+
if !errors.Is(err, tc.err) {
6768
t.Errorf("error is %v, but should have been %v", err, tc.err)
6869
}
6970
})

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/taigrr/systemctl
22

3-
go 1.17
3+
go 1.12

helpers_test.go

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package systemctl
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"syscall"
78
"testing"
@@ -58,13 +59,13 @@ func TestGetStartTime(t *testing.T) {
5859
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
5960
defer cancel()
6061
_, err := GetStartTime(ctx, tc.unit, tc.opts)
61-
if err != tc.err {
62+
if !errors.Is(err, tc.err) {
6263
t.Errorf("error is %v, but should have been %v", err, tc.err)
6364
}
6465
})
6566
}
6667
// 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) {
6869
if userString != "root" && userString != "system" {
6970
t.Skip("skipping superuser test while running as user")
7071
}
@@ -93,12 +94,13 @@ func TestGetStartTime(t *testing.T) {
9394
}
9495

9596
func TestGetNumRestarts(t *testing.T) {
96-
testCases := []struct {
97+
type testCase struct {
9798
unit string
9899
err error
99100
opts Options
100101
runAsUser bool
101-
}{
102+
}
103+
testCases := []testCase{
102104
// Run these tests only as a user
103105

104106
// try nonexistant unit in user mode as user
@@ -118,23 +120,25 @@ func TestGetNumRestarts(t *testing.T) {
118120
{"nginx", nil, Options{UserMode: false}, false},
119121
}
120122
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)
135139
}
136140
// 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) {
138142
if testing.Short() {
139143
t.Skip("skipping in short mode")
140144
}
@@ -154,9 +158,9 @@ func TestGetNumRestarts(t *testing.T) {
154158
}
155159
syscall.Kill(pid, syscall.SIGKILL)
156160
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)
160164
break
161165
} else if running {
162166
break
@@ -173,12 +177,13 @@ func TestGetNumRestarts(t *testing.T) {
173177
}
174178

175179
func TestGetMemoryUsage(t *testing.T) {
176-
testCases := []struct {
180+
type testCase struct {
177181
unit string
178182
err error
179183
opts Options
180184
runAsUser bool
181-
}{
185+
}
186+
testCases := []testCase{
182187
// Run these tests only as a user
183188

184189
// try nonexistant unit in user mode as user
@@ -198,23 +203,25 @@ func TestGetMemoryUsage(t *testing.T) {
198203
{"nginx", nil, Options{UserMode: false}, false},
199204
}
200205
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)
215222
}
216223
// 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) {
218225
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
219226
defer cancel()
220227
bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false})
@@ -232,12 +239,14 @@ func TestGetMemoryUsage(t *testing.T) {
232239
}
233240

234241
func TestGetPID(t *testing.T) {
235-
testCases := []struct {
242+
type testCase struct {
236243
unit string
237244
err error
238245
opts Options
239246
runAsUser bool
240-
}{
247+
}
248+
249+
testCases := []testCase{
241250
// Run these tests only as a user
242251

243252
// try nonexistant unit in user mode as user
@@ -257,22 +266,24 @@ func TestGetPID(t *testing.T) {
257266
{"nginx", nil, Options{UserMode: false}, false},
258267
}
259268
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)
274285
}
275-
t.Run(fmt.Sprintf("prove pid changes"), func(t *testing.T) {
286+
t.Run("prove pid changes", func(t *testing.T) {
276287
if testing.Short() {
277288
t.Skip("skipping in short mode")
278289
}

0 commit comments

Comments
 (0)