Skip to content

Commit dd36207

Browse files
authored
Merge pull request #38 from everettraven/improvement/limit-jsonyaml-output
Limit output to only the set of validations that have errors/warnings
2 parents 69e6522 + 6499d1d commit dd36207

File tree

18 files changed

+68
-1212
lines changed

18 files changed

+68
-1212
lines changed

pkg/runner/results.go

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,62 @@ type Results struct {
4444
ServedVersionValidation map[string]map[string][]validations.ComparisonResult `json:"servedVersionValidation,omitempty"`
4545
}
4646

47+
// MarshalJSON is a custom JSON marshalling function
48+
// to ensure that we only include in the JSON/YAML rendered
49+
// output the set of validations that returned some form
50+
// of information (warnings/errors).
51+
func (rr *Results) MarshalJSON() ([]byte, error) {
52+
out := &struct {
53+
CRDValidation []validations.ComparisonResult `json:"crdValidation,omitempty"`
54+
SameVersionValidation map[string]map[string][]validations.ComparisonResult `json:"sameVersionValidation,omitempty"`
55+
ServedVersionValidation map[string]map[string][]validations.ComparisonResult `json:"servedVersionValidation,omitempty"`
56+
}{}
57+
58+
for _, result := range rr.CRDValidation {
59+
if result.IsZero() {
60+
continue
61+
}
62+
63+
out.CRDValidation = append(out.CRDValidation, result)
64+
}
65+
66+
out.SameVersionValidation = dropZeroResultsFromVersionedComparisonResults(rr.SameVersionValidation)
67+
out.ServedVersionValidation = dropZeroResultsFromVersionedComparisonResults(rr.ServedVersionValidation)
68+
69+
return json.Marshal(out) //nolint:wrapcheck
70+
}
71+
72+
// dropZeroResultsFromVersionedComparisonResults is a utility
73+
// function for dropping any results from a versioned comparison result
74+
// that does not contain any additional information (warnings/errors),
75+
// which is useful for only rendering the exact validations
76+
// that failed/warned.
77+
func dropZeroResultsFromVersionedComparisonResults(versionedComparisonResults map[string]map[string][]validations.ComparisonResult) map[string]map[string][]validations.ComparisonResult {
78+
versionMap := map[string]map[string][]validations.ComparisonResult{}
79+
80+
for version, paths := range versionedComparisonResults {
81+
pathMap := map[string][]validations.ComparisonResult{}
82+
83+
for path, comparisonResults := range paths {
84+
results := []validations.ComparisonResult{}
85+
86+
for _, result := range comparisonResults {
87+
if result.IsZero() {
88+
continue
89+
}
90+
91+
results = append(results, result)
92+
}
93+
94+
pathMap[path] = results
95+
}
96+
97+
versionMap[version] = pathMap
98+
}
99+
100+
return versionMap
101+
}
102+
47103
// Format is a representation of an output format.
48104
type Format string
49105

@@ -100,8 +156,6 @@ func (rr *Results) RenderYAML() (string, error) {
100156
func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
101157
var out strings.Builder
102158

103-
out.WriteString("# CRD Validations\n")
104-
105159
for _, result := range rr.CRDValidation {
106160
if len(result.Errors) > 0 {
107161
for _, err := range result.Errors {
@@ -114,15 +168,8 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
114168
out.WriteString(fmt.Sprintf("- **%s** - `WARNING` - %s\n", result.Name, err))
115169
}
116170
}
117-
118-
if len(result.Errors) == 0 && len(result.Warnings) == 0 {
119-
out.WriteString(fmt.Sprintf("- **%s** - ✓\n", result.Name))
120-
}
121171
}
122172

123-
out.WriteString("\n\n")
124-
out.WriteString("# Same Version Validations\n")
125-
126173
for version, result := range rr.SameVersionValidation {
127174
for property, results := range result {
128175
for _, propertyResult := range results {
@@ -137,17 +184,10 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
137184
out.WriteString(fmt.Sprintf("- **%s** - *%s* - %s - `WARNING` - %s\n", version, property, propertyResult.Name, err))
138185
}
139186
}
140-
141-
if len(propertyResult.Errors) == 0 && len(propertyResult.Warnings) == 0 {
142-
out.WriteString(fmt.Sprintf("- **%s** - *%s* - %s - ✓\n", version, property, propertyResult.Name))
143-
}
144187
}
145188
}
146189
}
147190

