/
testrun.go
199 lines (175 loc) · 5.49 KB
/
testrun.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package pages
import (
"context"
"fmt"
"net/http"
"sort"
"strings"
"time"
"github.com/gorilla/mux"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/test-infra/pkg/apis/testmachinery/v1beta1"
"github.com/gardener/test-infra/pkg/common"
metadata2 "github.com/gardener/test-infra/pkg/testmachinery/metadata"
"github.com/gardener/test-infra/pkg/testrunner"
"github.com/gardener/test-infra/pkg/tm-bot/ui/pages/pagination"
"github.com/gardener/test-infra/pkg/util"
"github.com/gardener/test-infra/pkg/util/output"
)
type detailedTestrunItem struct {
testrunItem
Steps testrunStepStatusItemList
RawStatus string
}
type testrunStepStatusItem struct {
step *v1beta1.StepStatus
Name string
Step string
Phase IconWithTooltip
StartTime string
Duration string
Location string
IsSystem bool
GrafanaURL string
}
func NewTestrunPage(p *Page) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
defer ctx.Done()
trName := client.ObjectKey{
Name: mux.Vars(r)["testrun"],
Namespace: mux.Vars(r)["namespace"],
}
tr := &v1beta1.Testrun{}
if err := p.runs.GetClient().Get(ctx, trName, tr); err != nil {
http.Redirect(w, r, "/404", http.StatusTemporaryRedirect)
return
}
argoHostURL, _ := testrunner.GetArgoHost(ctx, p.runs.GetClient())
grafanaHostURL, _ := testrunner.GetGrafanaHost(ctx, p.runs.GetClient())
metadata := metadata2.FromTestrun(tr)
startTime := ""
if tr.Status.StartTime != nil {
startTime = tr.Status.StartTime.Format(time.RFC822)
}
d := time.Duration(tr.Status.Duration) * time.Second
if tr.Status.Duration == 0 && !tr.Status.StartTime.IsZero() {
d = time.Since(tr.Status.StartTime.Time)
d = d / time.Second * time.Second // remove unnecessary milliseconds
}
statusTable := &strings.Builder{}
if len(tr.Status.Steps) != 0 {
output.RenderStatusTable(statusTable, tr.Status.Steps)
}
item := detailedTestrunItem{
testrunItem: testrunItem{
testrun: tr,
ID: tr.GetName(),
Namespace: tr.GetNamespace(),
Phase: RunPhaseIcon(tr.Status.Phase),
StartTime: startTime,
Duration: d.String(),
Progress: util.TestrunProgress(tr),
Dimension: metadata.GetDimensionFromMetadata("/"),
},
Steps: make(testrunStepStatusItemList, len(tr.Status.Steps)),
RawStatus: statusTable.String(),
}
if retries, ok := tr.Annotations[common.AnnotationRetries]; ok {
item.Retries = retries
}
if prevAttempt, ok := tr.Annotations[common.AnnotationPreviousAttempt]; ok {
item.PreviousAttempt = prevAttempt
}
if runID, ok := tr.Labels[common.LabelTestrunExecutionGroup]; ok {
item.RunID = runID
}
if argoHostURL != "" {
item.ArgoURL = testrunner.GetArgoURLFromHost(argoHostURL, tr)
}
if grafanaHostURL != "" {
item.GrafanaURL = testrunner.GetGrafanaURLFromHostForWorkflow(grafanaHostURL, tr.Status.Workflow)
}
for i, step := range tr.Status.Steps {
startTime := ""
if step.StartTime != nil {
startTime = step.StartTime.Format(time.RFC822)
}
d := time.Duration(step.Duration) * time.Second
if step.Duration == 0 && !step.StartTime.IsZero() {
d = time.Since(step.StartTime.Time)
d = d / time.Second * time.Second // remove unnecessary milliseconds
}
item.Steps[i] = testrunStepStatusItem{
step: step,
Name: step.TestDefinition.Name,
Step: step.Position.Step,
Phase: StepPhaseIcon(step.Phase),
StartTime: startTime,
Duration: d.String(),
Location: fmt.Sprintf("%s:%s", step.TestDefinition.Location.Repo, step.TestDefinition.Location.Revision),
IsSystem: util.IsSystemStep(step),
}
if grafanaHostURL != "" {
item.Steps[i].GrafanaURL = testrunner.GetGrafanaURLFromHostForStep(grafanaHostURL, tr.Status.Workflow, step.TestDefinition.Name)
}
}
sort.Sort(item.Steps)
p.handleSimplePage("testrun.html", item)(w, r)
}
}
type testrunItemList []testrunItem
func (l testrunItemList) GetPaginatedList(from, to int) pagination.Interface {
to++
if from > len(l) {
return make(testrunItemList, 0)
}
if to >= len(l) {
return l[from:]
}
return l[from:to]
}
func (l testrunItemList) Len() int { return len(l) }
func (l testrunItemList) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
func (l testrunItemList) Less(a, b int) bool {
aTr := l[a].testrun
bTr := l[b].testrun
if aTr.Status.Phase != bTr.Status.Phase {
if aTr.Status.Phase == v1beta1.RunPhaseRunning {
return true
}
if bTr.Status.Phase == v1beta1.RunPhaseRunning {
return false
}
}
if aTr.Status.StartTime == nil || bTr.Status.StartTime == nil {
return true
}
return bTr.Status.StartTime.Before(aTr.Status.StartTime)
}
type testrunStepStatusItemList []testrunStepStatusItem
func (l testrunStepStatusItemList) Len() int { return len(l) }
func (l testrunStepStatusItemList) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
func (l testrunStepStatusItemList) Less(a, b int) bool {
if l[a].step.Phase != l[b].step.Phase {
if l[a].step.Phase == v1beta1.StepPhaseRunning {
return true
}
if l[b].step.Phase == v1beta1.StepPhaseRunning {
return false
}
if l[a].step.Phase == v1beta1.StepPhaseInit {
return false
}
if l[b].step.Phase == v1beta1.StepPhaseInit {
return true
}
}
if l[a].step.StartTime == nil || l[b].step.StartTime == nil {
return true
}
return l[a].step.StartTime.Before(l[b].step.StartTime)
}