Skip to content

Commit 4677dfe

Browse files
committed
fix: tests for lifecyclehooks
1 parent 95ba583 commit 4677dfe

File tree

2 files changed

+178
-2
lines changed

2 files changed

+178
-2
lines changed

exp/controllers/awsmachinepool_controller_test.go

+178
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,184 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
812812
g.Eventually(recorder.Events).Should(Receive(ContainSubstring("DeletionInProgress")))
813813
})
814814
})
815+
t.Run("Lifecycle Hooks", func(t *testing.T) {
816+
t.Run("New lifecycle hook is added", func(t *testing.T) {
817+
g := NewWithT(t)
818+
setup(t, g)
819+
defer teardown(t, g)
820+
821+
newLifecycleHook := expinfrav1.AWSLifecycleHook{
822+
Name: "new-hook",
823+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
824+
}
825+
ms.AWSMachinePool.Spec.AWSLifecycleHooks = append(ms.AWSMachinePool.Spec.AWSLifecycleHooks, newLifecycleHook)
826+
827+
reconSvc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
828+
asgSvc.EXPECT().DescribeLifecycleHooks(ms.Name()).Return(nil, nil)
829+
asgSvc.EXPECT().DescribeLifecycleHook(ms.Name(), &newLifecycleHook).Return(nil, nil)
830+
asgSvc.EXPECT().CreateLifecycleHook(ms.Name(), &newLifecycleHook).Return(nil)
831+
reconSvc.EXPECT().ReconcileTags(gomock.Any(), gomock.Any()).Return(nil)
832+
asgSvc.EXPECT().GetASGByName(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
833+
g.Expect(scope.Name()).To(Equal("test"))
834+
835+
// No difference to `AWSMachinePool.spec`
836+
return &expinfrav1.AutoScalingGroup{
837+
Name: scope.Name(),
838+
Subnets: []string{
839+
"subnet-1",
840+
},
841+
MinSize: awsMachinePool.Spec.MinSize,
842+
MaxSize: awsMachinePool.Spec.MaxSize,
843+
MixedInstancesPolicy: awsMachinePool.Spec.MixedInstancesPolicy.DeepCopy(),
844+
}, nil
845+
})
846+
asgSvc.EXPECT().DescribeLifecycleHooks(gomock.Any()).Return(nil, nil).AnyTimes()
847+
asgSvc.EXPECT().SubnetIDs(gomock.Any()).Return([]string{"subnet-1"}, nil) // no change
848+
// No changes, so there must not be an ASG update!
849+
asgSvc.EXPECT().UpdateASG(gomock.Any()).Times(0)
850+
851+
err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
852+
g.Expect(err).To(Succeed())
853+
})
854+
t.Run("Lifecycle hook to remove", func(t *testing.T) {
855+
g := NewWithT(t)
856+
setup(t, g)
857+
defer teardown(t, g)
858+
859+
reconSvc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
860+
asgSvc.EXPECT().DescribeLifecycleHooks(ms.Name()).Return([]*expinfrav1.AWSLifecycleHook{
861+
{
862+
Name: "hook-to-remove",
863+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
864+
},
865+
}, nil)
866+
asgSvc.EXPECT().DeleteLifecycleHook(ms.Name(), &expinfrav1.AWSLifecycleHook{
867+
Name: "hook-to-remove",
868+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
869+
}).Return(nil)
870+
reconSvc.EXPECT().ReconcileTags(gomock.Any(), gomock.Any()).Return(nil)
871+
asgSvc.EXPECT().GetASGByName(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
872+
g.Expect(scope.Name()).To(Equal("test"))
873+
874+
// No difference to `AWSMachinePool.spec`
875+
return &expinfrav1.AutoScalingGroup{
876+
Name: scope.Name(),
877+
Subnets: []string{
878+
"subnet-1",
879+
},
880+
MinSize: awsMachinePool.Spec.MinSize,
881+
MaxSize: awsMachinePool.Spec.MaxSize,
882+
MixedInstancesPolicy: awsMachinePool.Spec.MixedInstancesPolicy.DeepCopy(),
883+
}, nil
884+
})
885+
asgSvc.EXPECT().DescribeLifecycleHooks(gomock.Any()).Return(nil, nil).AnyTimes()
886+
asgSvc.EXPECT().SubnetIDs(gomock.Any()).Return([]string{"subnet-1"}, nil) // no change
887+
// No changes, so there must not be an ASG update!
888+
asgSvc.EXPECT().UpdateASG(gomock.Any()).Times(0)
889+
890+
err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
891+
g.Expect(err).To(Succeed())
892+
})
893+
t.Run("One to add, one to remove", func(t *testing.T) {
894+
g := NewWithT(t)
895+
setup(t, g)
896+
defer teardown(t, g)
897+
newLifecycleHook := expinfrav1.AWSLifecycleHook{
898+
Name: "new-hook",
899+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
900+
}
901+
ms.AWSMachinePool.Spec.AWSLifecycleHooks = append(ms.AWSMachinePool.Spec.AWSLifecycleHooks, newLifecycleHook)
902+
903+
reconSvc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
904+
asgSvc.EXPECT().DescribeLifecycleHook(ms.Name(), &newLifecycleHook).Return(nil, nil)
905+
asgSvc.EXPECT().CreateLifecycleHook(ms.Name(), &newLifecycleHook).Return(nil)
906+
asgSvc.EXPECT().DescribeLifecycleHooks(ms.Name()).Return([]*expinfrav1.AWSLifecycleHook{
907+
{
908+
Name: "new-hook",
909+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
910+
},
911+
{
912+
Name: "hook-to-remove",
913+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
914+
},
915+
}, nil)
916+
asgSvc.EXPECT().DeleteLifecycleHook(ms.Name(), &expinfrav1.AWSLifecycleHook{
917+
Name: "hook-to-remove",
918+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
919+
}).Return(nil)
920+
reconSvc.EXPECT().ReconcileTags(gomock.Any(), gomock.Any()).Return(nil)
921+
asgSvc.EXPECT().GetASGByName(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
922+
g.Expect(scope.Name()).To(Equal("test"))
923+
924+
// No difference to `AWSMachinePool.spec`
925+
return &expinfrav1.AutoScalingGroup{
926+
Name: scope.Name(),
927+
Subnets: []string{
928+
"subnet-1",
929+
},
930+
MinSize: awsMachinePool.Spec.MinSize,
931+
MaxSize: awsMachinePool.Spec.MaxSize,
932+
MixedInstancesPolicy: awsMachinePool.Spec.MixedInstancesPolicy.DeepCopy(),
933+
}, nil
934+
})
935+
asgSvc.EXPECT().DescribeLifecycleHooks(gomock.Any()).Return(nil, nil).AnyTimes()
936+
asgSvc.EXPECT().SubnetIDs(gomock.Any()).Return([]string{"subnet-1"}, nil) // no change
937+
// No changes, so there must not be an ASG update!
938+
asgSvc.EXPECT().UpdateASG(gomock.Any()).Times(0)
939+
940+
err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
941+
g.Expect(err).To(Succeed())
942+
})
943+
t.Run("Update hook", func(t *testing.T) {
944+
g := NewWithT(t)
945+
setup(t, g)
946+
defer teardown(t, g)
947+
updateLifecycleHook := expinfrav1.AWSLifecycleHook{
948+
Name: "hook-to-update",
949+
LifecycleTransition: "autoscaling:EC2_INSTANCE_TERMINATING",
950+
}
951+
ms.AWSMachinePool.Spec.AWSLifecycleHooks = append(ms.AWSMachinePool.Spec.AWSLifecycleHooks, updateLifecycleHook)
952+
953+
reconSvc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
954+
asgSvc.EXPECT().DescribeLifecycleHook(ms.Name(), &updateLifecycleHook).Return(&expinfrav1.AWSLifecycleHook{
955+
Name: "hook-to-update",
956+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
957+
}, nil)
958+
asgSvc.EXPECT().LifecycleHookNeedsUpdate(&expinfrav1.AWSLifecycleHook{
959+
Name: "hook-to-update",
960+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
961+
}, &updateLifecycleHook).Return(true)
962+
asgSvc.EXPECT().UpdateLifecycleHook(ms.Name(), &updateLifecycleHook).Return(nil)
963+
asgSvc.EXPECT().DescribeLifecycleHooks(ms.Name()).Return([]*expinfrav1.AWSLifecycleHook{
964+
{
965+
Name: "hook-to-update",
966+
LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING",
967+
},
968+
}, nil)
969+
reconSvc.EXPECT().ReconcileTags(gomock.Any(), gomock.Any()).Return(nil)
970+
asgSvc.EXPECT().GetASGByName(gomock.Any()).DoAndReturn(func(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
971+
g.Expect(scope.Name()).To(Equal("test"))
972+
973+
// No difference to `AWSMachinePool.spec`
974+
return &expinfrav1.AutoScalingGroup{
975+
Name: scope.Name(),
976+
Subnets: []string{
977+
"subnet-1",
978+
},
979+
MinSize: awsMachinePool.Spec.MinSize,
980+
MaxSize: awsMachinePool.Spec.MaxSize,
981+
MixedInstancesPolicy: awsMachinePool.Spec.MixedInstancesPolicy.DeepCopy(),
982+
}, nil
983+
})
984+
asgSvc.EXPECT().DescribeLifecycleHooks(gomock.Any()).Return(nil, nil).AnyTimes()
985+
asgSvc.EXPECT().SubnetIDs(gomock.Any()).Return([]string{"subnet-1"}, nil) // no change
986+
// No changes, so there must not be an ASG update!
987+
asgSvc.EXPECT().UpdateASG(gomock.Any()).Times(0)
988+
989+
err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
990+
g.Expect(err).To(Succeed())
991+
})
992+
})
815993
}
816994

817995
// TODO: This was taken from awsmachine_controller_test, i think it should be moved to elsewhere in both locations like test/helpers.

exp/controllers/awsmanagedmachinepool_controller.go

-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636

3737
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
3838
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
39-
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud"
4039
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
4140
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services"
4241
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/ec2"
@@ -53,7 +52,6 @@ import (
5352
// AWSManagedMachinePoolReconciler reconciles a AWSManagedMachinePool object.
5453
type AWSManagedMachinePoolReconciler struct {
5554
client.Client
56-
asgServiceFactory func(cloud.ClusterScoper) services.ASGInterface
5755
Recorder record.EventRecorder
5856
Endpoints []scope.ServiceEndpoint
5957
EnableIAM bool

0 commit comments

Comments
 (0)