diff --git a/azure/services/scalesets/scalesets_test.go b/azure/services/scalesets/scalesets_test.go index 8dda0a94641..67ecc3219e1 100644 --- a/azure/services/scalesets/scalesets_test.go +++ b/azure/services/scalesets/scalesets_test.go @@ -66,7 +66,7 @@ func init() { _ = clusterv1.AddToScheme(scheme.Scheme) } -func TestReconcileVMSS(t *testing.T) { +func getDefaultVMSSSpec() azure.ResourceSpecGetter { defaultSpec := newDefaultVMSSSpec() defaultSpec.DataDisks = append(defaultSpec.DataDisks, infrav1.DataDisk{ NameSuffix: "my_disk_with_ultra_disks", @@ -76,10 +76,20 @@ func TestReconcileVMSS(t *testing.T) { StorageAccountType: "UltraSSD_LRS", }, }) - defaultInstances := newDefaultInstances() + + return &defaultSpec +} + +func getResultVMSS() compute.VirtualMachineScaleSet { resultVMSS := newDefaultVMSS("VM_SIZE") resultVMSS.ID = pointer.String(defaultVMSSID) + return resultVMSS +} + +func TestReconcileVMSS(t *testing.T) { + defaultInstances := newDefaultInstances() + testcases := []struct { name string expect func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) @@ -89,29 +99,31 @@ func TestReconcileVMSS(t *testing.T) { name: "create a vmss", expectedError: "", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { + spec := getDefaultVMSSSpec() // Validate spec - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(&defaultImage, nil) s.SaveVMImageToStatus(&defaultImage) s.GetBootstrapData(gomockinternal.AContext()).Return("fake-bootstrap-data", nil) s.MaxSurge().Return(1, nil) - r.CreateOrUpdateResource(gomockinternal.AContext(), &defaultSpec, serviceName). - Return(resultVMSS, nil) + r.CreateOrUpdateResource(gomockinternal.AContext(), spec, serviceName). + Return(getResultVMSS(), nil) s.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, nil) - s.ReconcileReplicas(gomockinternal.AContext(), converters.SDKToVMSS(resultVMSS, defaultInstances)).Return(nil) + s.ReconcileReplicas(gomockinternal.AContext(), converters.SDKToVMSS(getResultVMSS(), defaultInstances)).Return(nil) s.SetProviderID(azure.ProviderIDPrefix + defaultVMSSID) - s.SetVMSSState(converters.SDKToVMSS(resultVMSS, defaultInstances)) + s.SetVMSSState(converters.SDKToVMSS(getResultVMSS(), defaultInstances)) }, }, { name: "failed to list instances", expectedError: "failed to get existing instances: #: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { + spec := getDefaultVMSSSpec() // Validate spec - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, internalError) s.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, gomockinternal.ErrStrEq("failed to get existing instances: #: Internal Server Error: StatusCode=500")) }, @@ -120,7 +132,8 @@ func TestReconcileVMSS(t *testing.T) { name: "failed to get VM image", expectedError: "failed to get vm image: #: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + spec := getDefaultVMSSSpec() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(nil, internalError) s.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, gomockinternal.ErrStrEq("failed to get vm image: #: Internal Server Error: StatusCode=500")) @@ -130,7 +143,8 @@ func TestReconcileVMSS(t *testing.T) { name: "failed to get bootstrap data", expectedError: "failed to get bootstrap data: #: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + spec := getDefaultVMSSSpec() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(&defaultImage, nil) s.SaveVMImageToStatus(&defaultImage) @@ -143,7 +157,8 @@ func TestReconcileVMSS(t *testing.T) { name: "failed to get max surge", expectedError: "failed to get max surge: #: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + spec := getDefaultVMSSSpec() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(&defaultImage, nil) s.SaveVMImageToStatus(&defaultImage) @@ -156,14 +171,15 @@ func TestReconcileVMSS(t *testing.T) { name: "failed to create a vmss", expectedError: "#: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + spec := getDefaultVMSSSpec() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(&defaultImage, nil) s.SaveVMImageToStatus(&defaultImage) s.GetBootstrapData(gomockinternal.AContext()).Return("fake-bootstrap-data", nil) s.MaxSurge().Return(1, nil) - r.CreateOrUpdateResource(gomockinternal.AContext(), &defaultSpec, serviceName). + r.CreateOrUpdateResource(gomockinternal.AContext(), spec, serviceName). Return(nil, internalError) s.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, internalError) }, @@ -172,18 +188,19 @@ func TestReconcileVMSS(t *testing.T) { name: "failed to reconcile replicas", expectedError: "unable to reconcile replicas: #: Internal Server Error: StatusCode=500", expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, m *mock_scalesets.MockClientMockRecorder) { - s.ScaleSetSpec(gomockinternal.AContext()).Return(&defaultSpec).AnyTimes() + spec := getDefaultVMSSSpec() + s.ScaleSetSpec(gomockinternal.AContext()).Return(spec).AnyTimes() m.ListInstances(gomockinternal.AContext(), defaultSpec.ResourceGroup, defaultSpec.Name).Return(defaultInstances, nil) s.GetVMImage(gomockinternal.AContext()).Return(&defaultImage, nil) s.SaveVMImageToStatus(&defaultImage) s.GetBootstrapData(gomockinternal.AContext()).Return("fake-bootstrap-data", nil) s.MaxSurge().Return(1, nil) - r.CreateOrUpdateResource(gomockinternal.AContext(), &defaultSpec, serviceName). - Return(resultVMSS, nil) + r.CreateOrUpdateResource(gomockinternal.AContext(), spec, serviceName). + Return(getResultVMSS(), nil) s.UpdatePutStatus(infrav1.BootstrapSucceededCondition, serviceName, nil) - s.ReconcileReplicas(gomockinternal.AContext(), converters.SDKToVMSS(resultVMSS, defaultInstances)).Return(internalError) + s.ReconcileReplicas(gomockinternal.AContext(), converters.SDKToVMSS(getResultVMSS(), defaultInstances)).Return(internalError) }, }, { @@ -324,6 +341,7 @@ func TestDeleteVMSS(t *testing.T) { defaultInstances := newDefaultInstances() resultVMSS := newDefaultVMSS("VM_SIZE") resultVMSS.ID = pointer.String(defaultVMSSID) + // Be careful about race conditions if you need modify these. testcases := []struct { name string