sigs.k8s.io/cluster-api-provider-azure@v1.14.3/controllers/azuremanagedmachinepool_reconciler_test.go (about)

     1  /*
     2  Copyright 2020 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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure"
    29  	"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
    30  	gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
    31  )
    32  
    33  func TestIsAgentPoolVMSSNotFoundError(t *testing.T) {
    34  	cases := []struct {
    35  		Name     string
    36  		Err      error
    37  		Expected bool
    38  	}{
    39  		{
    40  			Name:     "WithANotFoundError",
    41  			Err:      NewAgentPoolVMSSNotFoundError("foo", "baz"),
    42  			Expected: true,
    43  		},
    44  		{
    45  			Name:     "WithAWrappedNotFoundError",
    46  			Err:      errors.Wrap(NewAgentPoolVMSSNotFoundError("foo", "baz"), "boom"),
    47  			Expected: true,
    48  		},
    49  		{
    50  			Name:     "NotTheRightKindOfError",
    51  			Err:      errors.New("foo"),
    52  			Expected: false,
    53  		},
    54  		{
    55  			Name:     "NilError",
    56  			Err:      nil,
    57  			Expected: false,
    58  		},
    59  	}
    60  
    61  	for _, c := range cases {
    62  		c := c
    63  		t.Run(c.Name, func(t *testing.T) {
    64  			t.Parallel()
    65  			g := gomega.NewWithT(t)
    66  			g.Expect(errors.Is(c.Err, NewAgentPoolVMSSNotFoundError("foo", "baz"))).To(gomega.Equal(c.Expected))
    67  		})
    68  	}
    69  }
    70  
    71  func TestAzureManagedMachinePoolServicePause(t *testing.T) {
    72  	type pausingServiceReconciler struct {
    73  		*mock_azure.MockServiceReconciler
    74  		*mock_azure.MockPauser
    75  	}
    76  
    77  	cases := map[string]struct {
    78  		expectedError string
    79  		expect        func(svc pausingServiceReconciler)
    80  	}{
    81  		"service paused": {
    82  			expectedError: "",
    83  			expect: func(svc pausingServiceReconciler) {
    84  				gomock.InOrder(
    85  					svc.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(nil),
    86  				)
    87  			},
    88  		},
    89  		"service pause fails": {
    90  			expectedError: "failed to pause machine pool ammp: some error happened",
    91  			expect: func(svc pausingServiceReconciler) {
    92  				gomock.InOrder(
    93  					svc.MockPauser.EXPECT().Pause(gomockinternal.AContext()).Return(errors.New("some error happened")),
    94  				)
    95  			},
    96  		},
    97  	}
    98  
    99  	for name, tc := range cases {
   100  		tc := tc
   101  		t.Run(name, func(t *testing.T) {
   102  			g := gomega.NewWithT(t)
   103  
   104  			t.Parallel()
   105  			mockCtrl := gomock.NewController(t)
   106  			defer mockCtrl.Finish()
   107  
   108  			newPausingServiceReconciler := func() pausingServiceReconciler {
   109  				return pausingServiceReconciler{
   110  					mock_azure.NewMockServiceReconciler(mockCtrl),
   111  					mock_azure.NewMockPauser(mockCtrl),
   112  				}
   113  			}
   114  			svcMock := newPausingServiceReconciler()
   115  
   116  			tc.expect(svcMock)
   117  
   118  			s := &azureManagedMachinePoolService{
   119  				agentPoolsSvc: svcMock,
   120  				scope: &scope.ManagedMachinePoolScope{
   121  					InfraMachinePool: &infrav1.AzureManagedMachinePool{
   122  						ObjectMeta: metav1.ObjectMeta{
   123  							Name: "ammp",
   124  						},
   125  					},
   126  				},
   127  			}
   128  
   129  			err := s.Pause(context.TODO())
   130  			if tc.expectedError != "" {
   131  				g.Expect(err).To(gomega.HaveOccurred())
   132  				g.Expect(err).To(gomega.MatchError(tc.expectedError))
   133  			} else {
   134  				g.Expect(err).NotTo(gomega.HaveOccurred())
   135  			}
   136  		})
   137  	}
   138  }