@@ -258,7 +258,7 @@ fn remote_server_options(matches: &clap::ArgMatches) -> Result<Option<Sources>>
258
258
Ok ( sources)
259
259
}
260
260
261
- #[ derive( Deserialize , Debug , Clone , Default ) ]
261
+ #[ derive( Deserialize , Debug , Clone , Default , PartialEq ) ]
262
262
pub enum PolicyMode {
263
263
#[ serde( rename = "monitor" ) ]
264
264
Monitor ,
@@ -312,7 +312,7 @@ pub enum PolicyOrPolicyGroupSettings {
312
312
}
313
313
314
314
/// `PolicyGroupMember` represents a single policy that is part of a policy group.
315
- #[ derive( Deserialize , Debug , Clone ) ]
315
+ #[ derive( Deserialize , Debug , Clone , PartialEq ) ]
316
316
#[ serde( deny_unknown_fields, rename_all = "camelCase" ) ]
317
317
pub struct PolicyGroupMember {
318
318
/// Thge URL where the policy is located
@@ -332,10 +332,11 @@ impl PolicyGroupMember {
332
332
}
333
333
334
334
/// Describes a policy that can be either an individual policy or a group policy.
335
- #[ derive( Deserialize , Debug , Clone ) ]
336
- #[ serde( untagged, rename_all = "camelCase" ) ]
335
+ #[ derive( Deserialize , Debug , Clone , PartialEq ) ]
336
+ #[ serde( untagged) ]
337
337
pub enum PolicyOrPolicyGroup {
338
338
/// An individual policy
339
+ #[ serde( rename_all = "camelCase" ) ]
339
340
Policy {
340
341
/// The URL where the policy is located
341
342
url : String ,
@@ -351,6 +352,7 @@ pub enum PolicyOrPolicyGroup {
351
352
context_aware_resources : BTreeSet < ContextAwareResource > ,
352
353
} ,
353
354
/// A group of policies that are evaluated together using a given expression
355
+ #[ serde( rename_all = "camelCase" ) ]
354
356
PolicyGroup {
355
357
/// The mode of the policy
356
358
#[ serde( default ) ]
@@ -433,6 +435,89 @@ mod tests {
433
435
use std:: io:: Write ;
434
436
use tempfile:: NamedTempFile ;
435
437
438
+ #[ test]
439
+ fn read_policies_file_test ( ) {
440
+ let policies_yaml = r#"
441
+ ---
442
+ example:
443
+ url: ghcr.io/kubewarden/policies/context-aware-policy:0.1.0
444
+ settings: {}
445
+ allowedToMutate: true
446
+ contextAwareResources:
447
+ - apiVersion: v1
448
+ kind: Namespace
449
+ - apiVersion: v1
450
+ kind: Pod
451
+ group_policy:
452
+ policyMode: monitor
453
+ expression: "true"
454
+ message: "group policy message"
455
+ policies:
456
+ policy1:
457
+ url: ghcr.io/kubewarden/policies/policy1:0.1.0
458
+ settings: {}
459
+ policy2:
460
+ url: ghcr.io/kubewarden/policies/policy2:0.1.0
461
+ settings: {}
462
+ "# ;
463
+
464
+ let mut temp_file = NamedTempFile :: new ( ) . unwrap ( ) ;
465
+ temp_file. write_all ( policies_yaml. as_bytes ( ) ) . unwrap ( ) ;
466
+ let file_path = temp_file. into_temp_path ( ) ;
467
+
468
+ let policies = read_policies_file ( file_path. as_ref ( ) ) . unwrap ( ) ;
469
+
470
+ let expected_policies = HashMap :: from ( [
471
+ (
472
+ "example" . to_owned ( ) ,
473
+ PolicyOrPolicyGroup :: Policy {
474
+ url : "ghcr.io/kubewarden/policies/context-aware-policy:0.1.0" . to_owned ( ) ,
475
+ policy_mode : PolicyMode :: Protect ,
476
+ allowed_to_mutate : Some ( true ) ,
477
+ settings : Some ( HashMap :: new ( ) ) ,
478
+ context_aware_resources : BTreeSet :: from ( [
479
+ ContextAwareResource {
480
+ api_version : "v1" . to_owned ( ) ,
481
+ kind : "Namespace" . to_owned ( ) ,
482
+ } ,
483
+ ContextAwareResource {
484
+ api_version : "v1" . to_owned ( ) ,
485
+ kind : "Pod" . to_owned ( ) ,
486
+ } ,
487
+ ] ) ,
488
+ } ,
489
+ ) ,
490
+ (
491
+ "group_policy" . to_owned ( ) ,
492
+ PolicyOrPolicyGroup :: PolicyGroup {
493
+ policy_mode : PolicyMode :: Monitor ,
494
+ expression : "true" . to_owned ( ) ,
495
+ message : "group policy message" . to_owned ( ) ,
496
+ policies : HashMap :: from ( [
497
+ (
498
+ "policy1" . to_owned ( ) ,
499
+ PolicyGroupMember {
500
+ url : "ghcr.io/kubewarden/policies/policy1:0.1.0" . to_owned ( ) ,
501
+ settings : Some ( HashMap :: new ( ) ) ,
502
+ context_aware_resources : BTreeSet :: new ( ) ,
503
+ } ,
504
+ ) ,
505
+ (
506
+ "policy2" . to_string ( ) ,
507
+ PolicyGroupMember {
508
+ url : "ghcr.io/kubewarden/policies/policy2:0.1.0" . to_owned ( ) ,
509
+ settings : Some ( HashMap :: new ( ) ) ,
510
+ context_aware_resources : BTreeSet :: new ( ) ,
511
+ } ,
512
+ ) ,
513
+ ] ) ,
514
+ } ,
515
+ ) ,
516
+ ] ) ;
517
+
518
+ assert_eq ! ( expected_policies, policies) ;
519
+ }
520
+
436
521
#[ rstest]
437
522
#[ case:: settings_empty(
438
523
r#"
0 commit comments