-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patherrors_test.go
More file actions
124 lines (121 loc) · 4.21 KB
/
Copy patherrors_test.go
File metadata and controls
124 lines (121 loc) · 4.21 KB
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
package systemctl
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func TestErrorFuncs(t *testing.T) {
portableUserUnit := userTestUnit(t)
testCases := []struct {
name string
fn func(ctx context.Context, unit string, opts Options) error
lifecycle bool
errCases []struct {
unit string
err error
opts Options
runAsUser bool
}
}{
{
name: "Enable",
fn: func(ctx context.Context, unit string, opts Options) error { return Enable(ctx, unit, opts) },
lifecycle: false,
errCases: []struct {
unit string
err error
opts Options
runAsUser bool
}{
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
{portableUserUnit, nil, Options{UserMode: true}, true},
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
{"nginx", nil, Options{UserMode: false}, false},
},
},
{
name: "Disable",
fn: func(ctx context.Context, unit string, opts Options) error { return Disable(ctx, unit, opts) },
lifecycle: false,
errCases: []struct {
unit string
err error
opts Options
runAsUser bool
}{
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
{portableUserUnit, nil, Options{UserMode: true}, true},
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
{"nginx", nil, Options{UserMode: false}, false},
},
},
{
name: "Restart",
fn: func(ctx context.Context, unit string, opts Options) error { return Restart(ctx, unit, opts) },
lifecycle: true,
errCases: []struct {
unit string
err error
opts Options
runAsUser bool
}{
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
{portableUserUnit, nil, Options{UserMode: true}, true},
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
{"nginx", nil, Options{UserMode: false}, false},
},
},
{
name: "Start",
fn: func(ctx context.Context, unit string, opts Options) error { return Start(ctx, unit, opts) },
lifecycle: true,
errCases: []struct {
unit string
err error
opts Options
runAsUser bool
}{
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
{portableUserUnit, nil, Options{UserMode: true}, true},
{"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true},
{"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false},
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
{"nginx", nil, Options{UserMode: false}, false},
},
},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Errorcheck %s", tc.name), func(t *testing.T) {
for _, errCase := range tc.errCases {
t.Run(fmt.Sprintf("%s as %s", errCase.unit, userString), func(t *testing.T) {
if (userString == "root" || userString == "system") && errCase.runAsUser {
t.Skip("skipping user test while running as superuser")
} else if (userString != "root" && userString != "system") && !errCase.runAsUser {
t.Skip("skipping superuser test while running as user")
}
if tc.lifecycle && errCase.runAsUser && errCase.opts.UserMode {
requireUserBus(t)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := tc.fn(ctx, errCase.unit, errCase.opts)
if !errors.Is(err, errCase.err) {
t.Errorf("error is %v, but should have been %v", err, errCase.err)
}
})
}
})
}
}