sigs.k8s.io/cluster-api@v1.7.1/controllers/external/tracker_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 external 18 19 import ( 20 "testing" 21 22 "github.com/go-logr/logr" 23 . "github.com/onsi/gomega" 24 "github.com/pkg/errors" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "sigs.k8s.io/controller-runtime/pkg/controller" 27 "sigs.k8s.io/controller-runtime/pkg/handler" 28 "sigs.k8s.io/controller-runtime/pkg/log" 29 "sigs.k8s.io/controller-runtime/pkg/predicate" 30 "sigs.k8s.io/controller-runtime/pkg/source" 31 32 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 33 ) 34 35 var ( 36 logger = logr.New(log.NullLogSink{}) 37 ) 38 39 type fakeController struct { 40 controller.Controller 41 } 42 43 type watchCountController struct { 44 // can not directly embed an interface when a pointer receiver is 45 // used in any of the overriding methods. 46 *fakeController 47 // no.of times Watch was called 48 count int 49 raiseError bool 50 } 51 52 func newWatchCountController(raiseError bool) *watchCountController { 53 return &watchCountController{ 54 raiseError: raiseError, 55 } 56 } 57 58 func (c *watchCountController) Watch(_ source.Source, _ handler.EventHandler, _ ...predicate.Predicate) error { 59 c.count++ 60 if c.raiseError { 61 return errors.New("injected failure") 62 } 63 return nil 64 } 65 66 func TestRetryWatch(t *testing.T) { 67 g := NewWithT(t) 68 ctrl := newWatchCountController(true) 69 tracker := ObjectTracker{Controller: ctrl} 70 71 err := tracker.Watch(logger, &clusterv1.Cluster{}, nil) 72 g.Expect(err).To(HaveOccurred()) 73 g.Expect(ctrl.count).Should(Equal(1)) 74 // Calling Watch on same Object kind that failed earlier should be retryable. 75 err = tracker.Watch(logger, &clusterv1.Cluster{}, nil) 76 g.Expect(err).To(HaveOccurred()) 77 g.Expect(ctrl.count).Should(Equal(2)) 78 } 79 80 func TestWatchMultipleTimes(t *testing.T) { 81 g := NewWithT(t) 82 ctrl := &watchCountController{} 83 tracker := ObjectTracker{Controller: ctrl} 84 85 obj := &clusterv1.Cluster{ 86 TypeMeta: metav1.TypeMeta{ 87 Kind: "Cluster", 88 APIVersion: clusterv1.GroupVersion.Version, 89 }, 90 } 91 err := tracker.Watch(logger, obj, nil) 92 g.Expect(err).ToNot(HaveOccurred()) 93 g.Expect(ctrl.count).Should(Equal(1)) 94 // Calling Watch on same Object kind should not register watch again. 95 err = tracker.Watch(logger, obj, nil) 96 g.Expect(err).ToNot(HaveOccurred()) 97 g.Expect(ctrl.count).Should(Equal(1)) 98 }