Skip to content

Commit

Permalink
empty predicates do apply
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Nov 8, 2024
1 parent 8619914 commit a62ad35
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/configuration/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ impl Action {
.compiled_predicates
.get()
.expect("predicates must be compiled by now");
predicates
.iter()
.enumerate()
.all(|(pos, predicate)| match predicate.test() {
Ok(b) => b,
Err(err) => {
error!("Failed to evaluate {}: {}", self.predicates[pos], err);
panic!("Err out of this!")
}
})
predicates.is_empty()
|| predicates
.iter()
.enumerate()
.all(|(pos, predicate)| match predicate.test() {
Ok(b) => b,
Err(err) => {
error!("Failed to evaluate {}: {}", self.predicates[pos], err);
panic!("Err out of this!")
}
})
}

pub fn build_descriptors(&self) -> RepeatedField<RateLimitDescriptor> {
Expand Down Expand Up @@ -121,3 +122,27 @@ impl Action {
Some(res)
}
}

#[cfg(test)]
mod test {
use crate::configuration::action::Action;
use std::cell::OnceCell;

#[test]
fn empty_predicates_do_apply() {
let compiled_predicates = OnceCell::new();
compiled_predicates
.set(Vec::default())
.expect("predicates must not be compiled yet!");

let action = Action {
service: String::from("svc1"),
scope: String::from("sc1"),
predicates: vec![],
compiled_predicates,
data: vec![],
};

assert!(action.conditions_apply())
}
}
21 changes: 21 additions & 0 deletions src/configuration/action_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ impl ActionSet {
})
}
}

#[cfg(test)]
mod test {
use crate::configuration::action_set::ActionSet;

fn build_action_set(name: &str) -> ActionSet {
ActionSet::new(name.to_owned(), Default::default(), Vec::new())
}

#[test]
fn empty_route_rule_conditions_do_apply() {
let action_set_1 = build_action_set("as_1");
action_set_1
.route_rule_conditions
.compiled_predicates
.set(Vec::default())
.expect("Predicates must not be compiled yet!");

assert!(action_set_1.conditions_apply())
}
}

0 comments on commit a62ad35

Please sign in to comment.