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

feat: Add JUnit output format for test results #1508

Merged
merged 5 commits into from Apr 5, 2023

Conversation

oguzhand95
Copy link
Member

@oguzhand95 oguzhand95 commented Mar 29, 2023

References

https://github.com/gotestyourself/gotestsum/tree/main/internal/junitxml
https://github.com/testmoapp/junitxml


Example Output (-ojunit)

<testsuites errors="1" failures="2" skipped="4" tests="15">
  <testsuite description="Tests for verifying the company resource policy" name="CompanyTestSuite" file="company_test.yaml" errors="0" failures="2" skipped="4" tests="12">
    <properties></properties>
    <testcase file="company_test.yaml" classname="user.company.create" name="Company Test 1">
      <failure type="RESULT_FAILED">
        <actual>EFFECT_ALLOW</actual>
        <expected>EFFECT_DENY</expected>
      </failure>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.read" name="Company Test 1">
      <failure type="RESULT_FAILED">
        <actual>EFFECT_ALLOW</actual>
        <expected>EFFECT_DENY</expected>
      </failure>
    </testcase>
    <testcase file="company_test.yaml" classname="admin.company.create" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="admin.company.read" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.create" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.read" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
  </testsuite>
  <testsuite description="Tests for verifying the contact resource policy" name="ContactTestSuite" file="contact_test.yaml" errors="0" failures="0" skipped="0" tests="3">
    <properties></properties>
  </testsuite>
  <testsuite description="Tests for verifying the user resource policy" name="UserTestSuite" file="user_test.yaml" errors="1" failures="0" skipped="0" tests="0">
    <error type="RESULT_ERRORED">Invalid test suite: another test named User Test 1 already exists</error>
    <properties></properties>
  </testsuite>
</testsuites>

Example Output (--verbose -ojunit)

<testsuites errors="1" failures="2" skipped="4" tests="15">
  <testsuite description="Tests for verifying the company resource policy" name="CompanyTestSuite" file="company_test.yaml" errors="0" failures="2" skipped="4" tests="12">
    <properties>
      <property name="step[Company Test 1 - user.company.create]">
        <traces>
          <trace id="1">
            <condition>KIND_CONDITION</condition>
            <action>create</action>
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-001</rule>
            <result>true</result>
            <activated>true</activated>
          </trace>
          <trace id="2">
            <action>create</action>
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-001</rule>
            <effect>EFFECT_ALLOW</effect>
            <activated>true</activated>
          </trace>
          <trace id="3">
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-002</rule>
            <message>No matching roles or derived roles</message>
            <skipped>true</skipped>
          </trace>
        </traces>
      </property>
      <property name="step[Company Test 1 - user.company.read]">
        <traces>
          <trace id="1">
            <condition>KIND_CONDITION</condition>
            <action>read</action>
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-001</rule>
            <result>true</result>
            <activated>true</activated>
          </trace>
          <trace id="2">
            <action>read</action>
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-001</rule>
            <effect>EFFECT_ALLOW</effect>
            <activated>true</activated>
          </trace>
          <trace id="3">
            <policy>cerbos.resource.company.vdefault</policy>
            <rule>rule-002</rule>
            <message>No matching roles or derived roles</message>
            <skipped>true</skipped>
          </trace>
        </traces>
      </property>
    </properties>
    <testcase file="company_test.yaml" classname="admin.company.create" name="Company Test 1"></testcase>
    <testcase file="company_test.yaml" classname="admin.company.read" name="Company Test 1"></testcase>
    <testcase file="company_test.yaml" classname="user.company.create" name="Company Test 1">
      <failure type="RESULT_FAILED">
        <actual>EFFECT_ALLOW</actual>
        <expected>EFFECT_DENY</expected>
      </failure>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.read" name="Company Test 1">
      <failure type="RESULT_FAILED">
        <actual>EFFECT_ALLOW</actual>
        <expected>EFFECT_DENY</expected>
      </failure>
    </testcase>
    <testcase file="company_test.yaml" classname="admin.company.create" name="Company Test 2"></testcase>
    <testcase file="company_test.yaml" classname="admin.company.read" name="Company Test 2"></testcase>
    <testcase file="company_test.yaml" classname="user.company.create" name="Company Test 2"></testcase>
    <testcase file="company_test.yaml" classname="user.company.read" name="Company Test 2"></testcase>
    <testcase file="company_test.yaml" classname="admin.company.create" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="admin.company.read" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.create" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
    <testcase file="company_test.yaml" classname="user.company.read" name="Company Test 3">
      <skipped message="This test was skipped"></skipped>
    </testcase>
  </testsuite>
  <testsuite description="Tests for verifying the contact resource policy" name="ContactTestSuite" file="contact_test.yaml" errors="0" failures="0" skipped="0" tests="3">
    <properties></properties>
    <testcase file="contact_test.yaml" classname="admin.contact.create" name="Contact Test 1"></testcase>
    <testcase file="contact_test.yaml" classname="admin.contact.create" name="Contact Test 2"></testcase>
    <testcase file="contact_test.yaml" classname="admin.contact.create" name="Contact Test 3"></testcase>
  </testsuite>
  <testsuite description="Tests for verifying the user resource policy" name="UserTestSuite" file="user_test.yaml" errors="1" failures="0" skipped="0" tests="0">
    <error type="RESULT_ERRORED">Invalid test suite: another test named User Test 1 already exists</error>
    <properties></properties>
  </testsuite>
