Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky unit test #367

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/policy_evaluator_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ mod tests {
Ok(())
}

fn find_wapc_policy_id(policy: &Policy) -> Option<u64> {
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
map.iter()
.find(|(_, v)| *v == policy)
.map(|(k, _)| k.to_owned())
}

fn is_wapc_instance_registered(policy_id: u64) -> bool {
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
map.get(&policy_id).is_some()
}

#[test]
fn policy_wapc_mapping_is_cleaned_when_the_evaluator_is_dropped() {
// we need a real WASM module, we don't care about the contents yet
Expand All @@ -396,20 +412,13 @@ mod tests {
.execution_mode(PolicyExecutionMode::KubewardenWapc)
.engine(engine)
.policy_module(module);

let evaluator = builder.build().expect("cannot create evaluator");
{
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
assert_eq!(map.len(), 1);
}
let policy_id =
find_wapc_policy_id(&evaluator.policy).expect("cannot find the wapc we just created");

drop(evaluator);
{
let map = WAPC_POLICY_MAPPING
.read()
.expect("cannot get READ access to WAPC_POLICY_MAPPING");
assert_eq!(map.len(), 0);
}
assert!(!is_wapc_instance_registered(policy_id));
}

#[test]
Expand Down