Skip to content

Commit d116daf

Browse files
committed
Begin making more backwards compatible
First pass... Signed-off-by: Charlie Egan <[email protected]>
1 parent d9e42e8 commit d116daf

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

pkg/exporter/json_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func TestJSONExport(t *testing.T) {
7474
jsonExporter := JSONExporter{}
7575

7676
rc := &results.ResultCollection{
77-
&results.Result{ID: ruleToResult("r1"), Value: []string{}},
78-
&results.Result{ID: ruleToResult("r2"), Value: []string{"violation"}},
77+
&results.Result{ID: ruleToResult("r1"), Violations: []string{}},
78+
&results.Result{ID: ruleToResult("r2"), Violations: []string{"violation"}},
7979
}
8080

8181
expectedJSON := `{

pkg/results/results.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"log"
78
"sort"
89
"strings"
910

@@ -16,19 +17,22 @@ type outputResult map[string][]string
1617

1718
// Result holds the information about the result of a check
1819
type Result struct {
19-
ID string
20-
Value []string
21-
Package string
20+
ID string
21+
Violations []string
22+
Value interface{} // deprecated
23+
Package string
2224
}
2325

2426
// IsSuccessState returns true if Value is boolean and it is true.
2527
func (r *Result) IsSuccessState() bool {
26-
return len(r.Value) == 0
28+
success, ok := r.Value.(bool)
29+
return len(r.Violations) == 0 || (ok && success)
2730
}
2831

2932
// IsFailureState returns true if Value is boolean and it is false.
3033
func (r *Result) IsFailureState() bool {
31-
return len(r.Value) != 0
34+
success, ok := r.Value.(bool)
35+
return len(r.Violations) != 0 || (ok && !success)
3236
}
3337

3438
// ResultCollection is a collection of Result
@@ -99,14 +103,25 @@ func NewResultCollectionFromRegoResultSet(rs *rego.ResultSet) (*ResultCollection
99103

100104
rc := make(ResultCollection, 0, len(keys))
101105
for _, k := range keys {
102-
value, ok := values[k].([]string)
103-
if !ok {
104-
return nil, fmt.Errorf("format error, cannot unmarshall value '%+v' to []string", values[k])
106+
var violations []string
107+
boolValid, boolOk := values[k].(bool)
108+
strings, stringsOk := values[k].([]string)
109+
if boolOk {
110+
log.Printf("Using a boolean for `Value` is deprecated, found for key: (%s)", k)
111+
if boolValid {
112+
violations = []string{}
113+
} else {
114+
violations = []string{"missing violation context in rule definition"}
115+
}
116+
} else if stringsOk {
117+
violations = strings
118+
} else {
119+
return nil, fmt.Errorf("format error, cannot unmarshall value '%+v' to bool or []string", values[k])
105120
}
106121
rc = append(rc, &Result{
107-
ID: k,
108-
Value: value,
109-
Package: pkg,
122+
ID: k,
123+
Violations: violations,
124+
Package: pkg,
110125
})
111126
}
112127

@@ -165,7 +180,7 @@ func (r *ResultCollection) Serialize(w io.Writer) error {
165180
if len(result.Package) == 0 {
166181
return fmt.Errorf("missing Package in result with ID: %q", result.ID)
167182
}
168-
output[fmt.Sprintf("%s/%s", result.Package, result.ID)] = result.Value
183+
output[fmt.Sprintf("%s/%s", result.Package, result.ID)] = result.Violations
169184
}
170185

171186
e := json.NewEncoder(w)

pkg/results/results_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func TestIsSuccessState(t *testing.T) {
1616
result *Result
1717
want bool
1818
}{
19-
{&Result{ID: "1", Value: []string{}}, true},
20-
{&Result{ID: "2", Value: []string{"violation"}}, false},
19+
{&Result{ID: "1", Violations: []string{}}, true},
20+
{&Result{ID: "2", Violations: []string{"violation"}}, false},
2121
}
2222

2323
for idx, tc := range testCases {
@@ -34,9 +34,9 @@ func TestIsFailureState(t *testing.T) {
3434
result *Result
3535
want bool
3636
}{
37-
{&Result{ID: "1", Value: []string{}}, false},
38-
{&Result{ID: "2", Value: []string{"violation"}}, true},
39-
{&Result{ID: "3", Value: []string{"violation", "more violation"}}, true},
37+
{&Result{ID: "1", Violations: []string{}}, false},
38+
{&Result{ID: "2", Violations: []string{"violation"}}, true},
39+
{&Result{ID: "3", Violations: []string{"violation", "more violation"}}, true},
4040
}
4141

4242
for idx, tc := range testCases {
@@ -104,12 +104,12 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
104104
}
105105

106106
expectedResults := []*Result{
107-
&Result{ID: "_1_4_3", Value: []string{}, Package: "package.name"},
108-
&Result{ID: "_1_4_4", Value: []string{"violation"}, Package: "package.name"},
109-
&Result{ID: "_1_4_5", Value: []string{}, Package: "package.name"},
110-
&Result{ID: "_1_4_6", Value: []string{}, Package: "package.name"},
111-
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Value: []string{}, Package: "package.name"},
112-
&Result{ID: "node_pools_without_cloud_platform_scope", Value: []string{"violation"}, Package: "package.name"},
107+
&Result{ID: "_1_4_3", Violations: []string{}, Package: "package.name"},
108+
&Result{ID: "_1_4_4", Violations: []string{"violation"}, Package: "package.name"},
109+
&Result{ID: "_1_4_5", Violations: []string{}, Package: "package.name"},
110+
&Result{ID: "_1_4_6", Violations: []string{}, Package: "package.name"},
111+
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Violations: []string{}, Package: "package.name"},
112+
&Result{ID: "node_pools_without_cloud_platform_scope", Violations: []string{"violation"}, Package: "package.name"},
113113
}
114114

115115
rc, err := NewResultCollectionFromRegoResultSet(regoResultSet)
@@ -203,8 +203,8 @@ func errorsEqual(err1 error, err2 error) bool {
203203
}
204204

205205
func TestListPassing(t *testing.T) {
206-
a := &Result{ID: "a", Value: []string{}}
207-
b := &Result{ID: "b", Value: []string{"violation"}}
206+
a := &Result{ID: "a", Violations: []string{}}
207+
b := &Result{ID: "b", Violations: []string{"violation"}}
208208

209209
rc := &ResultCollection{a, b}
210210

@@ -228,8 +228,8 @@ func TestListPassing(t *testing.T) {
228228
}
229229

230230
func TestListFailing(t *testing.T) {
231-
a := &Result{ID: "a", Value: []string{}}
232-
b := &Result{ID: "b", Value: []string{"violation"}}
231+
a := &Result{ID: "a", Violations: []string{}}
232+
b := &Result{ID: "b", Violations: []string{"violation"}}
233233

234234
rc := &ResultCollection{a, b}
235235

@@ -253,9 +253,9 @@ func TestListFailing(t *testing.T) {
253253
}
254254

255255
func TestAdd(t *testing.T) {
256-
a := &Result{ID: "a", Value: []string{}}
257-
b := &Result{ID: "b", Value: []string{"violation"}}
258-
c := &Result{ID: "c", Value: []string{"violation"}}
256+
a := &Result{ID: "a", Violations: []string{}}
257+
b := &Result{ID: "b", Violations: []string{"violation"}}
258+
c := &Result{ID: "c", Violations: []string{"violation"}}
259259

260260
rc := NewResultCollection()
261261
rc.Add([]*Result{a})
@@ -269,8 +269,8 @@ func TestAdd(t *testing.T) {
269269
}
270270

271271
func TestByID(t *testing.T) {
272-
a := &Result{ID: "a", Value: []string{}}
273-
b := &Result{ID: "b", Value: []string{"violation"}}
272+
a := &Result{ID: "a"}
273+
b := &Result{ID: "b"}
274274

275275
rc := &ResultCollection{a, b}
276276

@@ -286,7 +286,7 @@ func TestByID(t *testing.T) {
286286
func TestSerialize(t *testing.T) {
287287
t.Run("returns error if Package if empty", func(t *testing.T) {
288288
rc := &ResultCollection{
289-
&Result{ID: "a", Value: []string{}, Package: ""},
289+
&Result{ID: "a", Violations: []string{}, Package: ""},
290290
}
291291

292292
var buf bytes.Buffer
@@ -301,10 +301,10 @@ func TestSerialize(t *testing.T) {
301301

302302
t.Run("writes desired JSON serialization of ResultCollection", func(t *testing.T) {
303303
rc := &ResultCollection{
304-
&Result{ID: "a", Package: "p1", Value: []string{}},
305-
&Result{ID: "b", Package: "p1", Value: []string{"violation"}},
306-
&Result{ID: "c", Package: "p1", Value: []string{}},
307-
&Result{ID: "d", Package: "p2", Value: []string{"violation"}},
304+
&Result{ID: "a", Package: "p1", Violations: []string{}},
305+
&Result{ID: "b", Package: "p1", Violations: []string{"violation"}},
306+
&Result{ID: "c", Package: "p1", Violations: []string{}},
307+
&Result{ID: "d", Package: "p2", Violations: []string{"violation"}},
308308
}
309309

310310
expectedSerialization := `{"p1/a":[],"p1/b":["violation"],"p1/c":[],"p2/d":["violation"]}`

0 commit comments

Comments
 (0)