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

Policy engine proptests #3985

Merged
merged 7 commits into from
Nov 19, 2020

Conversation

vadim-kovalyov
Copy link
Contributor

In this PR I've added one proptest scenario for Policy engine.

To make that work I had to introduce a method PolicyBuilder::from_definition(definition), that can accept struct, instead of json string.

Proptests guarder by the "proptest" feature.

dmolokanov
dmolokanov previously approved these changes Nov 18, 2020
Copy link
Contributor

@dmolokanov dmolokanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. I have some concerns about changing visibility to pub(crate) for fields of the definition and some other nits.

mqtt/policy/src/core/builder.rs Show resolved Hide resolved
.iter()
.cartesian_product(statement.operations())
.cartesian_product(statement.resources())
.map(|item| Request::new(item.0.0, item.0.1, item.1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something went wrong with formatting :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... there is a known issue with nested tuples and rustfmt which should be fixed in upcoming release. rust-lang/rustfmt#4355


// collect all combos of identity/operation/resource
// in the statement.
let requests = statement.identities()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, cool!

@@ -843,4 +833,98 @@ pub(crate) mod tests {
policy.starts_with(input)
}
}

#[cfg(feature = "proptest")]
mod proptests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to split proptest generators and test themselves and move generators to a separate file?

Copy link
Contributor Author

@vadim-kovalyov vadim-kovalyov Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, but at the moment I don't see a strong reason to do that. It adds more complexity with feature flagging and such..

#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
enum Effect {
enum EffectImpl {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understood this change. You have Effect, EffectImpl, and Decision. What are the differences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effect is a part of policy definition. It can be "undefined" in certain cases. We used to have two Effect enums, in mod.rs and builder.rs. They are a bit different, but probably can be combined together. I have some ideas I'll explore in the following PR.

Decision is a result of policy evaluation. It looks similar, but conceptually a different enum.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please put some docs in the comments for each of them covering differences, especially Effect/EffectImpl?

use itertools::Itertools;

// take very first statement, which should have top priority.
let statement = &definition.statements()[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is possible to receive an empty definition from the customer. Do we want to cover that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible, and in that case every request will return default decision. We don't need to cover that in proptests though, I believe we have a unit test for that.

.cartesian_product(statement.resources())
.map(|item| Request::new(item.0.0, item.0.1, item.1)
.expect("unable to create a request")
).collect::<Vec<_>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
).collect::<Vec<_>>();
);

I believe you don't need to allocate a vec only to iterate it later in for loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to materialize the collection, b/c I can't continue to use borrowed statement after I create a policy from it. Policy consumes the definition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that's right!

.map(|item| Request::new(item.0.0, item.0.1, item.1)
.expect("unable to create a request")
).collect::<Vec<_>>();
let requests = iproduct!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!

Copy link
Contributor

@dmolokanov dmolokanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@kodiakhq kodiakhq bot merged commit d3defcc into Azure:master Nov 19, 2020
vadim-kovalyov added a commit to vadim-kovalyov/iotedge that referenced this pull request Dec 9, 2020
In this PR I've added one proptest scenario for Policy engine.

To make that work I had to introduce a method `PolicyBuilder::from_definition(definition)`, that can accept struct, instead of json string.

Proptests guarder by the "proptest" feature.
gordonwang0 pushed a commit to gordonwang0/iotedge that referenced this pull request Jan 12, 2021
In this PR I've added one proptest scenario for Policy engine.

To make that work I had to introduce a method `PolicyBuilder::from_definition(definition)`, that can accept struct, instead of json string.

Proptests guarder by the "proptest" feature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants