generated from kubewarden/go-policy-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate_test.go
303 lines (273 loc) · 6.87 KB
/
validate_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
package main
import (
"encoding/json"
"fmt"
"testing"
corev1 "github.com/kubewarden/k8s-objects/api/core/v1"
metav1 "github.com/kubewarden/k8s-objects/apimachinery/pkg/apis/meta/v1"
kubewarden "github.com/kubewarden/policy-sdk-go"
"github.com/kubewarden/policy-sdk-go/pkg/capabilities/kubernetes"
"github.com/kubewarden/policy-sdk-go/pkg/capabilities/mocks"
kubewarden_protocol "github.com/kubewarden/policy-sdk-go/protocol"
kubewarden_testing "github.com/kubewarden/policy-sdk-go/testing"
)
func TestValidation(t *testing.T) {
cases := []struct {
desc string
namespaceResourceQuota NamespaceResourceQuota
projectResourceQuota ProjectResourceQuota
isValid bool
}{
{
"valid",
NamespaceResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "10",
},
},
ProjectResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "20",
},
UsedLimit: ResourceQuotaLimit{
Pods: "2",
},
},
true,
},
{
"not valid",
NamespaceResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "10",
},
},
ProjectResourceQuota{
Limit: ResourceQuotaLimit{
Pods: "20",
},
UsedLimit: ResourceQuotaLimit{
Pods: "18",
},
},
false,
},
}
for _, tc := range cases {
settings := Settings{}
projectID := "proj-id"
projectNs := "proj-ns"
annotations := make(map[string]string)
annotations[RancherProjectIDAnnotation] = fmt.Sprintf("%s:%s", projectNs, projectID)
namespaceResourceQuotaJSON, err := json.Marshal(tc.namespaceResourceQuota)
if err != nil {
t.Errorf("cannot marshal namespaceResourceQuota to JSON: %v", err)
}
annotations[RancherResourceQuotaAnnotation] = string(namespaceResourceQuotaJSON)
namespace := corev1.Namespace{
Metadata: &metav1.ObjectMeta{
Name: "test-ns",
Annotations: annotations,
},
}
project := Project{
Metadata: &metav1.ObjectMeta{
Name: projectID,
Namespace: projectNs,
},
Spec: &ProjectSpec{
DisplayName: "a project",
Description: "something used by the tests",
ResourceQuota: &tc.projectResourceQuota,
},
}
request, err := json.Marshal(&kubernetes.GetResourceRequest{
APIVersion: RancherProjectAPIVersion,
Kind: RancherProjectKind,
Name: projectID,
Namespace: &projectNs,
DisableCache: true,
})
if err != nil {
t.Errorf("cannot marshall request: %v", err)
}
wapcResponse, err := json.Marshal(&project)
if err != nil {
t.Errorf("cannot create mock client with a Project as payload: %v", err)
}
mockWapcClient := &mocks.MockWapcClient{}
mockWapcClient.On("HostCall", "kubewarden", "kubernetes", "get_resource", request).Return(wapcResponse, nil)
host.Client = mockWapcClient
payload, err := kubewarden_testing.BuildValidationRequest(&namespace, &settings)
if err != nil {
t.Errorf("Unexpected error: %+v", err)
}
responsePayload, err := validate(payload)
if err != nil {
t.Errorf("Unexpected error: %+v", err)
}
var response kubewarden_protocol.ValidationResponse
if err := json.Unmarshal(responsePayload, &response); err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if !response.Accepted && tc.isValid {
message := "no message set"
if response.Message != nil {
message = *response.Message
}
t.Errorf("%s - unexpected rejection: %v", tc.desc, message)
}
if response.Accepted && !tc.isValid {
t.Errorf("%s - should have been rejected", tc.desc)
}
}
}
func TestFindProject(t *testing.T) {
cases := []struct {
desc string
responseObject interface{}
responseError error
expectError *LookupError
}{
{
"No project found",
nil,
nil,
&LookupError{
StatusCode: kubewarden.Code(404),
Message: kubewarden.Message("not relevant"),
},
},
{
"waPC host error",
[]byte{},
fmt.Errorf("something went wrong with waPC host"),
&LookupError{
StatusCode: kubewarden.Code(500),
Message: kubewarden.Message("not relevant"),
},
},
{
"cannot unmarshal project",
[]string{"not", "a", "project"},
nil,
&LookupError{
StatusCode: kubewarden.Code(500),
Message: kubewarden.Message("not relevant"),
},
},
{
"project found",
&Project{
Metadata: &metav1.ObjectMeta{
Name: "a-project",
Namespace: "a-namespace",
},
Spec: &ProjectSpec{
DisplayName: "a project",
Description: "something used by the tests",
},
},
nil,
nil,
},
}
for _, tc := range cases {
projectID := "proj-id"
projectNs := "proj-ns"
request, err := json.Marshal(&kubernetes.GetResourceRequest{
APIVersion: RancherProjectAPIVersion,
Kind: RancherProjectKind,
Name: projectID,
Namespace: &projectNs,
DisableCache: true,
})
if err != nil {
t.Errorf("cannot marshall request: %v", err)
}
wapcResponse := []byte{}
if tc.responseObject != nil {
wapcResponse, err = json.Marshal(tc.responseObject)
if err != nil {
t.Errorf("cannot create mock client with a Project as payload: %v", err)
}
}
mockWapcClient := &mocks.MockWapcClient{}
mockWapcClient.On("HostCall", "kubewarden", "kubernetes", "get_resource", request).Return(wapcResponse, tc.responseError)
host.Client = mockWapcClient
_, lookupErr := findProject(projectID, projectNs)
if lookupErr == nil && tc.expectError != nil {
t.Errorf("%s - didn't get an error as expected", tc.desc)
}
if lookupErr != nil && tc.expectError == nil {
t.Errorf("%s - was not expected to fail with error: %v", tc.desc, lookupErr)
}
if lookupErr != nil && tc.expectError != nil {
if lookupErr.StatusCode != tc.expectError.StatusCode {
t.Errorf("%s - got the wrong status code. Expecting %d, got %d instead", tc.desc, tc.expectError.StatusCode, lookupErr.StatusCode)
}
}
}
}
func TestParseProjectAnnotation(t *testing.T) {
cases := []struct {
desc string
annotation string
projectID string
projectNs string
expectError bool
}{
{
"empty string",
"",
"",
"",
true,
},
{
"projectNs empty",
":ID",
"",
"ID",
true,
},
{
"projectID empty",
"NS:",
"NS",
"",
true,
},
{
"too many chunks",
"NS:ID:not expected",
"NS",
"ID",
true,
},
{
"all good",
"NS:ID",
"NS",
"ID",
false,
},
}
for _, tc := range cases {
projectID, projectNs, err := parseProjectIDAnnotation(tc.annotation)
if tc.expectError && err == nil {
t.Errorf("%s - was supposed to fail", tc.desc)
}
if !tc.expectError && err != nil {
t.Errorf("%s - was not supposed to fail. Got this err: %v", tc.desc, err)
}
if !tc.expectError && err == nil {
if projectID != tc.projectID {
t.Errorf("%s - wrong projectID. Got %s instead of %s", tc.desc, projectID, tc.projectID)
}
if projectNs != tc.projectNs {
t.Errorf("%s - wrong projectNs. Got %s instead of %s", tc.desc, projectNs, tc.projectNs)
}
}
}
}