@@ -71,3 +71,147 @@ func TestSeverityPrinter_BSprintf(t *testing.T) {
7171 want := "\x1b [1m\x1b [35mtest critical\x1b [0m\x1b [0m"
7272 assert .Equal (t , want , got )
7373}
74+
75+ func TestNewAggregater (t * testing.T ) {
76+ agg := NewAggregater ()
77+ assert .NotNil (t , agg )
78+ assert .NotNil (t , agg .cves )
79+ assert .NotNil (t , agg .cveToSeverity )
80+ assert .NotNil (t , agg .summaries )
81+ assert .Equal (t , 0 , len (agg .cves ))
82+ assert .Equal (t , 0 , len (agg .cveToSeverity ))
83+ assert .Equal (t , 0 , len (agg .summaries ))
84+ }
85+
86+ func TestAggregater_Add (t * testing.T ) {
87+ tests := []struct {
88+ name string
89+ scanResult * ScanResult
90+ wantStatus string
91+ wantCVECount int
92+ }{
93+ {
94+ name : "add result with error" ,
95+ scanResult : & ScanResult {
96+ Err : assert .AnError ,
97+ ImageInfo : & ImageInfo {},
98+ },
99+ wantStatus : "ERROR" ,
100+ wantCVECount : 0 ,
101+ },
102+ {
103+ name : "add result with nil findings" ,
104+ scanResult : & ScanResult {
105+ ImageScanFindings : nil ,
106+ ImageInfo : & ImageInfo {},
107+ },
108+ wantStatus : "N/A" ,
109+ wantCVECount : 0 ,
110+ },
111+ {
112+ name : "add result with findings" ,
113+ scanResult : & ScanResult {
114+ ImageInfo : & ImageInfo {
115+ ContainerName : "test-container" ,
116+ },
117+ ImageScanFindings : & ecrtypes.ImageScanFindings {
118+ Findings : []ecrtypes.ImageScanFinding {
119+ {
120+ Name : stringPtr ("CVE-2021-1234" ),
121+ Severity : ecrtypes .FindingSeverityCritical ,
122+ },
123+ {
124+ Name : stringPtr ("CVE-2021-5678" ),
125+ Severity : ecrtypes .FindingSeverityHigh ,
126+ },
127+ },
128+ },
129+ },
130+ wantStatus : "VULNERABLE" ,
131+ wantCVECount : 2 ,
132+ },
133+ }
134+
135+ for _ , tt := range tests {
136+ t .Run (tt .name , func (t * testing.T ) {
137+ agg := NewAggregater ()
138+ agg .Add (tt .scanResult )
139+ assert .Equal (t , 1 , len (agg .summaries ))
140+ assert .Equal (t , tt .wantStatus , agg .summaries [tt .scanResult .ImageInfo .ContainerName ][0 ].Status )
141+ assert .Equal (t , tt .wantCVECount , len (agg .cves ))
142+ })
143+
144+ }
145+ }
146+
147+ func TestAggregater_SummarizeTotal (t * testing.T ) {
148+ agg := NewAggregater ()
149+ agg .cves = map [string ]ecrtypes.ImageScanFinding {
150+ "CVE-2021-1" : {Name : stringPtr ("CVE-2021-1" ), Severity : ecrtypes .FindingSeverityCritical },
151+ "CVE-2021-2" : {Name : stringPtr ("CVE-2021-2" ), Severity : ecrtypes .FindingSeverityHigh },
152+ "CVE-2021-3" : {Name : stringPtr ("CVE-2021-3" ), Severity : ecrtypes .FindingSeverityMedium },
153+ "CVE-2021-4" : {Name : stringPtr ("CVE-2021-4" ), Severity : ecrtypes .FindingSeverityLow },
154+ "CVE-2021-5" : {Name : stringPtr ("CVE-2021-5" ), Severity : ecrtypes .FindingSeverityInformational },
155+ }
156+ agg .cveToSeverity = map [string ]string {
157+ "CVE-2021-1" : string (ecrtypes .FindingSeverityCritical ),
158+ "CVE-2021-2" : string (ecrtypes .FindingSeverityHigh ),
159+ "CVE-2021-3" : string (ecrtypes .FindingSeverityMedium ),
160+ "CVE-2021-4" : string (ecrtypes .FindingSeverityLow ),
161+ "CVE-2021-5" : string (ecrtypes .FindingSeverityInformational ),
162+ }
163+
164+ result := agg .SummarizeTotal ()
165+ assert .Equal (t , int32 (1 ), result .CriticalCount )
166+ assert .Equal (t , int32 (1 ), result .HighCount )
167+ assert .Equal (t , int32 (1 ), result .MediumCount )
168+ assert .Equal (t , int32 (1 ), result .LowCount )
169+ assert .Equal (t , int32 (1 ), result .InfoCount )
170+ assert .Equal (t , int32 (5 ), result .TotalCount )
171+ assert .Equal (t , ecrtypes .FindingSeverityCritical , result .HighestSeverity )
172+ }
173+
174+ func TestAggregater_FilterCvesBySeverity (t * testing.T ) {
175+ agg := NewAggregater ()
176+ agg .cves = map [string ]ecrtypes.ImageScanFinding {
177+ "CVE-2021-1" : {Name : stringPtr ("CVE-2021-1" ), Severity : ecrtypes .FindingSeverityCritical },
178+ "CVE-2021-2" : {Name : stringPtr ("CVE-2021-2" ), Severity : ecrtypes .FindingSeverityHigh },
179+ "CVE-2021-3" : {Name : stringPtr ("CVE-2021-3" ), Severity : ecrtypes .FindingSeverityCritical },
180+ }
181+ agg .cveToSeverity = map [string ]string {
182+ "CVE-2021-1" : string (ecrtypes .FindingSeverityCritical ),
183+ "CVE-2021-2" : string (ecrtypes .FindingSeverityHigh ),
184+ "CVE-2021-3" : string (ecrtypes .FindingSeverityCritical ),
185+ }
186+
187+ critical := agg .CriticalCves ()
188+ assert .Equal (t , 2 , len (critical ))
189+
190+ high := agg .HighCves ()
191+ assert .Equal (t , 1 , len (high ))
192+
193+ medium := agg .MediumCves ()
194+ assert .Equal (t , 0 , len (medium ))
195+ }
196+
197+ func TestAggregateResult_SeverityCounts (t * testing.T ) {
198+ result := & AggregateResult {
199+ CriticalCount : 1 ,
200+ HighCount : 2 ,
201+ MediumCount : 3 ,
202+ LowCount : 4 ,
203+ InfoCount : 5 ,
204+ }
205+
206+ counts := result .SeverityCounts ()
207+ assert .Equal (t , 5 , len (counts ))
208+ assert .Equal (t , 5 , counts [0 ].Count )
209+ assert .Equal (t , 4 , counts [1 ].Count )
210+ assert .Equal (t , 3 , counts [2 ].Count )
211+ assert .Equal (t , 2 , counts [3 ].Count )
212+ assert .Equal (t , 1 , counts [4 ].Count )
213+ }
214+
215+ func stringPtr (s string ) * string {
216+ return & s
217+ }
0 commit comments