-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_junit.go
112 lines (89 loc) · 2.76 KB
/
command_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package reporting
import (
"io/ioutil"
"github.com/google/uuid"
"github.com/joshdk/go-junit"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
)
const junitEventType = "TestRun"
var (
path string
dryRun bool
outputEvents bool
)
var cmdJUnit = &cobra.Command{
Use: "junit",
Short: "Send JUnit test run results to New Relic",
Long: `Send JUnit test run results to New Relic
`,
Example: `newrelic reporting junit --accountId 12345678 --path unit.xml`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
accountID := configAPI.RequireActiveProfileAccountID()
if configAPI.GetActiveProfileString(config.InsightsInsertKey) == "" {
log.Fatal("an Insights insert key is required, set one in your default profile or use the NEW_RELIC_INSIGHTS_INSERT_KEY environment variable")
}
id, err := uuid.NewRandom()
if err != nil {
log.Fatal(err)
}
xml, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
suites, err := junit.Ingest(xml)
if err != nil {
log.Fatalf("failed to ingest JUnit xml %v", err)
}
events := []map[string]interface{}{}
for _, suite := range suites {
for _, test := range suite.Tests {
events = append(events, createTestRunEvent(id, suite, test))
}
}
if outputEvents {
utils.LogIfFatal(output.Print(events))
}
if dryRun {
return
}
if err := client.NRClient.Events.CreateEventWithContext(utils.SignalCtx, accountID, events); err != nil {
log.Fatal(err)
}
log.Info("success")
},
}
func createTestRunEvent(testRunID uuid.UUID, suite junit.Suite, test junit.Test) map[string]interface{} {
e := map[string]interface{}{}
e["eventType"] = junitEventType
e["id"] = testRunID.String()
e["test"] = test.Name
e["classname"] = test.Classname
e["suite"] = suite.Name
e["package"] = suite.Package
e["status"] = test.Status
e["durationMs"] = test.Duration.Milliseconds()
if test.Error != nil {
e["errorMessage"] = test.Error.Error()
}
for key, value := range suite.Properties {
e[key] = value
}
for key, value := range test.Properties {
e[key] = value
}
return e
}
func init() {
Command.AddCommand(cmdJUnit)
cmdJUnit.Flags().StringVarP(&path, "path", "p", "", "the path to a JUnit-formatted test results file")
cmdJUnit.Flags().BoolVarP(&outputEvents, "output", "o", false, "output generated custom events to stdout")
cmdJUnit.Flags().BoolVar(&dryRun, "dryRun", false, "suppress posting custom events to NRDB")
utils.LogIfError(cmdJUnit.MarkFlagRequired("path"))
}