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

try and dump a junit xml #494

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions alerter/junit/junit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package junit

import (
"encoding/xml"
"fmt"
"io"
)

// WriteXML writes the XML representation of Testsuites t to writer w.
func (t *Testsuite) WriteXML(w io.Writer) error {
enc := xml.NewEncoder(w)
enc.Indent("", "\t")
if err := enc.Encode(t); err != nil {
return err
}
if err := enc.Flush(); err != nil {
return err
}
_, err := fmt.Fprintf(w, "\n")
return err
}

// Testsuite is a single JUnit testsuite containing testcases.
type Testsuite struct {
XMLName xml.Name `xml:"testsuite"` //what magic is this
// required attributes
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`

Testcases []Testcase `xml:"testcase,omitempty"`
}

// AddTestcase adds Testcase tc to this Testsuite.
func (t *Testsuite) AddTestcase(tc Testcase) {
t.Testcases = append(t.Testcases, tc)
t.Tests++
if tc.Failure != nil {
t.Failures++
}

}

// Testcase represents a single test with its results.
type Testcase struct {
// required attributes
Name string `xml:"name,attr"`
Classname string `xml:"classname,attr"`

Failure *Result `xml:"failure,omitempty"`
}

// Property represents a key/value pair.
type Property struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}

// Result represents the result of a single test.
type Result struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr,omitempty"`
}
45 changes: 40 additions & 5 deletions alerter/rules/localStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strings"

"github.com/Azure/adx-mon/alerter/junit"
alertrulev1 "github.com/Azure/adx-mon/api/v1"
"github.com/Azure/adx-mon/pkg/logger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -23,36 +24,70 @@ type fileStore struct {

func FromPath(path, region string) (*fileStore, error) {
s := &fileStore{}
var loaderrors []error
suite := junit.Testsuite{}
defer func() {
jxml, err := os.OpenFile("junit-adxmon-lint.xml", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Errorf("failed to open junit xml file: %s", err)
return
}
suite.WriteXML(jxml)
}()
// walk files in directory
err := filepath.WalkDir(path, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
return err //when does this happen on reading a dir? just get out
}
if info.IsDir() {
return nil
}
tc := junit.Testcase{
Name: path,
}
suite.AddTestcase(tc)

f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file '%s': %w", path, err)
loaderrors = append(loaderrors, fmt.Errorf("failed to open file '%s': %w", path, err))
tc.Failure = &junit.Result{
Message: fmt.Sprintf("failed to open file '%s': %s", path, err),
Type: "lint",
}
return nil
}
defer f.Close()
logger.Infof("reading file %q", path)
err = s.fromStream(f, region)
if err != nil {
return fmt.Errorf("failed to read file '%s': %w", path, err)
loaderrors = append(loaderrors, fmt.Errorf("failed to parse file '%s': %w", path, err))
tc.Failure = &junit.Result{
Message: fmt.Sprintf("failed to parse file '%s': %s", path, err),
Type: "lint",
}
return nil
}
return nil
})

if err != nil {
return nil, fmt.Errorf("failed to walk directory: %w", err)
}

knownRules := map[string]bool{}
for _, rule := range s.Rules() {
key := rule.Namespace + "/" + rule.Name
if knownRules[key] {
return nil, fmt.Errorf("duplicate rule %s", key)
loaderrors = append(loaderrors, fmt.Errorf("duplicate rule %s", key))
suite.AddTestcase(junit.Testcase{Name: "duplicate check", Failure: &junit.Result{Message: err.Error(), Type: "lint"}})
continue
}
knownRules[key] = true
}
return s, err
if len(loaderrors) > 0 {
return nil, fmt.Errorf("failed to load rules: %w", fmt.Errorf("%v", loaderrors))
}
return s, nil

}

Expand Down
Loading