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

opa test should show readable errors when equality failed #2546

Closed
grosser opened this issue Jul 15, 2020 · 6 comments
Closed

opa test should show readable errors when equality failed #2546

grosser opened this issue Jul 15, 2020 · 6 comments

Comments

@grosser
Copy link

grosser commented Jul 15, 2020

given a test like this:

test_canonify_cpu_m {
  canonify_cpu("1m") == 2
}
  • the test output (without --verbose) does not tell me what the actual number was
  • the test output with --verbose does not help either (actual output was 1)
data.k8sresourcelimits.test_canonify_cpu_m: FAIL (100.397µs)

  Enter data.k8sresourcelimits.test_canonify_cpu_m = _
  | Enter data.k8sresourcelimits.test_canonify_cpu_m
  | | Enter data.k8sresourcelimits.canonify_cpu
  | | | Fail is_number(orig)
  | | Enter data.k8sresourcelimits.canonify_cpu
  | | | Enter is_number(orig)
  | | | | Fail is_number(orig)
  | | Fail __local290__ = 2
  | | Enter data.k8sresourcelimits.canonify_cpu
  | | | Enter is_number(orig)
  | | | | Fail is_number(orig)
  | | | Fail not endswith(orig, "m")
  | Fail data.k8sresourcelimits.test_canonify_cpu_m = _

please either show "expected 3 to equal 2" (at least in vebose)
or a helper method like equal(a, b) that then generates a readable error message

@tsandall
Copy link
Member

This would be a nice feature. Personally, I write & test inside of VS Code in which case I can just highlight the part of the test that I'm interested in (e.g., canonify_cpu("1m")) and OPA: Evaluate Selection. If you're on the command-line the options would be opa eval (which is a bit low-level) or the REPL.

This is similar to #1581.

@grosser
Copy link
Author

grosser commented Jul 15, 2020

thx, the opa eval helped me build a script I can copy-paste my test-cases into :D

@anderseknert
Copy link
Member

It would be nice if this could use the same kind of logic for dealing with undefined as the upcoming print function, so that assertions failing on undefined values would still print something:

test_something {
    some_rule := result with input as { ... }
    
    result.number == 5
}

The above would then render something like Assertion in test_something failed: <undefined> == 5"

@schneefisch
Copy link

👍

@anderseknert
Copy link
Member

If anyone is looking for this before it's solved in OPA, I wrote a little test assertion library to help with this some time back.

@johanfylling johanfylling self-assigned this Oct 9, 2023
johanfylling added a commit to johanfylling/opa that referenced this issue Jun 12, 2024
Local variable values in test report
------------------------------------

When enabled (`--var-values` flag), the failure report for tests will contain values for variables local to the failing expression. e.g.:

```
FAILURES
--------------------------------------------------------------------------------
data.test.test_foo: FAIL (0ms)

  test.rego:8:
    	x == y + z
    	|    |   |
    	|    |   3
    	|    y + z: 5
    	|    y: 2
    	1

SUMMARY
--------------------------------------------------------------------------------
test.rego:
data.test.test_foo: FAIL (0ms)
--------------------------------------------------------------------------------
FAIL: 1/1
```

Local variable values in pretty trace
-------------------------------------

When enabled on `eval` and `test` commands (`--var-values` flag), local variable values are added to pretty trace:

```
query:1          Enter data.test.p = _                                {}
query:1          | Eval data.test.p = _                               {}
query:1          | Index data.test.p (matched 1 rule, early exit)     {}
/test.rego:4     | Enter data.test.p                                  {}
/test.rego:5     | | Eval x = 1                                       {}
/test.rego:6     | | Eval y = 2                                       {}
/test.rego:7     | | Eval z = 3                                       {}
/test.rego:8     | | Eval minus(z, y, __local3__)                     {y: 2, z: 3}
/test.rego:8     | | Eval x = __local3__                              {__local3__: 1, x: 1}
/test.rego:4     | | Exit data.test.p early                           {}
query:1          | Exit data.test.p = _                               {_: true, data.test.p: true}
query:1          Redo data.test.p = _                                 {_: true, data.test.p: true}
query:1          | Redo data.test.p = _                               {_: true, data.test.p: true}
/test.rego:4     | Redo data.test.p                                   {}
/test.rego:8     | | Redo x = __local3__                              {__local3__: 1, x: 1}
/test.rego:8     | | Redo minus(z, y, __local3__)                     {__local3__: 1, y: 2, z: 3}
/test.rego:7     | | Redo z = 3                                       {z: 3}
/test.rego:6     | | Redo y = 2                                       {y: 2}
/test.rego:5     | | Redo x = 1                                       {x: 1}
```

Fixing: open-policy-agent#2546

Signed-off-by: Johan Fylling <[email protected]>
johanfylling added a commit that referenced this issue Jun 25, 2024
@ashutosh-narkar
Copy link
Member

Fixed in #6815.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

6 participants