forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
368 lines (336 loc) · 9.47 KB
/
convert.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"path"
"strconv"
"strings"
"time"
"github.com/GoogleCloudPlatform/testgrid/resultstore"
"github.com/GoogleCloudPlatform/testgrid/util/gcs"
)
func dur(seconds float64) time.Duration {
return time.Duration(seconds * float64(time.Second))
}
// convertSuiteMeta converts a junit result in gcs to a ResultStore Suite.
func convertSuiteMeta(suiteMeta gcs.SuitesMeta) resultstore.Suite {
out := resultstore.Suite{
Name: path.Base(suiteMeta.Path),
Files: []resultstore.File{
{
ContentType: "text/xml",
ID: path.Base(suiteMeta.Path),
URL: suiteMeta.Path, // ensure the junit.xml file appears in artifacts list
},
},
}
for _, suite := range suiteMeta.Suites.Suites {
child := resultstore.Suite{
Name: suite.Name,
Duration: dur(suite.Time),
}
for _, test := range suite.Results {
if test.Properties != nil {
for _, p := range test.Properties.PropertyList {
resultProperty := resultstore.Property{
Key: fmt.Sprintf("%s:%s", test.Name, p.Name),
Value: p.Value,
}
child.Properties = append(child.Properties, resultProperty)
}
}
}
switch {
case suite.Failures > 0 && suite.Tests >= suite.Failures:
child.Failures = append(child.Failures, resultstore.Failure{
Message: fmt.Sprintf("%d out of %d tests failed (%.1f%% passing)", suite.Failures, suite.Tests, float64(suite.Tests-suite.Failures)*100.0/float64(suite.Tests)),
})
case suite.Failures > 0:
child.Failures = append(child.Failures, resultstore.Failure{
Message: fmt.Sprintf("%d tests failed", suite.Failures),
})
}
for _, result := range suite.Results {
name, tags := stripTags(result.Name)
class := result.ClassName
if class == "" {
class = strings.Join(tags, " ")
} else {
class += " " + strings.Join(tags, " ")
}
c := resultstore.Case{
Name: strings.TrimSpace(name),
Class: strings.TrimSpace(class),
Duration: dur(result.Time),
Result: resultstore.Completed,
}
const max = 5000 // truncate messages to this length
msg := result.Message(max)
switch {
case result.Failure != nil:
// failing tests have a completed result with an error
if msg == "" {
msg = "unknown failure"
}
c.Failures = append(c.Failures, resultstore.Failure{
Message: msg,
})
case result.Skipped != nil:
c.Result = resultstore.Skipped
if msg != "" { // skipped results do not require an error, but may.
c.Errors = append(c.Errors, resultstore.Error{
Message: msg,
})
}
}
child.Cases = append(child.Cases, c)
if c.Duration > child.Duration {
child.Duration = c.Duration
}
}
if child.Duration > out.Duration {
// Assume suites run in parallel, so choose max
out.Duration = child.Duration
}
out.Suites = append(out.Suites, child)
}
return out
}
// Convert converts build metadata stored in gcp into the corresponding ResultStore Invocation, Target and Test.
func convert(project, details string, url gcs.Path, result downloadResult, maxFiles int) (resultstore.Invocation, resultstore.Target, resultstore.Test) {
started := result.started
finished := result.finished
artifacts := result.artifactURLs
basePath := trailingSlash(url.String())
artifactsPath := basePath + "artifacts/"
buildLog := basePath + "build-log.txt"
bucket := url.Bucket()
jobName := prowJobName(url)
inv := resultstore.Invocation{
Project: project,
Details: details,
Files: []resultstore.File{
{
ID: resultstore.InvocationLog,
ContentType: "text/plain",
URL: buildLog, // ensure build-log.txt appears as the invocation log
},
},
Properties: []resultstore.Property{
{
Key: "Job",
Value: jobName,
},
{
Key: "Pull",
Value: started.Pull, // may be empty if pull value is not specified
},
},
}
startedProperties := startedReposToProperties(started.Repos)
inv.Properties = append(inv.Properties, startedProperties...)
// Files need a unique identifier, trim the common prefix and provide this.
seen := map[string]bool{}
uniqPath := func(s string) string {
want := strings.TrimPrefix(s, basePath)
var idx int
attempt := want
for {
if !seen[attempt] {
seen[attempt] = true
return attempt
}
idx++
attempt = want + " - " + strconv.Itoa(idx)
}
}
for i, a := range artifacts {
artifacts[i] = "gs://" + bucket + "/" + a
}
var total int
for _, a := range artifacts { // add started.json, etc to the invocation artifact list.
if total >= maxFiles {
continue
}
if strings.HasPrefix(a, artifactsPath) {
continue // things under artifacts/ are owned by the test
}
if a == buildLog {
continue // Handle this in InvocationLog
}
total++
inv.Files = append(inv.Files, resultstore.File{
ID: uniqPath(a),
ContentType: "text/plain",
URL: a,
})
}
if started.Timestamp > 0 {
inv.Start = time.Unix(started.Timestamp, 0)
if finished.Timestamp != nil {
inv.Duration = time.Duration(*finished.Timestamp-started.Timestamp) * time.Second
}
}
const day = 24 * 60 * 60
switch {
case finished.Timestamp == nil && started.Timestamp < time.Now().Unix()+day:
inv.Status = resultstore.Running
inv.Description = "In progress..."
case finished.Passed != nil && *finished.Passed:
inv.Status = resultstore.Passed
inv.Description = "Passed"
case finished.Timestamp == nil:
inv.Status = resultstore.Failed
inv.Description = "Timed out"
default:
inv.Status = resultstore.Failed
inv.Description = "Failed"
}
test := resultstore.Test{
Action: resultstore.Action{
Node: started.Node,
},
Suite: resultstore.Suite{
Name: "test",
Files: []resultstore.File{
{
ID: resultstore.TargetLog,
ContentType: "text/plain",
URL: buildLog, // ensure build-log.txt appears as the target log.
},
},
},
}
for _, suiteMeta := range result.suiteMetas {
child := convertSuiteMeta(suiteMeta)
test.Suite.Suites = append(test.Suite.Suites, child)
for _, f := range child.Files {
f.ID = uniqPath(f.URL)
test.Suite.Files = append(test.Suite.Files, f)
}
}
for _, a := range artifacts {
if total >= maxFiles {
continue
}
if !strings.HasPrefix(a, artifactsPath) {
continue // Non-artifacts (started.json, etc) are owned by the invocation
}
if a == buildLog {
continue // Already in the list.
}
// TODO(fejta): use set.Strings instead
var found bool
for _, sm := range result.suiteMetas {
if sm.Path == a {
found = true
break
}
}
if found {
continue
}
total++
test.Suite.Files = append(test.Suite.Files, resultstore.File{
ID: uniqPath(a),
ContentType: "text/plain",
URL: a,
})
}
if total >= maxFiles {
// TODO(fejta): expose this to edge case to user in a better way
inv.Files = append(inv.Files, resultstore.File{
ID: fmt.Sprintf("exceeded %d files", maxFiles),
ContentType: "text/plain",
URL: basePath,
})
}
test.Suite.Start = inv.Start
test.Action.Start = inv.Start
test.Suite.Duration = inv.Duration
test.Action.Duration = inv.Duration
test.Status = inv.Status
test.Description = inv.Description
target := resultstore.Target{
Start: inv.Start,
Duration: inv.Duration,
Status: inv.Status,
Description: inv.Description,
Properties: []resultstore.Property{},
}
for _, suites := range test.Suite.Suites {
for _, s := range suites.Suites {
target.Properties = append(target.Properties, s.Properties...)
}
}
return inv, target, test
}
func startedReposToProperties(gitRepos map[string]string) []resultstore.Property {
var properties []resultstore.Property
knownOrg := make(map[string]bool)
knownBranch := make(map[string]bool)
for repo, branch := range gitRepos {
orgRepo := strings.SplitN(repo, "/", 2)
org := orgRepo[0]
repoName := ""
if len(orgRepo) == 2 {
repoName = orgRepo[1]
} else {
repoName = org
}
if _, ok := knownOrg[org]; !ok {
knownOrg[org] = true
orgName := resultstore.Property{
Key: "Org",
Value: org,
}
properties = append(properties, orgName)
}
if _, ok := knownBranch[branch]; !ok {
knownBranch[branch] = true
branchName := resultstore.Property{
Key: "Branch",
Value: branch,
}
properties = append(properties, branchName)
}
repos := []resultstore.Property{
{
Key: "Repo",
Value: repoName,
},
{
Key: "Repo",
Value: repo,
},
{
Key: "Repo",
Value: fmt.Sprintf("%s:%s", repo, branch),
},
}
properties = append(properties, repos...)
}
return properties
}
// prowJobName returns the prow job name parsed from the GCS bucket.
// If parsing fails, it returns an empty string.
// TODO: use prowjob.json when PR 15785 is done.
func prowJobName(url gcs.Path) string {
paths := strings.Split(strings.TrimSuffix(url.Object(), "/"), "/")
// Expect the returned split to contain ["logs", <job name>, <uid>]
if len(paths) < 3 {
return ""
}
return paths[len(paths)-2]
}