Skip to content

Commit

Permalink
Fix subtests running in same environment and reconciliation bug which…
Browse files Browse the repository at this point in the history
… was not covered because of that (#602)
  • Loading branch information
AndiDog authored Jul 11, 2024
1 parent d64510b commit 89a59a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
41 changes: 34 additions & 7 deletions exp/controllers/awsmachinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,15 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
})

t.Run("ReconcileLaunchTemplate not mocked", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

launchTemplateIDExisting := "lt-existing"

t.Run("nothing exists, so launch template and ASG must be created", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(pointer.String("ami-abcdef123"), nil)
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(pointer.String("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script"))).Return("lt-ghijkl456", nil)
Expand All @@ -497,6 +497,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
})

t.Run("launch template and ASG exist and need no update", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

// Latest ID and version already stored, no need to retrieve it
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
ms.AWSMachinePool.Status.LaunchTemplateVersion = pointer.String("1")
Expand Down Expand Up @@ -538,6 +544,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
})

t.Run("launch template and ASG exist and only AMI ID changed", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

// Latest ID and version already stored, no need to retrieve it
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
ms.AWSMachinePool.Status.LaunchTemplateVersion = pointer.String("1")
Expand Down Expand Up @@ -585,6 +597,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
})

t.Run("launch template and ASG exist and only bootstrap data secret name changed", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

// Latest ID and version already stored, no need to retrieve it
ms.AWSMachinePool.Status.LaunchTemplateID = launchTemplateIDExisting
ms.AWSMachinePool.Status.LaunchTemplateVersion = pointer.String("1")
Expand Down Expand Up @@ -635,6 +653,12 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
})

t.Run("launch template and ASG created from zero, then bootstrap config reference changes", func(t *testing.T) {
g := NewWithT(t)
setup(t, g)
reconciler.reconcileServiceFactory = nil // use real implementation, but keep EC2 calls mocked (`ec2ServiceFactory`)
reconSvc = nil // not used
defer teardown(t, g)

ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(nil, "", nil, nil)
ec2Svc.EXPECT().DiscoverLaunchTemplateAMI(gomock.Any()).Return(pointer.String("ami-abcdef123"), nil)
ec2Svc.EXPECT().CreateLaunchTemplate(gomock.Any(), gomock.Eq(pointer.String("ami-abcdef123")), gomock.Eq(userDataSecretKey), gomock.Eq([]byte("shell-script"))).Return("lt-ghijkl456", nil)
Expand All @@ -650,7 +674,6 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
g.Expect(err).To(Succeed())

g.Expect(ms.AWSMachinePool.Status.LaunchTemplateID).ToNot(BeEmpty())
g.Expect(pointer.StringDeref(ms.AWSMachinePool.Status.LaunchTemplateVersion, "")).ToNot(BeEmpty())

// Data secret name changes
newBootstrapSecret := &corev1.Secret{
Expand All @@ -665,6 +688,10 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
g.Expect(testEnv.Create(ctx, newBootstrapSecret)).To(Succeed())
ms.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName = pointer.String(newBootstrapSecret.Name)

// Since `AWSMachinePool.status.launchTemplateVersion` isn't set yet,
// the controller will ask for the current version and then set the status.
ec2Svc.EXPECT().GetLaunchTemplateLatestVersion(gomock.Any()).Return("1", nil)

ec2Svc.EXPECT().GetLaunchTemplate(gomock.Eq("test")).Return(
&expinfrav1.AWSLaunchTemplate{
Name: "test",
Expand Down
4 changes: 3 additions & 1 deletion pkg/cloud/services/ec2/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ func (s *Service) ReconcileLaunchTemplate(
return nil, err
}
scope.SetLaunchTemplateLatestVersionStatus(launchTemplateVersion)
return nil, scope.PatchObject()
if err = scope.PatchObject(); err != nil {
return nil, err
}
}

annotation, err := MachinePoolAnnotationJSON(scope, TagsLastAppliedAnnotation)
Expand Down

0 comments on commit 89a59a6

Please sign in to comment.