Skip to content

Commit 0e4ec6e

Browse files
committed
-s "adding test cases"
1 parent e7b7b2a commit 0e4ec6e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

pkg/controller/controller_test.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func newResource(name string, modifiers ...func(app *unstructured.Unstructured))
6565
return &app
6666
}
6767

68-
func newController(t *testing.T, ctx context.Context, client dynamic.Interface, opts ...Opts) (*notificationController, *mocks.MockAPI, error) {
68+
func newController(t *testing.T, ctx context.Context, client dynamic.Interface, factorySupport bool, opts ...Opts) (*notificationController, *mocks.MockAPI, error) {
6969
mockCtrl := gomock.NewController(t)
7070
go func() {
7171
<-ctx.Done()
@@ -90,7 +90,12 @@ func newController(t *testing.T, ctx context.Context, client dynamic.Interface,
9090

9191
go informer.Run(ctx.Done())
9292

93-
c := NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
93+
var c *notificationController
94+
if factorySupport {
95+
c = NewControllerWithFactorySupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
96+
} else {
97+
c = NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
98+
}
9499
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
95100
return nil, nil, errors.New("failed to sync informers")
96101
}
@@ -105,7 +110,7 @@ func TestSendsNotificationIfTriggered(t *testing.T) {
105110
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
106111
}))
107112

108-
ctrl, api, err := newController(t, ctx, newFakeClient(app))
113+
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
109114
assert.NoError(t, err)
110115

111116
receivedObj := map[string]interface{}{}
@@ -136,7 +141,7 @@ func TestDoesNotSendNotificationIfAnnotationPresent(t *testing.T) {
136141
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
137142
notifiedAnnotationKey: mustToJson(state),
138143
}))
139-
ctrl, api, err := newController(t, ctx, newFakeClient(app))
144+
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
140145
assert.NoError(t, err)
141146

142147
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: true, Templates: []string{"test"}}}, nil)
@@ -158,7 +163,7 @@ func TestRemovesAnnotationIfNoTrigger(t *testing.T) {
158163
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
159164
notifiedAnnotationKey: mustToJson(state),
160165
}))
161-
ctrl, api, err := newController(t, ctx, newFakeClient(app))
166+
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
162167
assert.NoError(t, err)
163168

164169
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)
@@ -173,6 +178,14 @@ func TestRemovesAnnotationIfNoTrigger(t *testing.T) {
173178
}
174179

175180
func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
181+
controllerRunAndVerifyResult(t, false)
182+
}
183+
184+
func TestSendsNotificationUsingAPIFromFactory(t *testing.T) {
185+
controllerRunAndVerifyResult(t, true)
186+
}
187+
188+
func controllerRunAndVerifyResult(t *testing.T, factorySupport bool) {
176189
ctx, cancel := context.WithCancel(context.TODO())
177190
defer cancel()
178191

@@ -191,7 +204,7 @@ func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
191204
patchCh <- action.(kubetesting.PatchAction).GetPatch()
192205
return true, nil, nil
193206
})
194-
ctrl, api, err := newController(t, ctx, client)
207+
ctrl, api, err := newController(t, ctx, client, false)
195208
assert.NoError(t, err)
196209
api.EXPECT().RunTrigger("my-trigger", gomock.Any()).Return([]triggers.ConditionResult{{Triggered: false}}, nil)
197210

@@ -322,7 +335,7 @@ func TestWithEventCallback(t *testing.T) {
322335
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
323336
}))
324337

325-
ctrl, api, err := newController(t, ctx, newFakeClient(app), WithEventCallback(mockEventCallback))
338+
ctrl, api, err := newController(t, ctx, newFakeClient(app), false, WithEventCallback(mockEventCallback))
326339
ctrl.namespaceSupport = false
327340
assert.NoError(t, err)
328341
ctrl.apiFactory = &mocks.FakeFactory{Api: api, Err: tc.apiErr}

pkg/mocks/factory.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ func (f *FakeFactory) GetAPIsFromNamespace(namespace string) (map[string]api.API
1818
}
1919

2020
func (f *FakeFactory) GetAPIsFromFactory(resource interface{}) (map[string]api.API, error) {
21-
return nil, f.Err
21+
apiMap := make(map[string]api.API)
22+
apiMap["key1"] = f.Api
23+
return apiMap, f.Err
2224
}

0 commit comments

Comments
 (0)