-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds '--junit-file' option to connectivity test and when option is provided connectivity test output is written to the destination file as junit xml format. Signed-off-by: Birol Bilgin <[email protected]>
- Loading branch information
Showing
7 changed files
with
284 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package junit | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// TestSuites represents collection of test suites. | ||
type TestSuites struct { | ||
XMLName xml.Name `xml:"testsuites"` | ||
|
||
Tests int `xml:"tests,attr"` | ||
Disabled int `xml:"disabled,attr"` | ||
Errors int `xml:"errors,attr"` | ||
Failures int `xml:"failures,attr"` | ||
Time float64 `xml:"time,attr"` | ||
|
||
TestSuites []*TestSuite `xml:"testsuite"` | ||
} | ||
|
||
// WriteReport writes Junit XML output to a writer interface. | ||
func (ts *TestSuites) WriteReport(w io.Writer) error { | ||
if _, err := fmt.Fprint(w, xml.Header); err != nil { | ||
return err | ||
} | ||
|
||
encoder := xml.NewEncoder(w) | ||
encoder.Indent(" ", " ") | ||
return encoder.Encode(ts) | ||
} | ||
|
||
// TestSuite represents collection of test cases. | ||
type TestSuite struct { | ||
XMLName xml.Name `xml:"testsuite"` | ||
|
||
Name string `xml:"name,attr"` | ||
ID int `xml:"id,attr"` | ||
Package string `xml:"package,attr"` | ||
Tests int `xml:"tests,attr"` | ||
Disabled int `xml:"disabled,attr,omitempty"` | ||
Errors int `xml:"errors,attr"` | ||
Failures int `xml:"failures,attr"` | ||
Skipped int `xml:"skipped,attr,omitempty"` | ||
Time float64 `xml:"time,attr"` | ||
Timestamp string `xml:"timestamp,attr"` | ||
Hostname string `xml:"hostname,attr,omitempty"` | ||
|
||
Properties *Properties `xml:"properties,omitempty"` | ||
|
||
TestCases []*TestCase `xml:"testcase"` | ||
|
||
SystemOut string `xml:"system-out,omitempty"` | ||
SystemErr string `xml:"system-err,omitempty"` | ||
} | ||
|
||
// Properties represents additional information of a test suite. | ||
type Properties struct { | ||
Properties []Property `xml:"property"` | ||
} | ||
|
||
// Property represents a property in Properties. | ||
type Property struct { | ||
XMLName xml.Name `xml:"property"` | ||
|
||
Name string `xml:"name,attr"` | ||
Value string `xml:"value,attr"` | ||
} | ||
|
||
// TestCase represents a single test case within a test suite. | ||
type TestCase struct { | ||
XMLName xml.Name `xml:"testcase"` | ||
|
||
Name string `xml:"name,attr"` | ||
Classname string `xml:"classname,attr"` | ||
Status string `xml:"status,attr,omitempty"` | ||
Time float64 `xml:"time,attr"` | ||
|
||
Skipped *Skipped `xml:"skipped,omitempty"` | ||
Error *Error `xml:"error,omitempty"` | ||
Failure *Failure `xml:"failure,omitempty"` | ||
|
||
SystemOut string `xml:"system-out,omitempty"` | ||
SystemErr string `xml:"system-err,omitempty"` | ||
} | ||
|
||
// Skipped represents a skipped information in a test case. | ||
type Skipped struct { | ||
XMLName xml.Name `xml:"skipped"` | ||
|
||
Message string `xml:"message,attr,omitempty"` | ||
} | ||
|
||
// Error represents an error information in a test case. | ||
type Error struct { | ||
XMLName xml.Name `xml:"error"` | ||
|
||
Message string `xml:"message,attr,omitempty"` | ||
Type string `xml:"type,attr"` | ||
|
||
Value string `xml:",chardata"` | ||
} | ||
|
||
// Failure represents a failure information in a test case. | ||
type Failure struct { | ||
XMLName xml.Name `xml:"failure"` | ||
|
||
Message string `xml:"message,attr,omitempty"` | ||
Type string `xml:"type,attr"` | ||
|
||
Value string `xml:",chardata"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package junit | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
var suitesText = `<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuites tests="3" disabled="1" errors="0" failures="1" time="14.7"> | ||
<testsuite name="testSuite" id="0" package="test" tests="3" errors="0" failures="1" skipped="1" time="14.7" timestamp="2023-05-08T11:24:14"> | ||
<testcase name="test1" classname="test" status="passed" time="7.7"></testcase> | ||
<testcase name="test2" classname="test" status="skipped" time="0"> | ||
<skipped message="test2 skipped"></skipped> | ||
</testcase> | ||
<testcase name="test3" classname="test" status="failed" time="7"> | ||
<failure message="test3 failed" type="failure">failure > expected</failure> | ||
</testcase> | ||
</testsuite> | ||
</testsuites>` | ||
|
||
var suites = TestSuites{ | ||
Tests: 3, | ||
Disabled: 1, | ||
Failures: 1, | ||
Time: 14.7, | ||
TestSuites: []*TestSuite{ | ||
{ | ||
Name: "testSuite", | ||
Package: "test", | ||
Tests: 3, | ||
Skipped: 1, | ||
Failures: 1, | ||
Time: 14.7, | ||
Timestamp: "2023-05-08T11:24:14", | ||
TestCases: []*TestCase{ | ||
{Name: "test1", Classname: "test", Status: "passed", Time: 7.7}, | ||
{Name: "test2", Classname: "test", Status: "skipped", Time: 0, | ||
Skipped: &Skipped{Message: "test2 skipped"}}, | ||
{Name: "test3", Classname: "test", Status: "failed", Time: 7, | ||
Failure: &Failure{Message: "test3 failed", Type: "failure", Value: "failure > expected"}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestWriteReport(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
if err := suites.WriteReport(buf); err != nil { | ||
t.Fatalf("Expected error <nil>, got %s", err) | ||
} | ||
|
||
if out := buf.String(); out != suitesText { | ||
t.Errorf("Expected output '%s', is not equal to '%s'", suitesText, out) | ||
} | ||
} |