148-
out.WriteString("\n\n")
149-
out.WriteString("# Served Version Validations\n")
150-
151191
for version, result := range rr.ServedVersionValidation {
152192
for property, results := range result {
153193
for _, propertyResult := range results {
@@ -162,10 +202,6 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
162202
out.WriteString(fmt.Sprintf("- **%s** - *%s* - %s - `WARNING` - %s\n", version, property, propertyResult.Name, err))
163203
}
164204
}
165-
166-
if len(propertyResult.Errors) == 0 && len(propertyResult.Warnings) == 0 {
167-
out.WriteString(fmt.Sprintf("- **%s** - *%s* - %s - ✓\n", version, property, propertyResult.Name))
168-
}
169205
}
170206
}
171207
}
@@ -179,8 +215,6 @@ func (rr *Results) RenderMarkdown() string { //nolint:gocognit,cyclop
179215
func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
180216
var out strings.Builder
181217

182-
out.WriteString("CRD Validations\n")
183-
184218
for _, result := range rr.CRDValidation {
185219
if len(result.Errors) > 0 {
186220
for _, err := range result.Errors {
@@ -193,15 +227,8 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
193227
out.WriteString(fmt.Sprintf("- %s - WARNING - %s\n", result.Name, err))
194228
}
195229
}
196-
197-
if len(result.Errors) == 0 && len(result.Warnings) == 0 {
198-
out.WriteString(fmt.Sprintf("- %s - ✓\n", result.Name))
199-
}
200230
}
201231

202-
out.WriteString("\n\n")
203-
out.WriteString("Same Version Validations\n")
204-
205232
for version, result := range rr.SameVersionValidation {
206233
for property, results := range result {
207234
for _, propertyResult := range results {
@@ -216,17 +243,10 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
216243
out.WriteString(fmt.Sprintf("- %s - %s - %s - WARNING - %s\n", version, property, propertyResult.Name, err))
217244
}
218245
}
219-
220-
if len(propertyResult.Errors) == 0 && len(propertyResult.Warnings) == 0 {
221-
out.WriteString(fmt.Sprintf("- %s - %s - %s - ✓\n", version, property, propertyResult.Name))
222-
}
223246
}
224247
}
225248
}
226249

227-
out.WriteString("\n\n")
228-
out.WriteString("Served Version Validations\n")
229-
230250
for version, result := range rr.ServedVersionValidation {
231251
for property, results := range result {
232252
for _, propertyResult := range results {
@@ -241,10 +261,6 @@ func (rr *Results) RenderPlainText() string { //nolint:gocognit,cyclop
241261
out.WriteString(fmt.Sprintf("- %s - %s - %s - WARNING - %s\n", version, property, propertyResult.Name, err))
242262
}
243263
}
244-
245-
if len(propertyResult.Errors) == 0 && len(propertyResult.Warnings) == 0 {
246-
out.WriteString(fmt.Sprintf("- %s - %s - %s - ✓\n", version, property, propertyResult.Name))
247-
}
248264
}
249265
}
250266
}

pkg/validations/registry.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ type ComparisonResult struct {
5858
Warnings []string `json:"warnings,omitempty"`
5959
}
6060

61+
// IsZero is a utility method used to
62+
// identify if the ComparisonResult
63+
// returns any information (warnings/errors)
64+
// for its associated validation.
65+
func (cr ComparisonResult) IsZero() bool {
66+
return len(cr.Errors) == 0 && len(cr.Warnings) == 0
67+
}
68+
6169
// Factory is a function used for creating a Validation based on a
6270
// provided configuration. Should return an error if the Validation
6371
// cannot be successfully created with the provided configuration.

