@@ -83,58 +83,131 @@ func TestNewAggregater(t *testing.T) {
8383}
8484
8585func TestAggregater_Add (t * testing.T ) {
86- tests := []struct {
87- name string
88- scanResult * ScanResult
89- wantStatus string
90- wantCVECount int
91- }{
92- {
93- name : "add result with error" ,
94- scanResult : & ScanResult {
95- Err : assert .AnError ,
86+ t .Run ("adds single CVE from scan result" , func (t * testing.T ) {
87+ agg := NewAggregater ()
88+ scanResult := ScanResult {
89+ ImageInfo : ImageInfo {ContainerName : "container1" },
90+ Cves : []CVE {
91+ {Name : "CVE-2021-1234" , Severity : ecrtypes .FindingSeverityCritical },
9692 },
97- wantStatus : "ERROR" ,
98- wantCVECount : 0 ,
99- },
100- {
101- name : "add result with nil findings" ,
102- scanResult : & ScanResult {},
103- wantStatus : "N/A" ,
104- wantCVECount : 0 ,
105- },
106- {
107- name : "add result with findings" ,
108- scanResult : & ScanResult {
109- ImageInfo : ImageInfo {
110- ContainerName : "test-container" ,
111- },
112- Cves : []CVE {
113- {
114- Name : "CVE-2021-1234" ,
115- Severity : ecrtypes .FindingSeverityCritical ,
116- },
117- {
118- Name : "CVE-2021-5678" ,
119- Severity : ecrtypes .FindingSeverityHigh ,
120- },
121- },
93+ }
94+
95+ agg .Add (scanResult )
96+
97+ assert .Equal (t , 1 , len (agg .cves ))
98+ assert .Equal (t , "CVE-2021-1234" , agg .cves ["CVE-2021-1234" ].Name )
99+ assert .Contains (t , agg .cveToContainers ["CVE-2021-1234" ].Values (), "container1" )
100+ })
101+
102+ t .Run ("adds multiple CVEs from single scan result" , func (t * testing.T ) {
103+ agg := NewAggregater ()
104+ scanResult := ScanResult {
105+ ImageInfo : ImageInfo {ContainerName : "container1" },
106+ Cves : []CVE {
107+ {Name : "CVE-2021-1111" , Severity : ecrtypes .FindingSeverityCritical },
108+ {Name : "CVE-2021-2222" , Severity : ecrtypes .FindingSeverityHigh },
109+ {Name : "CVE-2021-3333" , Severity : ecrtypes .FindingSeverityMedium },
122110 },
123- wantStatus : "VULNERABLE" ,
124- wantCVECount : 2 ,
125- },
126- }
111+ }
127112
128- for _ , tt := range tests {
129- t .Run (tt .name , func (t * testing.T ) {
130- agg := NewAggregater ()
131- agg .Add (tt .scanResult )
132- assert .Equal (t , 1 , len (agg .summaries ))
133- assert .Equal (t , tt .wantStatus , agg .summaries [tt .scanResult .ImageInfo .ContainerName ][0 ].Status )
134- assert .Equal (t , tt .wantCVECount , len (agg .cves ))
135- })
113+ agg .Add (scanResult )
136114
137- }
115+ assert .Equal (t , 3 , len (agg .cves ))
116+ assert .NotNil (t , agg .cves ["CVE-2021-1111" ])
117+ assert .NotNil (t , agg .cves ["CVE-2021-2222" ])
118+ assert .NotNil (t , agg .cves ["CVE-2021-3333" ])
119+ })
120+
121+ t .Run ("adds same CVE from multiple containers" , func (t * testing.T ) {
122+ agg := NewAggregater ()
123+ scanResult1 := ScanResult {
124+ ImageInfo : ImageInfo {ContainerName : "container1" },
125+ Cves : []CVE {
126+ {Name : "CVE-2021-1234" , Severity : ecrtypes .FindingSeverityCritical },
127+ },
128+ }
129+ scanResult2 := ScanResult {
130+ ImageInfo : ImageInfo {ContainerName : "container2" },
131+ Cves : []CVE {
132+ {Name : "CVE-2021-1234" , Severity : ecrtypes .FindingSeverityCritical },
133+ },
134+ }
135+
136+ agg .Add (scanResult1 )
137+ agg .Add (scanResult2 )
138+
139+ assert .Equal (t , 1 , len (agg .cves ))
140+ containers := agg .cveToContainers ["CVE-2021-1234" ].Values ()
141+ assert .Equal (t , 2 , len (containers ))
142+ assert .Contains (t , containers , "container1" )
143+ assert .Contains (t , containers , "container2" )
144+ })
145+
146+ t .Run ("stores scan result summary" , func (t * testing.T ) {
147+ agg := NewAggregater ()
148+ scanResult := ScanResult {
149+ ImageInfo : ImageInfo {ContainerName : "container1" },
150+ Cves : []CVE {},
151+ }
152+
153+ agg .Add (scanResult )
154+
155+ assert .Equal (t , 1 , len (agg .summaries ["container1" ]))
156+ })
157+
158+ t .Run ("appends multiple summaries for same container" , func (t * testing.T ) {
159+ agg := NewAggregater ()
160+ scanResult1 := ScanResult {
161+ ImageInfo : ImageInfo {ContainerName : "container1" },
162+ Cves : []CVE {},
163+ }
164+ scanResult2 := ScanResult {
165+ ImageInfo : ImageInfo {ContainerName : "container1" },
166+ Cves : []CVE {},
167+ }
168+
169+ agg .Add (scanResult1 )
170+ agg .Add (scanResult2 )
171+
172+ assert .Equal (t , 2 , len (agg .summaries ["container1" ]))
173+ })
174+
175+ t .Run ("does not duplicate CVE when added multiple times from same container" , func (t * testing.T ) {
176+ agg := NewAggregater ()
177+ scanResult1 := ScanResult {
178+ ImageInfo : ImageInfo {ContainerName : "container1" },
179+ Cves : []CVE {
180+ {Name : "CVE-2021-1234" , Severity : ecrtypes .FindingSeverityCritical },
181+ },
182+ }
183+ scanResult2 := ScanResult {
184+ ImageInfo : ImageInfo {ContainerName : "container1" },
185+ Cves : []CVE {
186+ {Name : "CVE-2021-1234" , Severity : ecrtypes .FindingSeverityCritical },
187+ },
188+ }
189+
190+ agg .Add (scanResult1 )
191+ agg .Add (scanResult2 )
192+
193+ assert .Equal (t , 1 , len (agg .cves ))
194+ containers := agg .cveToContainers ["CVE-2021-1234" ].Values ()
195+ assert .Equal (t , 1 , len (containers ))
196+ assert .Contains (t , containers , "container1" )
197+ })
198+
199+ t .Run ("handles empty CVE list" , func (t * testing.T ) {
200+ agg := NewAggregater ()
201+ scanResult := ScanResult {
202+ ImageInfo : ImageInfo {ContainerName : "container1" },
203+ Cves : []CVE {},
204+ }
205+
206+ agg .Add (scanResult )
207+
208+ assert .Equal (t , 0 , len (agg .cves ))
209+ assert .Equal (t , 1 , len (agg .summaries ["container1" ]))
210+ })
138211}
139212
140213func TestAggregater_SummarizeTotal (t * testing.T ) {
0 commit comments