forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputenricher.go
119 lines (101 loc) · 3.25 KB
/
outputenricher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package reporter
import (
"context"
"encoding/json"
"fmt"
"os"
"sort"
"time"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
)
//NewCapturedOutputEnricher captures additional interesting cluster info and adds it to the captured output
// to enrich existing reporters, like the junit reporter, with additional data.
func NewCapturedOutputEnricher(reporters ...ginkgo.Reporter) *capturedOutputEnricher {
return &capturedOutputEnricher{
reporters: reporters,
}
}
type capturedOutputEnricher struct {
reporters []ginkgo.Reporter
additionalOutput interface{}
}
func (j *capturedOutputEnricher) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
for _, report := range j.reporters {
report.SpecSuiteWillBegin(config, summary)
}
}
func (j *capturedOutputEnricher) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
for _, report := range j.reporters {
report.BeforeSuiteDidRun(setupSummary)
}
}
func (j *capturedOutputEnricher) SpecWillRun(specSummary *types.SpecSummary) {
j.additionalOutput = ""
for _, report := range j.reporters {
report.SpecWillRun(specSummary)
}
}
func (j *capturedOutputEnricher) SpecDidComplete(specSummary *types.SpecSummary) {
if specSummary.State.IsFailure() {
if j.additionalOutput != "" {
specSummary.CapturedOutput = fmt.Sprintf("%s\n%s", specSummary.CapturedOutput, j.additionalOutput)
}
}
for _, report := range j.reporters {
report.SpecDidComplete(specSummary)
}
}
func (j *capturedOutputEnricher) JustAfterEach(specSummary ginkgo.GinkgoTestDescription) {
if specSummary.Failed {
j.additionalOutput = j.collect(specSummary.Duration)
}
}
func (j *capturedOutputEnricher) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
for _, report := range j.reporters {
report.AfterSuiteDidRun(setupSummary)
}
}
func (j *capturedOutputEnricher) SpecSuiteDidEnd(summary *types.SuiteSummary) {
for _, report := range j.reporters {
report.SpecSuiteDidEnd(summary)
}
}
func (j *capturedOutputEnricher) collect(duration time.Duration) string {
virtCli, err := kubecli.GetKubevirtClient()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get client: %v\n", err)
return ""
}
duration += 5 * time.Second
since := time.Now().Add(-duration)
return j.getWarningEvents(virtCli, since)
}
func (j *capturedOutputEnricher) getWarningEvents(virtCli kubecli.KubevirtClient, since time.Time) string {
events, err := virtCli.CoreV1().Events(v1.NamespaceAll).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.DefaultLogger().Reason(err).Errorf("Failed to fetch events")
return ""
}
e := events.Items
sort.Slice(e, func(i, j int) bool {
return e[i].LastTimestamp.After(e[j].LastTimestamp.Time)
})
eventsToPrint := v1.EventList{}
for _, event := range e {
if event.LastTimestamp.Time.After(since) && event.Type == v1.EventTypeWarning {
eventsToPrint.Items = append(eventsToPrint.Items, event)
}
}
rawEvents, err := json.MarshalIndent(eventsToPrint, "", " ")
if err != nil {
log.DefaultLogger().Reason(err).Errorf("Failed to marshal events")
return ""
}
return string(rawEvents)
}