Skip to content

Commit

Permalink
enhancement: Add JUnit output format for test results
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
  • Loading branch information
oguzhand95 committed Apr 3, 2023
1 parent a5c6b10 commit d96ced2
Show file tree
Hide file tree
Showing 50 changed files with 1,383 additions and 337 deletions.
4 changes: 4 additions & 0 deletions api/genpb/cerbos/policy/v1/hashpb_helpers.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 77 additions & 67 deletions api/genpb/cerbos/policy/v1/policy.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/genpb/cerbos/policy/v1/policy.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions api/genpb/cerbos/policy/v1/policy_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/genpb/cerbos/private/v1/hashpb_helpers.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/genpb/cerbos/response/v1/hashpb_helpers.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/public/cerbos/policy/v1/policy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ message TestResults {
Summary summary = 4;
string error = 5;
repeated TestCase test_cases = 6;
string description = 7;
}

message TestCase {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cerbos/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Cmd struct { //nolint:govet // Kong prints fields in order, so we don't wan
Tests string `help:"Path to the directory containing tests. Defaults to policy directory." type:"existingdir"`
RunRegex string `help:"Run only tests that match this regex" name:"run"`
SkipTests bool `help:"Skip tests"`
Output flagset.OutputFormat `help:"Output format (${enum})" default:"tree" enum:"tree,list,json" short:"o"`
Output flagset.OutputFormat `help:"Output format (${enum})" default:"tree" enum:"tree,list,json,junit" short:"o"`
Color *outputcolor.Level `help:"Output color level (auto,never,always,256,16m). Defaults to auto." xor:"color"`
NoColor bool `help:"Disable colored output" xor:"color"`
Verbose bool `help:"Verbose output on test failure"`
Expand Down
4 changes: 4 additions & 0 deletions cmd/cerbos/compile/internal/compilation/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package compilation

import (
"fmt"

internalerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/errors"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/flagset"
"github.com/cerbos/cerbos/internal/compile"
Expand All @@ -18,6 +20,8 @@ func Display(p *printer.Printer, errs compile.ErrorList, output flagset.OutputFo
return displayJSON(p, errs, colorLevel)
case flagset.OutputFormatList, flagset.OutputFormatTree:
return displayList(p, errs)
case flagset.OutputFormatJUnit:
return fmt.Errorf("invalid option %s", flagset.OutputFormatJUnit)
}

return internalerrors.ErrFailed
Expand Down
7 changes: 4 additions & 3 deletions cmd/cerbos/compile/internal/flagset/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package flagset
type OutputFormat string

const (
OutputFormatTree OutputFormat = "tree"
OutputFormatList OutputFormat = "list"
OutputFormatJSON OutputFormat = "json"
OutputFormatTree OutputFormat = "tree"
OutputFormatList OutputFormat = "list"
OutputFormatJSON OutputFormat = "json"
OutputFormatJUnit OutputFormat = "junit"
)
4 changes: 4 additions & 0 deletions cmd/cerbos/compile/internal/lint/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package lint

import (
"fmt"

internalerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/errors"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/flagset"
"github.com/cerbos/cerbos/internal/outputcolor"
Expand All @@ -18,6 +20,8 @@ func Display(p *printer.Printer, errs *index.BuildError, output flagset.OutputFo
return displayJSON(p, errs, colorLevel)
case flagset.OutputFormatList, flagset.OutputFormatTree:
return displayList(p, errs)
case flagset.OutputFormatJUnit:
return fmt.Errorf("invalid option %s", flagset.OutputFormatJUnit)
}

return internalerrors.ErrFailed
Expand Down
20 changes: 20 additions & 0 deletions cmd/cerbos/compile/internal/verification/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package verification

import (
"encoding/xml"
"fmt"
"strings"

"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/cerbos/cerbos/internal/outputcolor"
"github.com/cerbos/cerbos/internal/printer"
"github.com/cerbos/cerbos/internal/printer/colored"
"github.com/cerbos/cerbos/internal/verify/junit"
)

const (
Expand Down Expand Up @@ -52,11 +55,28 @@ func Display(p *printer.Printer, results *policyv1.TestResults, output flagset.O
return displayTree(p, pterm.DefaultTree, results, verbose)
case flagset.OutputFormatList:
return displayTree(p, pterm.TreePrinter{Indent: listIndent, VerticalString: " "}, results, verbose)
case flagset.OutputFormatJUnit:
return displayJUnit(p, results, verbose)
default:
return nil
}
}

func displayJUnit(p *printer.Printer, results *policyv1.TestResults, verbose bool) error {
r, err := junit.Build(results, verbose)
if err != nil {
return fmt.Errorf("failed to build JUnit XML: %w", err)
}

output, err := xml.MarshalIndent(r, "", strings.Repeat(" ", listIndent))
if err != nil {
return fmt.Errorf("failed to marshal xml: %w", err)
}

p.Println(string(output))
return nil
}

func displayTree(p *printer.Printer, tp pterm.TreePrinter, results *policyv1.TestResults, verbose bool) error {
output := buildTestOutput(results, verbose)

Expand Down
1 change: 0 additions & 1 deletion internal/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func WithStoreIdentifier(p *policyv1.Policy, storeIdentifier string) *policyv1.P
p.Metadata = &policyv1.Metadata{}
}

//nolint:staticcheck
p.Metadata.StoreIdentifer = storeIdentifier // TODO: Remove this after deprecated StoreIdentifer no longer exists
p.Metadata.StoreIdentifier = storeIdentifier

Expand Down
101 changes: 51 additions & 50 deletions internal/test/testdata/verify/cases/case_001.yaml.golden
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
{
"suites": [
"suites": [
{
"file": "suite_test.yaml",
"name": "TestSuite",
"summary": {
"overallResult": "RESULT_PASSED",
"testsCount": 5,
"resultCounts": [
"file": "suite_test.yaml",
"name": "TestSuite",
"summary": {
"overallResult": "RESULT_PASSED",
"testsCount": 5,
"resultCounts": [
{
"result": "RESULT_PASSED",
"count": 5
"result": "RESULT_PASSED",
"count": 5
}
]
},
"testCases": [
"testCases": [
{
"name": "John and his leave request",
"principals": [
"name": "John and his leave request",
"principals": [
{
"name": "john",
"resources": [
"name": "john",
"resources": [
{
"name": "john_leave_request",
"actions": [
"name": "john_leave_request",
"actions": [
{
"name": "view:public",
"details": {
"result": "RESULT_PASSED"
"name": "view:public",
"details": {
"result": "RESULT_PASSED"
}
},
{
"name": "approve",
"details": {
"result": "RESULT_PASSED"
"name": "approve",
"details": {
"result": "RESULT_PASSED"
}
},
{
"name": "defer",
"details": {
"result": "RESULT_PASSED"
"name": "defer",
"details": {
"result": "RESULT_PASSED"
}
}
]
Expand All @@ -48,18 +48,18 @@
]
},
{
"name": "With global now",
"principals": [
"name": "With global now",
"principals": [
{
"name": "bev",
"resources": [
"name": "bev",
"resources": [
{
"name": "stale_leave_request",
"actions": [
"name": "stale_leave_request",
"actions": [
{
"name": "remind",
"details": {
"result": "RESULT_PASSED"
"name": "remind",
"details": {
"result": "RESULT_PASSED"
}
}
]
Expand All @@ -69,18 +69,18 @@
]
},
{
"name": "With local now",
"principals": [
"name": "With local now",
"principals": [
{
"name": "bev",
"resources": [
"name": "bev",
"resources": [
{
"name": "stale_pending_leave_request",
"actions": [
"name": "stale_pending_leave_request",
"actions": [
{
"name": "remind",
"details": {
"result": "RESULT_PASSED"
"name": "remind",
"details": {
"result": "RESULT_PASSED"
}
}
]
Expand All @@ -89,16 +89,17 @@
}
]
}
]
],
"description": "Tests for verifying something"
}
],
"summary": {
"overallResult": "RESULT_PASSED",
"testsCount": 5,
"resultCounts": [
"summary": {
"overallResult": "RESULT_PASSED",
"testsCount": 5,
"resultCounts": [
{
"result": "RESULT_PASSED",
"count": 5
"result": "RESULT_PASSED",
"count": 5
}
]
}
Expand Down

0 comments on commit d96ced2

Please sign in to comment.