-
Notifications
You must be signed in to change notification settings - Fork 111
feat(mindev): add --junit-file flag to test command and add Starlark testing docs #6690
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,6 +193,73 @@ For more information on the rego print statement, the following blog post is a | |
| good resource: | ||
| [Introducing the OPA print function](https://blog.openpolicyagent.org/introducing-the-opa-print-function-809da6a13aee) | ||
|
|
||
| ## Testing with Starlark | ||
|
|
||
| In addition to evaluating rules against a single entity, `mindev test` allows you to run comprehensive Starlark-based tests against your rule types. This is the recommended way to verify rule behavior, as it lets you mock external systems like GitHub REST/GraphQL APIs and Git filesystems. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two comments:
|
||
|
|
||
| You can run your Starlark tests using the following command: | ||
|
|
||
| ```bash | ||
| mindev test /path/to/test.star | ||
| ``` | ||
|
|
||
| By default, `mindev test .` will recursively find and run all `*_test.star` files in the current directory and its subdirectories. | ||
|
|
||
| ### Writing Starlark Tests | ||
|
|
||
| Test files use Starlark (a Python-like configuration language). A basic test file looks like this: | ||
|
|
||
| ```python | ||
| # test_rule.star | ||
| def test_valid_case(): | ||
| # 1. Define the input entity | ||
| entity = { | ||
| "repo_name": "example", | ||
| "repo_owner": "org" | ||
| } | ||
|
|
||
| # 2. Mock external calls if your rule uses the `rest` or `git` ingest types | ||
| def mock_http(content): | ||
| return { | ||
| "/repos/org/example/branches/main/protection": { | ||
| "enforce_admins": {"enabled": True} | ||
| } | ||
| } | ||
|
|
||
| # 3. Call the `eval` builtin to run the rule | ||
| result = eval( | ||
| rule="rule-types/github/branch_protection.yaml", | ||
| entity=entity, | ||
| mock_http=mock_http | ||
| ) | ||
|
|
||
| # 4. Assert the result | ||
| assert result["status"] == "success", "Expected rule to pass" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the expanded page, we should include information about what entries are in the "result" object, as well as the input arguments to "eval". |
||
|
|
||
| def test_failure_case(): | ||
| # ... mock the API to return invalid configuration ... | ||
| result = eval(...) | ||
| assert result["status"] == "error", "Expected rule to fail" | ||
| ``` | ||
|
|
||
| The `eval` builtin takes several optional arguments: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these arguments are not optional (at a minimum, the "rule" argument, and maybe the "entity" argument). |
||
| - `rule`: The path to the rule type YAML file | ||
| - `entity`: The entity object to evaluate (defaults to an empty entity if omitted) | ||
| - `profile`: A profile dictionary or path to evaluate the rule in the context of a profile | ||
| - `params`: Rule parameters dictionary (matches the `param_schema`) | ||
| - `mock_http`: A function that receives the request content and returns a dictionary of URL paths to mocked JSON responses | ||
| - `mock_fs`: A function or dictionary to mock filesystem contents for `git` ingest | ||
|
|
||
| ### JUnit Output | ||
|
|
||
| If you want to use the test output in CI/CD pipelines, you can generate a JUnit XML report: | ||
|
|
||
| ```bash | ||
| mindev test . --junit-file report.xml | ||
| ``` | ||
|
|
||
| This will write the standard text output to your terminal, and output a structured `report.xml` file containing the test results. | ||
|
|
||
| ## Conclusion | ||
|
|
||
| Mindev is a powerful tool that helps you develop and debug rule types for | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to add this usage here, but we should think about a standalone page describing "how to test rules".
This would cover both manual and automated (CI) testing, including referencing the action and examples of setup, as well as some hints on effective testing usage, and documentation of the custom functions added to the starlark runtime.