forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junit.go
76 lines (62 loc) · 1.83 KB
/
junit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package outputs
import (
"fmt"
"io"
"strconv"
"time"
"github.com/aelsabbahy/goss/resource"
)
type JUnit struct{}
func (r JUnit) Output(w io.Writer, results <-chan []resource.TestResult, startTime time.Time) (exitCode int) {
var testCount, failed, skipped int
// ISO8601 timeformat
location, _ := time.LoadLocation("Etc/UTC")
timestamp := time.Now().In(location).Format(time.RFC3339)
var summary map[int]string
summary = make(map[int]string)
for resultGroup := range results {
for _, testResult := range resultGroup {
m := struct2map(testResult)
duration := strconv.FormatFloat(m["duration"].(float64)/1000/1000/1000, 'f', 3, 64)
summary[testCount] = "<testcase name=\"" +
testResult.ResourceType + " " +
testResult.ResourceId + " " +
testResult.Property + "\" " +
"time=\"" + duration + "\">\n"
if testResult.Result == resource.FAIL {
summary[testCount] += "<system-err>" +
humanizeResult2(testResult) +
"</system-err>\n"
summary[testCount] += "<failure>" +
humanizeResult2(testResult) +
"</failure>\n</testcase>\n"
failed++
} else {
if testResult.Result == resource.SKIP {
summary[testCount] += "<skipped/>"
skipped++
}
summary[testCount] += "<system-out>" +
humanizeResult2(testResult) +
"</system-out>\n</testcase>\n"
}
testCount++
}
}
duration := time.Since(startTime)
fmt.Fprintln(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
fmt.Fprintf(w, "<testsuite name=\"goss\" errors=\"0\" tests=\"%d\" "+
"failures=\"%d\" skipped=\"%d\" time=\"%.3f\" timestamp=\"%s\">\n",
testCount, failed, skipped, duration.Seconds(), timestamp)
for i := 0; i < testCount; i++ {
fmt.Fprintf(w, "%s", summary[i])
}
fmt.Fprintln(w, "</testsuite>")
if failed > 0 {
return 1
}
return 0
}
func init() {
RegisterOutputer("junit", &JUnit{})
}