Skip to content

Commit

Permalink
Fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Jun 12, 2023
1 parent 58806c3 commit ddffcd2
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions azure/services/scalesets/scalesets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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"))
},
Expand All @@ -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"))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
},
Expand All @@ -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)
},
},
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ddffcd2

Please sign in to comment.