Skip to content

Commit cac8bac

Browse files
AndiDogfiunchinho
authored andcommitted
Fix subtests running in same environment and reconciliation bug which was not covered because of that (#602)
1 parent 52b706a commit cac8bac

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

exp/controllers/awsmachinepool_controller_test.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,15 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
489489
})
490490

491491
t.Run("ReconcileLaunchTemplate not mocked", func(t *testing.T) {
492-
g := NewWithT(t)
493-
setup(t, g)
494-
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
495-
reconSvc = nil // not used
496-
defer teardown(t, g)
497-
498492
launchTemplateIDExisting := "lt-existing"
499493

500494
t.Run("nothing exists, so launch template and ASG must be created", func(t *testing.T) {
495+
g := NewWithT(t)
496+
setup(t, g)
497+
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
498+
reconSvc = nil // not used
499+
defer teardown(t, g)
500+
501501
ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil, nil)
502502
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
503503
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(ptr.To[string]("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script")), gomock.Eq(userdata.ComputeHash([]byte("shell-script")))).Return("lt-ghijkl456", nil)
@@ -514,6 +514,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
514514
})
515515

516516
t.Run("launch template and ASG exist and need no update", func(t *testing.T) {
517+
g := NewWithT(t)
518+
setup(t, g)
519+
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
520+
reconSvc = nil // not used
521+
defer teardown(t, g)
522+
517523
// Latest ID and version already stored, no need to retrieve it
518524
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
519525
ms.AWSMachinePool.Status.LaunchTemplateVersion = ptr.To[string]("1")
@@ -556,6 +562,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
556562
})
557563

558564
t.Run("launch template and ASG exist and only AMI ID changed", func(t *testing.T) {
565+
g := NewWithT(t)
566+
setup(t, g)
567+
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
568+
reconSvc = nil // not used
569+
defer teardown(t, g)
570+
559571
// Latest ID and version already stored, no need to retrieve it
560572
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
561573
ms.AWSMachinePool.Status.LaunchTemplateVersion = ptr.To[string]("1")
@@ -604,6 +616,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
604616
})
605617

606618
t.Run("launch template and ASG exist and only bootstrap data secret name changed", func(t *testing.T) {
619+
g := NewWithT(t)
620+
setup(t, g)
621+
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
622+
reconSvc = nil // not used
623+
defer teardown(t, g)
624+
607625
// Latest ID and version already stored, no need to retrieve it
608626
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
609627
ms.AWSMachinePool.Status.LaunchTemplateVersion = ptr.To[string]("1")
@@ -655,6 +673,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
655673
})
656674

657675
t.Run("launch template and ASG created from zero, then bootstrap config reference changes", func(t *testing.T) {
676+
g := NewWithT(t)
677+
setup(t, g)
678+
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
679+
reconSvc = nil // not used
680+
defer teardown(t, g)
681+
658682
ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil, nil)
659683
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(ptr.To[string]("ami-abcdef123"), nil)
660684
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(ptr.To[string]("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script")), gomock.Eq(userdata.ComputeHash([]byte("shell-script")))).Return("lt-ghijkl456", nil)
@@ -685,6 +709,10 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
685709
g.Expect(testEnv.Create(ctx, newBootstrapSecret)).To(Succeed())
686710
ms.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName = ptr.To[string](newBootstrapSecret.Name)
687711

712+
// Since `AWSMachinePool.status.launchTemplateVersion` isn't set yet,
713+
// the controller will ask for the current version and then set the status.
714+
ec2Svc.EXPECT().GetLaunchTemplateLatestVersion(gomock.Any()).Return("1", nil)
715+
688716
ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(
689717
&expinfrav1.AWSLaunchTemplate{
690718
Name: "test",

pkg/cloud/services/ec2/launchtemplate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ func (s *Service) ReconcileLaunchTemplate(
202202
return nil, err
203203
}
204204
scope.SetLaunchTemplateLatestVersionStatus(launchTemplateVersion)
205-
return nil, scope.PatchObject()
205+
if err = scope.PatchObject(); err != nil {
206+
return nil, err
207+
}
206208
}
207209

208210
annotation, err := MachinePoolAnnotationJSON(scope, TagsLastAppliedAnnotation)

0 commit comments

Comments
 (0)