Skip to content

Commit

Permalink
refine JSON reporter and reorg TestResult
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 3, 2023
1 parent 936c9f7 commit f058b0e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
26 changes: 5 additions & 21 deletions internal/report/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,35 @@ func (r *Reporter) Add(t report.TestResult) {

// Render sends the result to specified webhook WebhookURL and log the result to the specified json file
func (r *Reporter) Render() {
r.cleanup()
body, err := json.Marshal(r.Results)
if err != nil {
log.Error().Msgf("failed to generate test result (%v)", err)
log.Err(err).Msg("failed to generate test result.")
return
}

if r.WebhookURL != "" {
resp, err := http.Post(r.WebhookURL, "application/json", bytes.NewBuffer(body))
if err != nil {
log.Error().Err(err).Str("webhook", r.WebhookURL).Msg("failed to send test result to webhook.")
log.Err(err).Str("webhook", r.WebhookURL).Msg("failed to send test result to webhook.")
} else {
webhookBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= http.StatusBadRequest {
log.Error().Str("webhook", r.WebhookURL).Msgf("failed to send test result to webhook, status: '%d', msg:'%v'", resp.StatusCode, string(webhookBody))
log.Error().Str("webhook", r.WebhookURL).Msgf("failed to send test result to webhook, status: %d, msg: %q.", resp.StatusCode, string(webhookBody))
}
if resp.StatusCode%100 == 2 {
log.Info().Str("webhook", r.WebhookURL).Msgf("test result has been sent successfully to webhook, msg: '%v'.", string(webhookBody))
log.Info().Str("webhook", r.WebhookURL).Msgf("test result has been sent successfully to webhook, msg: %q.", string(webhookBody))
}
}
}

if r.Filename != "" {
err = os.WriteFile(r.Filename, body, 0666)
if err != nil {
log.Error().Err(err).Msgf("failed to write test result to %s", r.Filename)
log.Err(err).Msgf("failed to write test result to %s.", r.Filename)
}
}
}

// cleanup removes any information that isn't relevant in the rendered report. Particularly when it comes to
// artifacts, this reporter is only interested in those that have been persisted to the file system.
func (r *Reporter) cleanup() {
for i, result := range r.Results {
var artifacts []report.Artifact
for _, a := range result.Artifacts {
if a.FilePath == "" {
continue
}
artifacts = append(artifacts, a)
}
r.Results[i].Artifacts = artifacts
}
}

// Reset resets the reporter to its initial state. This action will delete all test results.
func (r *Reporter) Reset() {
r.Results = make([]report.TestResult, 0)
Expand Down
5 changes: 3 additions & 2 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ type TestResult struct {
Browser string `json:"browser,omitempty"`
Platform string `json:"platform"`
DeviceName string `json:"deviceName,omitempty"`
URL string `json:"url"`
URL string `json:"url,omitempty"`
Artifacts []Artifact `json:"artifacts,omitempty"`
Origin string `json:"origin"`
Origin string `json:"origin,omitempty"`
BuildURL string `json:"buildURL,omitempty"`
RunID string `json:"runID,omitempty"`
RDC bool `json:"-"`
TimedOut bool `json:"-"`
PassThreshold bool `json:"-"`
Expand Down
2 changes: 2 additions & 0 deletions internal/saucecloud/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ func (r *ImgRunner) collectResults(results chan execResult, expected int) bool {
EndTime: res.endTime,
Status: res.status,
Artifacts: artifacts,
Platform: "Linux",
RunID: res.runID,
Attempts: []report.Attempt{{
ID: res.runID,
Duration: res.duration,
Expand Down

0 comments on commit f058b0e

Please sign in to comment.