progress.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reporters
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "strings"
  21. "time"
  22. "k8s.io/klog"
  23. "github.com/onsi/ginkgo/config"
  24. "github.com/onsi/ginkgo/types"
  25. )
  26. // ProgressReporter is a ginkgo reporter which tracks the total number of tests to be run/passed/failed/skipped.
  27. // As new tests are completed it updates the values and prints them to stdout and optionally, sends the updates
  28. // to the configured URL.
  29. type ProgressReporter struct {
  30. LastMsg string `json:"msg"`
  31. TestsTotal int `json:"total"`
  32. TestsCompleted int `json:"completed"`
  33. TestsSkipped int `json:"skipped"`
  34. TestsFailed int `json:"failed"`
  35. Failures []string `json:"failures,omitempty"`
  36. progressURL string
  37. client *http.Client
  38. }
  39. // NewProgressReporter returns a progress reporter which posts updates to the given URL.
  40. func NewProgressReporter(progressReportURL string) *ProgressReporter {
  41. rep := &ProgressReporter{
  42. Failures: []string{},
  43. progressURL: progressReportURL,
  44. }
  45. if len(progressReportURL) > 0 {
  46. rep.client = &http.Client{
  47. Timeout: time.Second * 10,
  48. }
  49. }
  50. return rep
  51. }
  52. // SpecSuiteWillBegin is invoked by ginkgo when the suite is about to start and is the first point in which we can
  53. // antipate the number of tests which will be run.
  54. func (reporter *ProgressReporter) SpecSuiteWillBegin(cfg config.GinkgoConfigType, summary *types.SuiteSummary) {
  55. reporter.TestsTotal = summary.NumberOfSpecsThatWillBeRun
  56. reporter.LastMsg = "Test Suite starting"
  57. reporter.sendUpdates()
  58. }
  59. // SpecSuiteDidEnd is the last method invoked by Ginkgo after all the specs are run.
  60. func (reporter *ProgressReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
  61. reporter.LastMsg = "Test Suite completed"
  62. reporter.sendUpdates()
  63. }
  64. // SpecDidComplete is invoked by Ginkgo each time a spec is completed (including skipped specs).
  65. func (reporter *ProgressReporter) SpecDidComplete(specSummary *types.SpecSummary) {
  66. testname := strings.Join(specSummary.ComponentTexts[1:], " ")
  67. switch specSummary.State {
  68. case types.SpecStateFailed:
  69. if len(specSummary.ComponentTexts) > 0 {
  70. reporter.Failures = append(reporter.Failures, testname)
  71. } else {
  72. reporter.Failures = append(reporter.Failures, "Unknown test name")
  73. }
  74. reporter.TestsFailed++
  75. reporter.LastMsg = fmt.Sprintf("FAILED %v", testname)
  76. case types.SpecStatePassed:
  77. reporter.TestsCompleted++
  78. reporter.LastMsg = fmt.Sprintf("PASSED %v", testname)
  79. case types.SpecStateSkipped:
  80. reporter.TestsSkipped++
  81. return
  82. default:
  83. return
  84. }
  85. reporter.sendUpdates()
  86. }
  87. // sendUpdates serializes the current progress and prints it to stdout and also posts it to the configured endpoint if set.
  88. func (reporter *ProgressReporter) sendUpdates() {
  89. b := reporter.serialize()
  90. fmt.Println(string(b))
  91. go reporter.postProgressToURL(b)
  92. }
  93. func (reporter *ProgressReporter) postProgressToURL(b []byte) {
  94. // If a progressURL and client is set/available then POST to it. Noop otherwise.
  95. if reporter.client == nil || len(reporter.progressURL) == 0 {
  96. return
  97. }
  98. resp, err := reporter.client.Post(reporter.progressURL, "application/json", bytes.NewReader(b))
  99. if err != nil {
  100. klog.Errorf("Failed to post progress update to %v: %v", reporter.progressURL, err)
  101. return
  102. }
  103. if resp.StatusCode >= 400 {
  104. klog.Errorf("Unexpected response when posting progress update to %v: %v", reporter.progressURL, resp.StatusCode)
  105. if resp.Body != nil {
  106. defer resp.Body.Close()
  107. respBody, err := ioutil.ReadAll(resp.Body)
  108. if err != nil {
  109. klog.Errorf("Failed to read response body from posting progress: %v", err)
  110. return
  111. }
  112. klog.Errorf("Response body from posting progress update: %v", respBody)
  113. }
  114. return
  115. }
  116. }
  117. func (reporter *ProgressReporter) serialize() []byte {
  118. b, err := json.Marshal(reporter)
  119. if err != nil {
  120. return []byte(fmt.Sprintf(`{"msg":"%v", "error":"%v"}`, reporter.LastMsg, err))
  121. }
  122. return b
  123. }
  124. // SpecWillRun is implemented as a noop to satisfy the reporter interface for ginkgo.
  125. func (reporter *ProgressReporter) SpecWillRun(specSummary *types.SpecSummary) {}
  126. // BeforeSuiteDidRun is implemented as a noop to satisfy the reporter interface for ginkgo.
  127. func (reporter *ProgressReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {}
  128. // AfterSuiteDidRun is implemented as a noop to satisfy the reporter interface for ginkgo.
  129. func (reporter *ProgressReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {}