Skip to content

Commit bd67b0f

Browse files
author
Daniel Jimenez
committed
Test propagation of errors on notifications delivery
1 parent 5d61955 commit bd67b0f

File tree

3 files changed

+87
-25
lines changed

3 files changed

+87
-25
lines changed

pkg/notify/kafka_test.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package notify
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
"testing"
@@ -11,6 +12,10 @@ import (
1112
"github.com/adevinta/vulnerability-db/pkg/store"
1213
)
1314

15+
var (
16+
mockEventStreamClientErr = errors.New("mockEventStreamErr")
17+
)
18+
1419
type event struct {
1520
id string
1621
payload []byte
@@ -28,10 +33,14 @@ func (e event) String() string {
2833

2934
type mockEventStreamClient struct {
3035
EventStreamClient
31-
e event
36+
err error
37+
e event
3238
}
3339

3440
func (m *mockEventStreamClient) Push(id string, payload []byte, metadata map[string][]byte) error {
41+
if m.err != nil {
42+
return m.err
43+
}
3544
m.e = event{
3645
id,
3746
payload,
@@ -45,16 +54,29 @@ func (m *mockEventStreamClient) verify(want event) bool {
4554
}
4655

4756
func TestKafka_PushFinding(t *testing.T) {
57+
type fields struct {
58+
logger *log.Logger
59+
eventStreamCli *mockEventStreamClient
60+
}
61+
62+
logger := log.New()
63+
4864
testCases := []struct {
49-
name string
50-
f FindingNotification
51-
want event
65+
name string
66+
fields fields
67+
f FindingNotification
68+
want event
69+
wantErr error
5270
}{
5371
{
5472
// Note: Define the whole FindingNotification struct and expected JSON payload
5573
// so test fails if a modification is made to the notification struct fields or
5674
// JSON export definition, so we are explicitly aware of a version change.
5775
name: "Should send notification with version",
76+
fields: fields{
77+
logger: logger,
78+
eventStreamCli: &mockEventStreamClient{},
79+
},
5880
f: FindingNotification{
5981
FindingExpanded: store.FindingExpanded{
6082
Finding: store.Finding{
@@ -192,18 +214,28 @@ func TestKafka_PushFinding(t *testing.T) {
192214
},
193215
},
194216
},
217+
{
218+
name: "Should propagate eventStreamClient error",
219+
fields: fields{
220+
logger: logger,
221+
eventStreamCli: &mockEventStreamClient{
222+
err: mockEventStreamClientErr,
223+
},
224+
},
225+
wantErr: mockEventStreamClientErr,
226+
},
195227
}
196228

197-
mock := &mockEventStreamClient{}
198-
kafka := NewKafkaNotifier(mock, log.New())
199-
200229
for _, tc := range testCases {
201230
t.Run(tc.name, func(t *testing.T) {
202-
if err := kafka.PushFinding(tc.f); err != nil {
203-
t.Fatalf("expected no error, but got: %v", err)
231+
kafka := NewKafkaNotifier(tc.fields.eventStreamCli, tc.fields.logger)
232+
233+
err := kafka.PushFinding(tc.f)
234+
if !errors.Is(err, tc.wantErr) {
235+
t.Fatalf("expected error: %v but got: %v", tc.wantErr, err)
204236
}
205-
if !mock.verify(tc.want) {
206-
t.Fatalf("error verifying finding notification event.\ngot: %v\nwant: %v", mock.e, tc.want)
237+
if !tc.fields.eventStreamCli.verify(tc.want) {
238+
t.Fatalf("error verifying finding notification event.\ngot: %v\nwant: %v", tc.fields.eventStreamCli.e, tc.want)
207239
}
208240
})
209241
}

pkg/notify/multi_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestMulti_PushFinding(t *testing.T) {
4141
wantCalled: 2,
4242
},
4343
{
44-
name: "Should replay notification to multiple notifiers",
44+
name: "Should propagate error",
4545
notifiers: []*mockNotifier{
4646
{},
4747
{

pkg/processor/processor_test.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const (
2424
pqNotFoundErr = "sql: no rows in result set"
2525
)
2626

27+
var (
28+
mockNotifierErr = errors.New("mockNotifierErr")
29+
)
30+
2731
type mockNotifier struct {
2832
calls uint8
2933
}
@@ -35,9 +39,13 @@ func (n *mockNotifier) PushFinding(f notify.FindingNotification) error {
3539

3640
type inMemMockNotifier struct {
3741
sent []notify.FindingNotification
42+
err error
3843
}
3944

4045
func (n *inMemMockNotifier) PushFinding(f notify.FindingNotification) error {
46+
if n.err != nil {
47+
return n.err
48+
}
4149
n.sent = append(n.sent, f)
4250
return nil
4351
}
@@ -216,11 +224,12 @@ func TestNotifyOpenFindings(t *testing.T) {
216224
}
217225

218226
logger := log.New()
219-
tests := []struct {
227+
testCases := []struct {
220228
name string
221229
fields fields
222230
input input
223231
expectedNotifs []notify.FindingNotification
232+
expectedErr error
224233
}{
225234
{
226235
name: "Should send notificatons adding check tag",
@@ -256,24 +265,45 @@ func TestNotifyOpenFindings(t *testing.T) {
256265
},
257266
},
258267
},
268+
{
269+
name: "Should propagate error from notifier",
270+
fields: fields{
271+
logger: logger,
272+
notifier: &inMemMockNotifier{
273+
err: mockNotifierErr,
274+
},
275+
store: &mockStore{
276+
returnFindingsExpanded: []store.FindingExpanded{
277+
{Finding: store.Finding{ID: "1"}},
278+
},
279+
},
280+
},
281+
input: input{
282+
findingsState: []store.FindingState{
283+
{ID: "1"},
284+
},
285+
tag: "test-tag",
286+
},
287+
expectedErr: mockNotifierErr,
288+
},
259289
}
260290

261-
for _, tt := range tests {
262-
t.Run(tt.name, func(t *testing.T) {
291+
for _, tc := range testCases {
292+
t.Run(tc.name, func(t *testing.T) {
263293
processor := &CheckProcessor{
264-
notifier: tt.fields.notifier,
265-
store: tt.fields.store,
266-
resultsClient: tt.fields.resClient,
267-
logger: tt.fields.logger,
294+
notifier: tc.fields.notifier,
295+
store: tc.fields.store,
296+
resultsClient: tc.fields.resClient,
297+
logger: tc.fields.logger,
268298
}
269299

270-
err := processor.notifyFindings(tt.input.findingsState, tt.input.tag)
271-
if err != nil {
272-
t.Fatalf("expected no error but got: %v", err)
300+
err := processor.notifyFindings(tc.input.findingsState, tc.input.tag)
301+
if !errors.Is(err, tc.expectedErr) {
302+
t.Fatalf("expected error: %v but got: %v", tc.expectedErr, err)
273303
}
274-
got := tt.fields.notifier.(*inMemMockNotifier).sent
275-
if !reflect.DeepEqual(tt.expectedNotifs, got) {
276-
t.Errorf("expected:\n%v\nbut got:\n%v", got, tt.expectedNotifs)
304+
got := tc.fields.notifier.(*inMemMockNotifier).sent
305+
if !reflect.DeepEqual(tc.expectedNotifs, got) {
306+
t.Fatalf("expected:\n%v\nbut got:\n%v", got, tc.expectedNotifs)
277307
}
278308
})
279309
}

0 commit comments

Comments
 (0)