@@ -118,3 +118,129 @@ func TestEnrichments(t *testing.T) {
118
118
}
119
119
t .Logf ("wrote:\n %s" , buf .String ())
120
120
}
121
+
122
+ func TestIterationWithBreak (t * testing.T ) {
123
+ ctx := context .Background ()
124
+ a , err := New ()
125
+ if err != nil {
126
+ t .Fatal (err )
127
+ }
128
+
129
+ var want , got struct {
130
+ V []* claircore.Vulnerability
131
+ E []driver.EnrichmentRecord
132
+ }
133
+
134
+ want .V = test .GenUniqueVulnerabilities (10 , "test" )
135
+ ref , err := a .UpdateVulnerabilities (ctx , "test" , "" , want .V )
136
+ if err != nil {
137
+ t .Error (err )
138
+ }
139
+ t .Logf ("ref: %v" , ref )
140
+
141
+ // We will break after getting vulnerabilities.
142
+ test .GenEnrichments (15 )
143
+ ref , err = a .UpdateEnrichments (ctx , "test" , "" , want .E )
144
+ if err != nil {
145
+ t .Error (err )
146
+ }
147
+ t .Logf ("ref: %v" , ref )
148
+
149
+ var buf bytes.Buffer
150
+ defer func () {
151
+ t .Logf ("wrote:\n %s" , buf .String ())
152
+ }()
153
+ r , w := io .Pipe ()
154
+ eg , ctx := errgroup .WithContext (ctx )
155
+ eg .Go (func () error { defer w .Close (); return a .Store (w ) })
156
+ eg .Go (func () error {
157
+ i , iErr := Iterate (io .TeeReader (r , & buf ))
158
+ i (func (o * driver.UpdateOperation , i RecordIter ) bool {
159
+ i (func (v * claircore.Vulnerability , e * driver.EnrichmentRecord ) bool {
160
+ switch o .Kind {
161
+ case driver .VulnerabilityKind :
162
+ got .V = append (got .V , v )
163
+ case driver .EnrichmentKind :
164
+ got .E = append (got .E , * e )
165
+ default :
166
+ t .Errorf ("unnexpected kind: %s" , o .Kind )
167
+ }
168
+ return true
169
+ })
170
+ // Stop the operation iter, effectively skipping enrichments.
171
+ return false
172
+ })
173
+ return iErr ()
174
+ })
175
+ if err := eg .Wait (); err != nil {
176
+ t .Error (err )
177
+ }
178
+ if ! cmp .Equal (got , want ) {
179
+ t .Error (cmp .Diff (got , want ))
180
+ }
181
+ }
182
+
183
+ func TestIterationWithSkip (t * testing.T ) {
184
+ ctx := context .Background ()
185
+ a , err := New ()
186
+ if err != nil {
187
+ t .Fatal (err )
188
+ }
189
+
190
+ var want , got struct {
191
+ V []* claircore.Vulnerability
192
+ E []driver.EnrichmentRecord
193
+ }
194
+
195
+ want .V = test .GenUniqueVulnerabilities (10 , "test" )
196
+ ref , err := a .UpdateVulnerabilities (ctx , "test" , "" , want .V )
197
+ if err != nil {
198
+ t .Error (err )
199
+ }
200
+ t .Logf ("ref: %v" , ref )
201
+
202
+ // We will skip the updater "skip this".
203
+ test .GenUniqueVulnerabilities (10 , "skip this" )
204
+
205
+ want .E = test .GenEnrichments (15 )
206
+ ref , err = a .UpdateEnrichments (ctx , "test" , "" , want .E )
207
+ if err != nil {
208
+ t .Error (err )
209
+ }
210
+ t .Logf ("ref: %v" , ref )
211
+
212
+ var buf bytes.Buffer
213
+ defer func () {
214
+ t .Logf ("wrote:\n %s" , buf .String ())
215
+ }()
216
+ r , w := io .Pipe ()
217
+ eg , ctx := errgroup .WithContext (ctx )
218
+ eg .Go (func () error { defer w .Close (); return a .Store (w ) })
219
+ eg .Go (func () error {
220
+ i , iErr := Iterate (io .TeeReader (r , & buf ))
221
+ i (func (o * driver.UpdateOperation , i RecordIter ) bool {
222
+ if o .Updater == "skip this" {
223
+ return true
224
+ }
225
+ i (func (v * claircore.Vulnerability , e * driver.EnrichmentRecord ) bool {
226
+ switch o .Kind {
227
+ case driver .VulnerabilityKind :
228
+ got .V = append (got .V , v )
229
+ case driver .EnrichmentKind :
230
+ got .E = append (got .E , * e )
231
+ default :
232
+ t .Errorf ("unnexpected kind: %s" , o .Kind )
233
+ }
234
+ return true
235
+ })
236
+ return true
237
+ })
238
+ return iErr ()
239
+ })
240
+ if err := eg .Wait (); err != nil {
241
+ t .Error (err )
242
+ }
243
+ if ! cmp .Equal (got , want ) {
244
+ t .Error (cmp .Diff (got , want ))
245
+ }
246
+ }
0 commit comments