Skip to content

Commit

Permalink
Adjust XML output
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Dec 20, 2023
1 parent 324934e commit 35fd838
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
60 changes: 37 additions & 23 deletions pkg/claimhelper/claimhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"path/filepath"
"strconv"
"strings"

"os"
"time"
Expand All @@ -41,7 +42,7 @@ const (
CNFFeatureValidationJunitXMLFileName = "validation_junit.xml"
CNFFeatureValidationReportKey = "cnf-feature-validation"
// dateTimeFormatDirective is the directive used to format date/time according to ISO 8601.
DateTimeFormatDirective = "2006-01-02T15:04:05+00:00"
DateTimeFormatDirective = "2006-01-02 15:04:05.000000 -0700 MST"

// States for test cases
TestStateFailed = "failed"
Expand All @@ -60,33 +61,33 @@ type FailureMessage struct {
}

type TestCase struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Classname string `xml:"classname,attr"`
Status string `xml:"status,attr"`
Time string `xml:"time,attr"`
SystemErr string `xml:"system-err,omitempty"`
Skipped SkippedMessage `xml:"skipped,omitempty"`
Failure FailureMessage `xml:"failure,omitempty"`
Text string `xml:",chardata"`
Name string `xml:"name,attr,omitempty"`
Classname string `xml:"classname,attr,omitempty"`
Status string `xml:"status,attr,omitempty"`
Time string `xml:"time,attr,omitempty"`
SystemErr string `xml:"system-err,omitempty"`
Skipped *SkippedMessage
Failure *FailureMessage
}

