forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliance_results.go
174 lines (153 loc) · 4.33 KB
/
compliance_results.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
package cmd
import (
"archive/tar"
"compress/gzip"
"io"
"sort"
"strings"
"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/util"
"github.com/onsi/ginkgo/reporters"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
complianceResultsLong = templates.LongDesc(`
Shows the results of the compliance tests
`)
complianceResultsExample = templates.Examples(`
# Show the compliance results
jx compliance results
`)
)
// ComplianceStatusOptions options for "compliance results" command
type ComplianceResultsOptions struct {
CommonOptions
}
// NewCmdComplianceResults creates a command object for the "compliance results" action, which
// shows the results of E2E compliance tests
func NewCmdComplianceResults(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &ComplianceResultsOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "results",
Short: "Shows the results of compliance tests",
Long: complianceResultsLong,
Example: complianceResultsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
return cmd
}
// Run implements the "compliance results" command
func (o *ComplianceResultsOptions) Run() error {
cc, err := o.Factory.CreateComplianceClient()
if err != nil {
return errors.Wrap(err, "could not create the compliance client")
}
cfg := &client.RetrieveConfig{
Namespace: complianceNamespace,
}
reader, err := cc.RetrieveResults(cfg)
if err != nil {
return errors.Wrap(err, "could not retrieve the compliance results")
}
resultsReader, err := untarResults(reader)
if err != nil {
return errors.Wrap(err, "could not extract the compliance results from archive")
}
gzr, err := gzip.NewReader(resultsReader)
if err != nil {
return errors.Wrap(err, "could not create a gzip reader for compliance results ")
}
testResults, err := cc.GetTests(gzr, "all")
if err != nil {
return errors.Wrap(err, "could not get the results of the compliance tests from the archive")
}
testResults = filterTests(
func(tc reporters.JUnitTestCase) bool {
return !results.Skipped(tc)
}, testResults)
sort.Sort(StatusSortedTestCases(testResults))
o.printResults(testResults)
return nil
}
// StatusSotedTestCase implements Sort by status of a list of test case
type StatusSortedTestCases []reporters.JUnitTestCase
var statuses = map[string]int{
"FAILED": 0,
"PASSED": 1,
"SKIPPED": 2,
"UNKNOWN": 3,
}
func (s StatusSortedTestCases) Len() int { return len(s) }
func (s StatusSortedTestCases) Less(i, j int) bool {
si := statuses[status(s[i])]
sj := statuses[status(s[j])]
return si < sj
}
func (s StatusSortedTestCases) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (o *ComplianceResultsOptions) printResults(junitResults []reporters.JUnitTestCase) {
table := o.CreateTable()
table.SetColumnAlign(1, util.ALIGN_LEFT)
table.SetColumnAlign(2, util.ALIGN_LEFT)
table.AddRow("STATUS", "TEST")
for _, t := range junitResults {
table.AddRow(status(t), t.Name)
}
table.Render()
}
func status(junitResult reporters.JUnitTestCase) string {
if results.Skipped(junitResult) {
return "SKIPPED"
} else if results.Failed(junitResult) {
return "FAILED"
} else if results.Passed(junitResult) {
return "PASSED"
} else {
return "UNKNOWN"
}
}
func untarResults(src io.Reader) (io.Reader, error) {
tarReader := tar.NewReader(src)
for {
header, err := tarReader.Next()
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
if strings.HasSuffix(header.Name, ".tar.gz") {
reader, writer := io.Pipe()
//TODO propagate the error out of the goroutine
go func(writer *io.PipeWriter) {
defer writer.Close()
io.Copy(writer, tarReader)
}(writer)
return reader, nil
}
}
return nil, errors.New("no compliance results archive found")
}
func filterTests(predicate func(testCase reporters.JUnitTestCase) bool, testCases []reporters.JUnitTestCase) []reporters.JUnitTestCase {
out := make([]reporters.JUnitTestCase, 0)
for _, tc := range testCases {
if predicate(tc) {
out = append(out, tc)
}
}
return out
}