test/defaultvaluechanged/expected.json

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,12 @@
11
{
2-
"crdValidation": [
3-
{
4-
"name": "storedVersionRemoval"
5-
},
6-
{
7-
"name": "existingFieldRemoval"
8-
},
9-
{
10-
"name": "scope"
11-
}
12-
],
132
"sameVersionValidation": {
143
"v1alpha1": {
154
"^.spec.mode": [
16-
{
17-
"name": "required"
18-
},
19-
{
20-
"name": "type"
21-
},
22-
{
23-
"name": "minLength"
24-
},
255
{
266
"name": "default",
277
"errors": [
288
"default value changed : \"\\\"Managed\\\"\" -\u003e \"\\\"Unmanaged\\\"\""
299
]
30-
},
31-
{
32-
"name": "maximum"
33-
},
34-
{
35-
"name": "minimum"
36-
},
37-
{
38-
"name": "enum"
39-
},
40-
{
41-
"name": "maxProperties"
42-
},
43-
{
44-
"name": "description"
45-
},
46-
{
47-
"name": "minItems"
48-
},
49-
{
50-
"name": "minProperties"
51-
},
52-
{
53-
"name": "maxLength"
54-
},
55-
{
56-
"name": "maxItems"
57-
},
58-
{
59-
"name": "unhandled"
6010
}
6111
]
6212
}

test/defaultvaluenetnew/expected.json

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,12 @@
11
{
2-
"crdValidation": [
3-
{
4-
"name": "existingFieldRemoval"
5-
},
6-
{
7-
"name": "scope"
8-
},
9-
{
10-
"name": "storedVersionRemoval"
11-
}
12-
],
132
"sameVersionValidation": {
143
"v1alpha1": {
154
"^.spec.mode": [
16-
{
17-
"name": "enum"
18-
},
19-
{
20-
"name": "maxItems"
21-
},
22-
{
23-
"name": "minItems"
24-
},
25-
{
26-
"name": "maximum"
27-
},
28-
{
29-
"name": "maxProperties"
30-
},
315
{
326
"name": "default",
337
"errors": [
348
"default added when there was none previously : \"\\\"Managed\\\"\""
359
]
36-
},
37-
{
38-
"name": "minimum"
39-
},
40-
{
41-
"name": "maxLength"
42-
},
43-
{
44-
"name": "type"
45-
},
46-
{
47-
"name": "required"
48-
},
49-
{
50-
"name": "minLength"
51-
},
52-
{
53-
"name": "minProperties"
54-
},
55-
{
56-
"name": "description"
57-
},
58-
{
59-
"name": "unhandled"
6010
}
6111
]
6212
}

test/defaultvalueremoved/expected.json

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,12 @@
11
{
2-
"crdValidation": [
3-
{
4-
"name": "storedVersionRemoval"
5-
},
6-
{
7-
"name": "existingFieldRemoval"
8-
},
9-
{
10-
"name": "scope"
11-
}
12-
],
132
"sameVersionValidation": {
143
"v1alpha1": {
154
"^.spec.mode": [
16-
{
17-
"name": "minProperties"
18-
},
19-
{
20-
"name": "required"
21-
},
22-
{
23-
"name": "minItems"
24-
},
25-
{
26-
"name": "enum"
27-
},
28-
{
29-
"name": "description"
30-
},
31-
{
32-
"name": "maxItems"
33-
},
34-
{
35-
"name": "maximum"
36-
},
37-
{
38-
"name": "maxProperties"
39-
},
40-
{
41-
"name": "minimum"
42-
},
435
{
446
"name": "default",
457
"errors": [
468
"default value removed : \"\\\"Managed\\\"\""
479
]
48-
},
49-
{
50-
"name": "maxLength"
51-
},
52-
{
53-
"name": "type"
54-
},
55-
{
56-
"name": "minLength"
57-
},
58-
{
59-
"name": "unhandled"
6010
}
6111
]
6212
}

0 commit comments

Comments
 (0)