sigs.k8s.io/cluster-api-provider-azure@v1.14.3/controllers/azuremanagedcontrolplane_reconciler_test.go (about) 1 /* 2 Copyright 2023 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 controllers 18 19 import ( 20 "context" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 "github.com/pkg/errors" 25 "go.uber.org/mock/gomock" 26 "sigs.k8s.io/cluster-api-provider-azure/azure" 27 "sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure" 28 gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock" 29 ) 30 31 func TestAzureManagedControlPlaneServicePause(t *testing.T) { 32 type pausingServiceReconciler struct { 33 *mock_azure.MockServiceReconciler 34 *mock_azure.MockPauser 35 } 36 37 cases := map[string]struct { 38 expectedError string 39 expect func(one pausingServiceReconciler, two pausingServiceReconciler, three pausingServiceReconciler) 40 }{ 41 "all services are paused in order": { 42 expectedError: "", 43 expect: func(one pausingServiceReconciler, two pausingServiceReconciler, three pausingServiceReconciler) { 44 gomock.InOrder( 45 one.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(nil), 46 two.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(nil), 47 three.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(nil)) 48 }, 49 }, 50 "service pause fails": { 51 expectedError: "failed to pause AzureManagedControlPlane service two: some error happened", 52 expect: func(one pausingServiceReconciler, two pausingServiceReconciler, _ pausingServiceReconciler) { 53 gomock.InOrder( 54 one.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(nil), 55 two.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(errors.New("some error happened")), 56 two.MockServiceReconciler.EXPECT().Name().Return("two")) 57 }, 58 }, 59 } 60 61 for name, tc := range cases { 62 tc := tc 63 t.Run(name, func(t *testing.T) { 64 g := NewWithT(t) 65 66 t.Parallel() 67 mockCtrl := gomock.NewController(t) 68 defer mockCtrl.Finish() 69 70 newPausingServiceReconciler := func() pausingServiceReconciler { 71 return pausingServiceReconciler{ 72 mock_azure.NewMockServiceReconciler(mockCtrl), 73 mock_azure.NewMockPauser(mockCtrl), 74 } 75 } 76 svcOneMock := newPausingServiceReconciler() 77 svcTwoMock := newPausingServiceReconciler() 78 svcThreeMock := newPausingServiceReconciler() 79 80 tc.expect(svcOneMock, svcTwoMock, svcThreeMock) 81 82 s := &azureManagedControlPlaneService{ 83 services: []azure.ServiceReconciler{ 84 svcOneMock, 85 svcTwoMock, 86 svcThreeMock, 87 }, 88 } 89 90 err := s.Pause(context.TODO()) 91 if tc.expectedError != "" { 92 g.Expect(err).To(HaveOccurred()) 93 g.Expect(err).To(MatchError(tc.expectedError)) 94 } else { 95 g.Expect(err).NotTo(HaveOccurred()) 96 } 97 }) 98 } 99 }