forked from RedHatInsights/vuln4shift-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_test.go
492 lines (452 loc) · 17.8 KB
/
consumer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package digestwriter
import (
"app/base/utils"
"encoding/json"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/stretchr/testify/assert"
)
//
//// Unit test definitions for functions and methods defined in source file
//// consumer.go
//
//import (
// "app/base/utils"
// "app/digestwriter"
// "regexp"
// "testing"
// "time"
//
// "github.com/DATA-DOG/go-sqlmock"
// "github.com/Shopify/sarama"
// "github.com/stretchr/testify/assert"
//)
//
//// Unit test definitions for functions and methods defined in source file
//// consumer.go
//
//func init() {
// // needed because init function from utils/kafka is run way before this file,
// // so the Cfg object is empty.
// utils.Cfg.LoggingLevel = "DEBUG"
// // init the logger so it does not have to be initialized in each test
// digestwriter.SetupLogger()
//}
//
//// NewDummyConsumerWithProcessor function returns a new, not running, instance of
//// KafkaConsumer as well as the Processor it uses.
//func NewDummyConsumerWithProcessor(t *testing.T) (*utils.KafkaConsumer, *digestwriter.DigestConsumer, sqlmock.Sqlmock) {
// storage, mock := NewMockStorage(t)
// consumer, processor := digestwriter.NewDummyConsumerWithProcessor(storage)
// return consumer, processor, mock
//}
//
//// TestParseEmptyMessage checks how empty message is handled by
//// consumer.
//func TestParseEmptyMessage(t *testing.T) {
// // empty message to be parsed
// const emptyMessage = ""
//
// // try to parse the message
// _, err := digestwriter.ParseMessage([]byte(emptyMessage))
//
// // check for error - it should be reported
// assert.EqualError(t, err, "unexpected end of JSON input")
//}
//
//// TestParseMessageMissingRequiredFields checks for mandatory fields
//func TestParseMessageMissingRequiredFields(t *testing.T) {
// // message to be parsed
// jsonMessage := []byte(`{
// "no_digests_msg": "true"
// }`)
// // try to parse the message
// _, err := digestwriter.ParseMessage(jsonMessage)
// // check for error - it should be reported
// assert.EqualError(t, err, "missing required attribute 'AccountNumber'")
//
// jsonMessage = []byte(`{
// "AccountNumber": 2
// }`)
// _, err = digestwriter.ParseMessage(jsonMessage)
// assert.EqualError(t, err, "missing required attribute 'OrgID'")
//
// jsonMessage = []byte(`{
// "AccountNumber": 2,
// "OrgID": 1
// }`)
// _, err = digestwriter.ParseMessage(jsonMessage)
// assert.EqualError(t, err, "missing required attribute 'ClusterName'")
//
// // message to be parsed
// jsonMessage = []byte(`{
// "ClusterName": "a_name"
// }`)
// _, err = digestwriter.ParseMessage(jsonMessage)
// assert.EqualError(t, err, "missing required attribute 'AccountNumber'")
//
// jsonMessage = []byte(`{
// "AccountNumber": 2,
// "OrgID": 1,
// "ClusterName": "a_name"
// }`)
// _, err = digestwriter.ParseMessage(jsonMessage)
// assert.EqualError(t, err, "missing required attribute 'Images'")
//}
//
//// TestParseMessageNoDigests checks that a valid message with no digests
//// is handled successfully by the consumer.
//func TestParseMessageNoDigests(t *testing.T) {
// // message to be parsed
// jsonMessage := []byte(`{
// "AccountNumber": 2,
// "OrgID": 1,
// "ClusterName": "a_name",
// "Images": {
// "images": {}
// }
// }`)
//
// // try to parse the message
// parsed, err := digestwriter.ParseMessage(jsonMessage)
// // check that no errors occur
// assert.Nil(t, err, "parseMessage should not fail if it contains no digests")
// assert.NotNil(t, parsed.Images.Digests)
// assert.Equal(t, 0, len(*parsed.Images.Digests))
//}
//
//// TestExtractDigestsFromMessage verify extraction of digests from correct message
//func TestExtractDigestsFromMessage(t *testing.T) {
// // message to be parsed
// jsonMessage := []byte(`{
// "any_other_field": "whatever",
// "AccountNumber": 2,
// "OrgID": 1,
// "ClusterName": "a_name",
// "Images": {
// "images": {
// "first_digest": {
// "extra_content": [
// "more_content_1",
// "more_content_2",
// "more_content_3"
// ],
// "extra_content": "extra_content_value"
// },
// "second_digest": {
// "second_digest_inner_data": "some_value"
// },
// "third_digest": {
// "extra_content": [
// "more_content_1",
// "more_content_2",
// "more_content_3"
// ],
// "extra_content": "extra_content_value",
// "extra_content_2": "extra_content_2_value",
// "extra_content_3": "extra_content_3_value"
// }
// }
// },
// "some_other_field": [ 1, 2, 3, 4]
// }`)
//
// // try to parse the message
// parsed, err := digestwriter.ParseMessage(jsonMessage)
// assert.Nil(t, err, "JSON should have been parsed correctly")
//
// digests := digestwriter.ExtractDigestsFromMessage(parsed.Images.Digests)
// assert.Equal(t, 3, len(digests))
// assert.Contains(t, digests, "first_digest")
// assert.Contains(t, digests, "second_digest")
// assert.Contains(t, digests, "third_digest")
//}
//
//// TestProcessEmptyMessage check the behaviour of function processMessage with
//// empty message on input.
//func TestProcessEmptyMessage(t *testing.T) {
// processor := digestwriter.DigestConsumer{}
// // prepare an empty message
// message := sarama.ConsumerMessage{}
// // try to process the message
// err := processor.ProcessMessage(&message)
// // check for errors - it should be reported
// assert.EqualError(t, err, "unexpected end of JSON input")
//}
//
//// TestProcessWrongMessageMissingFields check the behaviour of the ProcessMessage
//// function when received message does not contain the required fields.
//func TestProcessWrongMessageMissingFields(t *testing.T) {
// processor := digestwriter.DigestConsumer{}
// // prepare a message with a required field missing
// message := sarama.ConsumerMessage{}
// ConsumerMessageNoAccount := `{
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `"
// }`
// ConsumerMessageNoOrgID := `{
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `",
// "AccountNumber": 3
// }`
// ConsumerMessageNoClusterName := `{
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `",
// "AccountNumber": 3,
// "OrgID": 2
// }`
//
// ConsumerMessageNoImages := `{
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `",
// "AccountNumber": 3,
// "ClusterName": "test",
// "OrgID": 1
// }`
// // try to process the messages and check for errors
// message.Value = []byte(ConsumerMessageNoAccount)
// err := processor.ProcessMessage(&message)
// assert.EqualError(t, err, "missing required attribute 'AccountNumber'")
//
// message.Value = []byte(ConsumerMessageNoOrgID)
// err = processor.ProcessMessage(&message)
// assert.EqualError(t, err, "missing required attribute 'OrgID'")
//
// message.Value = []byte(ConsumerMessageNoClusterName)
// err = processor.ProcessMessage(&message)
// assert.EqualError(t, err, "missing required attribute 'ClusterName'")
//
// message.Value = []byte(ConsumerMessageNoImages)
// err = processor.ProcessMessage(&message)
// assert.EqualError(t, err, "missing required attribute 'Images'")
//}
//
//// TestProcessWrongMessageEmptyImages check the behaviour of the ProcessMessage function
//// when received message does not contain any digest.
//func TestProcessWrongMessageEmptyImages(t *testing.T) {
// // construct dummy consumer just to make sure the processor is correctly set
// dummyConsumer, processor, mock := NewDummyConsumerWithProcessor(t)
// // prepare a message with empty 'Images' field
// message := sarama.ConsumerMessage{}
// ConsumerMessage := `{
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `",
// "AccountNumber": 3,
// "ClusterName": "test",
// "OrgID": 4,
// "Images": {}
// }`
// message.Value = []byte(ConsumerMessage)
//
// // try to process the message using the consumer's Processor pointer
// err := dummyConsumer.Processor.ProcessMessage(&message)
//
// // check for errors - nothing should be reported
// assert.Nil(t, err, "received message does not need to contain any digest")
// // check that the counters are incremented accordingly
// assert.Equal(t, 1, int(processor.GetNumberOfMessagesWithEmptyDigests()))
//
// //check that no processMessage is aborted without any call to Storage
// assert.Nil(t, mock.ExpectationsWereMet(), "no SQL queries should have been made")
//}
//
//// expect these SQL statements to be called when consumed message is valid and has at least 1 digest
//func setHappyPathExpectations(mock sqlmock.Sqlmock) {
// // expected SQL statements during this test [SIMPLIFIED. This behavior is tested in storage_test.go]
// expectedSelectFromAccount := `SELECT * FROM "account" WHERE`
// expectedInsertIntoAccount := `INSERT INTO "account"`
// expectedSelectFromCluster := `SELECT "cluster"."id","cluster"."uuid","cluster"."account_id" FROM "cluster"`
// expectedInsertIntoCluster := `INSERT INTO "cluster"`
// expectedSelectFromImage := `SELECT * FROM "image"`
// expectedInsertIntoClusterImage := `INSERT INTO "cluster_image"`
//
// mock.ExpectBegin()
// mock.ExpectQuery(regexp.QuoteMeta(expectedSelectFromAccount)).
// WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// mock.ExpectQuery(regexp.QuoteMeta(expectedInsertIntoAccount)).
// WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
// mock.ExpectQuery(regexp.QuoteMeta(expectedSelectFromCluster)).
// WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// mock.ExpectQuery(regexp.QuoteMeta(expectedInsertIntoCluster)).
// WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
// mock.ExpectQuery(regexp.QuoteMeta(expectedSelectFromImage)).
// WithArgs(sqlmock.AnyArg()).
// WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
// mock.ExpectExec(regexp.QuoteMeta(expectedInsertIntoClusterImage)).
// WithArgs(1, 1).
// WillReturnResult(sqlmock.NewResult(1, 1))
// mock.ExpectCommit()
//}
//
//// TestProcessMessageWithExpectedFields check the behaviour of function ProcessMessage when
//// received message contains the 'images' field.
//func TestProcessMessageWithRequiredFields(t *testing.T) {
// // construct dummy consumer
// dummyConsumer, processor, mock := NewDummyConsumerWithProcessor(t)
// setHappyPathExpectations(mock)
//
// message := sarama.ConsumerMessage{}
// ConsumerMessage := `{
// "OrgID": 4,
// "AccountNumber": 3,
// "ClusterName": "84f7eedc-0000-0000-9d4d-000000000000",
// "Images": {
// "pods": 1,
// "clusters": 2,
// "timestamp": "` + time.Now().UTC().Format(time.RFC3339) + `",
// "images": {
// "first_digest": {
// "extra_content": [
// "more_content_1",
// "more_content_2",
// "more_content_3"
// ],
// "extra_content": "extra_content_value"
// }
// }
// }
// }`
// message.Value = []byte(ConsumerMessage)
//
// err := dummyConsumer.Processor.ProcessMessage(&message)
//
// // check for errors - nothing should be reported
// assert.Nil(t, err, "input message should be processed correctly")
// // check that the counters are incremented accordingly
// assert.Equal(t, 0, int(processor.GetNumberOfMessagesWithEmptyDigests()))
// // check all SQL-related expectations were met
// checkAllExpectations(t, mock)
//}
const (
testCCXMessage = `{ "OrgID": 14, "Arch": "amd64", "AccountNumber": 14, "ClusterName": "daac83ee-a390-420d-b892-cb9e1d006eca", "Images": { "imageCount": 1, "images": { "images": { "first_digest": { "extra_content": [ "more_content_1", "more_content_2", "more_content_3" ], "extra_content": "extra_content_value" }, "second_digest": { "second_digest_inner_data": "some_value" } } }, "namespaces": { "test": { "shapes": [] } } }, "Version": 1, "RequestID": "test-req" }`
testCCXMessageNoImages = `{ "OrgID": 14, "AccountNumber": 14, "ClusterName": "daac83ee-a390-420d-b892-cb9e1d006eca", "Images": { "imageCount": 0, "images": {}}, "Version": 1, "RequestID": "test-req" }`
testCCXMessageInvalidUUID = `{ "OrgID": 14, "Arch": "invalid", "AccountNumber": 14, "ClusterName": "daae-a390-420d-b892-cb006eca", "Images": { "imageCount": 1, "images": { "images": { "first_digest": { "extra_content": [ "more_content_1", "more_content_2", "more_content_3" ], "extra_content": "extra_content_value" }, "second_digest": { "second_digest_inner_data": "some_value" } } }, "namespaces": { "test": { "shapes": [] } } }, "Version": 1, "RequestID": "test-req" }`
)
func setupTestPayloadTracker(t *testing.T) (*utils.SaramaAsyncWriterMock, *utils.KafkaProducer) {
SetupLogger()
utils.SetupLogger()
usePayloadTracker = true
topic := "payload-tracker-topic"
writer := utils.CreateSaramaAsyncWriterMock()
go writer.StartProcessing(t)
producer := utils.CreateKafkaProducerMock(topic, writer)
return writer, producer
}
func setupTestDigestConsumer(t *testing.T, payloadTracker *utils.KafkaProducer) *DigestConsumer {
storage, err := NewStorage()
assert.Nil(t, err)
return &DigestConsumer{
storage: storage,
numberOfMessagesWithEmptyDigests: 0,
PayloadTracker: payloadTracker,
}
}
func awaitProcessingMessage(t *testing.T, testProducer *utils.KafkaProducer, testConsumer *DigestConsumer, msg *sarama.ConsumerMessage, successProduced, producingErrors int) error {
// Should trigger producing Payload Tracker message
err := testConsumer.ProcessMessage(msg)
var errMsg string
if err != nil {
errMsg = err.Error()
}
for ts := time.Now(); ; {
if testProducer.Enqueued == 0 &&
testProducer.GetNumberOfSuccessfullyProducedMessages() == uint64(successProduced) &&
testProducer.GetNumberOfErrorsProducingMessages() == uint64(producingErrors) {
break
}
if time.Since(ts) > time.Millisecond*3000 {
t.Fatalf("Kafka producer could not finish producing messages within time constraints. Err=%s", errMsg)
}
}
return err
}
func TestProcessMessage(t *testing.T) {
testWriter, testProducer := setupTestPayloadTracker(t)
defer testProducer.Close()
testConsumer := setupTestDigestConsumer(t, testProducer)
msg := &sarama.ConsumerMessage{
Timestamp: time.Now(),
Value: []byte(testCCXMessage),
Topic: "ccx.image.sha.results",
}
assert.Nil(t, awaitProcessingMessage(t, testProducer, testConsumer, msg, 2, 0))
// Should send received and success message only
assert.Equal(t, uint64(2), testProducer.GetNumberOfSuccessfullyProducedMessages())
assert.Equal(t, uint64(0), testProducer.GetNumberOfErrorsProducingMessages())
assert.Equal(t, 0, testProducer.Enqueued)
assert.Equal(t, 2, len(testWriter.ProcessedMessages))
expectedMessages := make([]utils.PayloadTrackerEvent, 0, 2)
for _, msg := range testWriter.ProcessedMessages {
bs, err := msg.Value.Encode()
assert.Nil(t, err)
var ptEvent utils.PayloadTrackerEvent
assert.Nil(t, json.Unmarshal(bs, &ptEvent))
expectedMessages = append(expectedMessages, ptEvent)
}
assert.Equal(t, 2, len(expectedMessages))
assert.Equal(t, utils.PayloadTrackerStatusReceived, expectedMessages[0].Status)
assert.Equal(t, utils.PayloadTrackerStatusSuccess, expectedMessages[1].Status)
}
func TestProcessMessageNoImages(t *testing.T) {
testWriter, testProducer := setupTestPayloadTracker(t)
defer testProducer.Close()
testConsumer := setupTestDigestConsumer(t, testProducer)
msg := &sarama.ConsumerMessage{
Timestamp: time.Now(),
Value: []byte(testCCXMessageNoImages),
Topic: "ccx.image.sha.results",
}
assert.Nil(t, awaitProcessingMessage(t, testProducer, testConsumer, msg, 2, 0))
bs, err := testWriter.ProcessedMessages[1].Value.Encode()
assert.Nil(t, err)
var ptEvent utils.PayloadTrackerEvent
assert.Nil(t, json.Unmarshal(bs, &ptEvent))
assert.Equal(t, "error", ptEvent.Status)
assert.Equal(t, "no digests were retrieved from incoming message", ptEvent.StatusMsg)
}
func TestProcessMessageUUID(t *testing.T) {
testWriter, testProducer := setupTestPayloadTracker(t)
defer testProducer.Close()
testConsumer := setupTestDigestConsumer(t, testProducer)
msg := &sarama.ConsumerMessage{
Timestamp: time.Now(),
Value: []byte(testCCXMessageInvalidUUID),
Topic: "ccx.image.sha.results",
}
err := awaitProcessingMessage(t, testProducer, testConsumer, msg, 2, 0)
assert.Equal(t, "invalid UUID length: 28", err.Error())
bs, err := testWriter.ProcessedMessages[1].Value.Encode()
assert.Nil(t, err)
var ptEvent utils.PayloadTrackerEvent
assert.Nil(t, json.Unmarshal(bs, &ptEvent))
assert.Equal(t, "error", ptEvent.Status)
assert.Equal(t, "error updating cluster data", ptEvent.StatusMsg)
}
func TestDigestMessageParse(t *testing.T) {
SetupLogger()
utils.SetupLogger()
validCases := [][]byte{
[]byte(`{"OrgID":12341446,"AccountNumber":6341839,"ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
[]byte(`{"OrgID":"12341446","AccountNumber":6341839,"ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
[]byte(`{"OrgID":12341446,"AccountNumber":"6341839","ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
[]byte(`{"OrgID":12341446,"AccountNumber":null,"ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
}
invalidCases := [][]byte{
[]byte(`{"OrgID":null,"AccountNumber":6341839,"ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
[]byte(`{"AccountNumber":6341839,"ClusterName":"04a816ea-cd0b-47c3-b754-a9008b127d84","Images":{"imageCount":2,"images":{},"namespaces":{}},"Version":2,"RequestID":"cbcbfeb72f074dffad1528dd209b130e"}`),
}
for _, msg := range validCases {
_, err := parseMessage(msg)
assert.Nil(t, err)
}
for _, msg := range invalidCases {
_, err := parseMessage(msg)
assert.Equal(t, "OrgID cannot be null", err.Error())
}
}