Skip to content

Commit

Permalink
remove WaitForCacheSync (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmnote authored Aug 17, 2024
1 parent 8fd3d34 commit d926407
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 49 deletions.
4 changes: 0 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func getManagerOptions() ctrl.Options {
webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: tlsOpts,
})
fmt.Println(webhookServer)

metricsServerOptions := metricsserver.Options{
BindAddress: metricsAddr,
Expand All @@ -125,9 +124,6 @@ func getManagerOptions() ctrl.Options {
}

func run(mgr ctrl.Manager) error {
if !mgr.GetCache().WaitForCacheSync(context.Background()) {
return fmt.Errorf("failed to wait for cache sync")
}
ns, exists := os.LookupEnv("POD_NAMESPACE")
if !exists || ns == "" {
return errors.New("POD_NAMESPACE environment variable is not set or is empty")
Expand Down
71 changes: 26 additions & 45 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ import (
"github.com/kuoss/ingress-annotator/pkg/testutil/mockrulesstore"
)

func setupMockManager(mockCtrl *gomock.Controller) *mocks.MockManager {
mockManager := mocks.NewMockManager(mockCtrl)
mockCache := mocks.NewMockCache(mockCtrl)

scheme := fakeclient.NewScheme()

fakeClient := fakeclient.NewClient(nil, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-namespace",
Name: "ingress-annotator",
},
})

mockManager.EXPECT().GetCache().Return(mockCache).AnyTimes()
mockManager.EXPECT().GetClient().Return(fakeClient).AnyTimes()
mockManager.EXPECT().GetScheme().Return(scheme).AnyTimes()
mockManager.EXPECT().GetControllerOptions().Return(config.Controller{}).AnyTimes()
mockManager.EXPECT().Add(gomock.Any()).Return(nil).AnyTimes()
mockManager.EXPECT().AddHealthzCheck(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockManager.EXPECT().AddReadyzCheck(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockManager.EXPECT().GetLogger().Return(zap.New(zap.WriteTo(nil))).AnyTimes()
mockManager.EXPECT().GetAPIReader().Return(fakeClient).AnyTimes()
return mockManager
}
func TestGetManagerOptions(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
metricsAddr := fs.String("metrics-bind-address", "0", "")
Expand Down Expand Up @@ -55,66 +79,23 @@ func TestGetManagerOptions(t *testing.T) {
assert.Equal(t, "annotator.ingress.kubernetes.io", opts.LeaderElectionID, "Expected leader election ID to match")
}

func setupMockManager(mockCtrl *gomock.Controller) (*mocks.MockManager, *mocks.MockCache) {
mockManager := mocks.NewMockManager(mockCtrl)
mockCache := mocks.NewMockCache(mockCtrl)

scheme := fakeclient.NewScheme()

fakeClient := fakeclient.NewClient(nil, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-namespace",
Name: "ingress-annotator",
},
})

mockManager.EXPECT().GetCache().Return(mockCache).AnyTimes()
mockManager.EXPECT().GetClient().Return(fakeClient).AnyTimes()
mockManager.EXPECT().GetScheme().Return(scheme).AnyTimes()
mockManager.EXPECT().GetControllerOptions().Return(config.Controller{}).AnyTimes()
mockManager.EXPECT().AddHealthzCheck(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockManager.EXPECT().AddReadyzCheck(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
mockManager.EXPECT().GetLogger().Return(zap.New(zap.WriteTo(nil))).AnyTimes()
mockManager.EXPECT().GetAPIReader().Return(fakeClient).AnyTimes()
return mockManager, mockCache
}

func TestRun_PODNamespaceNotSet(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockManager, mockCache := setupMockManager(mockCtrl)

mockCache.EXPECT().WaitForCacheSync(gomock.Any()).Return(true).Times(1)
mockManager := setupMockManager(mockCtrl)

t.Setenv("POD_NAMESPACE", "")
err := run(mockManager)
assert.Error(t, err)
assert.Equal(t, "POD_NAMESPACE environment variable is not set or is empty", err.Error())
}

func TestRun_WaitForCacheSyncReturnsFalse(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockManager, mockCache := setupMockManager(mockCtrl)

mockCache.EXPECT().WaitForCacheSync(gomock.Any()).Return(false).Times(1)

t.Setenv("POD_NAMESPACE", "test-namespace")
err := run(mockManager)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to wait for cache sync")
}

func TestRun_SuccessfulRun(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockManager, mockCache := setupMockManager(mockCtrl)

mockCache.EXPECT().WaitForCacheSync(gomock.Any()).Return(true).Times(1)
mockManager.EXPECT().Add(gomock.Any()).Return(nil).AnyTimes()
mockManager := setupMockManager(mockCtrl)

mockRulesStore := new(mockrulesstore.RulesStore)
rules := &model.Rules{
Expand Down

0 comments on commit d926407

Please sign in to comment.