@@ -14,7 +14,7 @@ import (
1414
1515func TestJSONExport (t * testing.T ) {
1616 pm := & packaging.PolicyManifest {
17- SchemaVersion : "0.1.0 " ,
17+ SchemaVersion : "0.1.1 " ,
1818 Namespace : "test.org" ,
1919 ID : "test-pkg" ,
2020 PackageVersion : "1.2.3" ,
@@ -48,6 +48,17 @@ func TestJSONExport(t *testing.T) {
4848 "http://jetstack.io/docs2" ,
4949 },
5050 },
51+ packaging.Rule {
52+ ID : "r3" ,
53+ Name : "Another rule" ,
54+ Description : "This is another rule." ,
55+ Manual : false ,
56+ Remediation : "No remedy." ,
57+ Links : []string {
58+ "http://jetstack.io/docs" ,
59+ "http://jetstack.io/docs2" ,
60+ },
61+ },
5162 },
5263 },
5364 packaging.Section {
@@ -76,6 +87,7 @@ func TestJSONExport(t *testing.T) {
7687 rc := & results.ResultCollection {
7788 & results.Result {ID : ruleToResult ("r1" ), Violations : []string {}},
7889 & results.Result {ID : ruleToResult ("r2" ), Violations : []string {"violation" }},
90+ & results.Result {ID : "preflight_r3" , Violations : []string {"another violation" }},
7991 }
8092
8193 expectedJSON := `{
@@ -109,6 +121,19 @@ func TestJSONExport(t *testing.T) {
109121 "description": "This is another rule.",
110122 "name": "Another rule",
111123 "id": "r2"
124+ },
125+ {
126+ "missing": true,
127+ "success": false,
128+ "violations": [],
129+ "links": [
130+ "http://jetstack.io/docs",
131+ "http://jetstack.io/docs2"
132+ ],
133+ "remediation": "No remedy.",
134+ "description": "This is another rule.",
135+ "name": "Another rule",
136+ "id": "r3"
112137 }
113138 ],
114139 "description": "This is a section.",
@@ -177,3 +202,131 @@ func TestJSONExport(t *testing.T) {
177202 t .Fatalf ("got != want: %v" , differences )
178203 }
179204}
205+
206+ func TestJSONExportBackwardsCompatibility (t * testing.T ) {
207+ pm := & packaging.PolicyManifest {
208+ SchemaVersion : "0.1.0" ,
209+ Namespace : "test.org" ,
210+ ID : "test-pkg" ,
211+ PackageVersion : "1.2.3" ,
212+ Name : "Test Package" ,
213+ Description : "This is a test package." ,
214+ Sections : []packaging.Section {
215+ packaging.Section {
216+ ID : "section-1" ,
217+ Name : "Sample section" ,
218+ Description : "This is a section." ,
219+ Rules : []packaging.Rule {
220+ packaging.Rule {
221+ ID : "r1" ,
222+ Name : "A rule" ,
223+ Description : "This is a rule." ,
224+ Manual : false ,
225+ Remediation : "No remedy." ,
226+ Links : []string {
227+ "http://jetstack.io/docs" ,
228+ "http://jetstack.io/docs2" ,
229+ },
230+ },
231+ packaging.Rule {
232+ ID : "r2" ,
233+ Name : "Another rule" ,
234+ Description : "This is another rule." ,
235+ Manual : false ,
236+ Remediation : "No remedy." ,
237+ Links : []string {
238+ "http://jetstack.io/docs" ,
239+ "http://jetstack.io/docs2" ,
240+ },
241+ },
242+ },
243+ },
244+ },
245+ }
246+
247+ jsonExporter := JSONExporter {}
248+
249+ rc := & results.ResultCollection {
250+ & results.Result {ID : ruleToResult ("r1" ), Violations : []string {}},
251+ & results.Result {ID : legacyRuleToResult ("r2" ), Violations : []string {"violation" }},
252+ }
253+
254+ expectedJSON := `{
255+ "sections": [
256+ {
257+ "rules": [
258+ {
259+ "missing": false,
260+ "success": true,
261+ "violations": [],
262+ "links": [
263+ "http://jetstack.io/docs",
264+ "http://jetstack.io/docs2"
265+ ],
266+ "remediation": "No remedy.",
267+ "description": "This is a rule.",
268+ "name": "A rule",
269+ "id": "r1"
270+ },
271+ {
272+ "missing": false,
273+ "success": false,
274+ "violations": [
275+ "violation"
276+ ],
277+ "links": [
278+ "http://jetstack.io/docs",
279+ "http://jetstack.io/docs2"
280+ ],
281+ "remediation": "No remedy.",
282+ "description": "This is another rule.",
283+ "name": "Another rule",
284+ "id": "r2"
285+ }
286+ ],
287+ "description": "This is a section.",
288+ "name": "Sample section",
289+ "id": "section-1"
290+ }
291+ ],
292+ "description": "This is a test package.",
293+ "name": "Test Package",
294+ "package": "test-pkg",
295+ "package-information": {
296+ "id": "test-pkg",
297+ "namespace": "test.org",
298+ "version": "1.2.3"
299+ },
300+ "preflight-version": "development",
301+ "cluster": "",
302+ "timestamp": "0001-01-01T00:00:00Z",
303+ "id": ""
304+ }`
305+
306+ buf , err := jsonExporter .Export (context .Background (), pm , nil , rc )
307+ if err != nil {
308+ t .Fatalf ("unexpected err: %+v" , err )
309+ }
310+
311+ var got , want map [string ]interface {}
312+
313+ if err = json .Unmarshal ([]byte (expectedJSON ), & want ); err != nil {
314+ t .Fatalf ("%+v" , err )
315+ }
316+
317+ if err = json .Unmarshal (buf .Bytes (), & got ); err != nil {
318+ t .Fatalf ("%+v" , err )
319+ }
320+
321+ diff := gojsondiff .New ().CompareObjects (want , got )
322+
323+ if diff .Modified () {
324+ f := formatter .NewAsciiFormatter (want , formatter.AsciiFormatterConfig {ShowArrayIndex : true , Coloring : true })
325+ differences , err := f .Format (diff )
326+ if err != nil {
327+ t .Errorf ("could not format diff: %+v" , err )
328+ }
329+
330+ t .Fatalf ("got != want: %v" , differences )
331+ }
332+ }
0 commit comments