Skip to content

Commit

Permalink
Add threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Dec 19, 2022
2 parents 4736f80 + 2eff4fa commit d95c40c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Go
on:
push:
branches: [ "main" ]
tags:
- v*
pull_request:
branches: [ "main" ]

Expand All @@ -22,7 +24,12 @@ jobs:
uses: golangci/golangci-lint-action@v3

- name: Build
run: go build -v ./...
run: CGO_ENABLED=0 go build -a -tags netgo -ldflags '-s -w' -v ./...

- name: Compress binaries
uses: svenstaro/upx-action@v2
with:
file: junit2jira

- name: Test
run: go test -v ./...
Expand Down
34 changes: 32 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/andygrunwald/go-jira"
"github.com/hashicorp/go-multierror"
"github.com/joshdk/go-junit"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"io"
"log"
Expand All @@ -28,12 +29,14 @@ func main() {
jiraUrl := ""
junitReportsDir := ""
dryRun := false
threshold := 0
flag.StringVar(&jiraUrl, "jira-url", "https://issues.redhat.com/", "Url of JIRA instance")
flag.StringVar(&junitReportsDir, "junit-reports-dir", os.Getenv("ARTIFACT_DIR"), "Dir that contains jUnit reports XML files")
flag.BoolVar(&dryRun, "dry-run", false, "When set to true issues will NOT be created.")
flag.IntVar(&threshold, "threshold", 10, "Number of reported failures that should cause single issue creation.")
flag.Parse()

failedTests, err := findFailedTests(junitReportsDir, Env())
failedTests, err := findFailedTests(junitReportsDir, Env(), threshold)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -171,7 +174,7 @@ func Env() map[string]string {
return m
}

func findFailedTests(dirName string, env map[string]string) ([]testCase, error) {
func findFailedTests(dirName string, env map[string]string, threshold int) ([]testCase, error) {
failedTests := make([]testCase, 0)
testSuites, err := junit.IngestDir(dirName)
if err != nil {
Expand All @@ -186,9 +189,36 @@ func findFailedTests(dirName string, env map[string]string) ([]testCase, error)
}
}
log.Printf("Found %d failed tests", len(failedTests))

if len(failedTests) > threshold && threshold > 0 {
return mergeFailedTests(failedTests, env)
}

return failedTests, nil
}

func mergeFailedTests(failedTests []testCase, env map[string]string) ([]testCase, error) {
log.Println("Too many failed tests, reporting them as a one failure.")
msg := ""
suite := failedTests[0].Suite
for _, t := range failedTests {
summary, err := t.summary()
if err != nil {
return nil, errors.Wrapf(err, "could not get summary of %+v", t)
}
// If there are multiple suites, do not report them.
if suite != t.Suite {
suite = ""
}
msg += summary + "\n"
}
tc := NewTestCase(junit.Test{
Message: msg,
Classname: suite,
}, env)
return []testCase{tc}, nil
}

const (
desc = `
{{- if .Message }}
Expand Down
43 changes: 39 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

func TestParseJunitReport(t *testing.T) {
t.Run("not existing", func(t *testing.T) {
tests, err := findFailedTests("not existing", nil)
tests, err := findFailedTests("not existing", nil, 0)
assert.Error(t, err)
assert.Nil(t, tests)
})
t.Run("golang", func(t *testing.T) {
tests, err := findFailedTests("testdata/report.xml", nil)
tests, err := findFailedTests("testdata/report.xml", nil, 0)
assert.NoError(t, err)
assert.Equal(t, []testCase{
{
Expand All @@ -38,9 +38,44 @@ func TestParseJunitReport(t *testing.T) {
},
}, tests)
})
t.Run("golang with threshold", func(t *testing.T) {
tests, err := findFailedTests("testdata/report.xml", map[string]string{"JOB_NAME": "job-name"}, 1)
assert.NoError(t, err)
assert.Equal(t, []testCase{
{
Message: `github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests FAILED
github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests/TestSuccessfulRefresh FAILED
github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests/TestSuccessfulRefresh/no_secrets FAILED
`,
JobName: "job-name",
Suite: "github.com/stackrox/rox/sensor/kubernetes/localscanner",
},
}, tests)
})
t.Run("dir multiple suites with threshold", func(t *testing.T) {
tests, err := findFailedTests("testdata", map[string]string{"JOB_NAME": "job-name", "BUILD_ID": "1"}, 5)
assert.NoError(t, err)

assert.ElementsMatch(
t,
[]testCase{
{
Message: `DefaultPoliciesTest / Verify policy Apache Struts: CVE-2017-5638 is triggered FAILED
github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests FAILED
github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests/TestSuccessfulRefresh FAILED
github.com/stackrox/rox/sensor/kubernetes/localscanner / TestLocalScannerTLSIssuerIntegrationTests/TestSuccessfulRefresh/no_secrets FAILED
github.com/stackrox/rox/central/resourcecollection/datastore/store/postgres / TestCollectionsStore FAILED
github.com/stackrox/rox/central/resourcecollection/datastore/store/postgres / TestCollectionsStore/TestStore FAILED
`,
JobName: "job-name",
BuildId: "1",
},
},
tests,
)
})
t.Run("dir", func(t *testing.T) {
tests, err := findFailedTests("testdata", map[string]string{"BUILD_ID": "1"})
tests, err := findFailedTests("testdata", map[string]string{"BUILD_ID": "1"}, 0)
assert.NoError(t, err)

assert.ElementsMatch(
Expand Down Expand Up @@ -108,7 +143,7 @@ func TestParseJunitReport(t *testing.T) {
)
})
t.Run("gradle", func(t *testing.T) {
tests, err := findFailedTests("testdata/TEST-DefaultPoliciesTest.xml", map[string]string{"BUILD_ID": "1"})
tests, err := findFailedTests("testdata/TEST-DefaultPoliciesTest.xml", map[string]string{"BUILD_ID": "1"}, 0)
assert.NoError(t, err)

assert.Equal(
Expand Down

0 comments on commit d95c40c

Please sign in to comment.