Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package remote |
| 2 | |
| 3 | import ( |
| 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 |
| 14 | type Poster interface { |
| 15 | Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) |
| 16 | } |
| 17 | |
| 18 | /* |
| 19 | The ForwardingReporter is a Ginkgo reporter that forwards information to |
| 20 | a Ginkgo remote server. |
| 21 | |
| 22 | When streaming parallel test output, this repoter is automatically installed by Ginkgo. |
| 23 | |
| 24 | This is accomplished by passing in the GINKGO_REMOTE_REPORTING_SERVER environment variable to `go test`, the Ginkgo test runner |
| 25 | detects this environment variable (which should contain the host of the server) and automatically installs a ForwardingReporter |
| 26 | in place of Ginkgo's DefaultReporter. |
| 27 | */ |
| 28 | |
| 29 | type ForwardingReporter struct { |
| 30 | serverHost string |
| 31 | poster Poster |
| 32 | outputInterceptor OutputInterceptor |
| 33 | } |
| 34 | |
| 35 | func NewForwardingReporter(serverHost string, poster Poster, outputInterceptor OutputInterceptor) *ForwardingReporter { |
| 36 | return &ForwardingReporter{ |
| 37 | serverHost: serverHost, |
| 38 | poster: poster, |
| 39 | outputInterceptor: outputInterceptor, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func (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 | |
| 49 | func (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 | |
| 62 | func (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 | |
| 69 | func (reporter *ForwardingReporter) SpecWillRun(specSummary *types.SpecSummary) { |
| 70 | reporter.post("/SpecWillRun", specSummary) |
| 71 | } |
| 72 | |
| 73 | func (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 | |
| 80 | func (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 | |
| 87 | func (reporter *ForwardingReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) { |
| 88 | reporter.outputInterceptor.StopInterceptingAndReturnOutput() |
| 89 | reporter.post("/SpecSuiteDidEnd", summary) |
| 90 | } |