-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathcontroller_test.go
560 lines (470 loc) · 21.5 KB
/
controller_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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package controller
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/fake"
kubetesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
notificationApi "github.com/argoproj/notifications-engine/pkg/api"
"github.com/argoproj/notifications-engine/pkg/mocks"
"github.com/argoproj/notifications-engine/pkg/services"
"github.com/argoproj/notifications-engine/pkg/subscriptions"
"github.com/argoproj/notifications-engine/pkg/triggers"
)
var (
testGVR = schema.GroupVersionResource{Group: "argoproj.io", Resource: "applications", Version: "v1alpha1"}
testNamespace = "default"
logEntry = logrus.NewEntry(logrus.New())
notifiedAnnotationKey = subscriptions.NotifiedAnnotationKey()
)
func mustToJson(val interface{}) string {
res, err := json.Marshal(val)
if err != nil {
panic(err)
}
return string(res)
}
func withAnnotations(annotations map[string]string) func(obj *unstructured.Unstructured) {
return func(app *unstructured.Unstructured) {
app.SetAnnotations(annotations)
}
}
func newFakeClient(objects ...runtime.Object) *fake.FakeDynamicClient {
return fake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{testGVR: "List"}, objects...)
}
func newResource(name string, modifiers ...func(app *unstructured.Unstructured)) *unstructured.Unstructured {
app := unstructured.Unstructured{}
app.SetGroupVersionKind(schema.GroupVersionKind{Group: "argoproj.io", Kind: "application", Version: "v1alpha1"})
app.SetName(name)
app.SetNamespace(testNamespace)
for i := range modifiers {
modifiers[i](&app)
}
return &app
}
func newController(t *testing.T, ctx context.Context, client dynamic.Interface, opts ...Opts) (*notificationController, *mocks.MockAPI, error) {
mockCtrl := gomock.NewController(t)
go func() {
<-ctx.Done()
mockCtrl.Finish()
}()
mockAPI := mocks.NewMockAPI(mockCtrl)
resourceClient := client.Resource(testGVR)
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (object runtime.Object, err error) {
return resourceClient.List(context.Background(), options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
return resourceClient.Watch(context.Background(), options)
},
},
&unstructured.Unstructured{},
time.Minute,
cache.Indexers{},
)
go informer.Run(ctx.Done())
c := NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
return nil, nil, errors.New("failed to sync informers")
}
return c, mockAPI, nil
}
func newControllerWithNamespaceSupport(t *testing.T, ctx context.Context, client dynamic.Interface, opts ...Opts) (*notificationController, map[string]notificationApi.API, error) {
mockCtrl := gomock.NewController(t)
go func() {
<-ctx.Done()
mockCtrl.Finish()
}()
resourceClient := client.Resource(testGVR)
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (object runtime.Object, err error) {
return resourceClient.List(context.Background(), options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
return resourceClient.Watch(context.Background(), options)
},
},
&unstructured.Unstructured{},
time.Minute,
cache.Indexers{},
)
go informer.Run(ctx.Done())
apiMap := make(map[string]notificationApi.API)
mockAPIDefault := mocks.NewMockAPI(mockCtrl)
apiMap["default"] = mockAPIDefault
mockAPISelfService := mocks.NewMockAPI(mockCtrl)
apiMap["selfservice_namespace"] = mockAPISelfService
c := NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{ApiMap: apiMap}, opts...)
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
return nil, nil, errors.New("failed to sync informers")
}
return c, apiMap, nil
}
func TestSendsNotificationIfTriggered(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
receivedObj := map[string]interface{}{}
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
api.EXPECT().Send(mock.MatchedBy(func(obj map[string]interface{}) bool {
receivedObj = obj
return true
}), []string{"test"}, services.Destination{Service: "mock", Recipient: "recipient"}).Return(nil)
annotations, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
if err != nil {
logEntry.Errorf("Failed to process: %v", err)
}
assert.NoError(t, err)
state := NewState(annotations[notifiedAnnotationKey])
assert.NotNil(t, state[StateItemKey(false, "", "mock", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"})])
assert.Equal(t, app.Object, receivedObj)
}
func TestDoesNotSendNotificationIfAnnotationPresent(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
state := NotificationsState{}
_ = state.SetAlreadyNotified(false, "", "my-trigger", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"}, true)
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: mustToJson(state),
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
_, err = ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
if err != nil {
logEntry.Errorf("Failed to process: %v", err)
}
assert.NoError(t, err)
}
func TestDoesNotSendNotificationIfTooManyCommitStatusesReceived(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
setAnnotations := func(notifiedAnnoationKeyValue string) func(obj *unstructured.Unstructured) {
return withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: notifiedAnnoationKeyValue,
})
}
state := NotificationsState{}
_ = state.SetAlreadyNotified(false, "", "my-trigger", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"}, false)
app := newResource("test", setAnnotations(mustToJson(state)))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil).Times(2)
api.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(&services.TooManyCommitStatusesError{Sha: "sha", Context: "context"}).Times(1)
// First attempt should hit the TooManyCommitStatusesError.
// Returned annotations1 should contain the information about processed notification
// as a result of hitting the ToomanyCommitStatusesError error.
annotations1, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
assert.NoError(t, err)
// Persist the notification state in the annotations.
setAnnotations(annotations1[notifiedAnnotationKey])(app)
// The second attempt should see that the notification has already been processed
// and the value of the notification annotation should not change. In the second attempt api.Send is not called.
annotations2, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
assert.NoError(t, err)
assert.Equal(t, annotations1[notifiedAnnotationKey], annotations2[notifiedAnnotationKey])
}
func TestRetriesNotificationIfSendThrows(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
setAnnotations := func(notifiedAnnoationKeyValue string) func(obj *unstructured.Unstructured) {
return withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: notifiedAnnoationKeyValue,
})
}
state := NotificationsState{}
_ = state.SetAlreadyNotified(false, "", "my-trigger", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"}, false)
app := newResource("test", setAnnotations(mustToJson(state)))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil).Times(2)
api.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("boom")).Times(2)
// First attempt. The returned annotations should not contain the notification state due to the error.
annotations, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
assert.NoError(t, err)
assert.Empty(t, annotations[notifiedAnnotationKey])
// Second attempt. The returned annotations should not contain the notification state due to the error.
annotations, err = ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
assert.NoError(t, err)
assert.Empty(t, annotations[notifiedAnnotationKey])
}
func TestRemovesAnnotationIfNoTrigger(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
state := NotificationsState{}
_ = state.SetAlreadyNotified(false, "", "my-trigger", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"}, true)
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: mustToJson(state),
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)
annotations, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
if err != nil {
logEntry.Errorf("Failed to process: %v", err)
}
assert.NoError(t, err)
state = NewState(annotations[notifiedAnnotationKey])
assert.Empty(t, state)
}
func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
state := NotificationsState{}
_ = state.SetAlreadyNotified(false, "", "my-trigger", triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"}, true)
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
notifiedAnnotationKey: mustToJson(state),
}))
patchCh := make(chan []byte)
client := newFakeClient(app)
client.PrependReactor("patch", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
patchCh <- action.(kubetesting.PatchAction).GetPatch()
return true, nil, nil
})
ctrl, api, err := newController(t, ctx, client)
assert.NoError(t, err)
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)
go ctrl.Run(1, ctx.Done())
select {
case <-time.After(time.Second * 5):
t.Error("application was not patched")
case patchData := <-patchCh:
patch := map[string]interface{}{}
err = json.Unmarshal(patchData, &patch)
assert.NoError(t, err)
val, ok, err := unstructured.NestedFieldNoCopy(patch, "metadata", "annotations", notifiedAnnotationKey)
assert.NoError(t, err)
assert.True(t, ok)
assert.Nil(t, val)
}
}
func TestAnnotationIsTheSame(t *testing.T) {
t.Run("same", func(t *testing.T) {
app1 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
app2 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
assert.True(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("same-nil-nil", func(t *testing.T) {
app1 := newResource("test", withAnnotations(nil))
app2 := newResource("test", withAnnotations(nil))
assert.True(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("same-nil-emptyMap", func(t *testing.T) {
app1 := newResource("test", withAnnotations(nil))
app2 := newResource("test", withAnnotations(map[string]string{}))
assert.True(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("same-emptyMap-nil", func(t *testing.T) {
app1 := newResource("test", withAnnotations(map[string]string{}))
app2 := newResource("test", withAnnotations(nil))
assert.True(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("same-emptyMap-emptyMap", func(t *testing.T) {
app1 := newResource("test", withAnnotations(map[string]string{}))
app2 := newResource("test", withAnnotations(map[string]string{}))
assert.True(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("notSame-nil-map", func(t *testing.T) {
app1 := newResource("test", withAnnotations(nil))
app2 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
assert.False(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("notSame-map-nil", func(t *testing.T) {
app1 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
app2 := newResource("test", withAnnotations(nil))
assert.False(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
t.Run("notSame-map-map", func(t *testing.T) {
app1 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
app2 := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient2",
}))
assert.False(t, mapsEqual(app1.GetAnnotations(), app2.GetAnnotations()))
})
}
func TestWithEventCallback(t *testing.T) {
const triggerName = "my-trigger"
destination := services.Destination{Service: "mock", Recipient: "recipient"}
testCases := []struct {
description string
apiErr error
sendErr error
expectedDeliveries []NotificationDelivery
expectedErrors []error
expectedWarnings []error
}{
{
description: "EventCallback should be invoked with nil error on send success",
sendErr: nil,
expectedDeliveries: []NotificationDelivery{
{
Trigger: triggerName,
Destination: destination,
},
},
},
{
description: "EventCallback should be invoked with non-nil error on send failure",
sendErr: errors.New("this is a send error"),
expectedErrors: []error{
errors.New("failed to deliver notification my-trigger to {mock recipient}: this is a send error using the configuration in namespace "),
},
},
{
description: "EventCallback should be invoked with non-nil error on api failure",
apiErr: errors.New("this is an api error"),
expectedErrors: []error{
fmt.Errorf("this is an api error"),
},
},
}
var actualSequence *NotificationEventSequence
mockEventCallback := func(eventSequence NotificationEventSequence) {
actualSequence = &eventSequence
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
actualSequence = nil
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app), WithEventCallback(mockEventCallback))
ctrl.namespaceSupport = false
api.EXPECT().GetConfig().Return(notificationApi.Config{}).AnyTimes()
assert.NoError(t, err)
ctrl.apiFactory = &mocks.FakeFactory{Api: api, Err: tc.apiErr}
if tc.apiErr == nil {
api.EXPECT().RunTrigger(triggerName, gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
api.EXPECT().Send(mock.MatchedBy(func(obj map[string]interface{}) bool {
return true
}), []string{"test"}, destination).Return(tc.sendErr)
}
ctrl.processQueueItem()
assert.Equal(t, app, actualSequence.Resource)
assert.Equal(t, len(tc.expectedDeliveries), len(actualSequence.Delivered))
for i, event := range actualSequence.Delivered {
assert.Equal(t, tc.expectedDeliveries[i].Trigger, event.Trigger)
assert.Equal(t, tc.expectedDeliveries[i].Destination, event.Destination)
}
assert.Equal(t, tc.expectedErrors, actualSequence.Errors)
assert.Equal(t, tc.expectedWarnings, actualSequence.Warnings)
})
}
}
// verify annotations after calling processResourceWithAPI when using self-service
func TestProcessResourceWithAPIWithSelfService(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
ctrl, api, err := newController(t, ctx, newFakeClient(app))
assert.NoError(t, err)
ctrl.namespaceSupport = true
trigger := "my-trigger"
namespace := "my-namespace"
receivedObj := map[string]interface{}{}
//SelfService API: config has IsSelfServiceConfig set to true
api.EXPECT().GetConfig().Return(notificationApi.Config{IsSelfServiceConfig: true, Namespace: namespace}).AnyTimes()
api.EXPECT().RunTrigger(trigger, gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
api.EXPECT().Send(mock.MatchedBy(func(obj map[string]interface{}) bool {
receivedObj = obj
return true
}), []string{"test"}, services.Destination{Service: "mock", Recipient: "recipient"}).Return(nil)
annotations, err := ctrl.processResourceWithAPI(api, app, logEntry, &NotificationEventSequence{})
if err != nil {
logEntry.Errorf("Failed to process: %v", err)
}
assert.NoError(t, err)
state := NewState(annotations[notifiedAnnotationKey])
assert.NotZero(t, state[StateItemKey(true, namespace, trigger, triggers.ConditionResult{}, services.Destination{Service: "mock", Recipient: "recipient"})])
assert.Equal(t, app.Object, receivedObj)
}
// verify notification sent to both default and self-service configuration after calling processResourceWithAPI when using self-service
func TestProcessItemsWithSelfService(t *testing.T) {
const triggerName = "my-trigger"
destination := services.Destination{Service: "mock", Recipient: "recipient"}
var actualSequence *NotificationEventSequence
mockEventCallback := func(eventSequence NotificationEventSequence) {
actualSequence = &eventSequence
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
app := newResource("test", withAnnotations(map[string]string{
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))
ctrl, apiMap, err := newControllerWithNamespaceSupport(t, ctx, newFakeClient(app), WithEventCallback(mockEventCallback))
assert.NoError(t, err)
ctrl.namespaceSupport = true
//SelfService API: config has IsSelfServiceConfig set to true
apiMap["selfservice_namespace"].(*mocks.MockAPI).EXPECT().GetConfig().Return(notificationApi.Config{IsSelfServiceConfig: true, Namespace: "selfservice_namespace"}).Times(3)
apiMap["selfservice_namespace"].(*mocks.MockAPI).EXPECT().RunTrigger(triggerName, gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
apiMap["selfservice_namespace"].(*mocks.MockAPI).EXPECT().Send(mock.MatchedBy(func(obj map[string]interface{}) bool {
return true
}), []string{"test"}, destination).Return(nil).AnyTimes()
apiMap["default"].(*mocks.MockAPI).EXPECT().GetConfig().Return(notificationApi.Config{IsSelfServiceConfig: false, Namespace: "default"}).Times(3)
apiMap["default"].(*mocks.MockAPI).EXPECT().RunTrigger(triggerName, gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
apiMap["default"].(*mocks.MockAPI).EXPECT().Send(mock.MatchedBy(func(obj map[string]interface{}) bool {
return true
}), []string{"test"}, destination).Return(nil).AnyTimes()
ctrl.apiFactory = &mocks.FakeFactory{ApiMap: apiMap}
ctrl.processQueueItem()
assert.Equal(t, app, actualSequence.Resource)
expectedDeliveries := []NotificationDelivery{
{
Trigger: triggerName,
Destination: destination,
},
{
Trigger: triggerName,
Destination: destination,
},
}
for i, event := range actualSequence.Delivered {
assert.Equal(t, expectedDeliveries[i].Trigger, event.Trigger)
assert.Equal(t, expectedDeliveries[i].Destination, event.Destination)
}
}