1
1
mod common;
2
2
3
+ use std:: collections:: { BTreeSet , HashMap } ;
4
+
3
5
use common:: app;
4
6
5
7
use axum:: {
6
8
body:: Body ,
7
9
http:: { self , header, Request } ,
8
10
} ;
9
11
use http_body_util:: BodyExt ;
10
- use policy_evaluator:: admission_response:: AdmissionResponseStatus ;
11
- use policy_server:: api:: admission_review:: AdmissionReviewResponse ;
12
+ use policy_evaluator:: {
13
+ admission_response:: AdmissionResponseStatus ,
14
+ policy_fetcher:: verify:: config:: VerificationConfigV1 ,
15
+ } ;
16
+ use policy_server:: {
17
+ api:: admission_review:: AdmissionReviewResponse ,
18
+ config:: { Policy , PolicyMode } ,
19
+ } ;
12
20
use regex:: Regex ;
13
21
use tower:: ServiceExt ;
14
22
23
+ use crate :: common:: default_test_config;
24
+
15
25
#[ tokio:: test]
16
26
async fn test_validate ( ) {
17
- let app = app ( ) . await ;
27
+ let config = default_test_config ( ) ;
28
+ let app = app ( config) . await ;
18
29
19
30
let request = Request :: builder ( )
20
31
. method ( http:: Method :: POST )
@@ -46,7 +57,8 @@ async fn test_validate() {
46
57
47
58
#[ tokio:: test]
48
59
async fn test_validate_policy_not_found ( ) {
49
- let app = app ( ) . await ;
60
+ let config = default_test_config ( ) ;
61
+ let app = app ( config) . await ;
50
62
51
63
let request = Request :: builder ( )
52
64
. method ( http:: Method :: POST )
@@ -64,7 +76,8 @@ async fn test_validate_policy_not_found() {
64
76
65
77
#[ tokio:: test]
66
78
async fn test_validate_invalid_payload ( ) {
67
- let app = app ( ) . await ;
79
+ let config = default_test_config ( ) ;
80
+ let app = app ( config) . await ;
68
81
69
82
let request = Request :: builder ( )
70
83
. method ( http:: Method :: POST )
@@ -80,7 +93,8 @@ async fn test_validate_invalid_payload() {
80
93
81
94
#[ tokio:: test]
82
95
async fn test_validate_raw ( ) {
83
- let app = app ( ) . await ;
96
+ let config = default_test_config ( ) ;
97
+ let app = app ( config) . await ;
84
98
85
99
let request = Request :: builder ( )
86
100
. method ( http:: Method :: POST )
@@ -107,7 +121,8 @@ async fn test_validate_raw() {
107
121
108
122
#[ tokio:: test]
109
123
async fn test_validate_raw_policy_not_found ( ) {
110
- let app = app ( ) . await ;
124
+ let config = default_test_config ( ) ;
125
+ let app = app ( config) . await ;
111
126
112
127
let request = Request :: builder ( )
113
128
. method ( http:: Method :: POST )
@@ -125,7 +140,8 @@ async fn test_validate_raw_policy_not_found() {
125
140
126
141
#[ tokio:: test]
127
142
async fn test_validate_raw_invalid_payload ( ) {
128
- let app = app ( ) . await ;
143
+ let config = default_test_config ( ) ;
144
+ let app = app ( config) . await ;
129
145
130
146
let request = Request :: builder ( )
131
147
. method ( http:: Method :: POST )
@@ -141,7 +157,8 @@ async fn test_validate_raw_invalid_payload() {
141
157
142
158
#[ tokio:: test]
143
159
async fn test_audit ( ) {
144
- let app = app ( ) . await ;
160
+ let config = default_test_config ( ) ;
161
+ let app = app ( config) . await ;
145
162
146
163
let request = Request :: builder ( )
147
164
. method ( http:: Method :: POST )
@@ -171,7 +188,8 @@ async fn test_audit() {
171
188
172
189
#[ tokio:: test]
173
190
async fn test_audit_policy_not_found ( ) {
174
- let app = app ( ) . await ;
191
+ let config = default_test_config ( ) ;
192
+ let app = app ( config) . await ;
175
193
176
194
let request = Request :: builder ( )
177
195
. method ( http:: Method :: POST )
@@ -189,7 +207,8 @@ async fn test_audit_policy_not_found() {
189
207
190
208
#[ tokio:: test]
191
209
async fn test_audit_invalid_payload ( ) {
192
- let app = app ( ) . await ;
210
+ let config = default_test_config ( ) ;
211
+ let app = app ( config) . await ;
193
212
194
213
let request = Request :: builder ( )
195
214
. method ( http:: Method :: POST )
@@ -205,7 +224,8 @@ async fn test_audit_invalid_payload() {
205
224
206
225
#[ tokio:: test]
207
226
async fn test_timeout_protection_accept ( ) {
208
- let app = app ( ) . await ;
227
+ let config = default_test_config ( ) ;
228
+ let app = app ( config) . await ;
209
229
210
230
let request = Request :: builder ( )
211
231
. method ( http:: Method :: POST )
@@ -226,7 +246,8 @@ async fn test_timeout_protection_accept() {
226
246
227
247
#[ tokio:: test]
228
248
async fn test_timeout_protection_reject ( ) {
229
- let app = app ( ) . await ;
249
+ let config = default_test_config ( ) ;
250
+ let app = app ( config) . await ;
230
251
231
252
let request = Request :: builder ( )
232
253
. method ( http:: Method :: POST )
@@ -256,7 +277,23 @@ async fn test_timeout_protection_reject() {
256
277
257
278
#[ tokio:: test]
258
279
async fn test_policy_with_invalid_settings ( ) {
259
- let app = app ( ) . await ;
280
+ let mut config = default_test_config ( ) ;
281
+ config. policies . insert (
282
+ "invalid_settings" . to_owned ( ) ,
283
+ Policy {
284
+ url : "ghcr.io/kubewarden/tests/sleeping-policy:v0.1.0" . to_owned ( ) ,
285
+ policy_mode : PolicyMode :: Protect ,
286
+ allowed_to_mutate : None ,
287
+ settings : Some ( HashMap :: from ( [ (
288
+ "sleepMilliseconds" . to_owned ( ) ,
289
+ "abc" . into ( ) ,
290
+ ) ] ) ) ,
291
+ context_aware_resources : BTreeSet :: new ( ) ,
292
+ } ,
293
+ ) ;
294
+ config. continue_on_errors = true ;
295
+
296
+ let app = app ( config) . await ;
260
297
261
298
let request = Request :: builder ( )
262
299
. method ( http:: Method :: POST )
@@ -286,7 +323,20 @@ async fn test_policy_with_invalid_settings() {
286
323
287
324
#[ tokio:: test]
288
325
async fn test_policy_with_wrong_url ( ) {
289
- let app = app ( ) . await ;
326
+ let mut config = default_test_config ( ) ;
327
+ config. policies . insert (
328
+ "wrong_url" . to_owned ( ) ,
329
+ Policy {
330
+ url : "ghcr.io/kubewarden/tests/not_existing:v0.1.0" . to_owned ( ) ,
331
+ policy_mode : PolicyMode :: Protect ,
332
+ allowed_to_mutate : None ,
333
+ settings : None ,
334
+ context_aware_resources : BTreeSet :: new ( ) ,
335
+ } ,
336
+ ) ;
337
+ config. continue_on_errors = true ;
338
+
339
+ let app = app ( config) . await ;
290
340
291
341
let request = Request :: builder ( )
292
342
. method ( http:: Method :: POST )
0 commit comments