blob: 025eb5064483b24f40caace9c04ae9daf39d43c9 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package remote
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "net/http"
8
9 "github.com/onsi/ginkgo/config"
10 "github.com/onsi/ginkgo/types"
11)
12
13//An interface to net/http's client to allow the injection of fakes under test
14type Poster interface {
15 Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error)
16}
17
18/*
19The ForwardingReporter is a Ginkgo reporter that forwards information to
20a Ginkgo remote server.
21
22When streaming parallel test output, this repoter is automatically installed by Ginkgo.
23
24This is accomplished by passing in the GINKGO_REMOTE_REPORTING_SERVER environment variable to `go test`, the Ginkgo test runner
25detects this environment variable (which should contain the host of the server) and automatically installs a ForwardingReporter
26in place of Ginkgo's DefaultReporter.
27*/
28
29type ForwardingReporter struct {
30 serverHost string
31 poster Poster
32 outputInterceptor OutputInterceptor
33}
34
35func NewForwardingReporter(serverHost string, poster Poster, outputInterceptor OutputInterceptor) *ForwardingReporter {
36 return &ForwardingReporter{
37 serverHost: serverHost,
38 poster: poster,
39 outputInterceptor: outputInterceptor,
40 }
41}
42
43func (reporter *ForwardingReporter) post(path string, data interface{}) {
44 encoded, _ := json.Marshal(data)
45 buffer := bytes.NewBuffer(encoded)
46 reporter.poster.Post(reporter.serverHost+path, "application/json", buffer)
47}
48
49func (reporter *ForwardingReporter) SpecSuiteWillBegin(conf config.GinkgoConfigType, summary *types.SuiteSummary) {
50 data := struct {
51 Config config.GinkgoConfigType `json:"config"`
52 Summary *types.SuiteSummary `json:"suite-summary"`
53 }{
54 conf,
55 summary,
56 }
57
58 reporter.outputInterceptor.StartInterceptingOutput()
59 reporter.post("/SpecSuiteWillBegin", data)
60}
61
62func (reporter *ForwardingReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
63 output, _ := reporter.outputInterceptor.StopInterceptingAndReturnOutput()
64 reporter.outputInterceptor.StartInterceptingOutput()
65 setupSummary.CapturedOutput = output
66 reporter.post("/BeforeSuiteDidRun", setupSummary)
67}
68
69func (reporter *ForwardingReporter) SpecWillRun(specSummary *types.SpecSummary) {
70 reporter.post("/SpecWillRun", specSummary)
71}
72
73func (reporter *ForwardingReporter) SpecDidComplete(specSummary *types.SpecSummary) {
74 output, _ := reporter.outputInterceptor.StopInterceptingAndReturnOutput()
75 reporter.outputInterceptor.StartInterceptingOutput()
76 specSummary.CapturedOutput = output
77 reporter.post("/SpecDidComplete", specSummary)
78}
79
80func (reporter *ForwardingReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
81 output, _ := reporter.outputInterceptor.StopInterceptingAndReturnOutput()
82 reporter.outputInterceptor.StartInterceptingOutput()
83 setupSummary.CapturedOutput = output
84 reporter.post("/AfterSuiteDidRun", setupSummary)
85}
86
87func (reporter *ForwardingReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
88 reporter.outputInterceptor.StopInterceptingAndReturnOutput()
89 reporter.post("/SpecSuiteDidEnd", summary)
90}