|
8 | 8 | "github.com/yudai/gojsondiff" |
9 | 9 | "github.com/yudai/gojsondiff/formatter" |
10 | 10 |
|
| 11 | + "github.com/jetstack/preflight/api" |
11 | 12 | "github.com/jetstack/preflight/pkg/packaging" |
12 | 13 | "github.com/jetstack/preflight/pkg/reports" |
13 | 14 | "github.com/jetstack/preflight/pkg/results" |
@@ -334,3 +335,85 @@ func TestJSONExportBackwardsCompatibility(t *testing.T) { |
334 | 335 | t.Fatalf("got != want: %v", differences) |
335 | 336 | } |
336 | 337 | } |
| 338 | + |
| 339 | +func TestJSONExportIndex(t *testing.T) { |
| 340 | + clusterSummary := api.ClusterSummary{ |
| 341 | + Cluster: "exampleCluster", |
| 342 | + LatestReportSet: &api.ReportSet{ |
| 343 | + Cluster: "exampleCluster", |
| 344 | + Timestamp: api.Time{}, |
| 345 | + FailureCount: 4, |
| 346 | + SuccessCount: 1, |
| 347 | + Reports: []*api.ReportSummary{ |
| 348 | + &api.ReportSummary{ |
| 349 | + ID: "exampleReport1", |
| 350 | + Package: "examplePackage.ID.1", |
| 351 | + Cluster: "exampleCluster", |
| 352 | + Timestamp: api.Time{}, |
| 353 | + FailureCount: 2, |
| 354 | + SuccessCount: 1, |
| 355 | + }, |
| 356 | + &api.ReportSummary{ |
| 357 | + ID: "exampleReport2", |
| 358 | + Package: "examplePackage.ID.2", |
| 359 | + Cluster: "exampleCluster", |
| 360 | + Timestamp: api.Time{}, |
| 361 | + FailureCount: 2, |
| 362 | + SuccessCount: 0, |
| 363 | + }, |
| 364 | + }, |
| 365 | + }, |
| 366 | + } |
| 367 | + |
| 368 | + exporter := NewJSONExporter() |
| 369 | + var ctx context.Context |
| 370 | + |
| 371 | + actualJSON, err := exporter.ExportIndex(ctx, &clusterSummary) |
| 372 | + if err != nil { |
| 373 | + t.Errorf("error exporting: %v", err) |
| 374 | + } |
| 375 | + |
| 376 | + expectedJSON := `{ |
| 377 | + "cluster": "exampleCluster", |
| 378 | + "latestReportSet": { |
| 379 | + "timestamp": "0001-01-01T00:00:00Z", |
| 380 | + "failureCount": 4, |
| 381 | + "successCount": 1, |
| 382 | + "reports": [ |
| 383 | + { |
| 384 | + "id": "exampleReport1", |
| 385 | + "package": "examplePackage.ID.1", |
| 386 | + "failureCount": 2, |
| 387 | + "successCount": 1 |
| 388 | + }, |
| 389 | + { |
| 390 | + "id": "exampleReport2", |
| 391 | + "package": "examplePackage.ID.2", |
| 392 | + "failureCount": 2, |
| 393 | + "successCount": 0 |
| 394 | + } |
| 395 | + ] |
| 396 | + } |
| 397 | +}` |
| 398 | + |
| 399 | + var got, want map[string]interface{} |
| 400 | + |
| 401 | + if err = json.Unmarshal([]byte(expectedJSON), &want); err != nil { |
| 402 | + t.Fatalf("%+v", err) |
| 403 | + } |
| 404 | + |
| 405 | + if err = json.Unmarshal(actualJSON.Bytes(), &got); err != nil { |
| 406 | + t.Fatalf("%+v", err) |
| 407 | + } |
| 408 | + diff := gojsondiff.New().CompareObjects(want, got) |
| 409 | + |
| 410 | + if diff.Modified() { |
| 411 | + f := formatter.NewAsciiFormatter(want, formatter.AsciiFormatterConfig{ShowArrayIndex: true, Coloring: true}) |
| 412 | + differences, err := f.Format(diff) |
| 413 | + if err != nil { |
| 414 | + t.Errorf("could not format diff: %+v", err) |
| 415 | + } |
| 416 | + |
| 417 | + t.Fatalf("got != want: %v", differences) |
| 418 | + } |
| 419 | +} |
0 commit comments