generated from kubewarden/go-policy-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings_test.go
157 lines (144 loc) · 3.43 KB
/
settings_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
package main
import (
"testing"
)
func TestParsingSettingsWithAllValuesProvidedFromValidationReq(t *testing.T) {
request := `
{
"request": "doesn't matter here",
"settings": {
"allowedUnsafeSysctls": ["net.core.somaxconn"],
"forbiddenSysctls": ["kernel.shm_rmid_forced", "net.*"]
}
}
`
rawRequest := []byte(request)
settings, err := NewSettingsFromValidationReq(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
expected := []string{"net.core.somaxconn"}
for _, exp := range expected {
if !settings.AllowedUnsafeSysctls.Contains(exp) {
t.Errorf("Missing value %s", exp)
}
}
expected = []string{"kernel.shm_rmid_forced", "net.*"}
for _, exp := range expected {
if !settings.ForbiddenSysctls.Contains(exp) {
t.Errorf("Missing value %s", exp)
}
}
}
func TestParsingSettingsWithNoValueProvided(t *testing.T) {
request := `
{
"request": "doesn't matter here",
"settings": {
}
}
`
rawRequest := []byte(request)
settings, err := NewSettingsFromValidationReq(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
if settings.AllowedUnsafeSysctls.Cardinality() != 0 {
t.Errorf("Expected AllowedUnsafeSysctls to be empty")
}
if settings.ForbiddenSysctls.Cardinality() != 0 {
t.Errorf("Expected ForbiddenSysctls to be empty")
}
}
func TestSettingsAreValid(t *testing.T) {
for _, tcase := range []struct {
name string
request string
wantError bool
error string
}{
{
name: "empty settings",
request: `
{
"request": "doesn't matter here",
"settings": {
}
}
`,
},
{
name: "correct settings",
request: `
{
"request": "doesn't matter here",
"settings": {
"allowedUnsafeSysctls": ["net.core.somaxconn"],
"forbiddenSysctls": ["kernel.shm_rmid_forced", "net.*"]
}
}
`,
},
{
name: "allowedUnsafeSysctls doesn't accept patterns with *",
request: `
{
"request": "doesn't matter here",
"settings": {
"allowedUnsafeSysctls": ["net.*"],
"forbiddenSysctls": ["net.core.somaxconn"]
}
}
`,
wantError: true,
error: "allowedUnsafeSysctls doesn't accept patterns with `*`",
},
{
name: "globs need to be suffix",
request: `
{
"request": "doesn't matter here",
"settings": {
"allowedUnsafeSysctls": ["net.core.somaxconn"],
"forbiddenSysctls": ["kernel.shm_rmid_forced", "net.*.foo"]
}
}
`,
wantError: true,
error: "forbiddenSysctls only accepts patterns with `*` as suffix",
},
{
name: "sysctl in both fields",
request: `
{
"request": "doesn't matter here",
"settings": {
"allowedUnsafeSysctls": ["net.core.somaxconn"],
"forbiddenSysctls": ["net.core.somaxconn"]
}
}
`,
wantError: true,
error: "these sysctls cannot be allowed and forbidden at the same time: net.core.somaxconn",
},
} {
t.Run(tcase.name, func(t *testing.T) {
rawRequest := []byte(tcase.request)
settings, _ := NewSettingsFromValidationReq(rawRequest)
valid, err := settings.Valid()
if err != nil {
if !tcase.wantError {
t.Errorf("on test %q, got unexpected error '%v'", tcase.name, err)
} else {
if tcase.error != err.Error() {
t.Errorf("on test %q, got error '%v', wanted error '%v'", tcase.name, err, tcase.error)
}
}
}
if tcase.wantError && valid {
t.Errorf("on test %q, settings are valid and we got unexpected error '%v'",
tcase.name, err)
}
})
}
}