forked from opencontainers/distribution-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
616 lines (572 loc) · 17 KB
/
reporter.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package conformance
import (
"bytes"
"fmt"
"html/template"
"io"
"log"
"math"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
)
const (
flowIndex = 2
categoryIndex = 3
specIndex = 4
setupString = "Setup"
htmlTemplate string = `<html>
<head>
<title>OCI Distribution Conformance Tests</title>
<style>
body {
padding: 10px 20px 10px 20px;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAG0lEQVQYV2Pce7zwv7NlPyMDFMAZGAIwlRgqAFydCAVv5m4UAAAAAElFTkSuQmCC") repeat;
/* background made with http://www.patternify.com/ */
}
table {
border-collapse: collapse;
width: 100%;
background-color: white;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {
background-color: #ffe39b;
}
.result {
padding: 1.25em 0 .25em 0.8em;
border: 1px solid #e1e1e1;
border-radius: 5px;
margin-top: 10px;
}
.red {
background: #ffc8c8;
}
pre.fail-message {
background: #f9a5a5;
padding: 20px;
margin-right: 10px;
display: inline-block;
border-radius: 4px;
font-size: 1.25em;
width: 94%;
overflow-x: auto;
max-width: 85%;
}
.green {
background: #c8ffc8;
padding: 1.25em 0 1.25em 0.8em;
}
.grey {
background: lightgrey;
padding: 1.25em 0 1.25em 0.8em;
}
.toggle {
border: 2px solid #3e3e3e;
cursor: pointer;
width: 1em;
text-align: center;
font-weight: bold;
display: inline;
font-family: monospace;
padding: 0 .25em 0 .25em;
margin: 1em 1em 1em 0;
font-size: 12pt;
color: #3e3e3e;
border-radius: 3px;
}
pre.pre-box {
background: #343a40;
color: #fff;
padding: 10px;
border: 1px solid gray;
display: inline-block;
border-radius: 4px;
width: 97%;
font-size: 1.25em;
overflow-x: auto;
max-height: 60em;
overflow-y: auto;
max-width: 85%;
}
.summary {
width: 100%;
height: auto;
padding: 0 0 .5em 0;
border-radius: 6px;
border: 1px solid #cccddd;
background: white;
}
.summary-bullet {
width: 100%;
height: auto;
display: flex;
flex-wrap: wrap;
padding: .5em .1em .1em .5em;
}
.bullet-left {
width: 25%;
font-weight: bold;
font-size: 100%;
}
.bullet-right {
width: auto;
font-family: monospace;
font-size: 110%;
}
.quick-summary {
width: 70%;
display: flex;
margin: 0 auto 0 0;
font-weight: bold;
font-size: 1.2em;
}
.darkgreen {
color: green;
}
.darkred {
color: red;
padding: 0 0 0 2em;
}
.darkgrey {
color: grey;
padding: 0 0 0 2em;
}
.meter {
border: 1px solid black;
margin: 0 .5em 0 auto;
display: flex;
height: 25px;
width: 45%;
}
@media only screen and (max-width: 600px) {
.meter {
display: none;
}
}
.meter-green {
height: 100%;
background: green;
width: {{ .PercentPassed -}}%;
}
.meter-red {
height: 100%;
background: red;
width: {{ .PercentFailed -}}%;
}
.meter-grey {
height: 100%;
background: grey;
width: {{ .PercentSkipped -}}%;
}
.subcategory {
background: white;
padding: 0px 20px 20px 20px;
border: 1px solid #cccddd;
border-radius: 6px;
}
h2 {
margin-top: 45px;
}
h4 {
vertical-align: bottom;
cursor: pointer;
}
</style>
<script>
function toggleOutput(id) {
var elem = document.getElementById(id);
var button = document.getElementById(id + "-button");
if (elem.style['display'] === 'block') {
button.innerHTML = "+";
elem.style['display'] = 'none';
} else {
button.innerHTML = "-";
elem.style['display'] = 'block';
}
}
</script>
</head>
<body>
<h1>OCI Distribution Conformance Tests</h1>
<table>
<tr>
</tr>
<tr>
<td class="bullet-left">Summary</td>
<td>
<div class="quick-summary">
{{- if gt .SuiteSummary.NumberOfPassedSpecs 0 -}}
<span class="darkgreen">
{{- if .AllPassed -}}All {{ end -}}{{ .SuiteSummary.NumberOfPassedSpecs }} passed</span>
{{- end -}}
{{- if gt .SuiteSummary.NumberOfFailedSpecs 0 -}}
<span class="darkred">
{{- if .AllFailed -}}All {{ end -}}{{ .SuiteSummary.NumberOfFailedSpecs }} failed</span>
{{- end -}}
{{- if gt .SuiteSummary.NumberOfSkippedSpecs 0 -}}
<span class="darkgrey">
{{- if .AllSkipped -}}All {{ end -}}{{ .SuiteSummary.NumberOfSkippedSpecs }} skipped</span>
{{- end -}}
<div class="meter">
<div class="meter-green"></div>
<div class="meter-red"></div>
<div class="meter-grey"></div>
</div>
</div>
</td>
</tr>
<tr>
<td class="bullet-left">Start Time</td>
<td>{{ .StartTimeString }}</td>
</tr>
<tr>
<td class="bullet-left">End Time</td>
<td>{{ .EndTimeString }}</td>
</tr>
<tr>
<td class="bullet-left">Time Elapsed</td>
<td>{{ .RunTime }}</td>
</tr>
<tr>
<td class="bullet-left">Test Version</td>
<td>{{ .Version }}</td>
</tr>
<tr>
<td class="bullet-left">Configuration</td>
<td><div class="bullet-right">
{{ range $i, $s := .EnvironmentVariables }}
{{ $s }}<br />
{{ end }}
</div></td>
</tr>
</table>
<div>
{{with .Suite}}
{{$suite := .M}}
{{range $i, $suiteKey := .Keys}}
{{$wf := index $suite $suiteKey}}
{{with $wf}}
{{ if .IsEnabled }}
<h2>{{$suiteKey}}</h2>
<div class="subcategory">
{{$workflow := .M}}
{{range $j, $workflowKey := .Keys}}
<h3>{{$workflowKey}}</h3>
{{$ctg := index $workflow $workflowKey}}
{{with $ctg}}
{{$category := .M}}
{{range $k, $categoryKey := .Keys}}
{{$s := index $category $categoryKey}}
{{if eq $s.State 4}}
<div class="result red">
<div id="output-box-{{$s.ID}}-button" class="toggle" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">+</div>
<h4 style="display: inline;" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">{{$s.Title}}</h4>
<br>
<div>
<div id="output-box-{{$s.ID}}" style="display: none;">
<pre class="pre-box">{{$s.CapturedOutput}}</pre>
</div>
</div>
<pre class="fail-message">{{$s.Failure.Message}}</pre>
<br>
</div>
{{else if eq $s.State 3}}
<div class="result green">
<div id="output-box-{{$s.ID}}-button" class="toggle" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">+</div>
<h4 style="display: inline;" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">{{$s.Title}}</h4>
<br>
<div id="output-box-{{$s.ID}}" style="display: none;">
<pre class="pre-box">{{$s.CapturedOutput}}</pre>
</div>
</div>
{{else if eq $s.State 2}}
<div class="result grey">
<div id="output-box-{{$s.ID}}-button" class="toggle" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">+</div>
<h4 style="display: inline;" onclick="javascript:toggleOutput('output-box-{{$s.ID}}')">{{$s.Title}}</h4>
<br>
<div id="output-box-{{$s.ID}}" style="display: none;">
<pre class="pre-box">{{$s.Failure.Message}}</pre>
</div>
</div>
{{end}}
{{end}}<br>
{{end}}
{{end}}
{{end}}
{{end}}
</div>
{{end}}
{{end}}
</div>
</body>
</html>
`
)
type (
summaryMap struct {
M map[string]snapShotList
Keys []string
Size int
}
suite struct {
M map[string]*workflow
Keys []string
Size int
}
workflow struct {
M map[string]*category
IsEnabled bool
Keys []string
}
category struct {
M map[string]specSnapshot
Keys []string
}
specSnapshot struct {
types.SpecSummary
ID int
Title string
Category string
Suite string
IsSetup bool
}
snapShotList []specSnapshot
httpDebugWriter struct {
CapturedOutput []string
debug bool
}
httpDebugLogger struct {
l *log.Logger
w io.Writer
}
HTMLReporter struct {
htmlReportFilename string
Suite suite
SpecSummaryMap summaryMap
EnvironmentVariables []string
SuiteSummary *types.SuiteSummary
debugLogger *httpDebugWriter
debugIndex int
enabledMap map[string]bool
PercentPassed int
PercentFailed int
PercentSkipped int
startTime time.Time
endTime time.Time
StartTimeString string
EndTimeString string
RunTime string
AllPassed bool
AllFailed bool
AllSkipped bool
Version string
}
)
func (sm *summaryMap) Add(key string, sum *specSnapshot) {
sm.M[key] = append(sm.M[key], *sum)
sm.Size++
if !sm.containsKey(key) {
sm.Keys = append(sm.Keys, key)
}
}
func (sm *summaryMap) containsKey(key string) bool {
var containsKey bool
for _, k := range sm.Keys {
if k == key {
containsKey = true
break
}
}
return containsKey
}
func newSpecSnapshot(sum *types.SpecSummary, id int) *specSnapshot {
var isSetup bool
suite := sum.ComponentTexts[flowIndex]
title := sum.ComponentTexts[specIndex]
category := sum.ComponentTexts[categoryIndex]
if category == setupString {
isSetup = true
}
return &specSnapshot{SpecSummary: *sum, Title: title, ID: id, IsSetup: isSetup, Category: category,
Suite: suite}
}
func newHTTPDebugWriter(debug bool) *httpDebugWriter {
return &httpDebugWriter{debug: debug}
}
func (writer *httpDebugWriter) Write(b []byte) (int, error) {
s := string(b)
writer.CapturedOutput = append(writer.CapturedOutput, s)
if writer.debug {
fmt.Println(s)
}
return len(b), nil
}
func newHTTPDebugLogger(f io.Writer) *httpDebugLogger {
debugLogger := &httpDebugLogger{w: f, l: log.New(f, "", log.Ldate|log.Lmicroseconds)}
return debugLogger
}
func (l *httpDebugLogger) Errorf(format string, v ...interface{}) {
l.output("ERROR "+format, v...)
}
func (l *httpDebugLogger) Warnf(format string, v ...interface{}) {
l.output("WARN "+format, v...)
}
func (l *httpDebugLogger) Debugf(format string, v ...interface{}) {
re := regexp.MustCompile("(?i)(\"?\\w*(authorization|token|state)\\w*\"?(:|=)\\s*)(\")?\\s*((bearer|basic)? )?[^\\s&\"]*(\")?")
format = re.ReplaceAllString(format, "$1$4$5*****$7")
l.output("DEBUG "+format, v...)
}
func (l *httpDebugLogger) output(format string, v ...interface{}) {
if len(v) == 0 {
l.l.Print(format)
return
}
_, err := l.w.Write([]byte(fmt.Sprintf(format, v...)))
if err != nil {
l.Errorf(err.Error())
}
}
func newHTMLReporter(htmlReportFilename string) (h *HTMLReporter) {
enabledMap := map[string]bool{
titlePull: true,
titlePush: true,
titleContentDiscovery: true,
titleContentManagement: true,
}
if os.Getenv(envVarHideSkippedWorkflows) == "1" {
enabledMap = map[string]bool{
titlePull: !userDisabled(pull),
titlePush: !userDisabled(push),
titleContentDiscovery: !userDisabled(contentDiscovery),
titleContentManagement: !userDisabled(contentManagement),
}
}
return &HTMLReporter{
htmlReportFilename: htmlReportFilename,
debugLogger: httpWriter,
enabledMap: enabledMap,
SpecSummaryMap: summaryMap{M: make(map[string]snapShotList)},
Suite: suite{
M: make(map[string]*workflow),
Keys: []string{},
},
}
}
func (reporter *HTMLReporter) SpecDidComplete(specSummary *types.SpecSummary) {
b := new(bytes.Buffer)
for _, co := range httpWriter.CapturedOutput[reporter.debugIndex:] {
fmt.Fprintf(b, "%s\n", co)
}
specSummary.CapturedOutput = b.String()
snapshot := newSpecSnapshot(specSummary, reporter.Suite.Size)
reporter.save(snapshot)
reporter.debugIndex = len(reporter.debugLogger.CapturedOutput)
}
func (reporter *HTMLReporter) save(snapshot *specSnapshot) {
suite := &reporter.Suite
ct := snapshot.ComponentTexts
suiteName, categoryName, specTitle := ct[flowIndex], ct[categoryIndex], ct[specIndex]
//make the map of categories
if _, ok := suite.M[suiteName]; !ok {
suite.M[suiteName] = &workflow{M: make(map[string]*category), Keys: []string{},
IsEnabled: reporter.enabledMap[suiteName]}
suite.Keys = append(suite.Keys, suiteName)
}
//make the map of snapshots
if _, ok := suite.M[suiteName].M[categoryName]; !ok {
suite.M[suiteName].M[categoryName] = &category{M: make(map[string]specSnapshot), Keys: []string{}}
z := suite.M[suiteName]
z.Keys = append(z.Keys, categoryName)
}
z := suite.M[suiteName].M[categoryName]
z.Keys = append(z.Keys, specTitle)
suite.M[suiteName].M[categoryName].M[specTitle] = *snapshot
suite.Size++
}
func (reporter *HTMLReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
reporter.endTime = time.Now()
reporter.EndTimeString = reporter.endTime.Format("Jan 2 15:04:05.000 -0700 MST")
reporter.RunTime = reporter.endTime.Sub(reporter.startTime).String()
reporter.PercentPassed = getPercent(summary.NumberOfPassedSpecs, summary.NumberOfTotalSpecs)
reporter.PercentSkipped = getPercent(summary.NumberOfSkippedSpecs, summary.NumberOfTotalSpecs)
reporter.PercentFailed = getPercent(summary.NumberOfFailedSpecs, summary.NumberOfTotalSpecs)
reporter.SuiteSummary = summary
reporter.AllPassed = summary.NumberOfPassedSpecs == summary.NumberOfTotalSpecs
reporter.AllFailed = summary.NumberOfFailedSpecs == summary.NumberOfTotalSpecs
reporter.AllSkipped = summary.NumberOfSkippedSpecs == summary.NumberOfTotalSpecs
t, err := template.New("report").Parse(htmlTemplate)
if err != nil {
log.Fatal(err)
}
htmlReportFilenameAbsPath, err := filepath.Abs(reporter.htmlReportFilename)
if err != nil {
log.Fatal(err)
}
htmlReportFile, err := os.Create(htmlReportFilenameAbsPath)
if err != nil {
log.Fatal(err)
}
defer htmlReportFile.Close()
err = t.ExecuteTemplate(htmlReportFile, "report", &reporter)
if err != nil {
log.Fatal(err)
}
fmt.Printf("HTML report was created: %s", htmlReportFilenameAbsPath)
}
//unused by HTML reporter
func (reporter *HTMLReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
varsToCheck := []string{
envVarRootURL,
envVarNamespace,
envVarUsername,
envVarPassword,
envVarDebug,
envVarPull,
envVarPush,
envVarContentDiscovery,
envVarContentManagement,
envVarPushEmptyLayer,
envVarBlobDigest,
envVarManifestDigest,
envVarTagName,
envVarTagList,
envVarHideSkippedWorkflows,
envVarAuthScope,
envVarCrossmountNamespace,
}
for _, v := range varsToCheck {
var replacement string
if envVar := os.Getenv(v); envVar != "" {
replacement = envVar
if strings.Contains(v, "PASSWORD") || strings.Contains(v, "USERNAME") {
replacement = "*****"
}
} else {
continue
}
reporter.EnvironmentVariables = append(reporter.EnvironmentVariables,
fmt.Sprintf("%s=%s", v, replacement))
}
reporter.startTime = time.Now()
reporter.StartTimeString = reporter.startTime.Format("Jan 2 15:04:05.000 -0700 MST")
reporter.Version = Version
}
func (reporter *HTMLReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
}
func (reporter *HTMLReporter) SpecWillRun(specSummary *types.SpecSummary) {
}
func (reporter *HTMLReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
}
func getPercent(i, of int) int {
return int(math.Round(float64(i) / float64(of) * 100))
}