github.com/adevinta/lava@v0.7.2/internal/engine/store_test.go (about) 1 // Copyright 2023 Adevinta 2 3 package engine 4 5 import ( 6 "os" 7 "testing" 8 "time" 9 10 report "github.com/adevinta/vulcan-report" 11 "github.com/google/go-cmp/cmp" 12 "github.com/google/go-cmp/cmp/cmpopts" 13 ) 14 15 func TestReportStoreUploadCheckData(t *testing.T) { 16 testdata := []struct { 17 kind string 18 file string 19 wantNilErr bool 20 }{ 21 { 22 kind: "reports", 23 file: "testdata/store/empty_report.json", 24 wantNilErr: true, 25 }, 26 { 27 kind: "reports", 28 file: "testdata/store/invalid_report.json", 29 wantNilErr: false, 30 }, 31 { 32 kind: "logs", 33 file: "testdata/store/log.txt", 34 wantNilErr: true, 35 }, 36 { 37 kind: "reports", 38 file: "testdata/store/report.json", 39 wantNilErr: true, 40 }, 41 { 42 kind: "unknown", 43 wantNilErr: false, 44 }, 45 } 46 47 var ( 48 want = make(map[string]report.Report) 49 store reportStore 50 ) 51 for _, td := range testdata { 52 var ( 53 content []byte 54 rep report.Report 55 ) 56 57 if td.file != "" { 58 var err error 59 if content, err = os.ReadFile(td.file); err != nil { 60 t.Fatalf("error reading file: %v", err) 61 } 62 63 if err := rep.UnmarshalJSONTimeAsString(content); err == nil { 64 want[rep.CheckID] = rep 65 } 66 } 67 68 link, err := store.UploadCheckData(rep.CheckID, td.kind, time.Time{}, content) 69 70 if (err == nil) != td.wantNilErr { 71 t.Errorf("unexpected error: %v", err) 72 } 73 74 if link != "" { 75 t.Errorf("unexpected link: %v", link) 76 } 77 } 78 79 if diff := cmp.Diff(want, store.reports); diff != "" { 80 t.Errorf("reports mismatch (-want +got):\n%v", diff) 81 } 82 } 83 84 func TestReportStoreSummary(t *testing.T) { 85 updates := []struct { 86 report report.Report 87 want []string 88 }{ 89 { 90 report: report.Report{ 91 CheckData: report.CheckData{ 92 Target: "https://example.com/", 93 CheckID: "check1", 94 Status: "RUNNING", 95 ChecktypeName: "vulcan-semgrep", 96 }, 97 }, 98 want: []string{ 99 `checktype=vulcan-semgrep target=https://example.com/ start=0001-01-01 00:00:00 +0000 UTC status=RUNNING`, 100 }, 101 }, 102 { 103 report: report.Report{ 104 CheckData: report.CheckData{ 105 Target: "https://example.org/", 106 CheckID: "check2", 107 Status: "RUNNING", 108 ChecktypeName: "vulcan-trivy", 109 }, 110 }, 111 want: []string{ 112 `checktype=vulcan-semgrep target=https://example.com/ start=0001-01-01 00:00:00 +0000 UTC status=RUNNING`, 113 `checktype=vulcan-trivy target=https://example.org/ start=0001-01-01 00:00:00 +0000 UTC status=RUNNING`, 114 }, 115 }, 116 { 117 report: report.Report{ 118 CheckData: report.CheckData{ 119 Target: "https://example.com/", 120 CheckID: "check1", 121 Status: "FINISHED", 122 ChecktypeName: "vulcan-semgrep", 123 }, 124 }, 125 want: []string{ 126 `checktype=vulcan-semgrep target=https://example.com/ start=0001-01-01 00:00:00 +0000 UTC status=FINISHED`, 127 `checktype=vulcan-trivy target=https://example.org/ start=0001-01-01 00:00:00 +0000 UTC status=RUNNING`, 128 }, 129 }, 130 } 131 132 var rs reportStore 133 for _, update := range updates { 134 content, err := update.report.MarshalJSONTimeAsString() 135 if err != nil { 136 t.Fatalf("unexpected marshal error: %v", err) 137 } 138 if _, err := rs.UploadCheckData(update.report.CheckID, "reports", time.Now(), content); err != nil { 139 t.Fatalf("unexpected upload error: %v", err) 140 } 141 142 got := rs.Summary() 143 144 opt := cmpopts.SortSlices(func(a, b string) bool { return a < b }) 145 if diff := cmp.Diff(update.want, got, opt); diff != "" { 146 t.Errorf("summaries mismatch (-want +got):\n%v", diff) 147 } 148 } 149 }