flake_reporting_util.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2018 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 framework
  14. import (
  15. "bytes"
  16. "fmt"
  17. "sync"
  18. )
  19. // FlakeReport is a struct for managing the flake report.
  20. type FlakeReport struct {
  21. lock sync.RWMutex
  22. Flakes []string `json:"flakes"`
  23. FlakeCount int `json:"flakeCount"`
  24. }
  25. // NewFlakeReport returns a new flake report.
  26. func NewFlakeReport() *FlakeReport {
  27. return &FlakeReport{
  28. Flakes: []string{},
  29. }
  30. }
  31. func buildDescription(optionalDescription ...interface{}) string {
  32. switch len(optionalDescription) {
  33. case 0:
  34. return ""
  35. default:
  36. return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...)
  37. }
  38. }
  39. // RecordFlakeIfError records the error (if non-nil) as a flake along with an optional description.
  40. // This can be used as a replacement of framework.ExpectNoError() for non-critical errors that can
  41. // be considered as 'flakes' to avoid causing failures in tests.
  42. func (f *FlakeReport) RecordFlakeIfError(err error, optionalDescription ...interface{}) {
  43. if err == nil {
  44. return
  45. }
  46. msg := fmt.Sprintf("Unexpected error occurred: %v", err)
  47. desc := buildDescription(optionalDescription)
  48. if desc != "" {
  49. msg = fmt.Sprintf("%v (Description: %v)", msg, desc)
  50. }
  51. Logf(msg)
  52. f.lock.Lock()
  53. defer f.lock.Unlock()
  54. f.Flakes = append(f.Flakes, msg)
  55. f.FlakeCount++
  56. }
  57. // GetFlakeCount returns the flake count.
  58. func (f *FlakeReport) GetFlakeCount() int {
  59. f.lock.RLock()
  60. defer f.lock.RUnlock()
  61. return f.FlakeCount
  62. }
  63. // PrintHumanReadable returns string of flake report.
  64. func (f *FlakeReport) PrintHumanReadable() string {
  65. f.lock.RLock()
  66. defer f.lock.RUnlock()
  67. buf := bytes.Buffer{}
  68. buf.WriteString(fmt.Sprintf("FlakeCount: %v\n", f.FlakeCount))
  69. buf.WriteString("Flakes:\n")
  70. for _, flake := range f.Flakes {
  71. buf.WriteString(fmt.Sprintf("%v\n", flake))
  72. }
  73. return buf.String()
  74. }
  75. // PrintJSON returns the summary of frake report with JSON format.
  76. func (f *FlakeReport) PrintJSON() string {
  77. f.lock.RLock()
  78. defer f.lock.RUnlock()
  79. return PrettyPrintJSON(f)
  80. }
  81. // SummaryKind returns the summary of flake report.
  82. func (f *FlakeReport) SummaryKind() string {
  83. return "FlakeReport"
  84. }