type Testsuite struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Package string `xml:"package,attr"`
Tests string `xml:"tests,attr"`
Disabled string `xml:"disabled,attr"`
Name string `xml:"name,attr,omitempty"`
Package string `xml:"package,attr,omitempty"`
Tests string `xml:"tests,attr,omitempty"`
Disabled string `xml:"disabled,attr,omitempty"`
Skipped string `xml:"skipped,attr,omitempty"`
Errors string `xml:"errors,attr,omitempty"`
Failures string `xml:"failures,attr,omitempty"`
Time string `xml:"time,attr"`
Timestamp string `xml:"timestamp,attr"`
Time string `xml:"time,attr,omitempty"`
Timestamp string `xml:"timestamp,attr,omitempty"`
Properties struct {
Text string `xml:",chardata"`
Property []struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
Name string `xml:"name,attr,omitempty"`
Value string `xml:"value,attr,omitempty"`
} `xml:"property"`
} `xml:"properties"`
Testcase []TestCase `xml:"testcase"`
Expand All @@ -95,11 +96,11 @@ type Testsuite struct {
type TestSuitesXML struct {
XMLName xml.Name `xml:"testsuites"`
Text string `xml:",chardata"`
Tests string `xml:"tests,attr"`
Disabled string `xml:"disabled,attr"`
Tests string `xml:"tests,attr,omitempty"`
Disabled string `xml:"disabled,attr,omitempty"`
Errors string `xml:"errors,attr,omitempty"`
Failures string `xml:"failures,attr,omitempty"`
Time string `xml:"time,attr"`
Time string `xml:"time,attr,omitempty"`
Testsuite Testsuite `xml:"testsuite"`
}

Expand Down Expand Up @@ -178,7 +179,7 @@ func populateXMLFromClaim(c claim.Claim, startTime, endTime time.Time) TestSuite
xmlOutput.Failures = strconv.Itoa(failedTests)
xmlOutput.Disabled = strconv.Itoa(skippedTests)
xmlOutput.Errors = strconv.Itoa(0)
xmlOutput.Time = strconv.FormatFloat(endTime.Sub(startTime).Seconds(), 'f', 3, 64)
xmlOutput.Time = strconv.FormatFloat(endTime.Sub(startTime).Seconds(), 'f', 5, 64)

// <testsuite>
xmlOutput.Testsuite.Name = TestSuiteName
Expand All @@ -188,7 +189,7 @@ func populateXMLFromClaim(c claim.Claim, startTime, endTime time.Time) TestSuite
xmlOutput.Testsuite.Skipped = strconv.Itoa(skippedTests)
xmlOutput.Testsuite.Errors = strconv.Itoa(0)

xmlOutput.Testsuite.Time = strconv.FormatFloat(endTime.Sub(startTime).Seconds(), 'f', 3, 64)
xmlOutput.Testsuite.Time = strconv.FormatFloat(endTime.Sub(startTime).Seconds(), 'f', 5, 64)
xmlOutput.Testsuite.Timestamp = time.Now().UTC().Format(DateTimeFormatDirective)

// <properties>
Expand All @@ -202,16 +203,29 @@ func populateXMLFromClaim(c claim.Claim, startTime, endTime time.Time) TestSuite
testCase.Name = testID
testCase.Classname = TestSuiteName
testCase.Status = typedResult.State
testCase.Time = strconv.FormatFloat(float64(typedResult.Duration), 'f', 3, 64)

// Clean the time strings to remove the " m=" suffix
start, _ := time.Parse(DateTimeFormatDirective, strings.Split(typedResult.StartTime, " m=")[0])
end, _ := time.Parse(DateTimeFormatDirective, strings.Split(typedResult.EndTime, " m=")[0])

// Calculate the duration of the test case
difference := end.Sub(start)
testCase.Time = strconv.FormatFloat(difference.Seconds(), 'f', 10, 64)

// Populate the skipped message if the test case was skipped
if testCase.Status == TestStateSkipped {
testCase.Skipped = &SkippedMessage{}
testCase.Skipped.Text = typedResult.SkipReason
} else {
testCase.Skipped = nil
}

// Populate the failure message if the test case failed
if testCase.Status == TestStateFailed {
testCase.Failure = &FailureMessage{}
testCase.Failure.Text = typedResult.CheckDetails
} else {
testCase.Failure = nil
}

// Append the test case to the test suite
Expand Down
13 changes: 5 additions & 8 deletions pkg/claimhelper/claimhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestPopulateXMLFromClaim(t *testing.T) {
Disabled: strconv.Itoa(0),
Tests: strconv.Itoa(1),
Errors: strconv.Itoa(0),
Time: strconv.Itoa(60),
Time: strconv.Itoa(1),
Testsuite: Testsuite{

Name: "test-suite1",
Expand All @@ -84,8 +84,8 @@ func TestPopulateXMLFromClaim(t *testing.T) {
Testcase: []TestCase{
{
Name: "test-case1",
Time: strconv.Itoa(60),
Failure: FailureMessage{
Time: strconv.Itoa(1),
Failure: &FailureMessage{
Message: "my custom failure message",
},
},
Expand All @@ -97,9 +97,9 @@ func TestPopulateXMLFromClaim(t *testing.T) {

for _, tc := range testCases {
// Build some 1 minute duration start and end time
startTime, err := time.Parse(DateTimeFormatDirective, "2006-01-02T15:04:05+00:00")
startTime, err := time.Parse(DateTimeFormatDirective, "2023-12-20 14:51:33.000000 -0600 CST")
assert.Nil(t, err)
endTime, err := time.Parse(DateTimeFormatDirective, "2006-01-02T15:05:05+00:00")
endTime, err := time.Parse(DateTimeFormatDirective, "2023-12-20 14:51:34.000000 -0600 CST")
assert.Nil(t, err)

xmlResult := populateXMLFromClaim(generateClaim(map[string]claim.Result{"test-case1": tc.testResult}), startTime, endTime)
Expand All @@ -108,9 +108,6 @@ func TestPopulateXMLFromClaim(t *testing.T) {
assert.Equal(t, tc.expectedXMLResult.Failures, xmlResult.Failures)
assert.Equal(t, tc.expectedXMLResult.Tests, xmlResult.Tests)
assert.Equal(t, tc.expectedXMLResult.Errors, xmlResult.Errors)
assert.Equal(t, tc.expectedXMLResult.Testsuite.Skipped, xmlResult.Testsuite.Skipped)

// convert "60.000" string to 60 int
expectedTimeFloat, err := strconv.ParseFloat(tc.expectedXMLResult.Time, 32)
assert.Nil(t, err)
actualTimeFloat, err := strconv.ParseFloat(xmlResult.Time, 32)
Expand Down
17 changes: 17 additions & 0 deletions pkg/flags/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2020-2023 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

package flags

0 comments on commit 35fd838

Please sign in to comment.