-
Notifications
You must be signed in to change notification settings - Fork 4
/
settings_test.go
183 lines (157 loc) · 4.62 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
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
package main
import (
"testing"
"encoding/json"
kubewarden_protocol "github.com/kubewarden/policy-sdk-go/protocol"
)
func TestParseValidSettings(t *testing.T) {
settingsJSON := []byte(`
{
"denied_annotations": [ "foo", "bar" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": "cc-\\d+"
}
}`)
settings := Settings{}
err := json.Unmarshal(settingsJSON, &settings)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
expectedDeniedAnnotations := []string{"foo", "bar"}
for _, exp := range expectedDeniedAnnotations {
if !settings.DeniedAnnotations.Contains(exp) {
t.Errorf("Missing denied annotation %s", exp)
}
}
expectedMandatoryAnnotations := []string{"owner"}
for _, exp := range expectedMandatoryAnnotations {
if !settings.MandatoryAnnotations.Contains(exp) {
t.Errorf("Missing mandatory annotation %s", exp)
}
}
re, found := settings.ConstrainedAnnotations["cost-center"]
if !found {
t.Error("Didn't find the expected constrained annotation")
}
expectedRegexp := `cc-\d+`
if re.String() != expectedRegexp {
t.Errorf("Execpted regexp to be %v - got %v instead",
expectedRegexp, re.String())
}
}
func TestParseSettingsWithInvalidRegexp(t *testing.T) {
settingsJSON := []byte(`
{
"denied_annotations": [ "foo", "bar" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": "cc-[a+"
}
}`)
err := json.Unmarshal(settingsJSON, &Settings{})
if err == nil {
t.Errorf("Didn'g get expected error")
}
}
func TestDetectValidSettings(t *testing.T) {
request := `
{
"denied_annotations": [ "foo", "bar" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": "cc-\\d+"
}
}
`
rawRequest := []byte(request)
responsePayload, err := validateSettings(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
var response kubewarden_protocol.SettingsValidationResponse
if err := json.Unmarshal(responsePayload, &response); err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if !response.Valid {
t.Errorf("Expected settings to be valid: %s", *response.Message)
}
}
func TestDetectNotValidSettingsDueToBrokenRegexp(t *testing.T) {
request := `
{
"denied_annotations": [ "foo", "bar" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": "cc-[a+"
}
}
`
rawRequest := []byte(request)
responsePayload, err := validateSettings(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
var response kubewarden_protocol.SettingsValidationResponse
if err := json.Unmarshal(responsePayload, &response); err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if response.Valid {
t.Error("Expected settings to not be valid")
}
if *response.Message != "Provided settings are not valid: error parsing regexp: missing closing ]: `[a+`" {
t.Errorf("Unexpected validation error message: %s", *response.Message)
}
}
func TestDetectNotValidSettingsDueToConflictingDeniedAndConstrainedAnnotations(t *testing.T) {
request := `
{
"denied_annotations": [ "foo", "bar", "cost-center" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": ".*"
}
}
`
rawRequest := []byte(request)
responsePayload, err := validateSettings(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
var response kubewarden_protocol.SettingsValidationResponse
if err := json.Unmarshal(responsePayload, &response); err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if response.Valid {
t.Error("Expected settings to not be valid")
}
if *response.Message != "Provided settings are not valid: These annotations cannot be constrained and denied at the same time: cost-center" {
t.Errorf("Unexpected validation error message: %s", *response.Message)
}
}
func TestDetectNotValidSettingsDueToConflictingDeniedAndMandatoryAnnotations(t *testing.T) {
request := `
{
"denied_annotations": [ "foo", "bar", "owner" ],
"mandatory_annotations": ["owner"],
"constrained_annotations": {
"cost-center": ".*"
}
}
`
rawRequest := []byte(request)
responsePayload, err := validateSettings(rawRequest)
if err != nil {
t.Errorf("Unexpected error %+v", err)
}
var response kubewarden_protocol.SettingsValidationResponse
if err := json.Unmarshal(responsePayload, &response); err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if response.Valid {
t.Error("Expected settings to not be valid")
}
if *response.Message != "Provided settings are not valid: These annotations cannot be mandatory and denied at the same time: owner" {
t.Errorf("Unexpected validation error message: %s", *response.Message)
}
}