-
Notifications
You must be signed in to change notification settings - Fork 787
/
compliance_results.go
208 lines (183 loc) · 5.1 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
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
package cmd
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"sort"
"strings"
"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/heptio/sonobuoy/pkg/plugin/aggregation"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/onsi/ginkgo/reporters"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
var (
complianceResultsLong = templates.LongDesc(`
Shows the results of the compliance tests
`)
complianceResultsExample = templates.Examples(`
# Show the compliance results
jx compliance results
`)
)
// ComplianceResultsOptions 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 Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &ComplianceResultsOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
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()
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")
}
status, err := cc.GetStatus(complianceNamespace)
if err != nil {
return errors.Wrap(err, "failed to retrieve the compliance status")
}
if status.Status != aggregation.CompleteStatus && status.Status != aggregation.FailedStatus {
log.Infoln("Compliance results not ready. Run `jx compliance status` for status.")
return nil
}
cfg := &client.RetrieveConfig{
Namespace: complianceNamespace,
}
reader, errch := cc.RetrieveResults(cfg)
eg := &errgroup.Group{}
eg.Go(func() error { return <-errch })
eg.Go(func() error {
resultsReader, ec := untarResults(reader)
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)
err = <-ec
if err != nil {
return errors.Wrap(err, "could not extract the compliance results from archive")
}
return nil
})
err = eg.Wait()
if err != nil {
return errors.Wrap(err, "failed to retrieve the results")
}
return nil
}
// Exit the main goroutine with status
func (o *ComplianceResultsOptions) Exit(status int) {
os.Exit(status)
}
// StatusSortedTestCases 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, <-chan error) {
ec := make(chan error, 1)
tarReader := tar.NewReader(src)
reader, writer := io.Pipe()
for {
header, err := tarReader.Next()
if err != nil {
if err != io.EOF {
ec <- err
return reader, ec
}
break
}
if strings.HasSuffix(header.Name, ".tar.gz") {
go func(writer *io.PipeWriter, ec chan error) {
defer writer.Close()
defer close(ec)
_, err := io.Copy(writer, tarReader)
if err != nil {
ec <- err
}
tarReader.Next()
}(writer, ec)
break
}
}
return reader, ec
}
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
}