sigs.k8s.io/cluster-api-provider-azure@v1.14.3/pkg/coalescing/reconciler_test.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package coalescing 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/go-logr/logr" 25 . "github.com/onsi/gomega" 26 gtypes "github.com/onsi/gomega/types" 27 "github.com/pkg/errors" 28 "go.uber.org/mock/gomock" 29 "k8s.io/apimachinery/pkg/types" 30 mock_coalescing "sigs.k8s.io/cluster-api-provider-azure/pkg/coalescing/mocks" 31 "sigs.k8s.io/controller-runtime/pkg/log" 32 "sigs.k8s.io/controller-runtime/pkg/reconcile" 33 ) 34 35 func TestCoalescingReconciler_Reconcile(t *testing.T) { 36 var ( 37 defaultRequest = reconcile.Request{ 38 NamespacedName: types.NamespacedName{ 39 Name: "aName", 40 Namespace: "aNamespace", 41 }, 42 } 43 44 defaultRequestKey = "aNamespace/aName" 45 ) 46 47 cases := []struct { 48 Name string 49 Reconciler func(g *WithT, cacherMock *mock_coalescing.MockReconcileCacher, mockReconciler *mock_coalescing.MockReconciler) reconcile.Reconciler 50 Request reconcile.Request 51 MatchThis gtypes.GomegaMatcher 52 Error string 53 }{ 54 { 55 Name: "should call upstream reconciler if key does not exist in cache", 56 Reconciler: func(g *WithT, cacherMock *mock_coalescing.MockReconcileCacher, mockReconciler *mock_coalescing.MockReconciler) reconcile.Reconciler { 57 cacherMock.EXPECT().ShouldProcess(defaultRequestKey).Return(time.Now(), true) 58 cacherMock.EXPECT().Reconciled(defaultRequestKey) 59 mockReconciler.EXPECT().Reconcile(gomock.Any(), defaultRequest) 60 return NewReconciler(mockReconciler, cacherMock, logr.New(log.NullLogSink{})) 61 }, 62 Request: defaultRequest, 63 MatchThis: Equal(0 * time.Second), 64 }, 65 { 66 Name: "should not call upstream reconciler if key does exists in cache and is not expired", 67 Reconciler: func(g *WithT, cacherMock *mock_coalescing.MockReconcileCacher, mockReconciler *mock_coalescing.MockReconciler) reconcile.Reconciler { 68 cacherMock.EXPECT().ShouldProcess(defaultRequestKey).Return(time.Now().Add(30*time.Second), false) 69 return NewReconciler(mockReconciler, cacherMock, logr.New(log.NullLogSink{})) 70 }, 71 Request: defaultRequest, 72 MatchThis: And(BeNumerically("<=", 30*time.Second), BeNumerically(">", 29*time.Second)), 73 }, 74 { 75 Name: "should call upstream reconciler if key does not exist in cache and return error", 76 Reconciler: func(g *WithT, cacherMock *mock_coalescing.MockReconcileCacher, mockReconciler *mock_coalescing.MockReconciler) reconcile.Reconciler { 77 cacherMock.EXPECT().ShouldProcess(defaultRequestKey).Return(time.Now(), true) 78 mockReconciler.EXPECT().Reconcile(gomock.Any(), defaultRequest).Return(reconcile.Result{}, errors.New("boom")) 79 return NewReconciler(mockReconciler, cacherMock, logr.New(log.NullLogSink{})) 80 }, 81 Request: defaultRequest, 82 MatchThis: Equal(0 * time.Second), 83 Error: "boom", 84 }, 85 } 86 87 for _, c := range cases { 88 c := c 89 t.Run(c.Name, func(t *testing.T) { 90 g := NewWithT(t) 91 mockCtrl := gomock.NewController(t) 92 defer mockCtrl.Finish() 93 cacherMock := mock_coalescing.NewMockReconcileCacher(mockCtrl) 94 reconcilerMock := mock_coalescing.NewMockReconciler(mockCtrl) 95 subject := c.Reconciler(g, cacherMock, reconcilerMock) 96 result, err := subject.Reconcile(context.Background(), c.Request) 97 if c.Error != "" || err != nil { 98 g.Expect(err).To(And(HaveOccurred(), MatchError(c.Error))) 99 return 100 } 101 102 g.Expect(result.RequeueAfter).To(c.MatchThis) 103 }) 104 } 105 }