From f058b0e874fb096c0ebac3a6c238238121f3ea02 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Fri, 3 Nov 2023 10:28:44 -0700 Subject: [PATCH] refine JSON reporter and reorg TestResult --- internal/report/json/json.go | 26 +++++--------------------- internal/report/report.go | 5 +++-- internal/saucecloud/imagerunner.go | 2 ++ 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/internal/report/json/json.go b/internal/report/json/json.go index 76d20cad3..f3fa291a8 100644 --- a/internal/report/json/json.go +++ b/internal/report/json/json.go @@ -25,24 +25,23 @@ 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)) } } } @@ -50,26 +49,11 @@ func (r *Reporter) Render() { 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) diff --git a/internal/report/report.go b/internal/report/report.go index a2b1ab42a..83a371e5a 100644 --- a/internal/report/report.go +++ b/internal/report/report.go @@ -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:"-"` diff --git a/internal/saucecloud/imagerunner.go b/internal/saucecloud/imagerunner.go index 0afb272fd..c07695099 100644 --- a/internal/saucecloud/imagerunner.go +++ b/internal/saucecloud/imagerunner.go @@ -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,