Skip to content

Commit 40a8704

Browse files
test: update tests
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
1 parent 18454b7 commit 40a8704

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

src/evaluation/evaluation_environment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ mod tests {
438438
let validate_request =
439439
ValidateRequest::AdmissionRequest(build_admission_review_request().request);
440440
assert!(matches!(
441-
evaluation_environment.validate(policy_id, &validate_request).err().unwrap(),
441+
evaluation_environment.validate(policy_id, &validate_request).unwrap_err(),
442442
EvaluationError::PolicyInitialization(error) if error == "error"
443443
));
444444
}

src/policy_downloader.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ mod tests {
293293
}
294294

295295
#[test]
296-
fn download_and_verify_success() {
296+
fn verify_success() {
297297
let verification_cfg_yml = r#"---
298298
allOf:
299299
- kind: pubKey
@@ -363,7 +363,7 @@ mod tests {
363363
}
364364

365365
#[test]
366-
fn download_and_verify_error() {
366+
fn verify_error() {
367367
let verification_cfg_yml = r#"---
368368
allOf:
369369
- kind: githubAction
@@ -407,9 +407,11 @@ mod tests {
407407
// be downloaded
408408
assert_eq!(fetched_policies.len(), 1);
409409

410-
assert!(fetched_policies
411-
.get("registry://ghcr.io/kubewarden/tests/pod-privileged:v0.1.9")
412-
.unwrap()
413-
.is_err());
410+
assert!(matches!(
411+
fetched_policies
412+
.get("registry://ghcr.io/kubewarden/tests/pod-privileged:v0.1.9")
413+
.unwrap(),
414+
Err(error) if error.to_string().contains("Policy 'pod-privileged' cannot be verified: Image verification failed: missing signatures")
415+
));
414416
}
415417
}

tests/common/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ pub(crate) async fn app() -> Router {
4747
context_aware_resources: BTreeSet::new(),
4848
},
4949
),
50+
(
51+
"invalid_settings".to_owned(),
52+
Policy {
53+
url: "ghcr.io/kubewarden/tests/sleeping-policy:v0.1.0".to_owned(),
54+
policy_mode: PolicyMode::Protect,
55+
allowed_to_mutate: None,
56+
settings: Some(HashMap::from([(
57+
"sleepMilliseconds".to_owned(),
58+
"abc".into(),
59+
)])),
60+
context_aware_resources: BTreeSet::new(),
61+
},
62+
),
63+
(
64+
"wrong_url".to_owned(),
65+
Policy {
66+
url: "ghcr.io/kubewarden/tests/not_existing:v0.1.0".to_owned(),
67+
policy_mode: PolicyMode::Protect,
68+
allowed_to_mutate: None,
69+
settings: None,
70+
context_aware_resources: BTreeSet::new(),
71+
},
72+
),
5073
]);
5174

5275
let config = Config {

tests/integration_test.rs

+61
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::{
99
use http_body_util::BodyExt;
1010
use policy_evaluator::admission_response::AdmissionResponseStatus;
1111
use policy_server::api::admission_review::AdmissionReviewResponse;
12+
use regex::Regex;
1213
use tower::ServiceExt;
1314

1415
#[tokio::test]
@@ -252,3 +253,63 @@ async fn test_timeout_protection_reject() {
252253
)
253254
);
254255
}
256+
257+
#[tokio::test]
258+
async fn test_policy_with_invalid_settings() {
259+
let app = app().await;
260+
261+
let request = Request::builder()
262+
.method(http::Method::POST)
263+
.header(header::CONTENT_TYPE, "application/json")
264+
.uri("/validate/invalid_settings")
265+
.body(Body::from(include_str!("data/pod_sleep_100ms.json")))
266+
.unwrap();
267+
268+
let response = app.oneshot(request).await.unwrap();
269+
270+
assert_eq!(response.status(), 200);
271+
272+
let admission_review_response: AdmissionReviewResponse =
273+
serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes()).unwrap();
274+
275+
assert!(!admission_review_response.response.allowed);
276+
277+
let pattern =
278+
Regex::new(r"Policy settings are invalid:.*Error decoding validation payload.*invalid type: string.*expected u64.*")
279+
.unwrap();
280+
281+
let status = admission_review_response.response.status.unwrap();
282+
283+
assert_eq!(status.code, Some(500));
284+
assert!(pattern.is_match(&status.message.unwrap()));
285+
}
286+
287+
#[tokio::test]
288+
async fn test_policy_with_wrong_url() {
289+
let app = app().await;
290+
291+
let request = Request::builder()
292+
.method(http::Method::POST)
293+
.header(header::CONTENT_TYPE, "application/json")
294+
.uri("/audit/wrong_url")
295+
.body(Body::from(include_str!("data/pod_sleep_100ms.json")))
296+
.unwrap();
297+
298+
let response = app.oneshot(request).await.unwrap();
299+
300+
assert_eq!(response.status(), 200);
301+
302+
let admission_review_response: AdmissionReviewResponse =
303+
serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes()).unwrap();
304+
305+
assert!(!admission_review_response.response.allowed);
306+
307+
let pattern =
308+
Regex::new(r"Error while downloading policy 'wrong_url' from ghcr.io/kubewarden/tests/not_existing:v0.1.0.*")
309+
.unwrap();
310+
311+
let status = admission_review_response.response.status.unwrap();
312+
313+
assert_eq!(status.code, Some(500));
314+
assert!(pattern.is_match(&status.message.unwrap()));
315+
}

0 commit comments

Comments
 (0)