</testsuites>

@oguzhand95 oguzhand95 added the kind/enhancement New feature or request label Mar 29, 2023
@oguzhand95 oguzhand95 self-assigned this Mar 29, 2023
@codecov
Copy link

codecov bot commented Mar 29, 2023

Codecov Report

Merging #1508 (2c42c61) into main (f40a8a8) will increase coverage by 0.38%.
The diff coverage is 81.90%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1508      +/-   ##
==========================================
+ Coverage   53.68%   54.06%   +0.38%     
==========================================
  Files         121      122       +1     
  Lines       13472    13571      +99     
==========================================
+ Hits         7232     7337     +105     
+ Misses       5618     5609       -9     
- Partials      622      625       +3     
Impacted Files Coverage Δ
internal/policy/policy.go 35.11% <ø> (ø)
internal/verify/junit/junit.go 80.41% <80.41%> (ø)
internal/verify/test_fixture.go 86.28% <100.00%> (+0.04%) ⬆️
internal/verify/verify.go 83.78% <100.00%> (+0.14%) ⬆️

... and 4 files with indirect coverage changes

@charithe
Copy link
Contributor

Nice!

Test suites have a description field so I think that should go into the XML description attribute. There's a filename attribute for storing the file name.

- <testsuite name="company_test.yaml" description="CompanyTestSuite" errors="0" failures="0" skipped="4" tests="12">
+ <testsuite name="CompanyTestSuite" description="My company test" file="company_test.yaml"  errors="0" failures="0" skipped="4" tests="12">

Even testcases have a file attribute so it'd be good to populate that as well.

I think we can use steps to render the execution trace from --verbose.

@oguzhand95 oguzhand95 force-pushed the junit branch 8 times, most recently from a392d73 to d96ced2 Compare April 3, 2023 21:34
@oguzhand95 oguzhand95 marked this pull request as ready for review April 3, 2023 21:35
Copy link
Contributor

@charithe charithe left a comment

Choose a reason for hiding this comment

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

Nice work! There are a couple of small issues to fix:

  • If the output format is set to junit, compile or lint errors don't get displayed.
  • I realise now that the traces require more work because we have to essentially define the schema for how to render them. Therefore, let's remove that feature from this PR. Being able to render the test failures and successes without traces is already a big improvement. We can add traces later in a different PR.

Just FYI, the reason why traces require more work is because of the following:

  • The traces need to go inside the corresponding test case rather than the test suite.
  • I think that the trace output is supposed to look (roughly) like the following. (We need to work out the correct schema first.)
<property="step[cerbos.resource.company.vdefault - rule-001]" value="activated">
   <trace>
      <condition><![CDATA[R.id == "test"]]></condition>
      <action>read</action>
      <policy>cerbos.resource.company.vdefault</policy>
      <rule>rule-001</rule>
      <result>true</result>
      <outcome>activated</outcome>
   </trace>
</property>
<property="step[cerbos.resource.company.vdefault - rule-002]" value="skipped">
   <trace>
      <action>read</action>
      <policy>cerbos.resource.company.vdefault</policy>
      <rule>rule-002</rule>
      <outcome>skipped</outcome>
   </trace>
</property>

cmd/cerbos/compile/internal/compilation/display.go Outdated Show resolved Hide resolved
cmd/cerbos/compile/internal/lint/display.go Outdated Show resolved Hide resolved
@oguzhand95 oguzhand95 force-pushed the junit branch 3 times, most recently from 997f719 to 44cdb67 Compare April 5, 2023 07:27
@charithe charithe changed the title enhancement: Add JUnit output format for test results feat: Add JUnit output format for test results Apr 5, 2023
Copy link
Contributor

@charithe charithe left a comment

Choose a reason for hiding this comment

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

Let's make sure that the test output flag value defaults to the value of the output flag if the former is not explicitly set.

Regarding the problem of distinguishing why the command failed (lint, compile, or test) I think we can return different exit codes when the program terminates. You can override the default exit handler using kong.Exit. So, the exit codes could be the following:

  • 3 - Lint failure
  • 4 - Compile failure
  • 5 - Test failure

Finally, let's update the docs pages to include the new test-output flag and describe how it works.

cmd/cerbos/compile/compile.go Outdated Show resolved Hide resolved
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
@oguzhand95 oguzhand95 force-pushed the junit branch 3 times, most recently from d111f1e to 08f7272 Compare April 5, 2023 14:08
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
Copy link
Contributor

@charithe charithe left a comment

Choose a reason for hiding this comment

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

👍🏽

image

cmd/cerbos/compile/internal/flagset/format.go Outdated Show resolved Hide resolved
docs/modules/cli/pages/cerbos.adoc Outdated Show resolved Hide resolved
cmd/cerbos/compile/compile.go Outdated Show resolved Hide resolved
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
@oguzhand95 oguzhand95 merged commit a38efe6 into cerbos:main Apr 5, 2023
19 of 20 checks passed
@oguzhand95 oguzhand95 deleted the junit branch April 5, 2023 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request kind/feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants