Skip to content

Commit

Permalink
-s "adding test cases"
Browse files Browse the repository at this point in the history
  • Loading branch information
mayzhang2000 committed Aug 7, 2023
1 parent e7b7b2a commit 0e4ec6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 20 additions & 7 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newResource(name string, modifiers ...func(app *unstructured.Unstructured))
return &app
}

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

go informer.Run(ctx.Done())

c := NewControllerWithNamespaceSupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
var c *notificationController
if factorySupport {
c = NewControllerWithFactorySupport(resourceClient, informer, &mocks.FakeFactory{Api: mockAPI}, opts...)
} else {
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")
}
Expand All @@ -105,7 +110,7 @@ func TestSendsNotificationIfTriggered(t *testing.T) {
subscriptions.SubscribeAnnotationKey("my-trigger", "mock"): "recipient",
}))

ctrl, api, err := newController(t, ctx, newFakeClient(app))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false)
assert.NoError(t, err)

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

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

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

func TestUpdatedAnnotationsSavedAsPatch(t *testing.T) {
controllerRunAndVerifyResult(t, false)
}

func TestSendsNotificationUsingAPIFromFactory(t *testing.T) {
controllerRunAndVerifyResult(t, true)
}

func controllerRunAndVerifyResult(t *testing.T, factorySupport bool) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

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

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

ctrl, api, err := newController(t, ctx, newFakeClient(app), WithEventCallback(mockEventCallback))
ctrl, api, err := newController(t, ctx, newFakeClient(app), false, WithEventCallback(mockEventCallback))
ctrl.namespaceSupport = false
assert.NoError(t, err)
ctrl.apiFactory = &mocks.FakeFactory{Api: api, Err: tc.apiErr}
Expand Down
4 changes: 3 additions & 1 deletion pkg/mocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ func (f *FakeFactory) GetAPIsFromNamespace(namespace string) (map[string]api.API
}

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

0 comments on commit 0e4ec6e

Please sign in to comment.